当属性中存在静态属性时, 使用反射BeanUtils.getProperty获取属性值就会出错,请问如何忽略某一属性?private static final long serialVersionUID = -123456789L;
 for(Field fields : Ware.class.getDeclaredFields()){
            System.out.println(BeanUtils.getProperty(ware,fields.getName()));
 }java.lang.NoSuchMethodException: Unknown property 'serialVersionUID' on class 'class Ware'
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
at org.apache.commons.beanutils.BeanUtilsBean.getNestedProperty(BeanUtilsBean.java:715)
at org.apache.commons.beanutils.BeanUtilsBean.getProperty(BeanUtilsBean.java:741)
at org.apache.commons.beanutils.BeanUtils.getProperty(BeanUtils.java:382)

解决方案 »

  1.   

    Field fields = Ware.class.getDeclaredFields();
    int index;
    for(int i=0;i<fields.length;i++)
       if(t.getName().equals("serialVersionUID"))
           index = i;
    //remove the fields[index] from fieldsthen do what you want to do
      

  2.   

    for(Field fields : Ware.class.getDeclaredFields()){
                if( Modifier.isStatic(field.getModifiers()))  continue;
                System.out.println(BeanUtils.getProperty(ware,fields.getName()));
     }
      

  3.   


    import java.lang.reflect.Field;
    import java.lang.reflect.Method;public class Test {
        public int id;  
        public static int age;  
        public static void print(String str){  
            System.out.println(str);  
        }  
        public void action(){  
            System.out.println("do action");  
        }  
        
    public static void main(String []args) throws Exception{
    Test cft=new Test();  
        Class clazz=cft.getClass();  
                  
        Field field=clazz.getField("age");  
        field.set(null, 3);  
        System.out.println((Integer)field.get(clazz));  
          
        Field field0=clazz.getField("id");  
        field0.set(cft,4);  
        System.out.println("field id="+field0.getInt(cft));  
          
        Method method=clazz.getMethod("print", String.class);  
        method.invoke(null, "hi,can you see me");  
          
        Method method0=cft.getClass().getMethod("action");  
        method0.invoke(cft);  
    }
    }
    貌似可以也