开发者

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().su开发者_JS百科bstring(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();
        }

    }

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 );

Help!


You might want to look at the code for BeanUtils from apache commons. The following functions should be useful cloneBean() and copyProperties().


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 +
                '}';
    }
}


Given a getter Method m,

m.getReturnType().newInstance()

will construct an instance as long as f's type is a public concrete type (not an interface or abstract class) with a public zero argument constructor. And it won't work for public, non-static inner classes.

It also won't work for primitive return types like Integer.TYPE.

So given that large set of caveats, your best bet is to write a method that looks at the return type and creates an object. That way, you can return Collections.emptyList() for the abstract but commonly used return type List, and can return 0 for int.


I think it's much more simple if you want juste get and set value of a field by using methods provides by Java Reflection API rather than trying to get getters and setters method's names manually.

This type of function can be used to get a field value :

Object getFieldValue(Field afield, Object obj){

   if(!field.isAccessible()) field.setAccessible(true);

   return field.get(obj);

}

You can get the value of field by using the field.set(obj,value) ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜