绘制通过反射传递方法的参数 [英] Figure the parameter to pass of the invocation of a method via reflection
问题描述
我有一组实现通用接口的类,例如:
I have a set of classes that implement generic interface such as:
public inteface CustomGenerator {
Object generate(Reader r);
}
这些类可以相应地处理参数,并在预期类型中生成响应。
我有一个我需要填充的基类。为了填充类我使用这些custome生成器类和反射。
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);
}
}
为了清楚起见省略异常处理。
这是有效的,但我希望地址如下:
问题:
某些类型
s传入没有自定义生成器来解析它们,因为它们基本上是原语,1)我不想创建许多小类只是为了做一些简单的事情,如 Integer.valueOf (reader.nextString());
另一方面,当我调用 target.getClass()。getMethod
我需要参数类型,我需要处理原始值的情况。
所以我需要以某种方式找出这里:
target.getClass ().getMethod(setter,input.getClass());
什么是我需要传递的第二个参数。
Exception handling omitted for clarity.
This works but I want the address the following:
Problem:
Some of the type
s 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.
如何清理这个问题?
推荐答案
像这样?
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());
}
}
}
这篇关于绘制通过反射传递方法的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!