Skip to content

Commit

Permalink
Merge pull request #3479 from Rawi01/extensionmethod-record
Browse files Browse the repository at this point in the history
Add support for extension methods on records
  • Loading branch information
rzwitserloot authored Sep 17, 2023
2 parents 8e55a95 + f67b664 commit e3d4366
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/core/lombok/javac/handlers/HandleExtensionMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public void handle(final AnnotationValues<ExtensionMethod> annotation, final JCA

deleteAnnotationIfNeccessary(annotationNode, ExtensionMethod.class);
JavacNode typeNode = annotationNode.up();
boolean isClassOrEnumOrInterface = isClassOrEnumOrInterface(typeNode);
boolean isClassEnumInterfaceOrRecord = isClassEnumInterfaceOrRecord(typeNode);

if (!isClassOrEnumOrInterface) {
annotationNode.addError("@ExtensionMethod can only be used on a class or an enum or an interface");
if (!isClassEnumInterfaceOrRecord) {
annotationNode.addError("@ExtensionMethod can only be used on a class, an enum, an interface or a record");
return;
}

Expand Down
12 changes: 11 additions & 1 deletion src/core/lombok/javac/handlers/JavacHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2122,10 +2122,20 @@ public static boolean isClassOrEnum(JavacNode typeNode) {
return isClassAndDoesNotHaveFlags(typeNode, Flags.INTERFACE | Flags.ANNOTATION | RECORD);
}

public static boolean isClassOrEnumOrInterface(JavacNode typeNode) {
/**
* Returns {@code true} if the provided node is an actual class an enum or an interface and not some other type declaration (so, not an annotation definition, or record).
*/
public static boolean isClassEnumOrInterface(JavacNode typeNode) {
return isClassAndDoesNotHaveFlags(typeNode, Flags.ANNOTATION | RECORD);
}

/**
* Returns {@code true} if the provided node is an actual class, an enum, an interface or a record and not some other type declaration (so, not an annotation definition).
*/
public static boolean isClassEnumInterfaceOrRecord(JavacNode typeNode) {
return isClassAndDoesNotHaveFlags(typeNode, Flags.ANNOTATION);
}

/**
* Returns {@code true} if the provided node is an actual class, an enum or a record and not some other type declaration (so, not an annotation definition or interface).
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// version 14:
public record ExtensionMethodOnRecord() {
public void test() {
ExtensionMethodOnRecord.Extensions.intValue("1");
}

static class Extensions {
public static Integer intValue(String s) {
return Integer.valueOf(s);
}
}
}
18 changes: 18 additions & 0 deletions test/transform/resource/after-ecj/ExtensionMethodOnRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// version 14:
import lombok.experimental.ExtensionMethod;
public @ExtensionMethod(ExtensionMethodOnRecord.Extensions.class) record ExtensionMethodOnRecord() {
static class Extensions {
Extensions() {
super();
}
public static Integer intValue(String s) {
return Integer.valueOf(s);
}
}
public ExtensionMethodOnRecord() {
super();
}
public void test() {
ExtensionMethodOnRecord.Extensions.intValue("1");
}
}
16 changes: 16 additions & 0 deletions test/transform/resource/before/ExtensionMethodOnRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// version 14:
import lombok.experimental.ExtensionMethod;

@ExtensionMethod(ExtensionMethodOnRecord.Extensions.class)
public record ExtensionMethodOnRecord() {

public void test() {
"1".intValue();
}

static class Extensions {
public static Integer intValue(String s) {
return Integer.valueOf(s);
}
}
}

0 comments on commit e3d4366

Please sign in to comment.