mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 08:18:29 +00:00
[fixes #3450] Add support for extension methods on records
This commit is contained in:
1 parent
000ce6d19a
commit
f67b66456a
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -2119,10 +2119,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user