给定下面的实体-是否有可能避免使用/***** Get rid of this anonymous inner class ******/标记的匿名内部类。
问题在于“ SomeEntity”可以包含数百个字段,并且有多个实现此接口的类,每个类都需要元信息。这意味着开始生成成千上万个匿名内部类,这导致我的构建时间真的非常慢,并且随着类加载器加载所有类,系统的启动时间也非常慢。

我不能使用反射,因为我的类在发布前就被混淆了

public interface SomeEntity {

    ///// boolean someField
    // getter
    void someField(boolean someField);

    // setter
    boolean someField();

    final static String someField = "someField";

    ///// boolean someField2
    // getter
    void someField2(int someField2);

    // setter
    int someField2();

    final static String someField2 = "someField2";

}


public class SomeEntityImpl implements SomeEntity {
    boolean someField = false;
    int someField2 = 0;

    @Override
    public void someField(boolean someField) {
        this.someField = someField;
    }

    @Override
    public boolean someField() {
        return this.someField;
    }

    @Override
    public void someField2(int someField2) {
        this.someField2 = someField2;
    }

    @Override
    public int someField2() {
        return someField2;
    }
}


public class SomeEntityMeta {

    public static interface Meta<T> {
        T get(SomeEntity x);
        void set(SomeEntity x, T t);
        void copy(SomeEntity from, SomeEntity to);
    }

    Meta<Boolean> metaForSomeField = new Meta<Boolean>() {

        @Override
        public Boolean get(SomeEntity x) {
            return x.someField();
        }

        @Override
        public void set(SomeEntity someEntity, Boolean newFieldValue) {
            someEntity.someField(newFieldValue);
        }

        @Override
        public void copy(SomeEntity from, SomeEntity to) {
            to.someField(from.someField());
        }

    };
    Meta<Integer> metaForSomeField2 = new Meta<Integer>() {

        @Override
        public Integer get(SomeEntity someField2) {
            return someField2.someField2();
        }

        @Override
        public void set(SomeEntity someEntity, Integer newFieldValue) {
            someEntity.someField2(newFieldValue);
        }

        @Override
        public void copy(SomeEntity from, SomeEntity to) {
            to.someField2(from.someField2());
        }

    };

    Map<String, Meta<?>> metaMapForAllFields = new HashMap<String, Meta<?>>() {
        private static final long serialVersionUID = 1L;
        {
            put(SomeEntity.someField, metaForSomeField);
            put(SomeEntity.someField2, metaForSomeField2);
        }
    };

}
    // used as follows:


        public final static void main(String[] args) {
            SomeEntity someEntity = new SomeEntityImpl();
            SomeEntityMeta entityMeta = new SomeEntityMeta();
            ///
            Map<String, Object> fieldNameValueMap = getFieldNameValueMapFromExternalsSystem();

            for ( Entry<String, Object> fieldNameValueEntry : fieldNameValueMap.entrySet()) {
                Meta<Object> meta = (Meta<Object>) entityMeta.metaMapForAllFields.get(fieldNameValueEntry.getKey());
                meta.set(someEntity, fieldNameValueEntry.getValue());
            }
        }

最佳答案

我建议使SomeEntity由简单对象组成,这将消除对您的小元类的需求。

(很快就会有人提到反射或JavaBeans,我建议先尝试解决设计问题。)

09-15 21:00