mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 20:15:09 +00:00
null checks are no longer generated if you put @NonNull on primitives.
This commit is contained in:
1 parent
d16775032b
commit
e1ee1b7d2d
9 files changed
+58
-30
No files matched your search
@@ -27,6 +27,7 @@ import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Container for static utility methods useful for some of the standard lombok transformations, regardless of
|
||||
@@ -79,6 +80,12 @@ public class TransformationsUtil {
|
||||
return buildName(prefix, fieldName.toString());
|
||||
}
|
||||
|
||||
public static final Pattern PRIMITIVE_TYPE_NAME_PATTERN = Pattern.compile(
|
||||
"^(boolean|byte|short|int|long|float|double|char)$");
|
||||
|
||||
public static final Pattern NON_NULL_PATTERN = Pattern.compile("^no[tn]null$", Pattern.CASE_INSENSITIVE);
|
||||
public static final Pattern NULLABLE_PATTERN = Pattern.compile("^nullable$", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
/**
|
||||
* Generates a getter name from a given field name.
|
||||
*
|
||||
|
||||
@@ -32,6 +32,7 @@ import java.util.List;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Data;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.TransformationsUtil;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.eclipse.Eclipse;
|
||||
import lombok.eclipse.EclipseAnnotationHandler;
|
||||
@@ -89,7 +90,7 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
|
||||
//Skip static fields.
|
||||
if ( (fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0 ) continue;
|
||||
boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0;
|
||||
boolean isNonNull = findAnnotations(fieldDecl, NON_NULL_PATTERN).length != 0;
|
||||
boolean isNonNull = findAnnotations(fieldDecl, TransformationsUtil.NON_NULL_PATTERN).length != 0;
|
||||
if ( (isFinal || isNonNull) && fieldDecl.initialization == null ) nodesForConstructor.add(child);
|
||||
new HandleGetter().generateGetterForField(child, annotationNode.get());
|
||||
if ( !isFinal ) new HandleSetter().generateSetterForField(child, annotationNode.get());
|
||||
@@ -151,9 +152,12 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
|
||||
assigns.add(new Assignment(thisX, new SingleNameReference(field.name, p), (int)p));
|
||||
long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd;
|
||||
Argument argument = new Argument(field.name, fieldPos, copyType(field.type), Modifier.FINAL);
|
||||
Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
|
||||
Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
|
||||
if (nonNulls.length != 0) nullChecks.add(generateNullCheck(field));
|
||||
Annotation[] nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN);
|
||||
Annotation[] nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN);
|
||||
if (nonNulls.length != 0) {
|
||||
Statement nullCheck = generateNullCheck(field);
|
||||
if (nullCheck != null) nullChecks.add(nullCheck);
|
||||
}
|
||||
Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables);
|
||||
if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations;
|
||||
args.add(argument);
|
||||
@@ -201,7 +205,7 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
|
||||
|
||||
Argument argument = new Argument(field.name, fieldPos, copyType(field.type), 0);
|
||||
Annotation[] copiedAnnotations = copyAnnotations(
|
||||
findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN));
|
||||
findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN));
|
||||
if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations;
|
||||
args.add(new Argument(field.name, fieldPos, copyType(field.type), Modifier.FINAL));
|
||||
}
|
||||
|
||||
@@ -21,13 +21,13 @@
|
||||
*/
|
||||
package lombok.eclipse.handlers;
|
||||
|
||||
import static lombok.eclipse.Eclipse.*;
|
||||
import static lombok.eclipse.handlers.PKG.*;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.TransformationsUtil;
|
||||
import lombok.core.AST.Kind;
|
||||
import static lombok.eclipse.Eclipse.*;
|
||||
import lombok.eclipse.EclipseAnnotationHandler;
|
||||
import lombok.eclipse.EclipseAST.Node;
|
||||
|
||||
@@ -114,7 +114,7 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
|
||||
|
||||
MethodDeclaration method = generateGetter((TypeDeclaration) fieldNode.up().get(), field, getterName, modifier, pos);
|
||||
Annotation[] copiedAnnotations = copyAnnotations(
|
||||
findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN));
|
||||
findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN));
|
||||
if (copiedAnnotations.length != 0) {
|
||||
method.annotations = copiedAnnotations;
|
||||
}
|
||||
|
||||
@@ -142,12 +142,14 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> {
|
||||
method.bodyStart = method.declarationSourceStart = method.sourceStart = ast.sourceStart;
|
||||
method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = ast.sourceEnd;
|
||||
|
||||
Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
|
||||
Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
|
||||
Annotation[] nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN);
|
||||
Annotation[] nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN);
|
||||
if (nonNulls.length == 0) {
|
||||
method.statements = new Statement[] { assignment };
|
||||
} else {
|
||||
method.statements = new Statement[] { generateNullCheck(field), assignment };
|
||||
Statement nullCheck = generateNullCheck(field);
|
||||
if (nullCheck != null) method.statements = new Statement[] { nullCheck, assignment };
|
||||
else method.statements = new Statement[] { assignment };
|
||||
}
|
||||
Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables);
|
||||
if (copiedAnnotations.length != 0) param.annotations = copiedAnnotations;
|
||||
|
||||
@@ -29,7 +29,9 @@ import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.core.TransformationsUtil;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.eclipse.Eclipse;
|
||||
import lombok.eclipse.EclipseAST;
|
||||
|
||||
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
|
||||
@@ -55,6 +57,11 @@ import org.eclipse.jdt.internal.compiler.ast.TypeReference;
|
||||
class PKG {
|
||||
private PKG() {}
|
||||
|
||||
static boolean isPrimitive(TypeReference ref) {
|
||||
if (ref.dimensions() > 0) return false;
|
||||
return TransformationsUtil.PRIMITIVE_TYPE_NAME_PATTERN.matcher(Eclipse.toQualifiedName(ref.getTypeName())).matches();
|
||||
}
|
||||
|
||||
static int toModifier(AccessLevel value) {
|
||||
switch ( value ) {
|
||||
case MODULE:
|
||||
@@ -232,9 +239,6 @@ class PKG {
|
||||
type.add(method, Kind.METHOD).recursiveSetHandled();
|
||||
}
|
||||
|
||||
static final Pattern NON_NULL_PATTERN = Pattern.compile("^no[tn]null$", Pattern.CASE_INSENSITIVE);
|
||||
static final Pattern NULLABLE_PATTERN = Pattern.compile("^nullable$", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
static Annotation[] findAnnotations(FieldDeclaration field, Pattern namePattern) {
|
||||
List<Annotation> result = new ArrayList<Annotation>();
|
||||
for (Annotation annotation : field.annotations) {
|
||||
@@ -251,6 +255,7 @@ class PKG {
|
||||
}
|
||||
|
||||
static Statement generateNullCheck(AbstractVariableDeclaration variable) {
|
||||
if (isPrimitive(variable.type)) return null;
|
||||
AllocationExpression exception = new AllocationExpression();
|
||||
exception.type = new QualifiedTypeReference(fromQualifiedName("java.lang.NullPointerException"), new long[]{0, 0, 0});
|
||||
exception.arguments = new Expression[] { new StringLiteral(variable.name, 0, variable.name.length - 1, 0)};
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.lang.reflect.Modifier;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.TransformationsUtil;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.javac.JavacAnnotationHandler;
|
||||
import lombok.javac.JavacAST.Node;
|
||||
@@ -79,7 +80,7 @@ public class HandleData implements JavacAnnotationHandler<Data> {
|
||||
if ( (fieldFlags & Flags.STATIC) != 0 ) continue;
|
||||
if ( (fieldFlags & Flags.TRANSIENT) == 0 ) nodesForEquality = nodesForEquality.append(child);
|
||||
boolean isFinal = (fieldFlags & Flags.FINAL) != 0;
|
||||
boolean isNonNull = !findAnnotations(child, NON_NULL_PATTERN).isEmpty();
|
||||
boolean isNonNull = !findAnnotations(child, TransformationsUtil.NON_NULL_PATTERN).isEmpty();
|
||||
if ( (isFinal || isNonNull) && fieldDecl.init == null ) nodesForConstructor = nodesForConstructor.append(child);
|
||||
new HandleGetter().generateGetterForField(child, annotationNode.get());
|
||||
if ( !isFinal ) new HandleSetter().generateSetterForField(child, annotationNode.get());
|
||||
@@ -113,8 +114,8 @@ public class HandleData implements JavacAnnotationHandler<Data> {
|
||||
|
||||
for ( Node fieldNode : fields ) {
|
||||
JCVariableDecl field = (JCVariableDecl) fieldNode.get();
|
||||
List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN);
|
||||
List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN);
|
||||
List<JCAnnotation> nonNulls = findAnnotations(fieldNode, TransformationsUtil.NON_NULL_PATTERN);
|
||||
List<JCAnnotation> nullables = findAnnotations(fieldNode, TransformationsUtil.NULLABLE_PATTERN);
|
||||
JCVariableDecl param = maker.VarDef(maker.Modifiers(Flags.FINAL, nonNulls.appendList(nullables)), field.name, field.vartype, null);
|
||||
params = params.append(param);
|
||||
JCFieldAccess thisX = maker.Select(maker.Ident(fieldNode.toName("this")), field.name);
|
||||
@@ -122,7 +123,8 @@ public class HandleData implements JavacAnnotationHandler<Data> {
|
||||
assigns = assigns.append(maker.Exec(assign));
|
||||
|
||||
if (!nonNulls.isEmpty()) {
|
||||
nullChecks = nullChecks.append(generateNullCheck(maker, fieldNode));
|
||||
JCStatement nullCheck = generateNullCheck(maker, fieldNode);
|
||||
if (nullCheck != null) nullChecks = nullChecks.append(nullCheck);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,8 +170,8 @@ public class HandleData implements JavacAnnotationHandler<Data> {
|
||||
for ( JCExpression arg : typeApply.arguments ) tArgs = tArgs.append(arg);
|
||||
pType = maker.TypeApply(typeApply.clazz, tArgs);
|
||||
} else pType = field.vartype;
|
||||
List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN);
|
||||
List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN);
|
||||
List<JCAnnotation> nonNulls = findAnnotations(fieldNode, TransformationsUtil.NON_NULL_PATTERN);
|
||||
List<JCAnnotation> nullables = findAnnotations(fieldNode, TransformationsUtil.NULLABLE_PATTERN);
|
||||
JCVariableDecl param = maker.VarDef(maker.Modifiers(Flags.FINAL, nonNulls.appendList(nullables)), field.name, pType, null);
|
||||
params = params.append(param);
|
||||
args = args.append(maker.Ident(field.name));
|
||||
|
||||
@@ -25,6 +25,7 @@ import static lombok.javac.handlers.PKG.*;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.TransformationsUtil;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.javac.Javac;
|
||||
import lombok.javac.JavacAnnotationHandler;
|
||||
@@ -128,8 +129,8 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
|
||||
List<JCExpression> throwsClauses = List.nil();
|
||||
JCExpression annotationMethodDefaultValue = null;
|
||||
|
||||
List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN);
|
||||
List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN);
|
||||
List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN);
|
||||
List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN);
|
||||
return treeMaker.MethodDef(treeMaker.Modifiers(access, nonNulls.appendList(nullables)), methodName, methodType,
|
||||
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import static lombok.javac.handlers.PKG.*;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Setter;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.TransformationsUtil;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.javac.Javac;
|
||||
import lombok.javac.JavacAST;
|
||||
@@ -120,13 +121,15 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> {
|
||||
JCAssign assign = treeMaker.Assign(thisX, treeMaker.Ident(fieldDecl.name));
|
||||
|
||||
List<JCStatement> statements;
|
||||
List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN);
|
||||
List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN);
|
||||
List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN);
|
||||
List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN);
|
||||
|
||||
if (nonNulls.isEmpty()) {
|
||||
statements = List.<JCStatement>of(treeMaker.Exec(assign));
|
||||
}
|
||||
else {
|
||||
statements = List.<JCStatement>of(generateNullCheck(treeMaker, field), treeMaker.Exec(assign));
|
||||
} else {
|
||||
JCStatement nullCheck = generateNullCheck(treeMaker, field);
|
||||
if (nullCheck != null) statements = List.<JCStatement>of(nullCheck, treeMaker.Exec(assign));
|
||||
else statements = List.<JCStatement>of(treeMaker.Exec(assign));
|
||||
}
|
||||
|
||||
JCBlock methodBody = treeMaker.Block(0, statements);
|
||||
|
||||
@@ -51,6 +51,11 @@ class PKG {
|
||||
//Prevent instantiation
|
||||
}
|
||||
|
||||
static boolean isPrimitive(JCExpression ref) {
|
||||
String typeName = ref.toString();
|
||||
return TransformationsUtil.PRIMITIVE_TYPE_NAME_PATTERN.matcher(typeName).matches();
|
||||
}
|
||||
|
||||
static java.util.List<String> toAllGetterNames(JCVariableDecl field) {
|
||||
CharSequence fieldName = field.name;
|
||||
|
||||
@@ -258,9 +263,6 @@ class PKG {
|
||||
return e;
|
||||
}
|
||||
|
||||
static final Pattern NON_NULL_PATTERN = Pattern.compile("^no[tn]null$", Pattern.CASE_INSENSITIVE);
|
||||
static final Pattern NULLABLE_PATTERN = Pattern.compile("^nullable$", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
static List<JCAnnotation> findAnnotations(Node fieldNode, Pattern namePattern) {
|
||||
List<JCAnnotation> result = List.nil();
|
||||
for ( Node child : fieldNode.down() ) {
|
||||
@@ -278,7 +280,9 @@ class PKG {
|
||||
}
|
||||
|
||||
static JCStatement generateNullCheck(TreeMaker treeMaker, JavacAST.Node variable) {
|
||||
Name fieldName = ((JCVariableDecl) variable.get()).name;
|
||||
JCVariableDecl varDecl = (JCVariableDecl) variable.get();
|
||||
if (isPrimitive(varDecl.vartype)) return null;
|
||||
Name fieldName = varDecl.name;
|
||||
JCExpression npe = chainDots(treeMaker, variable, "java", "lang", "NullPointerException");
|
||||
JCTree exception = treeMaker.NewClass(null, List.<JCExpression>nil(), npe, List.<JCExpression>of(treeMaker.Literal(fieldName.toString())), null);
|
||||
JCStatement throwStatement = treeMaker.Throw(exception);
|
||||
|
||||
Reference in new issue
Block a user