Comprehensive Sun CodeModel Method

Does anyone know to generate the following generic method declaration using CodeModel:

public <T> T getValue(Class<T> clazz){...} 

using:

 ValueType value = getValue(ValueType.class); 

It seems that it is not handled by the existing implication.

I know that I can process the code as follows, but it requires a throw:

 public Object getValue(Class class){...} 

using:

 ValueType value = (ValueType)getValue(ValueType.class); 

Obviously, this is a bit dirty due to the actor.

+7
source share
1 answer

Create a method with the return type of Object , create a method, then overwrite the return type.

 final JDefinedClass exampleClass = codeModel._class( "com.example.ExampleClass" ); final JMethod method = exampleClass.method( JMod.PUBLIC, Object.class, "getValue" ); final JTypeVar t = method.generify( "T" ); method.type( t ); method.param( codeModel.ref( Class.class ).narrow( t ), "type" ); method.body()._return(JExpr._null()); 
+11
source

All Articles