Java反射:实例化具有指定类型的新对象 [英] Java Reflection: Instantiate a new object with specified type
问题描述
我是反思的新手,我尝试用它锻炼......
I'm newbie on reflection and I try to exercise with it...
这是代码......
This is the code...
for (java.lang.reflect.Field field : fields) {
String getter = "get"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
String setter = "set"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
java.lang.reflect.Method getterMethod;
java.lang.reflect.Method setterMethod;
try {
getterMethod = this.getClass().getMethod(getter, null);
Object valueGetted = getterMethod.invoke(this, null);
Class[] paramForSetter = new Class[1];
paramForSetter[0] = valueGetted.getClass();
setterMethod = p.getClass().getMethod(setter, paramForSetter);
setterMethod.invoke(p.getClass(),XXXX);
System.out.println("");
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-Trace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我认为这是一个愚蠢的问题..在XXXXX上,我想要一个类型和值为getted的对象......
I think that is a stupid question.. On XXXXX I want an object with the type and value of value getted...
我认为这很有用......但不是..
I thought that was useful... but is not..
setterMethod.invoke(p.getClass(),(valueGetted.getClass().getName()) valueGetted );
帮助!
推荐答案
正确的代码应如下所示:
注意事项:
- 需要使用getDeclaredFields()
- getters不以get for boolean fields $ b开头$ b - setter.invoke()需要使用正确的参数调用
- 使用field.getType()获取setter [处理原始类型]
The correct code should look as follows: Things to note: - need to use getDeclaredFields() - getters don't start with get for boolean fields - setter.invoke() needs to be called with the correct params - use of field.getType() while getting the setter [handles primitive types]
import java.lang.reflect.Field;
public class Main {
private static MyObject clone(MyObject p) {
final MyObject clone = new MyObject();
Field[] fields = p.getClass().getDeclaredFields();
for (java.lang.reflect.Field field : fields) {
// Boolean properties will hav eis prefix instead of get
String getter = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
String setter = "set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
java.lang.reflect.Method getterMethod;
java.lang.reflect.Method setterMethod;
try {
getterMethod = p.getClass().getMethod(getter, null);
Object valueGetted = getterMethod.invoke(p, null);
Class[] paramForSetter = new Class[1];
paramForSetter[0] = valueGetted.getClass();
setterMethod = p.getClass().getMethod(setter, field.getType());
setterMethod.invoke(clone, valueGetted);
System.out.println(" Successfully copied " + field.getName());
} catch (Exception ex) {
System.err.println(" Error copying " + field.getName() + ": " + ex.getMessage());
}
}
return clone;
}
public static void main(String[] args) {
MyObject m = new MyObject(1, 2L, "3", true);
System.out.println("Main.main: Original = " + m);
MyObject c = clone(m);
System.out.println("Main.main: Clone = " + c);
}
}
class MyObject {
private int myInt;
private Long myLong;
private String myString;
private Boolean myBool;
MyObject() {
}
MyObject(int myInt, Long myLong, String myString, Boolean myBool) {
this.myInt = myInt;
this.myLong = myLong;
this.myString = myString;
this.myBool = myBool;
}
public int getMyInt() {
return myInt;
}
public void setMyInt(int myInt) {
this.myInt = myInt;
}
public Long getMyLong() {
return myLong;
}
public void setMyLong(Long myLong) {
this.myLong = myLong;
}
public String getMyString() {
return myString;
}
public void setMyString(String myString) {
this.myString = myString;
}
public Boolean isMyBool() {
return myBool;
}
public void setMyBool(Boolean myBool) {
this.myBool = myBool;
}
@Override
public String toString() {
return "MyObject{" +
"myInt=" + myInt +
", myLong=" + myLong +
", myString='" + myString + '\'' +
", myBool=" + myBool +
'}';
}
}
这篇关于Java反射:实例化具有指定类型的新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!