2011-07-15 7 views
1

で独自のプライベートメソッドを呼び出す前に、私は多くのif-elseステートメントを持つSwingクラスを持っていました。 javaリフレクションを使用してすべてのif-elseを削除した後、独自のメソッドを正常に呼び出すことができます。しかし、私はまだメソッドにパラメータを渡すことはできません。以下のコードをActionEventパラメータを渡して動作させるには?Javaは固定パラメータ

public void actionPerformed(ActionEvent e) { 
    try { 
     //Method method = this.getClass().getDeclaredMethod(e.getActionCommand()); 
     Method method = this.getClass().getMethod(e.getActionCommand()); 
     method.invoke(this);     
    } catch (IllegalArgumentException e1) { 
     e1.printStackTrace(); 
    } catch (IllegalAccessException e1) { 
     e1.printStackTrace(); 
    } catch (InvocationTargetException e1) { 
     e1.printStackTrace(); 
    } catch (SecurityException e1) { 
     e1.printStackTrace(); 
    } catch (NoSuchMethodException e1) { 
     e1.printStackTrace(); 
    } 
} 
public void generate(ActionEvent e){ 
    System.out.println("Generating"); 
} 

答えて

2

だけMethod.invoke()に追加の引数として引数を渡す:

method.invoke(this, e); 
+0

私は、私を試してみましたこの例外を取得します:java.lang.NoSuchMethodException:com.upd.ui._Summary.generate() – Reusable

+1

@Reusable: 'getMethod()'呼び出しで**例外を受け取り、 'invoke (getMethod()だけがこれを投げることができます)!あなたはあなたのタイトルに 'private'と言いますが、コードにはプライベートメソッドはありません。 **あなたのメソッドが実際に 'private'の場合、' getMethod() 'として' getDeclaredMethod() 'を使用する必要があります** publicメソッドのみを見つける*。 –

+0

あなたは私の頭の中の他の質問に答えてくれます。私はこの方法を非公開にしたかったのです。なぜなら、これを他人に公開することが、なぜこの方法が公開されたのかという疑問を必ず生み出すからです。ありがとう! – Reusable

1

あなたがイベントを渡すためActionEeventと第二を取る方法を見つけるために、1を2つのメソッドを変更する必要があります。

try { 
    Method method = getClass().getMethod(e.getActionCommand(), ActionEvent.class); 
    method.invoke(this, e); 
} catch (Exception e) { 
    // log the 'e' exception 
} 
+0

それは働いた!ありがとう! btw、このコードは、公開メソッドとして宣言している場合にのみ機能します。私が「プライベート」に変わると失敗するのはなぜですか? – Reusable

2

この

Method method = this.getClass().getMethod(e.getActionCommand()); 

e.getActionCommand()参照、メソッド名"generate"を想定)引数なしで方法を反映しています。あなたは

Method method = this.getClass().getMethod(e.getActionCommand(), ActionEvent.class); 

を反映して、

method.invoke(this, e); 
を行う必要があるでしょう

:単に別の方法(オーバーロードをヒント)である、それは方法generate()が反映されますが、あなたはgenerate(ActionEvent e)を反映したいです

1

Class.getMethod()は、パブリックメソッドのみを検出します。 Class.getDeclaredmethod()が必要です。

また、あなたは引数の型を探すために必要があります:

Method method = getClass().getDeclaredMethod(e.getActionCommand(), ActionEvent.class); 

私はこのようなヘルパーメソッドでそれを行うことを好む:

public static Method findMethodByNameAndArgs(final Class<?> clazz, 
    final String name, final Object... args) { 
    for (final Method method : clazz.getDeclaredMethods()) { 
     if (method.getName().equals(name)) { 
      final Class<?>[] parameterTypes = method.getParameterTypes(); 
      if (parameterTypes.length == args.length) { 
       boolean matchArgs = true; 
       for (int i = 0; i < args.length; i++) { 
        final Object param = args[i]; 
        if (param != null && !parameterTypes[i].isInstance(param)) { 
         matchArgs = false; 
         break; 
        } 
       } 
       if (matchArgs) return method; 
      } 
     } 
    } 
    throw new IllegalArgumentException(
     "Found no method for name '" + name + "' and params " 
     + Arrays.toString(args)); 
} 
関連する問題