Questions tagged [javapoet]

JavaPoet is a Java API for generating .java source files.

JavaPoet is a Java API for generating .java source files and is the successor to JavaWriter.

It is available on Github: https://github.com/square/javapoet

104 questions
1
vote
1 answer

Enum generation using JavaPoet

How to generate the following enum class using JavaPoet? public enum Planet { MERCURY (3, 2), VENUS (4, 6) }
priyanka
  • 315
  • 3
  • 14
1
vote
1 answer

javapoet : comment into an interface

I would like to know if it is possible to add comments into an interface with JavaPoet ? Using addJavadoc() method could be enough, but I need both of them (javadoc and comments) for the files I have to generate. I'm currently looking for a way to…
1
vote
1 answer

Javapoet: Referencing other generated classes

JavaPoet: Version 1.5.1 JDK: 1.7 I am using annotations to generate the code. Here is something that I am trying. Following are available as part of the project: @GenService public abstract class BaseService { ... } @GenController public…
ajoshi
  • 349
  • 2
  • 10
1
vote
3 answers

How to get a reference to Modifier.PUBLIC that can not be applied in builder in MethodSpec's methodBuilder for JavaPoet

Attempting to implement the basic JavaPoet example (see below) in a Android ActivityWatcher class from LeakCanary: .addModifiers(Modifier.PUBLIC, Modifier.STATIC) The Modifier.PUBLIC and Modifier.STATIC, and the other .addModifiers statement …
Peter Birdsall
  • 3,159
  • 5
  • 31
  • 48
1
vote
2 answers

JavaPoet adding a list of enum constants

Is there a way to add a dynamic list of EnumConstants - I would expect to see addEnumConstants(). There seems to be no parallel to addFields() or addMethods()?
Dan
  • 9,681
  • 14
  • 55
  • 70
1
vote
1 answer

JavaPoet - get generic type

I'm playing around with annotation processor and JavaPoet. I have a method which I annotated with @MyAnno: @MyAnno Observable get(int id); I want to generate a class which will have a method: AsyncSubject get(int id); What I've…
rafakob
  • 3,946
  • 3
  • 26
  • 36
1
vote
1 answer

javapoet how to specify current generated instance as return result

I am writing an annotation Processor that generates Agenerated class from an annotated A class. I would like to be able to do something like AgeneratedInst.getFoo().getBar()... In order to do so I have to specify the return type which is the…
gropapa
  • 577
  • 6
  • 10
1
vote
1 answer

Generating static class initializer using javapoet

Is it possible to generate static initializer using javapoet? See an example of what I'm trying to generate below: class Foo { static int one = 1; static int two = 2; static int sum; static { sum = one + two; } } I…
Denis Itskovich
  • 4,383
  • 3
  • 32
  • 53
0
votes
0 answers

kotlinpoet generate code to call builder class continuously

Now i can generate code like this val builder = MyBuilder() builder.setA() builder.setB() builder.setC() val target = builder.build() I want to generate code like under val target = MyBuilder() .setA() .setB() .setC() .build()
Jade
  • 416
  • 5
  • 7
0
votes
0 answers

Dynamically Handling Return Types in Code Generation with JavaPoet

I'm facing an issue in my application where I need to generate code based on implementation properties provided by the user, such as methodName, className, arguments, and returnType. I'm using JavaPoet for code generation. My problem lies with the…
0
votes
1 answer

Is it possible to just add method to class with Java Poet?

I am wondering if is it possible to add method to class without having to rewrite whole class again using Java Poet? Thanks
0
votes
1 answer

How to generate annotations and use lombok with javapoet?

Is there any way (and any sense) to use Lombok when I am using javapoet? Here is example: TypeSpec typeSpec = TypeSpec .classBuilder("MyDtoWithLombok") .addModifiers(Modifier.PUBLIC) //.addAnnotation(NoArgsConstructor.class) …
KalEl
  • 35
  • 3
0
votes
1 answer

Getting java.lang.NoClassDefFoundError: com/squareup/javapoet/MethodSpec when integrating my custom annotation processor jar in IntelliJ

I have one simple application "Client" that uses some annotation, within it I am including the .jar for the annotation processor that I wrote, should write java class using JavaPoet to the console. The following are the configuration for…
0
votes
1 answer

How to read Annotation of a method argument using javapoet

I am trying to generate code by reading annotated method like @MyAnnotation public static int generatorMethod(@SomeOtherAnnotation Boolean someArg) I would like to copy the list of arguments as they are in the generated code As shown below: public…
0
votes
1 answer

How to add a parameterized return type of function in Java Poet

How would I generate a method with the following signature? public ServiceA anyFunctionName() { // code } Problem: Not sure how to add return type : ServiceA Assuming I have FQCN for ServiceA and…
abhilash_goyal
  • 711
  • 1
  • 10
  • 31