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

102 questions
4
votes
1 answer

Javapoet not found in processor

I created an Android library that uses JavaPoet to generate classes. It works well on my local workspace ; even if I include the library module into another project. Now I'm trying to put my project online through bintray. The project is uploaded…
Omar Aflak
  • 2,588
  • 16
  • 36
4
votes
1 answer

How to generate constructor that calls superclass constructor using JavaPoet

I want to generate a class that extends other class using JavaPoet. For example I have this class: @MyAnnotation public class ClassA { public ClassA(String paramA, int paramB) { // some code } } and I want to generate new class like this…
radzio
  • 2,592
  • 3
  • 21
  • 31
4
votes
1 answer

How to add static section in to java class in javapoet

Is there a anyway to add static code block into java class using javapoet library static { // whatever code is needed for initialization goes here }
Damith
  • 1,866
  • 3
  • 27
  • 38
3
votes
1 answer

How to add a parameterized super interface in JavaPoet?

I am writing an annotation processor that generates JSON serialization code. Here's my annotation that I use to identify the POJOs that need a serializer @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) public @interface JsonSerialize…
Carmine
  • 2,156
  • 3
  • 22
  • 48
3
votes
0 answers

Easily create (copy) methods in Javapoet using methods from annotated code

I wrote an annotation processor that creates a subclass. For example, if you have the following code: @MyAnnotation(Bar.class) class Foo : MyGenerated_Foo { } Then the annotation processor generates: class MyGenerated_Foo extends Bar { ...…
Sail Nepal
  • 31
  • 1
3
votes
1 answer

Generating source code with a gradle task based on existing classes with annotations on them

I am creating springboot applications, and most of the time I find myself writing boilerplate code for my models - repositories, services, controllers, builders... I do not want to do that. Based on my my experiences, previous works and researches I…
3
votes
1 answer

How Do I Generate an Annotation Without a Name with JavaPoet?

Suppose that I want to annotate a class with something like @RunWith(AndroidJUnit4.class). The general JavaPoet recipe would be: private static ClassName RUN_WITH=ClassName.get("org.junit.runner", "RunWith"); private static ClassName…
CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
3
votes
1 answer

How to add modifiers to methodspec in javapoet?

I try to add some modifiers to a methodspec but i'm stuck at the parameters. The parameters are from the type Modifier. The tutorial says that you can just pass Modifier.PUBLICbut Modifier.PUBLIC is an Integer value. Am i missing something here?…
LaaKii
  • 95
  • 10
3
votes
2 answers

Smart way to use inheritance in JavaPoet

I'm trying to build a class in this way (using the JavaPoet lib): theClass = TypeSpec.classBuilder(classe.getName()) …
user7244619
3
votes
1 answer

JavaPoet - how to add annotation to a field?

Can anyone show me how to add annotation to a field using JavaPoet? All examples I've managed to explore so far are about adding annotations to the classes and methods. It looks like I'm missing something pretty straighforward here.
shabunc
  • 17,863
  • 15
  • 68
  • 96
3
votes
1 answer

How to resolve unknown class and can not resolve symbol in Android Studio java library module

Using Android Studio and creating a java library module as part of a sub project I get an error on the following java statement: javaFile.writeTo(System.out); and it complains of can not resolve symbol 'writeTo' and unknown class…
Peter Birdsall
  • 2,299
  • 4
  • 21
  • 45
2
votes
1 answer

return proper typed objects when building a class using javapoet

I am working on an annotation processor and using JavaPoet to generate the output class from processing, but I can't seem to find a way to have a generated method return a properly typed object. For example, the output I would like to have is…
Neglected Sanity
  • 1,194
  • 1
  • 17
  • 36
2
votes
3 answers

JavaPoet check if TypeName is instance of List

In JavaPoet I can get a TypeName from every Class like this as an example for the List class. TypeName TYPE_LIST = ClassName.get(List.class); But how can I check now if a given TypeName is an instance of a List? Let's say I have a method which…
Cilenco
  • 6,264
  • 15
  • 62
  • 128
2
votes
1 answer

How to generate generic method using java poet?

I want generate below code using javapoet Javapoet is a library to autogenerate java code. @SuppressWarnings("unchecked") public static T[] returnArrayForType(T value) { Object array = Array.newInstance(value.getClass(), 1); …
Chetan
  • 3,661
  • 6
  • 40
  • 51