sun-codemodel

Sun CodeModel generic method

筅森魡賤 提交于 2019-12-21 12:17:39
问题 Does anyone know to to generate the following generic method declaration using CodeModel: public <T> T getValue(Class<T> clazz){...} usage: ValueType value = getValue(ValueType.class); Seems not to be handled by the existing implmentation. I know I could handle the code as follows, but it requires a cast: public Object getValue(Class class){...} usage: ValueType value = (ValueType)getValue(ValueType.class); Obviously this is a bit messy because of the cast. 回答1: Create the method with an

How to initialise a 2D array using CodeModel

狂风中的少年 提交于 2019-12-20 06:02:32
问题 I need to initialise a 2d array like below Object[][] someName = { {"param1","param2","param3"}, {"param4","param5","param6"} }; I tried like JExpression exp = JExpr.newArray(codeModel.ref(String.class)).add(JExpr.lit("param1").add(JExpr.lit("param2")); methodBlock.decl(JMod.NONE, codeModel.ref(Object.class).array().array(), dataProviderName, exp); but it initilases 1d array, any help is appreciated 回答1: It might not be exactly the same, but the following code initializes the example 2d array

What is the role of ClassOutline / JClass / CClass in CodeModel?

*爱你&永不变心* 提交于 2019-12-17 06:13:29
问题 My question concerns writing JAXB plugins, in particular JAXB codemodel. What is the role of ClassOutline (and it's companions) and JClass (and companions) and CClass (and companions)? When looking at the list of classes in corresponding packages it is not clear what is chicken and what is egg. My interpretation is that CClass ( CPropertyInfo , CEnumConstant , ...) are created by XJC at first draft parsing of XSD. Then some magic happens and this model is transformed into JClass ( JFieldVar ,

Java Codemodel - Annotate a method or class

孤街浪徒 提交于 2019-12-12 14:40:22
问题 I am using CodeModel to programmatically generate .java files. This is a snippet of code to create a method: JCodeModel jCodeModel = new JCodeModel(); JDefinedClass definedClass = jCodeModel._class("Foo"); //To generate method JMethod method = definedClass.method(3, String.class, "getCustomerInfo()"); When I run (assume all other necessary codes are there); public String getCustomerInfo() { } But I want to annotate above method like this: @GET @Path("/getCustomerInfo") public String

How do I remove unnecessary brackets using CodeModel's JExpr.plus method?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 11:40:00
问题 I'm using JExpr.plus() method to form a String and syntactically it is correct, but it has a lot of brackets. For example: JExpr.lit("ONE").plus(JExpr.lit("TWO")).plus(JExpr.lit("THREE")) returns (("ONE" + "TWO") + "THREE") and I would like it to be "ONE" + "TWO" + "THREE" 回答1: It looks like with codemodel right now you cant avoid the addition of the parentheses. Plus (+) is considered a BinaryOp which generates its code with the following class: Within com.sun.codemodel.JOp : static private

NoClassDefFoundError: org/junit/AfterClass during annotation processing

◇◆丶佛笑我妖孽 提交于 2019-12-08 05:55:39
问题 I am generating code with CodeModel during annotation processing with maven. That code is for JUnit testing: JMethod tearDownClass = testClass.method( JMod.PUBLIC | JMod.STATIC, Void.class, "tearDownClass"); tearDownClass._throws(Exception.class); tearDownClass.annotate(AfterClass.class); <- java.lang.NoClassDefFoundError Yet, the compilation process throws a java.lang.NoClassDefFoundError : org/junit/AfterClass when it tries to retrieve the AfterClass.class , which is an annotation itself.

How can I create a simple assignment statement in codemodel?

ぃ、小莉子 提交于 2019-12-08 05:09:56
问题 I want to create a simple statement using codemodel like :- String text = element.getText(); I don't want to assign it to any block for now, rather just return it because I would be needing the name of the variable to refer later. How can I create such a statement and in which type of variable to store it? Would it be a JStatement? If yes then how? 回答1: It looks like the codemodel api doesn't allow you to create an assigment without a Block . The JAssignment object is created via the JBlock

How to initialise a 2D array using CodeModel

我是研究僧i 提交于 2019-12-02 11:41:15
I need to initialise a 2d array like below Object[][] someName = { {"param1","param2","param3"}, {"param4","param5","param6"} }; I tried like JExpression exp = JExpr.newArray(codeModel.ref(String.class)).add(JExpr.lit("param1").add(JExpr.lit("param2")); methodBlock.decl(JMod.NONE, codeModel.ref(Object.class).array().array(), dataProviderName, exp); but it initilases 1d array, any help is appreciated It might not be exactly the same, but the following code initializes the example 2d array: JDefinedClass testClass = codeModel._class("TestClass"); JMethod runme = testClass.method(JMod.PUBLIC,

CodeModel help needed for right-hand singleton.getinstance() assignment

久未见 提交于 2019-12-01 13:17:41
I've been able to generate 99% of what I need with the CodeModel API, but I am stumped here... Using the various "directXX" methods does not add import statements to the generated code, and I can work without the "directXXX" type of methods except for one place in a generated class. Suppose I desire a generated method like: /** * Copies data from this Value-Obj instance, to the returned PERSON instance. * * @return PERSON * */ public PERSON mapVOToPERSON() throws MappingException { Mapper mapper = (com.blah.util.MapperSingleton.getMapperInstance()); return mapper.map(this, PERSON.class); } You

CodeModel help needed for right-hand singleton.getinstance() assignment

橙三吉。 提交于 2019-12-01 11:41:41
问题 I've been able to generate 99% of what I need with the CodeModel API, but I am stumped here... Using the various "directXX" methods does not add import statements to the generated code, and I can work without the "directXXX" type of methods except for one place in a generated class. Suppose I desire a generated method like: /** * Copies data from this Value-Obj instance, to the returned PERSON instance. * * @return PERSON * */ public PERSON mapVOToPERSON() throws MappingException { Mapper