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

How to add a CodeBlock as part of MethodSpec's statement in Javapoet

I want to create a lambda expression like: // some code CompletableFuture.handle((s,t) -> { if(t != null){ //some code} else{ // some code} }); //some code I have all logic of if-else in a CodeBlock code, and parent code in the MethodSpec method. I…
abhilash_goyal
  • 711
  • 1
  • 10
  • 31
1
vote
1 answer

Implementing generated interface with JavaPoet

I would like to use JavaPoet to generate an interface and a class implementing this interface. TypeSpec if = TypeSpec.interfaceBuilder("MyInterface") .build(); TypeSpec cl = TypeSpec.classBuilder("MyClass") …
Oliver
  • 3,815
  • 8
  • 35
  • 63
1
vote
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
  • 115
  • 3
  • 18
1
vote
2 answers

How to add two classes to same .java file without nesting?

Result: public class cls1 { short f1; short f2; byte f3; } public class cls2 { short f4; short f5; byte f6; } Result not like this : public class cls1 { short f1; short f2; byte f3; public…
Priya K
  • 23
  • 4
1
vote
1 answer

JavaPoet : How to add a field of type class , but I have only the class name, not the class?

String fieldName = (fieldchild.getAttribute("Name")); //gives field name : opcode String fieldCustomID = (fieldchild.getAttribute("ID")); //gives id: 2022 String classTypeofField = IdNameMappingList.get(fieldCustomID).toString(); //give class name…
Priya K
  • 23
  • 4
1
vote
1 answer

javapoet: MethodSpec.Builder.returns() generate error

I would like to generate a class from an interface. My interface is: @Annotation interface Object { String getName(); } I would expect this: public final class ObjectGenerated implements Object { public String getName() { return "my…
vikey89
  • 71
  • 6
1
vote
1 answer

Javapoet and generic class declarations

How do I get javapoet to generate the java code below? class B implements A { } I know there is a class WildcardTypeName, but it can only generate? extends U or ? super U. what I want is T extends U
yang zhang
  • 13
  • 2
1
vote
0 answers

How generate java code to src folder not to bin nor target?

I'm using JavaPoet to generate some classes. All correct! But I need to generate that code into src folder, not into target nor bin nor classes folder Now, I'm using follow code to write classes: URL[] urls = ((URLClassLoader)…
Moesio
  • 3,100
  • 1
  • 27
  • 36
1
vote
1 answer

JavaPoet: Creating two enums in the same file

In Java you can create a single file and it may contain more than one enum\class, for example something like this: Filename: Recorder.java public enum Recorder { RECORDER_A, RECORDER_B; }; enum Feature { FEATURE_A, FEATURE_B; }; I'm able to…
Shvalb
  • 1,835
  • 2
  • 30
  • 60
1
vote
1 answer

How to generate Class parameter with javapoet?

I need generate next java.class parameter for enum class: public enum ServiceType { //.... private final Class mClass; //< clazz) { this.mClass = clazz; } …
abbath0767
  • 946
  • 2
  • 11
  • 31
1
vote
1 answer

Annotation Processor : initialize a field

I am writing an annotation processor in android which generates a java file. I am using JavaPoet library for that. The purpose of generated file: It should have a list of names of the classes with a particular annotation that my processor supports…
Yash
  • 5,225
  • 4
  • 32
  • 65
1
vote
1 answer

How to generate a StringDef using Javapoet in Android

How do I generate a @StringDef enum in Javapoet? Annotations are only available for method or type builders and StringDefs are three annotation statements without neither of these.
Sir Codesalot
  • 7,045
  • 2
  • 50
  • 56
1
vote
1 answer

JavaPoet Generic Parameter

How would I generate a method with the following signature? public static MyOtherClass someMethod(T type)
M. Reza Nasirloo
  • 16,434
  • 2
  • 29
  • 41
1
vote
1 answer

How to recreate a class with JavaPoet?

I'm trying to recreate a class using JavaPoet. What's the best way to do this? Basically, I want to generate another class with one method with appended statements.
kaneda
  • 5,981
  • 8
  • 48
  • 73
1
vote
1 answer

merge static methods with annotation processing and javapoet

I have static methods in multiple classes that I want to merge in a new, generated, class for convenience reasons. I am using annotation processing and javapoet. My Problem: from annotation processing, I get the static methods as a list of…
Jan Galinski
  • 11,768
  • 8
  • 54
  • 77