Java : Method (リフレクション) - API使用例
Method (Java SE 18 & JDK 18) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
Method クラスを使うと、例えば private なメソッドを外部から実行できるようになります。
public class AccessTest {
private int sum(int a, int b) {
return a + b;
}
}
final var cls = AccessTest.class;
final var obj = new AccessTest();
final var method = cls.getDeclaredMethod("sum", int.class, int.class);
System.out.println(method); // private int AccessTest.sum(int,int)
System.out.println(method.canAccess(obj)); // false
//method.invoke(obj, 20, 30); // IllegalAccessException
System.out.println(method.trySetAccessible()); // true
System.out.println(method.canAccess(obj)); // true
final var ret = method.invoke(obj, 20, 30);
System.out.println(ret); // 50
Memberで宣言されたフィールド
DECLARED, PUBLIC
「Java API 使用例 : Member」をご参照ください。
メソッド
boolean equals (Object obj)
public class Foo {
public void aaa() {
}
public void bbb() {
}
}
final var foo1 = new Foo();
final var foo2 = new Foo();
final var method1 = foo1.getClass().getDeclaredMethod("aaa");
final var method2 = foo2.getClass().getDeclaredMethod("aaa");
System.out.println(method1.equals(method2)); // true
final var method3 = foo1.getClass().getDeclaredMethod("bbb");
System.out.println(method3.equals(method1)); // false
AnnotatedType getAnnotatedReturnType ()
public class Foo {
public int aaa() {
return 123;
}
public String bbb() {
return "abc";
}
}
final var cls = Foo.class;
final var method = cls.getDeclaredMethod("aaa");
System.out.println(method); // public int Foo.aaa()
final var type = method.getAnnotatedReturnType();
System.out.println(type); // int
final var cls = Foo.class;
final var method = cls.getDeclaredMethod("bbb");
System.out.println(method); // public java.lang.String Foo.bbb()
final var type = method.getAnnotatedReturnType();
System.out.println(type); // java.lang.String
<T extends Annotation> T getAnnotation (Class<T> annotationClass)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Foo {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Bar {
}
public class A {
@Foo
public void aaa() {
}
@Bar
public void bbb() {
}
}
final var method = A.class.getDeclaredMethod("aaa");
final var ret1 = method.getAnnotation(Foo.class);
System.out.println(ret1); // @Foo()
final var ret2 = method.getAnnotation(Bar.class);
System.out.println(ret2); // null
final var method = A.class.getDeclaredMethod("bbb");
final var ret1 = method.getAnnotation(Foo.class);
System.out.println(ret1); // null
final var ret2 = method.getAnnotation(Bar.class);
System.out.println(ret2); // @Bar()
Annotation[] getDeclaredAnnotations ()
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Foo {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Bar {
}
public class A {
@Foo
@Bar
public void aaa() {
}
}
final var method = A.class.getDeclaredMethod("aaa");
System.out.println("-- annotations --");
for (final var a : method.getDeclaredAnnotations()) {
System.out.println(a);
}
// 結果
// ↓
//-- annotations --
//@Foo()
//@Bar()
Class<?> getDeclaringClass ()
public class Foo {
public void aaa() {
}
}
final var cls = Foo.class;
final var method = cls.getDeclaredMethod("aaa");
System.out.println(method); // public void Foo.aaa()
final var ret = method.getDeclaringClass();
System.out.println(ret); // class Foo
System.out.println(cls.equals(ret)); // true
Object getDefaultValue ()
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Foo {
String value() default "abc";
}
public class A {
@Foo("XYZ")
public void aaa() {
}
}
final var cls = Foo.class;
final var method = cls.getDeclaredMethod("value");
System.out.println(method); // public abstract java.lang.String Foo.value()
final var ret = method.getDefaultValue();
System.out.println(ret); // abc
final var cls = A.class;
final var method = cls.getDeclaredMethod("aaa");
System.out.println(method); // public void A.aaa()
final var ret = method.getDefaultValue();
System.out.println(ret); // null
final var foo = method.getAnnotation(Foo.class);
System.out.println(foo.value()); // XYZ
Class<?>[] getExceptionTypes ()
public class Foo<T extends Exception> {
public void aaa() throws T, IOException {
}
}
final var cls = Foo.class;
final var method = cls.getDeclaredMethod("aaa");
System.out.println(method);
System.out.println("-- getExceptionTypes --");
for (final var type : method.getExceptionTypes()) {
System.out.println(type);
}
System.out.println("-- getGenericExceptionTypes --");
for (final var type : method.getGenericExceptionTypes()) {
System.out.println(type);
}
// 結果
// ↓
//public void Foo.aaa() throws java.lang.Exception,java.io.IOException
//-- getExceptionTypes --
//class java.lang.Exception
//class java.io.IOException
//-- getGenericExceptionTypes --
//T
//class java.io.IOException
Type[] getGenericExceptionTypes ()
このメソッドの使用例は、getExceptionTypes() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
Type[] getGenericParameterTypes ()
public class Foo<T> {
public void aaa(int num, T value) {
}
}
final var cls = Foo.class;
final var method = cls.getDeclaredMethod("aaa", int.class, Object.class);
System.out.println(method);
System.out.println("-- getParameterTypes --");
for (final var type : method.getParameterTypes()) {
System.out.println(type);
}
System.out.println("-- getGenericParameterTypes --");
for (final var type : method.getGenericParameterTypes()) {
System.out.println(type);
}
// 結果
// ↓
//public void Foo.aaa(int,java.lang.Object)
//-- getParameterTypes --
//int
//class java.lang.Object
//-- getGenericParameterTypes --
//int
//T
Type getGenericReturnType ()
public interface Foo<T> {
T aaa();
String bbb();
}
final var cls = Foo.class;
final var method = cls.getDeclaredMethod("aaa");
System.out.println(method); // public abstract java.lang.Object Foo.aaa()
final var ret1 = method.getReturnType();
System.out.println(ret1); // class java.lang.Object
final var ret2 = method.getGenericReturnType();
System.out.println(ret2); // T
final var cls = Foo.class;
final var method = cls.getDeclaredMethod("bbb");
System.out.println(method); // public abstract java.lang.String Foo.bbb()
final var ret1 = method.getReturnType();
System.out.println(ret1); // class java.lang.String
final var ret2 = method.getGenericReturnType();
System.out.println(ret2); // class java.lang.String
int getModifiers ()
public class Foo {
public void aaa() {
}
protected void bbb() {
}
private void ccc() {
}
}
final var cls = Foo.class;
for (final var method : cls.getDeclaredMethods()) {
final var mod = method.getModifiers();
System.out.println(method.getName() + " : mod = " + mod);
System.out.println(" isPublic : " + Modifier.isPublic(mod));
System.out.println(" isProtected : " + Modifier.isProtected(mod));
System.out.println(" isPrivate : " + Modifier.isPrivate(mod));
}
// 結果
// ↓
//aaa : mod = 1
// isPublic : true
// isProtected : false
// isPrivate : false
//bbb : mod = 4
// isPublic : false
// isProtected : true
// isPrivate : false
//ccc : mod = 2
// isPublic : false
// isProtected : false
// isPrivate : true
String getName ()
public class Foo {
public String aaa(int num) {
return "abc";
}
}
final var cls = Foo.class;
final var method = cls.getDeclaredMethod("aaa", int.class);
System.out.println(method); // public java.lang.String Foo.aaa(int)
System.out.println(method.getName()); // aaa
Annotation[][] getParameterAnnotations ()
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Foo {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Bar {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Baz {
}
public class A {
public void aaa(@Foo @Bar int num, @Baz String str) {
}
}
final var method = A.class.getDeclaredMethod("aaa", int.class, String.class);
System.out.println(method);
System.out.println("-- annotations --");
for (final var a : method.getParameterAnnotations()) {
System.out.println(Arrays.toString(a));
}
// 結果
// ↓
//public void A.aaa(int,java.lang.String)
//-- annotations --
//[@Foo(), @Bar()]
//[@Baz()]
int getParameterCount ()
public class Foo {
public void aaa() {
}
public void bbb(int num) {
}
public void ccc(int num, String str) {
}
}
final var cls = Foo.class;
final var method1 = cls.getDeclaredMethod("aaa");
System.out.println(method1); // public void Foo.aaa()
System.out.println(method1.getParameterCount()); // 0
final var method2 = cls.getDeclaredMethod("bbb", int.class);
System.out.println(method2); // public void Foo.bbb(int)
System.out.println(method2.getParameterCount()); // 1
final var method3 = cls.getDeclaredMethod("ccc", int.class, String.class);
System.out.println(method3); // public void Foo.ccc(int,java.lang.String)
System.out.println(method3.getParameterCount()); // 2
Class<?>[] getParameterTypes ()
このメソッドの使用例は、getGenericParameterTypes() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
Class<?> getReturnType ()
このメソッドの使用例は、getGenericReturnType() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
TypeVariable<Method>[] getTypeParameters ()
public class Foo {
public <T, U> void aaa(T t, U u) {
}
}
final var method = Foo.class.getDeclaredMethod("aaa", Object.class, Object.class);
System.out.println(method);
System.out.println("-- parameters --");
for (final var param : method.getTypeParameters()) {
System.out.println(param);
}
// 結果
// ↓
//public void Foo.aaa(java.lang.Object,java.lang.Object)
//-- parameters --
//T
//U
int hashCode ()
public static class Foo {
public void aaa() {
}
public void aaa(int num) {
}
public void bbb() {
}
}
final var cls = Foo.class;
final var ret1 = cls.getDeclaredMethod("aaa").hashCode();
System.out.println(ret1); // -409596127
final var ret2 = cls.getDeclaredMethod("aaa", int.class).hashCode();
System.out.println(ret2); // -409596127
final var ret3 = cls.getDeclaredMethod("bbb").hashCode();
System.out.println(ret3); // -409597118
Object invoke (Object obj, Object... args)
public class AccessTest2 {
public String aaa() {
return "abc";
}
private int sum(int a, int b) {
return a + b;
}
}
final var cls = AccessTest2.class;
final var obj = new AccessTest2();
final var method = cls.getDeclaredMethod("aaa");
System.out.println(method); // public java.lang.String AccessTest2.aaa()
System.out.println(method.canAccess(obj)); // true
final var ret = method.invoke(obj);
System.out.println(ret); // abc
final var cls = AccessTest2.class;
final var obj = new AccessTest2();
final var method = cls.getDeclaredMethod("sum", int.class, int.class);
System.out.println(method); // private int AccessTest2.sum(int,int)
System.out.println(method.canAccess(obj)); // false
//method.invoke(obj, 20, 30); // IllegalAccessException
System.out.println(method.trySetAccessible()); // true
System.out.println(method.canAccess(obj)); // true
final var ret = method.invoke(obj, 20, 30);
System.out.println(ret); // 50
boolean isBridge ()
public class Foo {
@Override
public Foo clone() {
throw new UnsupportedOperationException();
}
}
final var cls = Foo.class;
System.out.println("-- methods --");
for (final var method : cls.getDeclaredMethods()) {
System.out.println(method);
System.out.println(method.isBridge());
}
// 結果
// ↓
//-- methods --
//public Foo Foo.clone()
//false
//public java.lang.Object Foo.clone() throws java.lang.CloneNotSupportedException
//true
boolean isDefault ()
public interface Foo {
void aaa();
default void bbb() {
}
}
final var cls = Foo.class;
final var method1 = cls.getDeclaredMethod("aaa");
System.out.println(method1); // public abstract void Foo.aaa()
System.out.println(method1.isDefault()); // false
final var method2 = cls.getDeclaredMethod("bbb");
System.out.println(method2); // public default void Foo.bbb()
System.out.println(method2.isDefault()); // true
boolean isSynthetic ()
public class IsSyntheticTest {
interface Foo<T> {
void aaa(T value);
}
class Bar implements Foo<String> {
@Override
public void aaa(String value) {
}
}
public static void main(String[] args) {
final var cls = Bar.class;
for (final var method : cls.getDeclaredMethods()) {
System.out.println(method);
System.out.println(method.isSynthetic());
}
// 結果
// ↓
//public void IsSyntheticTest$Bar.aaa(java.lang.String)
//false
//public void IsSyntheticTest$Bar.aaa(java.lang.Object)
//true
}
}
boolean isVarArgs ()
public class Foo {
public void aaa(int num) {
}
public void bbb(int num, String... args) {
}
}
final var method = Foo.class.getDeclaredMethod("aaa", int.class);
System.out.println(method); // public void Foo.aaa(int)
System.out.println(method.isVarArgs()); // false
final var method = Foo.class.getDeclaredMethod("bbb", int.class, String[].class);
System.out.println(method); // public void Foo.bbb(int,java.lang.String[])
System.out.println(method.isVarArgs()); // true
void setAccessible (boolean flag)
public class AccessTest {
private int sum(int a, int b) {
return a + b;
}
}
final var cls = AccessTest.class;
final var obj = new AccessTest();
final var method = cls.getDeclaredMethod("sum", int.class, int.class);
System.out.println(method.canAccess(obj)); // false
method.setAccessible(true);
System.out.println(method.canAccess(obj)); // true
method.setAccessible(false);
System.out.println(method.canAccess(obj)); // false
String toGenericString ()
public class Foo<T> {
public void aaa(int num) {
}
public void bbb(T value) {
}
}
final var method = Foo.class.getDeclaredMethod("aaa", int.class);
final var ret1 = method.toGenericString();
System.out.println(ret1); // public void Foo.aaa(int)
final var ret2 = method.toString();
System.out.println(ret2); // public void Foo.aaa(int)
final var method = Foo.class.getDeclaredMethod("bbb", Object.class);
final var ret1 = method.toGenericString();
System.out.println(ret1); // public void Foo.bbb(T)
final var ret2 = method.toString();
System.out.println(ret2); // public void Foo.bbb(java.lang.Object)
String toString ()
このメソッドの使用例は、toGenericString() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
Executableで宣言されたメソッド
getAnnotatedExceptionTypes, getAnnotatedParameterTypes, getAnnotatedReceiverType, getAnnotationsByType, getParameters
「Java API 使用例 : Executable」をご参照ください。
AccessibleObjectで宣言されたメソッド
canAccess, getAnnotations, getDeclaredAnnotation, getDeclaredAnnotationsByType, isAccessible, isAnnotationPresent, setAccessible, trySetAccessible
「Java API 使用例 : AccessibleObject」をご参照ください。
AnnotatedElementで宣言されたメソッド
getAnnotations, getDeclaredAnnotation, getDeclaredAnnotationsByType, isAnnotationPresent
「Java API 使用例 : AnnotatedElement」をご参照ください。