mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 16:15:16 +00:00
Builder.Default now throws out a few more warnings if misused.
This commit is contained in:
1 parent
f8f9e2a025
commit
11fdfb56f7
4 files changed
+85
-30
No files matched your search
@@ -191,6 +191,17 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> {
|
||||
EclipseNode isDefault = findAnnotation(Builder.Default.class, fieldNode);
|
||||
boolean isFinal = ((fd.modifiers & ClassFileConstants.AccFinal) != 0) || (valuePresent && !hasAnnotation(NonFinal.class, fieldNode));
|
||||
|
||||
BuilderFieldData bfd = new BuilderFieldData();
|
||||
bfd.rawName = fieldNode.getName().toCharArray();
|
||||
bfd.name = removePrefixFromField(fieldNode);
|
||||
bfd.type = fd.type;
|
||||
bfd.singularData = getSingularData(fieldNode, ast);
|
||||
|
||||
if (bfd.singularData != null && isDefault != null) {
|
||||
isDefault.addError("@Builder.Default and @Singular cannot be mixed.");
|
||||
isDefault = null;
|
||||
}
|
||||
|
||||
if (fd.initialization == null && isDefault != null) {
|
||||
isDefault.addWarning("@Builder.Default requires an initializing expression (' = something;').");
|
||||
isDefault = null;
|
||||
@@ -201,22 +212,12 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> {
|
||||
fieldNode.addWarning("@Builder will ignore the initializing expression entirely. If you want the initializing expression to serve as default, add @Builder.Default. if it is not supposed to be settable during building, add @Builder.Constant.");
|
||||
}
|
||||
|
||||
BuilderFieldData bfd = new BuilderFieldData();
|
||||
bfd.rawName = fieldNode.getName().toCharArray();
|
||||
bfd.name = removePrefixFromField(fieldNode);
|
||||
bfd.type = fd.type;
|
||||
bfd.singularData = getSingularData(fieldNode, ast);
|
||||
if (isDefault != null) {
|
||||
if (bfd.singularData != null) {
|
||||
isDefault.addError("@Builder.Default and @Singular cannot be mixed.");
|
||||
isDefault = null;
|
||||
} else {
|
||||
bfd.nameOfDefaultProvider = prefixWith(DEFAULT_PREFIX, bfd.name);
|
||||
bfd.nameOfSetFlag = prefixWith(bfd.name, SET_PREFIX);
|
||||
|
||||
MethodDeclaration md = generateDefaultProvider(bfd.nameOfDefaultProvider, fieldNode, ast);
|
||||
if (md != null) injectMethod(tdParent, md);
|
||||
}
|
||||
bfd.nameOfDefaultProvider = prefixWith(DEFAULT_PREFIX, bfd.name);
|
||||
bfd.nameOfSetFlag = prefixWith(bfd.name, SET_PREFIX);
|
||||
|
||||
MethodDeclaration md = generateDefaultProvider(bfd.nameOfDefaultProvider, fieldNode, ast);
|
||||
if (md != null) injectMethod(tdParent, md);
|
||||
}
|
||||
addObtainVia(bfd, fieldNode);
|
||||
builderFields.add(bfd);
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package lombok.eclipse.handlers;
|
||||
|
||||
import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Annotation;
|
||||
import org.mangosdk.spi.ProviderFor;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.HandlerPriority;
|
||||
import lombok.eclipse.EclipseAnnotationHandler;
|
||||
import lombok.eclipse.EclipseNode;
|
||||
|
||||
@ProviderFor(EclipseAnnotationHandler.class)
|
||||
@HandlerPriority(-1025) //HandleBuilder's level, minus one.
|
||||
public class HandleBuilderDefault extends EclipseAnnotationHandler<Builder.Default> {
|
||||
@Override public void handle(AnnotationValues<Builder.Default> annotation, Annotation ast, EclipseNode annotationNode) {
|
||||
EclipseNode annotatedField = annotationNode.up();
|
||||
if (annotatedField.getKind() != Kind.FIELD) return;
|
||||
EclipseNode classWithAnnotatedField = annotatedField.up();
|
||||
if (!hasAnnotation(Builder.class, classWithAnnotatedField)) {
|
||||
annotationNode.addWarning("@Builder.Default requires @Builder on the class for it to mean anything.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,6 +145,16 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> {
|
||||
JCVariableDecl fd = (JCVariableDecl) fieldNode.get();
|
||||
JavacNode isDefault = findAnnotation(Builder.Default.class, fieldNode, true);
|
||||
boolean isFinal = (fd.mods.flags & Flags.FINAL) != 0 || (valuePresent && !hasAnnotation(NonFinal.class, fieldNode));
|
||||
BuilderFieldData bfd = new BuilderFieldData();
|
||||
bfd.rawName = fd.name;
|
||||
bfd.name = removePrefixFromField(fieldNode);
|
||||
bfd.type = fd.vartype;
|
||||
bfd.singularData = getSingularData(fieldNode);
|
||||
|
||||
if (bfd.singularData != null && isDefault != null) {
|
||||
isDefault.addError("@Builder.Default and @Singular cannot be mixed.");
|
||||
isDefault = null;
|
||||
}
|
||||
|
||||
if (fd.init == null && isDefault != null) {
|
||||
isDefault.addWarning("@Builder.Default requires an initializing expression (' = something;').");
|
||||
@@ -156,22 +166,12 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> {
|
||||
fieldNode.addWarning("@Builder will ignore the initializing expression entirely. If you want the initializing expression to serve as default, add @Builder.Default. if it is not supposed to be settable during building, add @Builder.Constant.");
|
||||
}
|
||||
|
||||
BuilderFieldData bfd = new BuilderFieldData();
|
||||
bfd.rawName = fd.name;
|
||||
bfd.name = removePrefixFromField(fieldNode);
|
||||
bfd.type = fd.vartype;
|
||||
bfd.singularData = getSingularData(fieldNode);
|
||||
if (isDefault != null) {
|
||||
if (bfd.singularData != null) {
|
||||
isDefault.addError("@Builder.Default and @Singular cannot be mixed.");
|
||||
isDefault = null;
|
||||
} else {
|
||||
bfd.nameOfDefaultProvider = parent.toName("$default$" + bfd.name);
|
||||
bfd.nameOfSetFlag = parent.toName(bfd.name + "$set");
|
||||
JCMethodDecl md = generateDefaultProvider(bfd.nameOfDefaultProvider, fieldNode);
|
||||
recursiveSetGeneratedBy(md, ast, annotationNode.getContext());
|
||||
if (md != null) injectMethod(tdParent, md);
|
||||
}
|
||||
bfd.nameOfDefaultProvider = parent.toName("$default$" + bfd.name);
|
||||
bfd.nameOfSetFlag = parent.toName(bfd.name + "$set");
|
||||
JCMethodDecl md = generateDefaultProvider(bfd.nameOfDefaultProvider, fieldNode);
|
||||
recursiveSetGeneratedBy(md, ast, annotationNode.getContext());
|
||||
if (md != null) injectMethod(tdParent, md);
|
||||
}
|
||||
addObtainVia(bfd, fieldNode);
|
||||
builderFields.add(bfd);
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package lombok.javac.handlers;
|
||||
|
||||
import static lombok.javac.handlers.JavacHandlerUtil.*;
|
||||
|
||||
import org.mangosdk.spi.ProviderFor;
|
||||
|
||||
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.HandlerPriority;
|
||||
import lombok.javac.JavacAnnotationHandler;
|
||||
import lombok.javac.JavacNode;
|
||||
|
||||
@ProviderFor(JavacAnnotationHandler.class)
|
||||
@HandlerPriority(-1025) //HandleBuilder's level, minus one.
|
||||
public class HandleBuilderDefault extends JavacAnnotationHandler<Builder.Default> {
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override public void handle(AnnotationValues<Builder.Default> annotation, JCAnnotation ast, JavacNode annotationNode) {
|
||||
JavacNode annotatedField = annotationNode.up();
|
||||
if (annotatedField.getKind() != Kind.FIELD) return;
|
||||
JavacNode classWithAnnotatedField = annotatedField.up();
|
||||
if (!hasAnnotation(Builder.class, classWithAnnotatedField) && !hasAnnotation(lombok.experimental.Builder.class, classWithAnnotatedField)) {
|
||||
annotationNode.addWarning("@Builder.Default requires @Builder on the class for it to mean anything.");
|
||||
deleteAnnotationIfNeccessary(annotationNode, Builder.Default.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user