Java Reflectionを使用して実行時にメソッドを呼び出す
1. 概要
この短い記事では、invoke methods at runtime using the Java Reflection APIを実行する方法について簡単に説明します。
2. 準備をする
次の例で使用する簡単なクラスを作成しましょう。
public class Operations {
public double publicSum(int a, double b) {
return a + b;
}
public static double publicStaticMultiply(float a, long b) {
return a * b;
}
private boolean privateAnd(boolean a, boolean b) {
return a && b;
}
protected int protectedMax(int a, int b) {
return a > b ? a : b;
}
}
3. Methodオブジェクトの取得
まず、呼び出したいメソッドを反映するMethodオブジェクトを取得する必要があります。 メソッドが定義されている型を表すClassオブジェクトは、これを行う2つの方法を提供します。
3.1. getMethod()
getMethod()を使用して、クラスまたはそのスーパークラスで定義されている静的メソッドまたはインスタンスであるパブリックメソッドを見つけることができます。
最初の引数としてメソッド名を受け取り、その後にメソッドの引数のタイプを受け取ります。
Method sumInstanceMethod
= Operations.class.getMethod("publicSum", int.class, double.class);
Method multiplyStaticMethod
= Operations.class.getMethod(
"publicStaticMultiply", float.class, long.class);
3.2. getDeclaredMethod()
getDeclaredMethod()を使用して、クラスで定義されている任意のメソッドを取得できます。 これには、パブリックメソッド、保護されたアクセス、デフォルトアクセス、プライベートメソッドさえ含まれますが、継承されたメソッドは除外されます。
getMethod()と同じパラメータを受け取ります。
Method andPrivateMethod
= Operations.class.getDeclaredMethod(
"privateAnd", boolean.class, boolean.class);
Method maxProtectedMethod
= Operations.class.getDeclaredMethod("protectedMax", int.class, int.class);
4. メソッドの呼び出し
Methodインスタンスが配置されたら、invoke()を呼び出して、基になるメソッドを実行し、返されたオブジェクトを取得できます。
4.1. インスタンスメソッド
インスタンスメソッドを呼び出すには、invoke()の最初の引数は、呼び出されるメソッドを反映するMethodのインスタンスである必要があります。
@Test
public void givenObject_whenInvokePublicMethod_thenCorrect() {
Method sumInstanceMethod
= Operations.class.getMethod("publicSum", int.class, double.class);
Operations operationsInstance = new Operations();
Double result
= (Double) sumInstanceMethod.invoke(operationsInstance, 1, 3);
assertThat(result, equalTo(4.0));
}
4.2. 静的メソッド
これらのメソッドはインスタンスを呼び出す必要がないため、最初の引数としてnullを渡すことができます。
@Test
public void givenObject_whenInvokeStaticMethod_thenCorrect() {
Method multiplyStaticMethod
= Operations.class.getDeclaredMethod(
"publicStaticMultiply", float.class, long.class);
Double result
= (Double) multiplyStaticMethod.invoke(null, 3.5f, 2);
assertThat(result, equalTo(7.0));
}
5. メソッドのアクセシビリティ
By default, not all reflected methods are accessible.これは、JVMがそれらを呼び出すときにアクセス制御チェックを実施することを意味します。
たとえば、定義クラスの外部のプライベートメソッド、またはサブクラスまたはそのクラスのパッケージの外部から保護されたメソッドを呼び出そうとすると、IllegalAccessExceptionが取得されます。
@Test(expected = IllegalAccessException.class)
public void givenObject_whenInvokePrivateMethod_thenFail() {
Method andPrivateMethod
= Operations.class.getDeclaredMethod(
"privateAnd", boolean.class, boolean.class);
Operations operationsInstance = new Operations();
Boolean result
= (Boolean) andPrivateMethod.invoke(operationsInstance, true, false);
assertFalse(result);
}
@Test(expected = IllegalAccessException.class)
public void givenObject_whenInvokeProtectedMethod_thenFail() {
Method maxProtectedMethod
= Operations.class.getDeclaredMethod(
"protectedMax", int.class, int.class);
Operations operationsInstance = new Operations();
Integer result
= (Integer) maxProtectedMethod.invoke(operationsInstance, 2, 4);
assertThat(result, equalTo(4));
}
By calling setAccesible(true) on a reflected method object, the JVM suppresses the access control checksであり、例外をスローせずにメソッドを呼び出すことができます。
@Test
public void givenObject_whenInvokePrivateMethod_thenCorrect() {
// ...
andPrivateMethod.setAccessible(true);
// ...
Boolean result
= (Boolean) andPrivateMethod.invoke(operationsInstance, true, false);
assertFalse(result);
}
@Test
public void givenObject_whenInvokeProtectedMethod_thenCorrect() {
// ...
maxProtectedMethod.setAccessible(true);
// ...
Integer result
= (Integer) maxProtectedMethod.invoke(operationsInstance, 2, 4);
assertThat(result, equalTo(4));
}
6. 結論
この簡単な記事では、リフレクションを使用して実行時にクラスのインスタンスメソッドと静的メソッドを呼び出す方法を説明しました。 また、リフレクトされたメソッドオブジェクトのアクセス可能フラグを変更して、プライベートメソッドおよび保護されたメソッドを呼び出すときにJavaアクセス制御チェックを抑制する方法も示しました。
いつものように、サンプルコードはover on Githubにあります。