1

My problem is I do have a class that is created, compiled and initialized at runtime. I did this as writing the file as TestClass which is File f, then compile with:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
          compiler.run(null,null,null,f.getPath());

After that I load my class and use my methods etc. according to this example; http://viralpatel.net/blogs/java-dynamic-class-loading-java-reflection-api/

Now my problem is I need to do this:

MyTask<T> m = new MyTask<T>(0,0,0);

T should be my dynamically created class instead of Integer.class etc. However I couldn't find a way for it yet. If you do examine the example at the link I gave above, you will see I can have a instance of my class as in Object form and I can a Class instance for my dynamic class. However, whatever I tried I couldn't find the solution for this.

It keeps saying that Class myClass, can not be used as a type. So how can I use this dynamically created class as a type. Thank you very much.

mtilhan
  • 258
  • 4
  • 12
  • you can have the solution here in this link [link](http://stackoverflow.com/questions/2320404/creating-classes-dynamically-with-java) – Abdelhak Nov 17 '15 at 18:41

2 Answers2

2

There is no useful way to express in source code a type that does not exist at compile time. It would not anyway gain you anything more than using Object as a type parameter could do, because Java generics provide compile-time type checking, not run-time type checking.

It might be that your purposes could be served by creating an interface that your dynamic class will implement, and using the interface type as your type parameter.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • My dynamically created class will contain N amount of variable (they will be primitive but there is no limit from my side). Is there any chance that I can use a static interface and access all variables from the interface if I use that interface as generics? – mtilhan Nov 17 '15 at 19:14
  • Only if the variables are `static` and `final`. Interfaces do not have instance variables or mutable static variables. You can can put accessor methods on your interface, however, and have your dynamic class implement those methods to provide access. – John Bollinger Nov 17 '15 at 19:51
  • It won't work for me. I need more flexibility. Variables will be useless to me if they are static or final and I can't try to put accessor methods for every variable since I don't know how many will be and Interface should be static. Any chance that I can access from super class to child class? – mtilhan Nov 17 '15 at 20:15
  • You can use a superclass as the type parameter but that does not help you access members of subclasses, except via methods defined by the superclass (and possibly overridden by the subclass). – John Bollinger Nov 17 '15 at 20:25
  • Overall, if you don't know at compile time the names and types of the members you want to access, then the only alternative I see for accessing them at all is reflection (or JNI, which amounts to the same thing). If you plan to access them that way, then type parameters are moot -- you might as well just use `Object`, or avoid type parameterization altogether. – John Bollinger Nov 17 '15 at 20:28
  • Actually I am intending to avoid altogether but problem is I am writing this code on a API which has Tasks that uses type parameterization so I need to be able to send it :(. – mtilhan Nov 17 '15 at 20:35
  • I'm inclined to think that you should rethink your approach altogether. If you stick with dynamically-generated classes, however, then I've already covered your available options. You can specify as type parameter any type that your dynamic class inherits from -- an ancestor class or an interface it or one of its ancestors implements. `Object` is certainly one such, and probably the easiest. Apparently this will provide no advantage to you other than to silence the compiler. – John Bollinger Nov 17 '15 at 20:41
0

You can't due to the fact that the generic type information is available only at compile time. When you create a class at runtime, there's no generic information available anywhere any more.

If your class implements an interface, you should use that as the type in code. Something along the lines of

MyInterface foo = myDynamicClass.newInstance();
someGenericMethod(foo);

public <T extends MyInterface> void someGenericMethod(T param) {}
// Or more likely, if there's no other classes that extend MyInterface
public void someGenericMethod(MyInterface param) {}

Of course it may not make any sense to even bother with generic type information, since it's used for static type checking and you're working with a dynamic class.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • My dynamically created class will contain N amount of variable (they will be primitive but there is no limit from my side). Is there any chance that I can use a static interface and access all variables from the interface if I use that interface as generics? – mtilhan Nov 17 '15 at 19:32