1

I have to create a list of objects, which are configured according to the name of some classes received as input.

For each object I have to call a method, which add an operation that is created dynamically.

However I don't know exactly ho to resolve the problem. Please see an example below.

String className; // this is an input parameter

final Class<?> classType = Class.forName(className);

// here I would like to use classType instead of "?" but it gives me an error.
Task<?> task = TaskFactory.createTask((String)classType.getField("_TYPE").get(null))); 

tasks.put(task, null);

task.addOperation(new Operation<classType>() { // this gives an error 

   @Override
   public void onNewInput(classType input) { // this gives an error 

        System.out.println(input)
   }
});
Filburt
  • 17,626
  • 12
  • 64
  • 115
Maverik
  • 2,358
  • 6
  • 34
  • 44
  • What are Task, TaskFactory and Operation? – Ray Mar 17 '14 at 09:15
  • `classType` is not a type, but an instance of the class `Class`. Your attempt at creating an anonymous class, `new Operation`, is an error, because Java generics work with types. The equivalent of your code with a type known at compile-time is e.g. `new Operation` (which is an error). – Andreas Troelsen Mar 17 '14 at 09:18
  • @Ray Task and Operation are abstract interfaces. TaskFactory is a class. – Maverik Mar 17 '14 at 09:19
  • @AndreasTroelsen Thanks. So is something possible to do, in a different way, or not? – Maverik Mar 17 '14 at 09:21
  • @Maverik It depends on how Task, Operation, and TaskFactory work. If you created them, you can probably sort something out, but I'm not sure if you'll be able to actually inject methods and fields without some sort of "instrumentation", which I'm afraid I can't help with. – Andreas Troelsen Mar 17 '14 at 09:24
  • related: http://stackoverflow.com/questions/4818228/how-to-instantiate-a-java-util-arraylist-with-generic-class-using-reflection - generic types like `` must be known at compile time, `` can't be resolved until runtime so that can't work. – zapl Mar 17 '14 at 09:28

1 Answers1

1

As you can see from the comments, the surrounding infrastructure and the intention are not entirely clear. However, you can achieve a certain degree of type-safety with a "helper" method that captures the type of the given Task, and allows you to work with this type internally:

public class RuntimeType
{
    public static void main(String[] args) throws Exception
    {
        String className = "";
        final Class<?> classType = Class.forName(className);
        Task<?> task = TaskFactory.createTask((String)classType.getField("_TYPE").get(null));
        addOperation(task);
    }

    private static <T> void addOperation(Task<T> task)
    {
        task.addOperation(new Operation<T>() 
        { 
            @Override
            public void onNewInput(T input) 
            { 
                System.out.println(input);
            }
        });        
    }
}

class TaskFactory
{
    public static Task<?> createTask(String string)
    {
        return null;
    }
}

class Task<T>
{
    public void addOperation(Operation<T> operation)
    {
    }

}

interface Operation<T>
{
    void onNewInput(T input);
}
Marco13
  • 53,703
  • 9
  • 80
  • 159