0

I am trying to pass two generics with known types (accumulationFunction, resultBindings) into the reflective invoke routine below, but I'm having trouble. Can someone help me understand how this can be achieved?

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);
        }
    }

}
7
  • 2
    You are generating a class and are calling the generated code then via Reflection? Why don’t you let the generated class implement an existing interface that allows you to invoke the method without Reflection? Commented Oct 28, 2014 at 16:39
  • 1
    You should add the exception trace. But something strange : dynamicGra must be a static method (you invoke with null value). Then you asked for a 4 args method and you invoke it with 6 args... Commented Oct 28, 2014 at 16:40
  • @nomoa, What I'm asking specifically is, how do I pass those two missing parameters to getMethod Commented Oct 28, 2014 at 16:45
  • @Holger I suppose I could do that in the next iteration when I get this working, thats a good idea. Commented Oct 28, 2014 at 16:46
  • Generic information is lost at runtime. Maybe I missed something but getMethod should be called like that :getMethod("dynamicGra", Instance.class, ExecutionParms.class, WorkDataDefinition.class, KnownWorkData.class, KnownWorkData.class, TaskContext.class) Commented Oct 28, 2014 at 16:49

1 Answer 1

2

As you cannot overload method with generics and different parameters (see: Oracle’s tutorial “Restrictions on Generics”) you don't have to bother about generics with the reflection API.

There is no ambiguity, you can simply get your method like that:

Method m = this.generatedClazz.getMethod("dynamicGra",
                            Instance.class,
                            ExecutionParms.class,
                            WorkDataDefinition.class,
                            KnownWorkData.class,
                            KnownWorkData.class,
                            TaskContext.class);

Then you invoked with null so it means that dynamicGra must be static, if it's not the case you must pass the instance of generatedClazz on which you want to call the method :

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.