Java通過反射實現簡單對象的拷貝

來源:互聯網
上載者:User
代碼實現
package me.andy.practice.annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;public class ReflectUtils {    public static Object copy(Object resource) throws Exception {        Class<? extends Object> classType = resource.getClass();        Object newObject = classType.newInstance();        Field[] declaredFields = classType.getDeclaredFields();        for (Field filed : declaredFields) {            String firstLetter = filed.getName().substring(0, 1).toUpperCase();            String getMethodName = "get" + firstLetter + filed.getName().substring(1);            String setMethodName = "set" + firstLetter + filed.getName().substring(1);            Method getMethod = classType.getMethod(getMethodName, new Class[]{});            Method setMethod = classType.getMethod(setMethodName, new Class[]{filed.getType()});            Object value = getMethod.invoke(resource, new Object[]{});            setMethod.invoke(newObject, new Object[]{value});        }        return newObject;    }}

  • 代碼解釋

Object newObject = classType.newInstance();

等價於

Constructor<? extends Object> constructor = classType.getConstructor(new Class[]{});

Object newObject = constructor.newInstance(new Object[]{});


Field[] declaredFields = classType.getDeclaredFields()返回當前類聲明的所有方法(包括私人共有)

classType.getFields()返回當前類共有方法,包括繼承父類的

測試代碼

package me.andy.practice.reflect;import me.andy.practice.annotation.ReflectUtils;import org.junit.Test;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import static junit.framework.Assert.assertEquals;public class ReflectTest {    @Test    public void test_copy() throws Exception {        Person person = new Person("andy", 1);        Person copyPerson = (Person) ReflectUtils.copy(person);        assertEquals("andy", copyPerson.getName());        assertEquals(Integer.valueOf(1), copyPerson.getAge());    }}

package me.andy.practice.reflect;public class Person {    private String name;    private Integer age;    public Person() {    }    public Person(String name, Integer age) {        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.