Figure the parameter to pass of the invocation of a method via reflection

牧云@^-^@ 提交于 2019-12-25 06:56:29

问题


I have a set of classes that implement generic interface such as:

public inteface CustomGenerator {  
   Object generate(Reader r);  
} 

These classes can process the parameter accordingly and generate the response in the expected type.
I have a base class that I need to populate. To populate class I use these custome generator classes and reflection.

public static void copy(CustomClass target, String tag, Reader reader) {  
   Object input = CustomGenerators.get(tag).generate(reader); 
   String setter = setterNames.get(tag);   
   Method m = target.getClass().getMethod(setter, input.getClass());  
   m.invoke(target, input);   
}  

}

Exception handling omitted for clarity. This works but I want the address the following:
Problem:
Some of the types passed in don't have a custom generator to parse them because they are essentially primitives and 1) I didn't want to create many small classes just to do something trivial like Integer.valueOf(reader.nextString());
On the other side, when I call the target.getClass().getMethod I need the parameter type and I need to handle the cases of primitive values.
So I would need somehow to figure out here:
target.getClass().getMethod(setter, input.getClass()); what is the second parameter I need to pass.

How can I cleanly fix this?


回答1:


How about something like this?

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class Main {
    static final Map<String, CustomGenerator> generators = new HashMap<String, CustomGenerator>();
    static final Map<String, String> setterNames = new HashMap<String, String>();
    static final Map<Class<?>, Class<?>> wrappers = new HashMap<Class<?>, Class<?>>();

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        wrappers.put(int.class, Integer.class);
        wrappers.put(double.class, Double.class);
        wrappers.put(boolean.class, Boolean.class);
        //...

        generators.put("int", new PrimitiveCustomGenerator(int.class));
        generators.put("double", new PrimitiveCustomGenerator(double.class));
        generators.put("boolean", new PrimitiveCustomGenerator(boolean.class));
        //...

        setterNames.put("int", "setInt");
        //...

        CustomClass holder = new CustomClass();
        System.out.println(holder.intValue);

        copy(holder, "int", new Reader());
        System.out.println(holder.intValue);
    }

    static void assertThat(boolean condition) {
        if (!condition) {
            throw new IllegalStateException();
        }
    }

    public static class CustomClass {
        private int intValue;

        public void setInt(int intValue) {
            this.intValue = intValue;
        }
    }

    public static void copy(CustomClass target, String tag, Reader reader) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        CustomGenerator generator = generators.get(tag);
        Object input = generator.generate(reader);

        String setter = setterNames.get(tag);
        Method m = target.getClass().getMethod(setter, generator.getTargetClass());
        m.invoke(target, input);
    }

    public static class Reader {
        public String nextString() {
            return "22";
        }
    }

    public interface CustomGenerator {
        Class getTargetClass();
        Object generate(Reader r) throws InvocationTargetException, IllegalAccessException;
    }

    public abstract static class AbstractCustomGenerator implements CustomGenerator {
        protected Class<?> targetClass;

        AbstractCustomGenerator(Class<?> targetClass) {
            this.targetClass = targetClass;
        }

        public Class<?> getTargetClass() {
            return targetClass;
        }
    }

    public static class PrimitiveCustomGenerator extends AbstractCustomGenerator {

        private final Method valueOfMethod;
        private final Class<?> wrapperClass;

        PrimitiveCustomGenerator(Class<?> targetClass) throws NoSuchMethodException {
            super(targetClass);
            assertThat(targetClass.isPrimitive());

            // use google-guava lib
            // Class wrapperClass = com/google/common/primitives/Primitives.wrap(targetClass);
            wrapperClass = wrappers.get(targetClass);

            valueOfMethod = wrapperClass.getMethod("valueOf", String.class);
            assertThat(Modifier.isStatic(valueOfMethod.getModifiers()));
        }

        public Object generate(Reader r) throws InvocationTargetException, IllegalAccessException {
            return valueOfMethod.invoke(wrapperClass, r.nextString());
        }
    }
}


来源:https://stackoverflow.com/questions/34886382/figure-the-parameter-to-pass-of-the-invocation-of-a-method-via-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!