这样的事情怎么样?
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());
}
}
}