Search code examples
sun-codemodeljcodemodel

Create a method which returns the class that i want to generate in Codemodel


I need to generate a class which must have a method that returns an object of the same class.

I want to generate something like this

public class A{
   public A method1(){
       ...
   }
 }

The problem is that I have to pass the Class object of the class being returned by the method. If i do that, because my class is not built yet I will get a ClassNotFoundException. Is there a way of achieving this?

Thanks.


Solution

  • After a little digging I managed to find the solution. If you do not have access to a class and thus you cannot provide its corresponding Class object, use the method directClass(String className) from Codemodel object which takes as input a String representing the class name and returns the corresponding JClass object.