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

Adding copyright info generated java code - Jcodemodel

I am generating java source code using JCodeModel. I would to add copyright information to the generated code. Is this possible currently? I tried using javadoc()in JDefinedClass , it adds the information only above the class definition.
Sam
  • 1,298
  • 6
  • 30
  • 65
0
votes
1 answer

Instantiate a field level HashMap in JCodeModel

I want to declare and instantiate a HashMap in one go in JCodeModel. I do: jc.field(JMod.PRIVATE, HashMap.class, "initAttributes"); which declares it but doesn't instantiate it. How do I instantiate it? Thanks
More Than Five
  • 9,959
  • 21
  • 77
  • 127
0
votes
1 answer

Adding a type for a variable

jcodemodel makes it possible to add private attributes by doing something like: JFieldVar quantity = jc.field(JMod.PRIVATE, Integer.class, "myAtt"); However, what happens instead of using a class from the JDK you are using a class that won't…
More Than Five
  • 9,959
  • 21
  • 77
  • 127
0
votes
1 answer

Exception message in JCodemodel

I am using JCodemodel to generate java classes dynamically. Below is the code for creating a switch statement whose the default case would be to throw an Exception. JSwitch valueswitch; AbstractJClass exception =…
Sneha
  • 317
  • 4
  • 15
0
votes
1 answer

How can I generate/declare annotation field inside of Annotation?

I want to get something like this: public @interface Unfinished { String value(); String value() default "someVal"; } The problem is I cannot use JFieldVar because it looks like this: String value; //no parentheses String value = "someVal";…
aderesh
  • 927
  • 10
  • 22
0
votes
1 answer

Convert Java file (from JCodeModel) to Java class by code (instead of Javac etc.)

I do have the following code, that uses a XSD file to create a Java file. Now, I need to convert the Java file (from JCodeModel) to a Java class, that I can create/use in my project. Unfortunately, it is created during runtime, so I cannot add it to…
sjantke
  • 605
  • 4
  • 9
  • 35
0
votes
2 answers

How do I add a generic type parameter to a class which I extend with JCodeModel?

I am playing with JCodeModel and trying to generate a class; thanks to this link I was able to come up with this: public final class CodeModelTest { private CodeModelTest() { throw new Error("no instantiation is permitted"); } …
fge
  • 119,121
  • 33
  • 254
  • 329
0
votes
1 answer

JCodeModel method writing, issues with "new"

Need help in converting the following piece of code to jcodemodel understandable format @Override public final void blah() { XStream xstream = new XStream(new DomDriver()); String xml = xstream.toXML(this); } Any help appreciated.
beingsuplab
  • 155
  • 1
  • 1
  • 12
0
votes
1 answer

Add import using code model

I am trying to import a class in my code using code model. This is my code. JCodeModel model = new JCodeModel(); JClass mapper = model.directClass("com.another.Mapper"); JDefinedClass dc = model._class("com.example.Something"); JMethod method =…
Arun M R Nair
  • 653
  • 7
  • 30
0
votes
1 answer

JCodeModel: Attributes and Lists with special class-types

I started to work with the JCodeModel today. I want to add Attributes with other types than int, String, boolean etc. to my JDefinedClass. The final Java Code built by the JCodeModel should look like: public Class Team { private int teamID; …
t990
  • 3
  • 3
0
votes
1 answer

How to generate static initialization block with jcodeModel

I need to generate something like this with jcodemodel package com.example; public class Main { static int a; static { a = 5; } public static void main (String[] args) { } } Google did not help. There is a…
user1745356
  • 4,462
  • 7
  • 42
  • 70
0
votes
1 answer

How to narrow a anonymous class?

i want to generate some code like below: AbstractSomeClass someClassObj = new AbstractSomeClass { ... } I tried the following: JDefinedClass anonymousSomeClass =…
Cheng
  • 572
  • 8
  • 12
0
votes
1 answer

Save Jcodemodel Object after exit

I have a issue with JCodeModel (SUN). My program is running every day, and I want to add some function to classes which was created before the current running. JcodeModel support this? If not, there is any option to save the JCodemodel Object in…
Or Smith
  • 3,556
  • 13
  • 42
  • 69
0
votes
1 answer

JCodeModel declaring a field with dotted class as type

I'm using JCodeModel for auto generating some code. At some point, the code model seems to declare all of my String types as: java.lang.String someFieldName; I came across this problem a few times and solved it myself (one possible reason is…
Gumba
  • 275
  • 1
  • 3
  • 10
-1
votes
1 answer

How can we generate this method using JCodeModel

I am using JCodeModel but cannot find how I can generate a method that returns a call from its super class. Relevant parts of what I have .. JDefinedClass jc = jp._class("NewPremiumDTO"); JMethod m = jc.method(JMod.PUBLIC, LP.class,…
AnonymousAlias
  • 1,149
  • 2
  • 27
  • 68
1 2
3