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

javapoet how do I create a static import

How do I create a static import using JavaPoet? The code I'm trying to generate looks like this: import static com.test.Types.*;
Dan
  • 9,681
  • 14
  • 55
  • 70
3
votes
2 answers

Assign an Array to a MethodSpec Statement in JavaPoet?

I use JavaPoet to create Java code. I defined the following array: String[] testArr = new String[] {"1","2"}; and a constructor: ArrayTypeName stringArray = ArrayTypeName.of(String.class); MethodSpec constroctMethod =…
Michael
  • 32,527
  • 49
  • 210
  • 370
2
votes
1 answer

Java annotation processing with JavaPoet - Attempt to recreate a file for type

I am using java annotation processing API with JavaPoet to generate a file and maven to compile my project. It works as expected, resulting file is generated under…
hendrix
  • 3,364
  • 8
  • 31
  • 46
2
votes
2 answers

Generate java source code under my project's source code package

I have my annotation processor: public class MyAnnotationProcessor extends AbstractProcessor { ... @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { // Here I deal with the…
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
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,770
  • 6
  • 23
  • 46
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,951
  • 17
  • 72
  • 152
2
votes
1 answer

Build method with open parameter list with javapoet

is it possible with javapoet to create a method with an open parameter list? To create a method with a String[] parameter is no problem: curEnumBuilder.addMethod(MethodSpec.methodBuilder("myMethod") .addParameter(String[].class,…
opfau
  • 731
  • 9
  • 37
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
  • 4,735
  • 8
  • 48
  • 57
2
votes
1 answer

Maven build is successful but missing a dependency. JDWP exit error

[SOLVED] Sigh, just had to Project > Clean > Build. Using Run As > Maven build... was not having it. I'm trying to use JavaPoet to build an enum based off annotations I've created, but I'm having trouble adding the Maven dependency. I resolve…
jseashell
  • 745
  • 9
  • 19
2
votes
0 answers

Why annotation processor not generated code?

I have simple annotation processor. @SupportedSourceVersion(SourceVersion.RELEASE_7) @AutoService(Processor.class) public class FirstProcessor extends AbstractProcessor { private Messager messager; private EnumGenerator…
abbath0767
  • 946
  • 2
  • 11
  • 31
2
votes
1 answer

Java annotation processing: reference generated type in other generated code

@GenerateInterface class A {} @GenerateInterface class B { void setA(IA a) {} } My annotation processor should generate these interfaces: interface IA {} interface IB { void setA(IA a); } B compiles fine with correct import statement. IB…
Erik Hofer
  • 2,296
  • 2
  • 18
  • 39
2
votes
2 answers

Pass array as value of annotation param in JavaPoet

Using JavaPoet I'm trying to annotate a class with an annotation which has an array as a parameter value i.e. @MyCustom(param = { Bar.class, Another.class }) class Foo { } I use AnnotationSpec.builder and its addMember() method: List
2
votes
1 answer

How to find the bounds of a TypeMirror & turn them into a JavaPoet TypeSpec?

I have an annotation processor that takes an annotated class and attempts to create a subclass of it: package test; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.JavaFile; import com.squareup.javapoet.TypeSpec; import…
mg_england
  • 83
  • 7
2
votes
1 answer

JavaPoet: how to build annotation within annotation

I try to generate the expression with annotation below: @NamedQueries({@NamedQuery(name = "E.findAll", query = "SELECT e FROM E e")}) I tried the code below: .addAnnotation(AnnotationSpec.builder(NamedQueries.class) …
RedArrow
  • 588
  • 8
  • 18