mirror of
https://github.com/tiennm99/lombok.git
synced 2026-09-05 08:17:35 +00:00
Support With(By) on records and record components
This also replaces the javac/eclipse specific code for searching the parent fields of an annotation by a search based on the lombok AST.
This commit is contained in:
@@ -160,17 +160,15 @@ public abstract class LombokNode<A extends AST<A, L, N>, L extends LombokNode<A,
|
||||
List<L> fields = new ArrayList<L>();
|
||||
for (L potentialField : type.down()) {
|
||||
if (potentialField.getKind() != Kind.FIELD) continue;
|
||||
if (fieldContainsAnnotation(potentialField.get(), get())) fields.add(potentialField);
|
||||
for (L child : potentialField.down()) {
|
||||
if (child.getKind() != Kind.ANNOTATION) continue;
|
||||
if (child.get() == get()) fields.add(potentialField);
|
||||
}
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return {@code true} if the annotation is attached to the field.
|
||||
*/
|
||||
protected abstract boolean fieldContainsAnnotation(N field, N annotation);
|
||||
|
||||
/**
|
||||
* Returns the direct parent node in the AST tree of this node. For example, a local variable declaration's
|
||||
* direct parent can be e.g. an If block, but its {@code up()} {@code LombokNode} is the {@code Method} that contains it.
|
||||
|
||||
@@ -143,16 +143,6 @@ public class EclipseNode extends lombok.core.LombokNode<EclipseAST, EclipseNode,
|
||||
}
|
||||
}
|
||||
|
||||
@Override protected boolean fieldContainsAnnotation(ASTNode field, ASTNode annotation) {
|
||||
if (!(field instanceof FieldDeclaration)) return false;
|
||||
FieldDeclaration f = (FieldDeclaration) field;
|
||||
if (f.annotations == null) return false;
|
||||
for (Annotation childAnnotation : f.annotations) {
|
||||
if (childAnnotation == annotation) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override public String getName() {
|
||||
final char[] n;
|
||||
|
||||
@@ -29,9 +29,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
|
||||
import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
|
||||
@@ -138,10 +136,7 @@ public class HandleWithBy extends EclipseAnnotationHandler<WithBy> {
|
||||
|
||||
switch (node.getKind()) {
|
||||
case FIELD:
|
||||
Set<EclipseNode> fields = new LinkedHashSet<EclipseNode>();
|
||||
fields.add(node);
|
||||
fields.addAll(annotationNode.upFromAnnotationToFields());
|
||||
createWithByForFields(level, fields, annotationNode, true, onMethod);
|
||||
createWithByForFields(level, annotationNode.upFromAnnotationToFields(), annotationNode, true, onMethod);
|
||||
break;
|
||||
case TYPE:
|
||||
if (!onMethod.isEmpty()) {
|
||||
|
||||
@@ -186,16 +186,6 @@ public class JavacNode extends lombok.core.LombokNode<JavacAST, JavacNode, JCTre
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override protected boolean fieldContainsAnnotation(JCTree field, JCTree annotation) {
|
||||
if (!(field instanceof JCVariableDecl)) return false;
|
||||
JCVariableDecl f = (JCVariableDecl) field;
|
||||
if (f.mods.annotations == null) return false;
|
||||
for (JCAnnotation childAnnotation : f.mods.annotations) {
|
||||
if (childAnnotation == annotation) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenient shortcut to the owning JavacAST object's getTreeMaker method.
|
||||
*
|
||||
|
||||
@@ -3,4 +3,9 @@ record WithByOnRecord(String a, String b) {
|
||||
public WithByOnRecord withABy(final java.util.function.Function<? super String, ? extends String> transformer) {
|
||||
return new WithByOnRecord(transformer.apply(this.a), this.b);
|
||||
}
|
||||
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public WithByOnRecord withBBy(final java.util.function.Function<? super String, ? extends String> transformer) {
|
||||
return new WithByOnRecord(this.a, transformer.apply(this.b));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
record WithByOnRecordComponent(String a, String b) {
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public WithByOnRecordComponent withABy(final java.util.function.Function<? super String, ? extends String> transformer) {
|
||||
return new WithByOnRecordComponent(transformer.apply(this.a), this.b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
record WithOnRecordComponent(String a, String b) {
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public WithOnRecordComponent withA(final String a) {
|
||||
return this.a == a ? this : new WithOnRecordComponent(a, this.b);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import lombok.experimental.WithBy;
|
||||
record WithByOnRecord(String a, String b) {
|
||||
@WithBy record WithByOnRecord(String a, String b) {
|
||||
/* Implicit */ private final String a;
|
||||
/* Implicit */ private final String b;
|
||||
public WithByOnRecord( String a, String b) {
|
||||
public WithByOnRecord(String a, String b) {
|
||||
super();
|
||||
.a = a;
|
||||
.b = b;
|
||||
@@ -10,4 +10,7 @@ record WithByOnRecord(String a, String b) {
|
||||
public @java.lang.SuppressWarnings("all") WithByOnRecord withABy(final java.util.function.Function<? super String, ? extends String> transformer) {
|
||||
return new WithByOnRecord(transformer.apply(this.a), this.b);
|
||||
}
|
||||
public @java.lang.SuppressWarnings("all") WithByOnRecord withBBy(final java.util.function.Function<? super String, ? extends String> transformer) {
|
||||
return new WithByOnRecord(this.a, transformer.apply(this.b));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import lombok.experimental.WithBy;
|
||||
record WithByOnRecordComponent(String a, String b) {
|
||||
/* Implicit */ private final String a;
|
||||
/* Implicit */ private final String b;
|
||||
public WithByOnRecordComponent( String a, String b) {
|
||||
super();
|
||||
.a = a;
|
||||
.b = b;
|
||||
}
|
||||
public @java.lang.SuppressWarnings("all") WithByOnRecordComponent withABy(final java.util.function.Function<? super String, ? extends String> transformer) {
|
||||
return new WithByOnRecordComponent(transformer.apply(this.a), this.b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import lombok.With;
|
||||
record WithOnRecordComponent(String a, String b) {
|
||||
/* Implicit */ private final String a;
|
||||
/* Implicit */ private final String b;
|
||||
public WithOnRecordComponent( String a, String b) {
|
||||
super();
|
||||
.a = a;
|
||||
.b = b;
|
||||
}
|
||||
public @java.lang.SuppressWarnings("all") WithOnRecordComponent withA(final String a) {
|
||||
return ((this.a == a) ? this : new WithOnRecordComponent(a, this.b));
|
||||
}
|
||||
}
|
||||
@@ -2,5 +2,6 @@
|
||||
|
||||
import lombok.experimental.WithBy;
|
||||
|
||||
record WithByOnRecord(@WithBy String a, String b) {
|
||||
@WithBy
|
||||
record WithByOnRecord(String a, String b) {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// version 14:
|
||||
|
||||
import lombok.experimental.WithBy;
|
||||
|
||||
record WithByOnRecordComponent(@WithBy String a, String b) {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// version 14:
|
||||
|
||||
import lombok.With;
|
||||
|
||||
record WithOnRecordComponent(@With String a, String b) {
|
||||
}
|
||||
Reference in New Issue
Block a user