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
5
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,862
  • 4
  • 26
  • 35
4
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…
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,918
  • 21
  • 39
4
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
  • 23,119
  • 19
  • 77
  • 102
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,982
  • 3
  • 28
  • 42
3
votes
1 answer

How to get type and generic type from javax.lang.model.VariableElement?

I'm using annotation processing and javapoet library to generate some source code. Say, I've a VariableElement fieldElement and if System.out.println("type:- " + fieldElement.asType().toString()); prints type:-…
Drunken Daddy
  • 7,326
  • 14
  • 70
  • 104
3
votes
2 answers

How to create a type with a given name in JavaPoet?

In JavaPoet I need to create an instance of TypeName representing an inner class of a given name. I have a TypeSpec.Builder classBuilder of the outer class. No idea what to do next. Note that this inner class is present only in the generated…
porton
  • 5,214
  • 11
  • 47
  • 95
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…
vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59
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

JavaPoet - ParameterizedTypeName with nested ParameterizedTypeName

How can I create following: HashMap> mHashMap = new HashMap(); I have following: ParameterizedTypeName subType = ParameterizedTypeName.get(Pair.class, Boolean.class, Object.class); ParameterizedTypeName mainType =…
prom85
  • 16,896
  • 17
  • 122
  • 242
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
  • 986,068
  • 189
  • 2,389
  • 2,491
3
votes
2 answers

Runtime generated repositories and entities

In my SpringBoot application i'm generating hibernate entity classes and repositories using javapoet and than compiling these generated source files respectively using OpenHFT library at runtime. My purpose is being able to persist these runtime…
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
  • 11
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

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
  • 3,159
  • 5
  • 31
  • 48