Questions tagged [jcodemodel]

CodeModel is a Java library for code generators

CodeModel is a Java library for code generators; it provides a way to generate Java programs in a way much nicer than PrintStream.println(). This project is a spin-off from the JAXB RI for its schema compiler to generate Java source files.

Every CodeModel node is always owned by one JCodeModel object at any given time (which can be often accesesd by the owner() method.) As such, when you generate Java code, most of the operation works in a top-down fashion. For example, you create a class from JCodeModel, which gives you a JDefinedClass. Then you invoke a method on it to generate a new method, which gives you JMethod, and so on. There are a few exceptions to this, most notably building JExpressions, but generally you work with CodeModel in a top-down fashion. Because of this design, most of the CodeModel classes aren't directly instanciable.

45 questions
1
vote
0 answers

Comparing the class body in JCodeModel with the body of a .java file

I have a JCodeModel object which includes a class body, would be written to a .java file: codeModel.build(new File("src").getAbsoluteFile()); There might already be the same .java class file in the folder, which I tend to compare with the current…
DummyBeginner
  • 411
  • 10
  • 34
1
vote
1 answer

Nested Annotation in JCodeModel

I would like to create a CompoundIndexes annotation from the XML file like below. However, I don't know how to put CompoundIndex Annotation with a parameter inside the CompoundIndexes Annotation. Here's what I've tried. JAnnotationUse…
Say_hii
  • 11
  • 1
1
vote
1 answer

JAXB, XJC: Generating field to Class Outline

My question concerns writing JAXB plugins, in particular ClassOutline internals. In com.sun.tools.xjc.outline.ClassOutline there are fields: target ref implClass implRef Code: /** * This {@link ClassOutline} holds information about this {@link…
jsosnowski
  • 1,560
  • 3
  • 26
  • 56
1
vote
1 answer

Use JCodeModel to generate toString() method

I am attempting to generate Java Value Objects using com.sun.codemodel.JCodeModel. I have managed to generate hashcode() and equals() methods but I am struggling with toString(); I require the following toString() implementation return "ClassName…
Hector
  • 4,016
  • 21
  • 112
  • 211
1
vote
2 answers

Extend inner class using code model

How can I extend my generated class from an inner static class? For instance, I have class Foo { static class Bar { } } And I want my generated class to be extended from Bar JCodeModel codeModel = new JCodeModel() JClass aClass =…
forcelain
  • 1,576
  • 3
  • 13
  • 16
1
vote
1 answer

How to generate Optional> in JCodeModel?

I want to create JCodeModel for the below method: private static Optional> getTypeOfId(String fieldName) { switch (fieldName.toLowerCase()) { case "IDFORCLASSX": { return…
1
vote
2 answers

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…
theAsker
  • 527
  • 3
  • 14
1
vote
1 answer

Does Java codemodel support GenericEntity

Does Java codemodel support GenericEntity ? I am trying to generate a code like below using jcodemodel: Object obj = new GenericEntity>(listStr){} But I am not able do that. I used below code : JType jObjType = ((JClass)…
Moon Mysterious
  • 164
  • 1
  • 14
1
vote
1 answer

Define enum constant body in JCodeModel

I'd like to create the following Java source code using JCodeModel public enum MyEnum { FIRST_CONSTANT { @Override public String toString() { return "first"; } }, SECOND_CONSTANT { @Override …
ooxi
  • 3,159
  • 2
  • 28
  • 41
1
vote
1 answer

How do I force-enclose a CodeModel expression in brackets?

I want to generate some very common code using Sun's CodeModel while ((sbt = reader.readLine()) != null) { } However when I write: JWhileLoop whileJsonBuilder = block._while(JExpr …
druk
  • 553
  • 6
  • 16
0
votes
1 answer

JCodeModel ref not imported

I'm trying to create a class (MoMoTest.class) which extends a generic abstract class (MappingObject.class). Everything looks good, except that JCodeModel doesn't import the narrowed class (MoTest.class), although I created a JClass of it with…
user
  • 211
  • 2
  • 12
0
votes
1 answer

How to initialise a 2D array using CodeModel

I need to initialise a 2d array like below Object[][] someName = { {"param1","param2","param3"}, {"param4","param5","param6"} }; I tried like …
Monis Majeed
  • 1,358
  • 14
  • 21
0
votes
1 answer

JCodeModel Create Annotation

Hi I am relatively new to JCodeModel. I have the follwoing annotation over my class @JsonSubTypes({ @JsonSubTypes.Type(value = com.abc.MyClass.class, name = "MyClass")}) class MyClass { } How can I create Annotation for the following using…
prit kalra
  • 319
  • 4
  • 18
0
votes
1 answer

Using JCodeModel, SonarLint reports classes from "sun.*" packages should not be used

I am trying to use com.sun.codemodel.JCodeModel in some code that is generating Java Pojo classes from JSON schemas but SonarLint is reporting that classes from "sun.*" packages should not be used. Is there any replacement for JCodeModel in a…
J Harby
  • 31
  • 3
0
votes
1 answer

auto-initialize Fields created by JCodeModel

I create a new Java class through a Json, i.e. : { "package" : "crud.vaadin", "className" : "StudentForm", "extension" : "AbstractForm", "extensiongeneric" : "Person", "properties" : { "Name" : { "type" :…
cookieDope
  • 33
  • 2
  • 9