我正在尝试将两个已知类型的泛型(accumulationFunction
,resultBindings
)传递到下面的反射调用例程中,但我遇到了麻烦。有人可以帮我理解如何实现这一目标吗?
package com.amir.method.compiler;
//hidden imports
import java.util.Set;
public class CompiledReferencedAttributeMethod implements CompiledMethod {
final Class<?> generatedClazz;
//how do i pass the arguments below into the invoke routine??
final KnownWorkData<AccumulationFunction> accumulationFunction;
final KnownWorkData<Set<Executable<InstanceSetValue>>> resultBindings;
public CompiledReferencedAttributeMethod(final int hash,
final KnownWorkData<AccumulationFunction> accumulationFunction,
final KnownWorkData<Set<Executable<InstanceSetValue>>> resultBindings) {
this.generatedClazz = ReferencedAttributeMethodGenerator.get().compileMethod(
"com.amir.hotspot.GeneratedGRAMethod" +hash, "dynamicGra", accumulationFunction, resultBindings);
this.accumulationFunction = accumulationFunction;
this.resultBindings = resultBindings;
}
@Override
public WorkDataValue invokeCompiled(final Instance onInst,
final ExecutionParms parm,
final WorkDataDefinition returntype,
final TaskContext context) {
try {
return (WorkDataValue) this.generatedClazz
.getMethod("dynamicGra",
Instance.class,
ExecutionParms.class,
WorkDataDefinition.class,
TaskContext.class)
.invoke(null, onInst, parm, returntype, this.accumulationFunction, this.resultBindings, context);
} catch(Exception e) {
throw new ExecuteCompiledMethodException(this.toString(), e);
}
}
}
答案 0 :(得分:2)
由于您无法使用泛型和不同参数重载方法(请参阅:Oracle’s tutorial “Restrictions on Generics”),因此您不必使用反射API来处理泛型。
没有歧义,你可以简单地得到你的方法:
Method m = this.generatedClazz.getMethod("dynamicGra",
Instance.class,
ExecutionParms.class,
WorkDataDefinition.class,
KnownWorkData.class,
KnownWorkData.class,
TaskContext.class);
然后你用null调用,这意味着dynamicGra 必须是静态的,如果不是这种情况你必须传递你想要调用方法的generatedClazz实例: / p>
Object instance = this.generatedClazz.newInstance(); // A new one or any other reference of generatedClazz
m.invoke(instance, onInst, parm, returntype, this.accumulationFunction, this.resultBindings, context);