Skip to content

Commit

Permalink
[fixes projectlombok#3248] Add support for jakarta generated annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawi01 committed Sep 15, 2023
1 parent 65773f5 commit 1e929c9
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/core/lombok/ConfigurationKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private ConfigurationKeys() {}
* <em>BREAKING CHANGE</em>: Starting with lombok v1.16.20, defaults to {@code false} instead of {@code true}, as this annotation is broken in JDK9.
*
* @see ConfigurationKeys#ADD_JAVAX_GENERATED_ANNOTATIONS
* @see ConfigurationKeys#ADD_JAKARTA_GENERATED_ANNOTATIONS
* @see ConfigurationKeys#ADD_LOMBOK_GENERATED_ANNOTATIONS
* @deprecated Since version 1.16.14, use {@link #ADD_JAVAX_GENERATED_ANNOTATIONS} instead.
*/
Expand All @@ -74,6 +75,13 @@ private ConfigurationKeys() {}
*/
public static final ConfigurationKey<Boolean> ADD_JAVAX_GENERATED_ANNOTATIONS = new ConfigurationKey<Boolean>("lombok.addJavaxGeneratedAnnotation", "Generate @javax.annotation.Generated on all generated code (default: follow lombok.addGeneratedAnnotation).") {};

/**
* lombok configuration: {@code lombok.addJakartaGeneratedAnnotation} = {@code true} | {@code false}.
*
* If {@code true}, lombok generates {@code @jakarta.annotation.Generated("lombok")} on all fields, methods, and types that are generated.
*/
public static final ConfigurationKey<Boolean> ADD_JAKARTA_GENERATED_ANNOTATIONS = new ConfigurationKey<Boolean>("lombok.addJakartaGeneratedAnnotation", "Generate @jakarta.annotation.Generated on all generated code (default: false).") {};

/**
* lombok configuration: {@code lombok.addLombokGeneratedAnnotation} = {@code true} | {@code false}.
*
Expand Down
4 changes: 4 additions & 0 deletions src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2088,6 +2088,7 @@ public static EclipseNode injectType(final EclipseNode typeNode, final TypeDecla
private static final char[] GENERATED_CODE = "generated code".toCharArray();
private static final char[] LOMBOK = "lombok".toCharArray();
private static final char[][] JAVAX_ANNOTATION_GENERATED = Eclipse.fromQualifiedName("javax.annotation.Generated");
private static final char[][] JAKARTA_ANNOTATION_GENERATED = Eclipse.fromQualifiedName("jakarta.annotation.Generated");
private static final char[][] LOMBOK_GENERATED = Eclipse.fromQualifiedName("lombok.Generated");
private static final char[][] EDU_UMD_CS_FINDBUGS_ANNOTATIONS_SUPPRESSFBWARNINGS = Eclipse.fromQualifiedName("edu.umd.cs.findbugs.annotations.SuppressFBWarnings");

Expand All @@ -2111,6 +2112,9 @@ public static Annotation[] addGenerated(EclipseNode node, ASTNode source, Annota
if (HandlerUtil.shouldAddGenerated(node)) {
result = addAnnotation(source, result, JAVAX_ANNOTATION_GENERATED, new StringLiteral(LOMBOK, 0, 0, 0));
}
if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_JAKARTA_GENERATED_ANNOTATIONS))) {
result = addAnnotation(source, result, JAKARTA_ANNOTATION_GENERATED, new StringLiteral(LOMBOK, 0, 0, 0));
}
if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_LOMBOK_GENERATED_ANNOTATIONS))) {
result = addAnnotation(source, result, LOMBOK_GENERATED);
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/lombok/javac/handlers/JavacHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,9 @@ public static void addGenerated(JCModifiers mods, JavacNode node, JavacNode sour
if (HandlerUtil.shouldAddGenerated(node)) {
addAnnotation(mods, node, source, "javax.annotation.Generated", node.getTreeMaker().Literal("lombok"));
}
if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_JAKARTA_GENERATED_ANNOTATIONS))) {
addAnnotation(mods, node, source, "jakarta.annotation.Generated", node.getTreeMaker().Literal("lombok"));
}
if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_LOMBOK_GENERATED_ANNOTATIONS))) {
addAnnotation(mods, node, source, "lombok.Generated", null);
}
Expand Down
15 changes: 15 additions & 0 deletions test/stubs/jakarta/annotation/Generated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package jakarta.annotation;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.SOURCE;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Documented
@Retention(SOURCE)
@Target({PACKAGE, TYPE, ANNOTATION_TYPE, METHOD, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, PARAMETER})
public @interface Generated {
String[] value();
}
15 changes: 15 additions & 0 deletions test/stubs/javax/annotation/Generated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package javax.annotation;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.SOURCE;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Documented
@Retention(SOURCE)
@Target({PACKAGE, TYPE, ANNOTATION_TYPE, METHOD, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, PARAMETER})
public @interface Generated {
String[] value();
}
10 changes: 10 additions & 0 deletions test/transform/resource/after-delombok/GeneratedJavaxJakarta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class GeneratedJavaxJakarta {
int x;

@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
@jakarta.annotation.Generated("lombok")
public int getX() {
return this.x;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//version :8
class GeneratedJavaxOnLombokOn {
int x;
@java.lang.SuppressWarnings("all")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//version :8
class GeneratedOffJavaxOn {
int x;
@java.lang.SuppressWarnings("all")
Expand Down
9 changes: 9 additions & 0 deletions test/transform/resource/after-delombok/GeneratedOn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class GeneratedOn {
int x;

@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
public int getX() {
return this.x;
}
}
9 changes: 9 additions & 0 deletions test/transform/resource/after-ecj/GeneratedJavaxJakarta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class GeneratedJavaxJakarta {
@lombok.Getter int x;
GeneratedJavaxJakarta() {
super();
}
public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") @jakarta.annotation.Generated("lombok") int getX() {
return this.x;
}
}
9 changes: 9 additions & 0 deletions test/transform/resource/after-ecj/GeneratedOn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class GeneratedOn {
@lombok.Getter int x;
GeneratedOn() {
super();
}
public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") int getX() {
return this.x;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//CONF: lombok.addGeneratedAnnotation = false
import lombok.*;
import static lombok.AccessLevel.NONE;

Expand Down
6 changes: 6 additions & 0 deletions test/transform/resource/before/GeneratedJavaxJakarta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//CONF: lombok.addJavaxGeneratedAnnotation = true
//CONF: lombok.addJakartaGeneratedAnnotation = true
class GeneratedJavaxJakarta {
@lombok.Getter
int x;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//CONF: lombok.addJavaxGeneratedAnnotation = true
//CONF: lombok.addLombokGeneratedAnnotation = true
//version :8
class GeneratedJavaxOnLombokOn {
@lombok.Getter
int x;
Expand Down
1 change: 0 additions & 1 deletion test/transform/resource/before/GeneratedOffJavaxOn.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//CONF: lombok.addGeneratedAnnotation = false
//CONF: lombok.addJavaxGeneratedAnnotation = true
//version :8
class GeneratedOffJavaxOn {
@lombok.Getter
int x;
Expand Down
5 changes: 5 additions & 0 deletions test/transform/resource/before/GeneratedOn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//CONF: lombok.addGeneratedAnnotation = true
class GeneratedOn {
@lombok.Getter
int x;
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5 Not generating equals: One of equals or hashCode exists. You should either write both of these or none of these (in the latter case, lombok generates them).
4 Not generating equals: One of equals or hashCode exists. You should either write both of these or none of these (in the latter case, lombok generates them).
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5 Not generating equals: One of equals or hashCode exists. You should either write both of these or none of these (in the latter case, lombok generates them).
4 Not generating equals: One of equals or hashCode exists. You should either write both of these or none of these (in the latter case, lombok generates them).

0 comments on commit 1e929c9

Please sign in to comment.