@StaticMetamodel(Test.class)
public class Test_{
public static volatile SingularAttribute<Test, Boolean> var1;
public static volatile SingularAttribute<Test, Boolean> var2;
public static volatile SingularAttribute<Test, Boolean> var3;
}
Test_.var1
Test_.var2
Test_.var3
但我如何通过循环浏览列表来做到这一点:
List<String> vars = new ArrayList<String>();
vars.add("var1");
vars.add("var2");
vars.add("var3");
for (int i=0; i < vars.size(); i++) {
String var = vars.get(i).toString();
System.out.println(var); // prints: var1, var2, var3
// How? (s. update below)
}
var
.
使现代化
我尝试使用java反射,正如
@naXa
// suppose to be
System.out.println(Test_.var1); // com.example.entities.Test.var1
System.out.println(Test_.var1.getType()); // boolean
// looping through list
Class object = Test_.class;
try {
Field field = object.getDeclaredField(var);
field.setAccessible(true); // public static volatile javax.persistence.metamodel.SingularAttribute com.example.entities.Test.var1
System.out.println(field); // com.example.entities.Test.var1
System.out.println(field.getType()); // interface javax.persistence.metamodel.SingularAttribute
// but <field> is still no Expression <Boolean>
} catch(NoSuchFieldException e) {
System.out.println("Error");
}
它不会返回正确的名称或类型(请参阅代码中的输出注释)。