Merge pull request #3479 from Rawi01/extensionmethod-record

Add support for extension methods on records
This commit is contained in:
Reinier Zwitserloot authored and GitHub committed 2023-09-17 12:30:42 +02:00
commit e3d43668eb
5 files changed
+60 -4

No files matched your search

@@ -73,10 +73,10 @@ public class HandleExtensionMethod extends JavacAnnotationHandler<ExtensionMetho
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;
}
@@ -2122,10 +2122,20 @@ public class JavacHandlerUtil {
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).
*/
@@ -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);
}
}
}
@@ -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");
}
}
@@ -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);
}
}
}