2013-08-17 12 views
2

我想遍历类字段以下列方式字段的字段值如何让这个类

我有类

public class Parent{ 

String name; 
String lastName; 

} 

public class Child extends Parent{ 

int childNumber; 
} 

在我想有一个父类“得到“方法

孩子将继承

此方法将通过它的名字

返回一个字段的值

我如何检索该字段的值?

我想的方法是这样的:

public Object get(String key){ 
      Field field; 
     Object result = null; 
     try { 
      field = this.getClass().getDeclaredField(key); 
      if(field!=null) { 
       field.setAccessible(true); 
      } 
      result = field.get(this); // this is where my problem, i don't know how to retrieve the field's value 
     } catch (NoSuchFieldException | SecurityException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

堆栈跟踪

java.lang.IllegalArgumentException: Can not set int field com.database.User.userID to java.lang.String 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) 
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source) 
    at sun.reflect.UnsafeIntegerFieldAccessorImpl.getInt(Unknown Source) 
    at sun.reflect.UnsafeIntegerFieldAccessorImpl.get(Unknown Source) 
    at java.lang.reflect.Field.get(Unknown Source) 
    at com.database.DatabaseObject.get(DatabaseObject.java:106) 
    at com.database.DatabaseObject.set(DatabaseObject.java:128) 
    at com.database.DatabaseObject.getInsertPreparedStatement(DatabaseObject.java:64) 
    at com.database.Database.insert(Database.java:95) 
    at com.lenabru.webservice.ElectronicArenaWebService.register(ElectronicArenaWebService.java:21) 
    at com.database.Main.main(Main.java:29) 
+0

什么对您提供的代码段不起作用? – hexafraction

+0

我把堆栈跟踪的帖子中,java认为我想设置字段,并没有得到它 –

+0

你可能会展示你如何使用代码? (这是什么'这是指和您试图访问的字段的名称?) –

回答

1

为了让您将有这个堆栈跟踪有一个字符串值传递给field.get,不this。做一个完整的重建和测试。

以下是UnsafeFieldAccessorImpl类的相关方法,如您所见,如果您尝试传入字符串而不是可从声明字段的类中分配的对象,则只能获得给定的,令人困惑的错误消息:

protected void ensureObj(Object o) { 
    // NOTE: will throw NullPointerException, as specified, if o is null 
    if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) { 
     throwSetIllegalArgumentException(o); 
    } 
} 

protected void throwSetIllegalArgumentException(Object o) { 
    throwSetIllegalArgumentException(o != null ? o.getClass().getName() : "", ""); 
} 

protected void throwSetIllegalArgumentException(String attemptedType, 
               String attemptedValue) { 
    throw new IllegalArgumentException(getSetMessage(attemptedType,attemptedValue)); 
} 

protected String getSetMessage(String attemptedType, String attemptedValue) { 
    String err = "Can not set"; 
    if (Modifier.isStatic(field.getModifiers())) 
     err += " static"; 
    if (isFinal) 
     err += " final"; 
    err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " to "; 
    if (attemptedValue.length() > 0) { 
     err += "(" + attemptedType + ")" + attemptedValue; 
    } else { 
     if (attemptedType.length() > 0) 
      err += attemptedType; 
     else 
      err += "null value"; 
    } 
    return err; 
} 
0

也许,如果你想获得访问bean字段使用BeanUtilscommons-beanutils最简单的方法:

System.out.println(BeanUtils.getProperty(child, "name")); // get value from property 

BeanUtils.setProperty(c, "lastName", "Ivan Ivanov"); // set value to property 
1

我建议你做的两件事情之一:

  1. 如果你真正的代码有尽可能少的领域,你在这里展示的例子中,为每个字段添加getXxx()方法。例如,Parent应该有getName()(应该是getFirstName()?)和getLastName()方法,而Child应该有getChildNumber()

  2. 如果你的代码比这更复杂,使用Map,并通过委托给Map实现你get()set()方法。

反思供编程人员构建需要在非常低的水平,以操纵对象复杂的工具。我们其他人通常有更好的解决方案。