我尝试使用javabeans反射来根据属性的名称来设置/获取属性的值。
当我试图编译这段代码时
class TestReflection
{
public TestReflection()
{
}
private Integer field;
public Integer getField()
{
return this.field;
}
public void setField(Integer x)
{
this.field = x;
}
}
// .
// .
// .
TestReflection ref = new TestReflection();
Object value = new PropertyDescriptor("field",
ref.class).getReadMethod().invoke(ref); // ERROR我发现了一个错误:
Test.java:84: error: cannot find symbol
ref.class).getReadMethod().invoke(ref);
symbol: class ref我怎样才能纠正这个错误?
发布于 2015-04-06 11:30:51
将ref.class替换为ref.getClass()
new PropertyDescriptor("field", ref.getClass())类文本.class仅在类型上可用,而不是该类型的变量,即:
new PropertyDescriptor("field", TestReflection.class)请注意,这就是编译器抛出cannot find symbol错误的原因:当它遇到X.class时,它尝试搜索一个名为X的类或类型。
发布于 2015-04-06 11:32:10
使用ref.getClass()方法而不是ref.class。
https://stackoverflow.com/questions/29470360
复制相似问题