如何使用Reflection复制对象内容而不是对象引用?

时间:2016-09-30 16:53:23

标签: java reflection

我尝试使用代码copyCustom()复制对象并使用原始对象内容创建新对象。

一旦我得到新对象。我使用get / set方法修改了一些内容(字符串内容),不幸的是原始对象的内容也发生了变化。

换句话说,新对象指的是原始对象。我只需要一个深度复制的对象,而不需要任何对原始对象的引用。

    @SuppressWarnings("unchecked")
private <T> T copyCustom(T entity) throws IllegalAccessException, InstantiationException {
    Class<?> clazz = entity.getClass();
    T newEntity = (T) entity.getClass().newInstance();

    while (clazz != null) {
        copyFields(entity, newEntity, clazz);
        clazz = clazz.getSuperclass();
    }

    return newEntity;
}

private <T> T copyFields(T entity, T newEntity, Class<?> clazz) throws IllegalAccessException {
    List<Field> fields = new ArrayList<Field>();
    for (Field field : clazz.getDeclaredFields()) {
        fields.add(field);
    }
    for (Field field : fields) {

        field.setAccessible(true);
        // next we change the modifier in the Field instance to
        // not be final anymore, thus tricking reflection into
        // letting us modify the static final field
        Field modifiersField;
        try {
            modifiersField = Field.class.getDeclaredField("modifiers");
             modifiersField.setAccessible(true);
                int modifiers = modifiersField.getInt(field);
                // blank out the final bit in the modifiers int
                modifiers &= ~Modifier.FINAL;
                modifiersField.setInt(field, modifiers);
                field.set(newEntity, field.get(entity));
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    return newEntity;
}

public class Product extends ProductDataObject
{
    private static final long   serialVersionUID = -2574755579959571978L;

     private InvestmentProductKey                                   fInvestmentProductKey                   = null;
private ProductKey                                          fProductKey                             = null;
private LabelDouble                                             fTotalNetOfAssets                       = null;
private LabelString                                             fPrimaryBenchmark                       = null;
private LabelBoolean                                            fFundAgeLT3Mth                          = null;
private LabelBoolean                                            fFundAgeBTW3Mth12Mth                    = null;
private LabelBoolean                                            fFundAgeBTW12Mth36Mth                   = null;
private LabelBoolean                                            fFundAgeBTW36Mth60Mth                   = null;
private LabelBoolean                                            f
private LabelString[]                                           fGeography                              = null;
private LabelBoolean                                            fCapacityAvailableIndicator             = null;
private LabelString                                             fEquityCapitalization                   = null;
private LabelDate                                               fFiscalYearEndDate                      = null;
private LabelString                                             fFundStatus                             = null;
private LabelDate                                               fExpireDateContractExpenseLimits        = null;
private LabelDouble                                             fMaximumExpenseRatio                    = null;
private LabelString                                             fInvestments                            = null;

private YieldsAndDividends[]                                    fYieldsAndDividends                     = null;

}

0 个答案:

没有答案