2015-04-06 82 views
0

我尝试通过该属性的名称使用javabeans反射设置/获取属性值。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 

如何解决这个错误?

回答

0

ref.getClass()更换ref.class

new PropertyDescriptor("field", ref.getClass()) 

类字面.class仅适用于类型,该类型的不是一个变量,即:

new PropertyDescriptor("field", TestReflection.class) 

注意,这是为什么编译器抛出一个cannot find symbol错误:当它遇到X.class时,它将尝试搜索名为X的类或类型。

0

使用ref.getClass()方法代替ref.class。