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
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
  • 2,759
  • 2
  • 17
  • 29
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
  • 3,798
  • 3
  • 26
  • 48
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
0 answers

Annotation Processor Generation No Files Created (JavaPoet-AutoService)

I am trying to learn processor generation, I use JavaPoet and Google AutoService for that purpose. I compile my code with maven-compiler-plugin without any errors, generated sources folder appears but I can't see any generated file inside of it. I…
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
0 answers

Generate Mockito Unit tests with JavaPoet

As the title says I'm trying to generate unit test classes using JavaPoet Here is the test I want to generate: @Test public void TestSettingConstantFieldConstantValue() { Target mockedTarget = Mockito.mock(Target.class); …
Tali
  • 59
  • 1
  • 7
0
votes
0 answers

Java Annotation Processing SuperClass Data extraction

Hi I am creating a code generation program, as part of it I need to extract info of the GenericType passed into a class which extends an Abstract class, for further clarification This is the class I annotated with my annotation @MyAnnotation public…
0
votes
0 answers

Generating doc using dokka of generated code

I am generating code using Annotation processor and able to add java docs comment by using kotlinpoet api addKdoc. But when I am generating javadoc/Kdoc using dokka, these generated codes does not get included in Kdoc. Is there any way to generate…
srbhakta
  • 65
  • 2
  • 5
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
  • 603
  • 1
  • 10
  • 22
0
votes
1 answer

Android studio shows very specific incorrect errors with JavaPoet library

I've written an annotation processor using JavaPoet library and included it as a Java Library module to my project. It compiles and works fine. The problem is - Android Studio inline compiler shows me errors that shouldn't be there. For some reason…
A. Stef
  • 23
  • 1
  • 4
0
votes
1 answer

java.lang.IllegalArgumentException: Unsupported class file major version 57

I am getting below error when i build my application.Any help would be appreciated. Root:build.gradle buildscript { ext.kotlin_version = '1.3.50' repositories { google() jcenter() } dependencies { classpath…
Ananth
  • 93
  • 3
  • 13
0
votes
0 answers

How to guarantee java file is created before call a function

I am using JavaPoet to generate code, here is the core part of my code snippet: ... TypeSpec typeSpec = createTypeSpec(); JavaFile javaFile = JavaFile.builder(myPackage, typeSpec).build(); // write to file javaFile.writeTo(filer); // I want to…
user842225
  • 4,171
  • 6
  • 42
  • 74
0
votes
1 answer

How to construct enum with custom type using JavaPoet

Is it possible to generate the following enum class using JavaPoet? public enum EnumName { import com.sth.sth.SomeClass1; import com.sth.sth.SomeClass2; ITEM1(new CustomType("string1", "string 2", SomeClass1.class)), ITEM2(new…
Juraj V.
  • 11
  • 3
0
votes
1 answer

How to generate code dynamically with javapoet? Change the method parameters

I'm trying to generate java code that depends on other variables. In this specific moment, I'm trying to generate the code depending on the protocol. So I can receive a CoapResponse object or a Response object form ( javax). I don't know if it is…