クラスのパラメータを持つ特定のメソッドの名前を返したいとします。クラスのパラメータを持つ特定のメソッドの名前を返す方法
私はまた、メソッド名を返すが、クラス名とパッケージ名を含めているプログラムを持っています。
import java.lang.reflect.*;
public class ClassDemo {
public static void main(String[] args) {
ClassDemo cls = new ClassDemo();
Class c = cls.getClass();
try {
// parameter type is null
Method m = c.getMethod("show", null);
System.out.println("method = " + m.toString());
}
catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
try {
// method Long
Class[] cArg = new Class[1];
cArg[0] = Long.class;
Method lMethod = c.getMethod("showLong", cArg);
System.out.println("method = " + lMethod.toString());
}
catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
}
public Integer show() {
return 1;
}
public void showLong(Long l) {
this.l = l;
}
long l = 78655;
}
、結果は次のとおりです:コードがある
method = public java.lang.Integer ClassDemo.show()
method = public void ClassDemo.showLong(java.lang.Long)
私の質問、私はそれに関連するパラメータではなく、クラス名とパッケージ名なしでメソッド名を得ることができる方法はありますか?
私が意味する、その場合には、結果は次のようになります。
method = show()
method = showLong(Long)
私は私が何をしたい質問Getting the name of the current executing methodを見ましたが、そのありません。誰も私に何か解決策を教えてもらえますか?
あなたは '' lMethod.getName() 'と' lMethod.getParameterTypes()を呼び出し、そこから行くことができますか? – immibis
コードではないテキストにはコード書式を使用しないでください。 – EJP