mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 08:18:29 +00:00
@Setter will copy all NotNull and NonNull (case-insensitive) annotations to the parameter
@Getter will copy them to the getter method Added @NonNull to lombok to support null-checks in the setter
This commit is contained in:
1 parent
0cfa9e99d9
commit
f2c837bb47
7 files changed
+71
-25
No files matched your search
@@ -49,11 +49,14 @@ import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess;
|
||||
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Expression;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Literal;
|
||||
import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation;
|
||||
import org.eclipse.jdt.internal.compiler.ast.MemberValuePair;
|
||||
import org.eclipse.jdt.internal.compiler.ast.NormalAnnotation;
|
||||
import org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation;
|
||||
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
|
||||
@@ -251,6 +254,34 @@ public class Eclipse {
|
||||
return ref;
|
||||
}
|
||||
|
||||
public static Annotation[] copyAnnotations(Annotation[] annotations) {
|
||||
if (annotations == null) return null;
|
||||
Annotation[] outs = new Annotation[annotations.length];
|
||||
int idx = 0;
|
||||
for ( Annotation annotation : annotations ) {
|
||||
outs[idx++] = copyAnnotation(annotation);
|
||||
}
|
||||
return outs;
|
||||
}
|
||||
|
||||
public static Annotation copyAnnotation(Annotation annotation) {
|
||||
if (annotation instanceof MarkerAnnotation) {
|
||||
return new MarkerAnnotation(copyType(annotation.type), annotation.sourceStart);
|
||||
}
|
||||
|
||||
if (annotation instanceof SingleMemberAnnotation) {
|
||||
SingleMemberAnnotation result = new SingleMemberAnnotation(copyType(annotation.type), annotation.sourceStart);
|
||||
result.memberValue = ((SingleMemberAnnotation)annotation).memberValue;
|
||||
}
|
||||
|
||||
if (annotation instanceof NormalAnnotation) {
|
||||
NormalAnnotation result = new NormalAnnotation(copyType(annotation.type), annotation.sourceStart);
|
||||
result.memberValuePairs = ((NormalAnnotation)annotation).memberValuePairs;
|
||||
}
|
||||
|
||||
return annotation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the provided annotation type is likely to be the intended type for the given annotation node.
|
||||
*
|
||||
|
||||
@@ -27,7 +27,7 @@ import lombok.Getter;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.TransformationsUtil;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.eclipse.Eclipse;
|
||||
import static lombok.eclipse.Eclipse.*;
|
||||
import lombok.eclipse.EclipseAnnotationHandler;
|
||||
import lombok.eclipse.EclipseAST.Node;
|
||||
|
||||
@@ -64,7 +64,7 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
|
||||
public void generateGetterForField(Node fieldNode, ASTNode pos) {
|
||||
for ( Node child : fieldNode.down() ) {
|
||||
if ( child.getKind() == Kind.ANNOTATION ) {
|
||||
if ( Eclipse.annotationTypeMatches(Getter.class, child) ) {
|
||||
if ( annotationTypeMatches(Getter.class, child) ) {
|
||||
//The annotation will make it happen, so we can skip it.
|
||||
return;
|
||||
}
|
||||
@@ -87,7 +87,7 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
|
||||
}
|
||||
|
||||
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
|
||||
TypeReference fieldType = Eclipse.copyType(field.type);
|
||||
TypeReference fieldType = copyType(field.type);
|
||||
String fieldName = new String(field.name);
|
||||
boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0;
|
||||
String getterName = TransformationsUtil.toGetterName(fieldName, isBoolean);
|
||||
@@ -113,6 +113,10 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
|
||||
}
|
||||
|
||||
MethodDeclaration method = generateGetter((TypeDeclaration) fieldNode.up().get(), field, getterName, modifier, pos);
|
||||
Annotation[] nonNulls = findNonNullAnnotations(field);
|
||||
if (nonNulls.length != 0) {
|
||||
method.annotations = copyAnnotations(nonNulls);
|
||||
}
|
||||
|
||||
injectMethod(fieldNode.up(), method);
|
||||
|
||||
@@ -123,14 +127,14 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
|
||||
int modifier, ASTNode pos) {
|
||||
MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
|
||||
method.modifiers = modifier;
|
||||
method.returnType = Eclipse.copyType(field.type);
|
||||
method.returnType = copyType(field.type);
|
||||
method.annotations = null;
|
||||
method.arguments = null;
|
||||
method.selector = name.toCharArray();
|
||||
method.binding = null;
|
||||
method.thrownExceptions = null;
|
||||
method.typeParameters = null;
|
||||
method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
|
||||
method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
|
||||
Expression fieldExpression = new SingleNameReference(field.name, (field.declarationSourceStart << 32) | field.declarationSourceEnd);
|
||||
Statement returnStatement = new ReturnStatement(fieldExpression, field.sourceStart, field.sourceEnd);
|
||||
method.bodyStart = method.declarationSourceStart = method.sourceStart = pos.sourceStart;
|
||||
|
||||
@@ -22,12 +22,13 @@
|
||||
package lombok.eclipse.handlers;
|
||||
|
||||
import static lombok.eclipse.handlers.PKG.*;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Setter;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.TransformationsUtil;
|
||||
import lombok.core.AST.Kind;
|
||||
import lombok.eclipse.Eclipse;
|
||||
import static lombok.eclipse.Eclipse.*;
|
||||
import lombok.eclipse.EclipseAnnotationHandler;
|
||||
import lombok.eclipse.EclipseAST.Node;
|
||||
|
||||
@@ -77,7 +78,7 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> {
|
||||
public void generateSetterForField(Node fieldNode, ASTNode pos) {
|
||||
for ( Node child : fieldNode.down() ) {
|
||||
if ( child.getKind() == Kind.ANNOTATION ) {
|
||||
if ( Eclipse.annotationTypeMatches(Setter.class, child) ) {
|
||||
if ( annotationTypeMatches(Setter.class, child) ) {
|
||||
//The annotation will make it happen, so we can skip it.
|
||||
return;
|
||||
}
|
||||
@@ -134,27 +135,29 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> {
|
||||
method.modifiers = modifier;
|
||||
method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
|
||||
method.annotations = null;
|
||||
Argument param = new Argument(field.name, pos, Eclipse.copyType(field.type), 0);
|
||||
Argument param = new Argument(field.name, pos, copyType(field.type), 0);
|
||||
method.arguments = new Argument[] { param };
|
||||
method.selector = name.toCharArray();
|
||||
method.binding = null;
|
||||
method.thrownExceptions = null;
|
||||
method.typeParameters = null;
|
||||
method.scope = parent.scope == null ? null : new MethodScope(parent.scope, method, false);
|
||||
method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
|
||||
method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
|
||||
FieldReference thisX = new FieldReference(field.name, pos);
|
||||
thisX.receiver = new ThisReference(ast.sourceStart, ast.sourceEnd);
|
||||
Assignment assignment = new Assignment(thisX, new SingleNameReference(field.name, pos), (int)pos);
|
||||
method.bodyStart = method.declarationSourceStart = method.sourceStart = ast.sourceStart;
|
||||
method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = ast.sourceEnd;
|
||||
|
||||
Annotation nonNull = findNonNullannotation(field);
|
||||
if (nonNull == null) {
|
||||
Annotation[] nonNulls = findNonNullAnnotations(field);
|
||||
if (nonNulls.length == 0) {
|
||||
method.statements = new Statement[] { assignment };
|
||||
}
|
||||
else {
|
||||
param.annotations = copyAnnotations(nonNulls);
|
||||
|
||||
AllocationExpression exception = new AllocationExpression();
|
||||
exception.type = new QualifiedTypeReference(Eclipse.fromQualifiedName("java.lang.NullPointerException"), new long[]{0, 0, 0});
|
||||
exception.type = new QualifiedTypeReference(fromQualifiedName("java.lang.NullPointerException"), new long[]{0, 0, 0});
|
||||
exception.arguments = new Expression[] { new StringLiteral(field.name, 0, field.name.length - 1, 0)};
|
||||
ThrowStatement throwStatement = new ThrowStatement(exception, 0, 0);
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
package lombok.eclipse.handlers;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.core.AST.Kind;
|
||||
@@ -215,16 +217,18 @@ class PKG {
|
||||
type.add(method, Kind.METHOD).recursiveSetHandled();
|
||||
}
|
||||
|
||||
static Annotation findNonNullannotation(FieldDeclaration field) {
|
||||
static Annotation[] findNonNullAnnotations(FieldDeclaration field) {
|
||||
List<Annotation> result = new ArrayList<Annotation>();
|
||||
for (Annotation annotation : field.annotations) {
|
||||
TypeReference typeRef = annotation.type;
|
||||
if ( typeRef != null && typeRef.getTypeName()!= null ) {
|
||||
char[][] typeName = typeRef.getTypeName();
|
||||
if (new String(typeName[typeName.length - 1]).equals("NonNull")) {
|
||||
return annotation;
|
||||
String suspect = new String(typeName[typeName.length - 1]);
|
||||
if (suspect.equalsIgnoreCase("NonNull") || suspect.equalsIgnoreCase("NotNull")) {
|
||||
result.add(annotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return result.toArray(new Annotation[0]);
|
||||
}
|
||||
}
|
||||
@@ -128,7 +128,8 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
|
||||
List<JCExpression> throwsClauses = List.nil();
|
||||
JCExpression annotationMethodDefaultValue = null;
|
||||
|
||||
return treeMaker.MethodDef(treeMaker.Modifiers(access, List.<JCAnnotation>nil()), methodName, methodType,
|
||||
List<JCAnnotation> annotations = findNonNullAnnotations(field);
|
||||
return treeMaker.MethodDef(treeMaker.Modifiers(access, annotations), methodName, methodType,
|
||||
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
|
||||
}
|
||||
}
|
||||
@@ -121,10 +121,9 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> {
|
||||
JCFieldAccess thisX = treeMaker.Select(treeMaker.Ident(field.toName("this")), fieldDecl.name);
|
||||
JCAssign assign = treeMaker.Assign(thisX, treeMaker.Ident(fieldDecl.name));
|
||||
|
||||
|
||||
List<JCStatement> statements;
|
||||
JCAnnotation nonNull = findNonNullAnnotation(field);
|
||||
if (nonNull == null) {
|
||||
List<JCAnnotation> nonNulls = findNonNullAnnotations(field);
|
||||
if (nonNulls.isEmpty()) {
|
||||
statements = List.<JCStatement>of(treeMaker.Exec(assign));
|
||||
}
|
||||
else {
|
||||
@@ -137,7 +136,8 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> {
|
||||
|
||||
JCBlock methodBody = treeMaker.Block(0, statements);
|
||||
Name methodName = field.toName(toSetterName(fieldDecl));
|
||||
JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(0), fieldDecl.name, fieldDecl.vartype, null);
|
||||
|
||||
JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(0, nonNulls), fieldDecl.name, fieldDecl.vartype, null);
|
||||
JCExpression methodType = treeMaker.Type(field.getSymbolTable().voidType);
|
||||
|
||||
List<JCTypeParameter> methodGenericParams = List.nil();
|
||||
|
||||
@@ -254,16 +254,19 @@ class PKG {
|
||||
return e;
|
||||
}
|
||||
|
||||
static JCAnnotation findNonNullAnnotation(Node fieldNode) {
|
||||
static List<JCAnnotation> findNonNullAnnotations(Node fieldNode) {
|
||||
List<JCAnnotation> result = List.nil();
|
||||
for ( Node child : fieldNode.down() ) {
|
||||
if ( child.getKind() == Kind.ANNOTATION ) {
|
||||
JCAnnotation annotation = (JCAnnotation) child.get();
|
||||
String name = annotation.annotationType.toString();
|
||||
if (name.equals("NonNull") || name.endsWith(".NonNull")) {
|
||||
return annotation;
|
||||
int idx = name.lastIndexOf(".");
|
||||
String suspect = idx == -1 ? name : name.substring(idx + 1);
|
||||
if (suspect.equalsIgnoreCase("NonNull") || suspect.equalsIgnoreCase("NotNull")) {
|
||||
result = result.append(annotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user