粗略地说,在Java中,匿名类提供了一种闭包:可以从匿名类内部访问(final)变量。在下文中,我将这种变量称为“闭包变量”。
根据此处的解释,匿名类的构造函数签名会因这些变量是常量还是非常量而有所不同。
是否有一种优雅的方式可以反射性地访问非常量“闭包变量”?
以下是展示这个挑战的测试类:
请注意,对于测试用例
根据此处的解释,匿名类的构造函数签名会因这些变量是常量还是非常量而有所不同。
是否有一种优雅的方式可以反射性地访问非常量“闭包变量”?
以下是展示这个挑战的测试类:
package com.example;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
public class ReflectiveInstantiationAnonymousClassTest {
@Test
public void testInstantiateConstantAnonymousClassReflectively() {
System.out
.println("About to define anonymous class with constant closure");
final String constantString = "constant String";
System.out.println("constantString is " + constantString);
Class<? extends Object> clazz = new Object() {
public String toString() {
return "Hello with " + constantString;
}
}.getClass();
Object anonymousClassInstance1 = instantiateAnonymousClass(clazz);
Object anonymousClassInstance2 = instantiateAnonymousClass(clazz);
System.out.println(anonymousClassInstance1);
System.out.println(anonymousClassInstance2);
}
@Test
public void testInstantiateNonConstantAnonymousClassReflectively() {
System.out
.println("About to define anonymous class with variable closure");
final String variableString = String.valueOf( System.currentTimeMillis() );
System.out.println("variableString is " + variableString);
Class<? extends Object> clazz = new Object() {
public String toString() {
return "Hello with " + variableString;
}
}.getClass();
Object anonymousClassInstance1 = instantiateAnonymousClass(clazz);
Object anonymousClassInstance2 = instantiateAnonymousClass(clazz);
System.out.println(anonymousClassInstance1);
System.out.println(anonymousClassInstance2);
}
@SuppressWarnings("unchecked")
private <T> T instantiateAnonymousClass(Class<T> clazz) {
T instance = null;
Constructor<?>[] allConstructors = clazz.getDeclaredConstructors();
System.out.println("-+-" + allConstructors.length + " constructor(s) defined by class " + clazz.getName() );
for(Constructor<?> constructor : allConstructors) {
System.out.println(" +- a constructor with " + constructor.getParameterTypes().length + " parameter(s): " + constructor.toGenericString() );
}
System.out.println();
System.out.println("Instantiating anonymous class");
try {
instance = (T) clazz.getDeclaredConstructors()[0].newInstance(this);
// .
// /|\
// how can I provide a variable closure variable there?--------+
} catch (InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException
| SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return instance;
}
}
测试用例testInstantiateConstantAnonymousClassReflectively
执行良好,并输出以下内容:
About to define anonymous class with constant closure
constantString is constant String
-+-1 constructor(s) defined by class com.example.ReflectiveInstantiationAnonymousClassTest$1
+- a constructor with 1 parameter(s): com.example.ReflectiveInstantiationAnonymousClassTest$1(com.example.ReflectiveInstantiationAnonymousClassTest)
Instantiating anonymous class
-+-1 constructor(s) defined by class com.example.ReflectiveInstantiationAnonymousClassTest$1
+- a constructor with 1 parameter(s): com.example.ReflectiveInstantiationAnonymousClassTest$1(com.example.ReflectiveInstantiationAnonymousClassTest)
Instantiating anonymous class
Hello with constant String
Hello with constant String
但是测试用例testInstantiate
Non
ConstantAnonymousClassReflectively
打印出以下内容:
About to define anonymous class with variable closure
variableString is 1371946280882
-+-1 constructor(s) defined by class com.example.ReflectiveInstantiationAnonymousClassTest$2
+- a constructor with 2 parameter(s): com.example.ReflectiveInstantiationAnonymousClassTest$2(com.example.ReflectiveInstantiationAnonymousClassTest,java.lang.String)
Instantiating anonymous class
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.example.ReflectiveInstantiationAnonymousClassTest.instantiateAnonymousClass(ReflectiveInstantiationAnonymousClassTest.java:60)
at com.example.ReflectiveInstantiationAnonymousClassTest.testInstantiateNonConstantAnonymousClassReflectively(ReflectiveInstantiationAnonymousClassTest.java:43)
请注意,对于测试用例
testInstantiateNonConstantAnonymousClassReflectively ,将生成一种不同类型的构造函数。此构造函数需要一个额外的String
参数,因为variableString
无法“烘焙”,只能在运行时确定。