javaee / jaxb-codemodel

CodeModel is a Java library for code generators. This content is migrated into JAXB RI. This is for legacy viewing only
https://javaee.github.io/jaxb-codemodel/
Other
40 stars 20 forks source link

generating generic method #4

Open glassfishrobot opened 13 years ago

glassfishrobot commented 13 years ago

Problem is generating a generic method like: public T foo(..)

{..}

The problem is in instantiating a JTypeVar.

Making the constructor public would solve my problem as follows: JTypeVar Ttype = new JTypeVar(codeModel, "T"); JMethod fooMethod = someClass.method(JMod.PRIVATE, Ttype, "foo"); fooMethod.generify("T");

Can you please make the constructor public or introduce some factory method or give me a hint how to generate generic methods, otherwise?

glassfishrobot commented 13 years ago

Reported by hubert_wagener

glassfishrobot commented 12 years ago

mattbenson said: I have a method:

JType naiveType(String name) { try

{ return codeModel.parseType(name); }

catch (ClassNotFoundException e)

{ return codeModel.directClass(name); }

}

if I, e.g., pass "T" to this method and use the resulting JType as the return value of my method, presumably the directClass() execution path is taken, and the final code output seems to do what I want.

glassfishrobot commented 12 years ago

july said: I need the same feature too, thanks.

glassfishrobot commented 12 years ago

johncarl81 said: It turns out that this is possible with CodeModel as it stands. Here's an example from a post on StackOverflow:

http://stackoverflow.com/q/9355160/654187

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

produces:

package com.example;

public class ExampleClass {

publicT getValue(Class type)

{ return null; }

}

Thanks to ajlane for figuring this one out.

glassfishrobot commented 7 years ago

This issue was imported from java.net JIRA CODEMODEL-4