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
8
votes
2 answers

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

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…
dma_k
  • 10,431
  • 16
  • 76
  • 128
4
votes
1 answer

How to generate comment inside a method with JCodeModel

I need something like this public void method() { //TODO generated sources } Here is how I generate a class and a method JCodeModel cm = new JCodeModel(); JDefinedClass dc = cm._class("MyClass"); JMethod method = dc.method(JMod.PUBLIC,…
user1745356
  • 4,462
  • 7
  • 42
  • 70
3
votes
1 answer

How to remove an annotation on a JDefinedClass

I am writing an custom annotator in jsonschema2pojo in order to tweak how this code generator annotates generated class with Jackson annotations. To simplify the usecase, I have a JClass at hand that is already annotation with JsonInclude(…
Samuel Kerrien
  • 6,965
  • 2
  • 29
  • 32
3
votes
1 answer

generation of java bytecode using JCodeModel

I've created JCodeModel that contains all classes I want to generate. The thing is that I want to generate bytecode (.class files) and a jar but not the sources. Is there an elegant way to do it without generating the .java files and later compiling…
DiSol
  • 414
  • 2
  • 11
3
votes
1 answer

Java: Generate Annotation with default value using Codemodel

I use the Sun CodeModel code generator for my project. During this I came to the point to generate an annotation class. This class would have an array member which takes an empty array as default values. See the following example: public class…
smeyersdev
  • 103
  • 7
3
votes
1 answer

is there any way to copy a java arraylist object into codemodel generated source?

i have a populated arraylist (non-codemodel) in a code generation class using codemodel, and i would like to use it in the generated code. is there any way to do this? it's "crossing worlds" a little, as the generated code does not reference or…
Piotr
  • 1,437
  • 6
  • 19
  • 24
3
votes
2 answers

JCodeModel - How to chain invoke methods

I am trying to figure out how to use a loop to build a JExpression that I can use in an .assign() call. I know that you can do something like JExpr.invoke("methodA").invoke("methodB") with JCodeModel, but I want to be able to add each .invoke()…
kriegh13
  • 33
  • 5
2
votes
1 answer

How to "generate source code" create and initialise of a hashMap inside a method using CodeModel

Source Code to generate class SomeClass{ public void someMethod(){ HashMap map = new HashMap(); } } Able to create as a global variable but i need to create it inside a method …
Monis Majeed
  • 1,358
  • 14
  • 21
2
votes
0 answers

How do I create Java 8 Consumer code via Sun JCodeModel

How do I create Java 8 code that looks like this, without using the version with "com.sun.codemodel.JExpr.direct(String)": Goal (the whole line would look like this, but the part I'm interested in is only the second parameter…
giro
  • 421
  • 1
  • 7
  • 19
2
votes
0 answers

Codemodel: how to implement method reference

I have a interface having getter methods like below public interface IAddress { AddressId getId(); String getCity(); String getCountry(); } I want to create another class where I want to reference the getter methods of IAddress like…
Sneha
  • 317
  • 4
  • 15
2
votes
1 answer

How to generate a generic method with JCodeModel?

I need to generate a generic method like public static T get(Class type) { ... return null; } Anybody done this before?
abedurftig
  • 1,118
  • 1
  • 12
  • 24
2
votes
1 answer

Code Model Import for Class with Embedded Enum

I'm working on generating some Java classes using CodeModel and I'm having some trouble adding import statements for classes that have embedded static Enum For example if I have a class and create an instance variable... Class clazz =…
BOC
  • 21
  • 4
2
votes
1 answer

Cannot find exception classes in CodeModel in XJC plugin

I'm generating custom Java code from a WSDL provided by PeopleSoft. I've written several XJC plugins to try to clean up the generated code to be easier to use – adding interfaces and custom methods, etc. However, I'd like to add a common interface…
Akido
  • 176
  • 2
  • 5
2
votes
2 answers

How to get generic type "T" of class when reused in method

I am building a code generator for Fluent API. I want to create a new class for every existing (POJO)-class. I dont have the existing classes under my control. I parse the existing methods via reflection and if I encounter a setter or an "add"…
Jan Galinski
  • 11,768
  • 8
  • 54
  • 77
1
vote
0 answers

Make a CodeModel object implement/inherit a parent, before Having it been saved as an actual class on disk

I need to add an implementaion of an interface to a populated JCodeModel object, before saving it to a .java file using codeModel.build("FilePath");. I'm using jsonschema2pojo library to create JCodeModel object. The code is this: JCodeModel…
DummyBeginner
  • 411
  • 10
  • 34
1
2 3