Finished work on @Accessors

This commit is contained in:
Reinier Zwitserloot
2012-03-26 21:57:24 +02:00
parent a514af4dcd
commit eb3250a2fa
9 changed files with 374 additions and 23 deletions
@@ -937,7 +937,7 @@ public class EclipseHandlerUtil {
*/
public static List<String> toAllGetterNames(EclipseNode field, boolean isBoolean) {
String fieldName = field.getName();
AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field);
AnnotationValues<Accessors> accessors = getAccessorsForField(field);
return TransformationsUtil.toAllGetterNames(accessors, fieldName, isBoolean);
}
@@ -977,6 +977,18 @@ public class EclipseHandlerUtil {
return TransformationsUtil.toSetterName(accessors, fieldName, isBoolean);
}
/**
* When generating a setter, the setter either returns void (beanspec) or Self (fluent).
* This method scans for the {@code Accessors} annotation to figure that out.
*/
public static boolean shouldReturnThis(EclipseNode field) {
if ((((FieldDeclaration) field.get()).modifiers & ClassFileConstants.AccStatic) != 0) return false;
AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field);
boolean forced = (accessors.getActualExpression("chain") != null);
Accessors instance = accessors.getInstance();
return instance.chain() || (instance.fluent() && !forced);
}
/**
* Checks if the field should be included in operations that work on 'all' fields:
* If the field is static, or starts with a '$', or is actually an enum constant, 'false' is returned, indicating you should skip it.
@@ -1006,7 +1018,7 @@ public class EclipseHandlerUtil {
EclipseNode current = field.up();
while (current != null) {
for (EclipseNode node : field.down()) {
for (EclipseNode node : current.down()) {
if (annotationTypeMatches(Accessors.class, node)) {
return createAnnotation(Accessors.class, node);
}
@@ -25,15 +25,18 @@ import static lombok.eclipse.Eclipse.*;
import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import lombok.AccessLevel;
import lombok.Setter;
import lombok.core.AST.Kind;
import lombok.core.AnnotationValues;
import lombok.core.TransformationsUtil;
import lombok.core.AST.Kind;
import lombok.eclipse.EclipseAnnotationHandler;
import lombok.eclipse.EclipseNode;
import lombok.eclipse.handlers.EclipseHandlerUtil.FieldAccess;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
@@ -43,9 +46,14 @@ import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.NameReference;
import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference;
import org.eclipse.jdt.internal.compiler.ast.ReturnStatement;
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
import org.eclipse.jdt.internal.compiler.ast.Statement;
import org.eclipse.jdt.internal.compiler.ast.ThisReference;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
@@ -149,6 +157,7 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> {
TypeReference fieldType = copyType(field.type, source);
boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0;
String setterName = toSetterName(fieldNode, isBoolean);
boolean shouldReturnThis = shouldReturnThis(fieldNode);
if (setterName == null) {
errorNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
return;
@@ -174,19 +183,40 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> {
}
}
MethodDeclaration method = generateSetter((TypeDeclaration) fieldNode.up().get(), fieldNode, setterName, modifier, source);
MethodDeclaration method = generateSetter((TypeDeclaration) fieldNode.up().get(), fieldNode, setterName, shouldReturnThis, modifier, source);
injectMethod(fieldNode.up(), method);
}
private MethodDeclaration generateSetter(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, ASTNode source) {
private MethodDeclaration generateSetter(TypeDeclaration parent, EclipseNode fieldNode, String name, boolean shouldReturnThis, int modifier, ASTNode source) {
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
int pS = source.sourceStart, pE = source.sourceEnd;
long p = (long)pS << 32 | pE;
MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
setGeneratedBy(method, source);
method.modifiers = modifier;
method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE;
if (shouldReturnThis) {
EclipseNode type = fieldNode;
while (type != null && type.getKind() != Kind.TYPE) type = type.up();
if (type != null && type.get() instanceof TypeDeclaration) {
TypeDeclaration typeDecl = (TypeDeclaration) type.get();
if (typeDecl.typeParameters != null && typeDecl.typeParameters.length > 0) {
TypeReference[] refs = new TypeReference[typeDecl.typeParameters.length];
int idx = 0;
for (TypeParameter param : typeDecl.typeParameters) {
TypeReference typeRef = new SingleTypeReference(param.name, (long)param.sourceStart << 32 | param.sourceEnd);
setGeneratedBy(typeRef, source);
refs[idx++] = typeRef;
}
method.returnType = new ParameterizedSingleTypeReference(typeDecl.name, refs, 0, p);
} else method.returnType = new SingleTypeReference(((TypeDeclaration)type.get()).name, p);
}
}
if (method.returnType == null) {
method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE;
shouldReturnThis = false;
}
setGeneratedBy(method.returnType, source);
if (isFieldDeprecated(fieldNode)) {
method.annotations = new Annotation[] { generateDeprecatedAnnotation(source) };
@@ -211,13 +241,24 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> {
Annotation[] nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN);
Annotation[] nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN);
List<Statement> statements = new ArrayList<Statement>(5);
if (nonNulls.length == 0) {
method.statements = new Statement[] { assignment };
statements.add(assignment);
} else {
Statement nullCheck = generateNullCheck(field, source);
if (nullCheck != null) method.statements = new Statement[] { nullCheck, assignment };
else method.statements = new Statement[] { assignment };
if (nullCheck != null) statements.add(nullCheck);
statements.add(assignment);
}
if (shouldReturnThis) {
ThisReference thisRef = new ThisReference(pS, pE);
setGeneratedBy(thisRef, source);
ReturnStatement returnThis = new ReturnStatement(thisRef, pS, pE);
setGeneratedBy(returnThis, source);
statements.add(returnThis);
}
method.statements = statements.toArray(new Statement[0]);
Annotation[] copiedAnnotations = copyAnnotations(source, nonNulls, nullables);
if (copiedAnnotations.length != 0) param.annotations = copiedAnnotations;
return method;
@@ -50,12 +50,14 @@ import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCReturn;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
/**
@@ -195,6 +197,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
private JCMethodDecl createSetter(long access, JavacNode field, TreeMaker treeMaker, JCTree source) {
String setterName = toSetterName(field);
boolean returnThis = shouldReturnThis(field);
if (setterName == null) return null;
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
@@ -202,26 +205,53 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
JCExpression fieldRef = createFieldAccessor(treeMaker, field, FieldAccess.ALWAYS_FIELD);
JCAssign assign = treeMaker.Assign(fieldRef, treeMaker.Ident(fieldDecl.name));
List<JCStatement> statements;
ListBuffer<JCStatement> statements = ListBuffer.lb();
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 {
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);
Name methodName = field.toName(setterName);
List<JCAnnotation> annsOnParam = nonNulls.appendList(nullables);
JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, annsOnParam), fieldDecl.name, fieldDecl.vartype, null);
//WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6.
JCExpression methodType = treeMaker.Type(new JCNoType(getCtcInt(TypeTags.class, "VOID")));
if (nonNulls.isEmpty()) {
statements.append(treeMaker.Exec(assign));
} else {
JCStatement nullCheck = generateNullCheck(treeMaker, field);
if (nullCheck != null) statements.append(nullCheck);
statements.append(treeMaker.Exec(assign));
}
JCExpression methodType = null;
if (returnThis) {
JavacNode typeNode = field;
while (typeNode != null && typeNode.getKind() != Kind.TYPE) typeNode = typeNode.up();
if (typeNode != null && typeNode.get() instanceof JCClassDecl) {
JCClassDecl type = (JCClassDecl) typeNode.get();
ListBuffer<JCExpression> typeArgs = ListBuffer.lb();
if (!type.typarams.isEmpty()) {
for (JCTypeParameter tp : type.typarams) {
typeArgs.append(treeMaker.Ident(tp.name));
}
methodType = treeMaker.TypeApply(treeMaker.Ident(type.name), typeArgs.toList());
} else {
methodType = treeMaker.Ident(type.name);
}
}
}
if (methodType == null) {
//WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6.
methodType = treeMaker.Type(new JCNoType(getCtcInt(TypeTags.class, "VOID")));
returnThis = false;
}
if (returnThis) {
JCReturn returnStatement = treeMaker.Return(treeMaker.Ident(field.toName("this")));
statements.append(returnStatement);
}
JCBlock methodBody = treeMaker.Block(0, statements.toList());
List<JCTypeParameter> methodGenericParams = List.nil();
List<JCVariableDecl> parameters = List.of(param);
List<JCExpression> throwsClauses = List.nil();
@@ -341,6 +341,20 @@ public class JavacHandlerUtil {
return TransformationsUtil.toSetterName(accessors, fieldName, isBoolean);
}
/**
* When generating a setter, the setter either returns void (beanspec) or Self (fluent).
* This method scans for the {@code Accessors} annotation to figure that out.
*/
public static boolean shouldReturnThis(JavacNode field) {
if ((((JCVariableDecl) field.get()).mods.flags & Flags.STATIC) != 0) return false;
AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
boolean forced = (accessors.getActualExpression("chain") != null);
Accessors instance = accessors.getInstance();
return instance.chain() || (instance.fluent() && !forced);
}
private static boolean isBoolean(JavacNode field) {
JCExpression varType = ((JCVariableDecl) field.get()).vartype;
return varType != null && varType.toString().equals("boolean");
@@ -355,7 +369,7 @@ public class JavacHandlerUtil {
JavacNode current = field.up();
while (current != null) {
for (JavacNode node : field.down()) {
for (JavacNode node : current.down()) {
if (annotationTypeMatches(Accessors.class, node)) {
return createAnnotation(Accessors.class, node);
}
@@ -0,0 +1,118 @@
class AccessorsFluent {
@lombok.experimental.Accessors(fluent = true)
private String fieldName = "";
@java.lang.SuppressWarnings("all")
public String fieldName() {
return this.fieldName;
}
@java.lang.SuppressWarnings("all")
public AccessorsFluent fieldName(final String fieldName) {
this.fieldName = fieldName;
return this;
}
}
@lombok.experimental.Accessors(fluent = true)
class AccessorsFluentOnClass {
private String fieldName = "";
@lombok.experimental.Accessors
private String otherFieldWithOverride = "";
@java.lang.SuppressWarnings("all")
public String fieldName() {
return this.fieldName;
}
@java.lang.SuppressWarnings("all")
public String getOtherFieldWithOverride() {
return this.otherFieldWithOverride;
}
@java.lang.SuppressWarnings("all")
public AccessorsFluentOnClass fieldName(final String fieldName) {
this.fieldName = fieldName;
return this;
}
}
class AccessorsChain {
@lombok.experimental.Accessors(chain = true)
private boolean isRunning;
@java.lang.SuppressWarnings("all")
public AccessorsChain setRunning(final boolean isRunning) {
this.isRunning = isRunning;
return this;
}
}
@lombok.experimental.Accessors(prefix = "f")
class AccessorsPrefix {
private String fieldName;
private String fActualField;
@java.lang.SuppressWarnings("all")
public void setActualField(final String fActualField) {
this.fActualField = fActualField;
}
}
@lombok.experimental.Accessors(prefix = {"f", ""})
class AccessorsPrefix2 {
private String fieldName;
private String fActualField;
@java.lang.SuppressWarnings("all")
public void setFieldName(final String fieldName) {
this.fieldName = fieldName;
}
@java.lang.SuppressWarnings("all")
public void setActualField(final String fActualField) {
this.fActualField = fActualField;
}
}
@lombok.experimental.Accessors(prefix = "f")
class AccessorsPrefix3 {
private String fName;
private String getName() {
return fName;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "AccessorsPrefix3(fName=" + this.getName() + ")";
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (!(o instanceof AccessorsPrefix3)) return false;
final AccessorsPrefix3 other = (AccessorsPrefix3)o;
if (!other.canEqual((java.lang.Object)this)) return false;
if (this.getName() == null ? other.getName() != null : !this.getName().equals((java.lang.Object)other.getName())) return false;
return true;
}
@java.lang.SuppressWarnings("all")
public boolean canEqual(final java.lang.Object other) {
return other instanceof AccessorsPrefix3;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = result * PRIME + (this.getName() == null ? 0 : this.getName().hashCode());
return result;
}
}
@@ -0,0 +1,96 @@
class AccessorsFluent {
private @lombok.Getter @lombok.Setter @lombok.experimental.Accessors(fluent = true) String fieldName = "";
public @java.lang.SuppressWarnings("all") String fieldName() {
return this.fieldName;
}
public @java.lang.SuppressWarnings("all") AccessorsFluent fieldName(final String fieldName) {
this.fieldName = fieldName;
return this;
}
AccessorsFluent() {
super();
}
}
@lombok.experimental.Accessors(fluent = true) @lombok.Getter class AccessorsFluentOnClass {
private @lombok.Setter String fieldName = "";
private @lombok.experimental.Accessors String otherFieldWithOverride = "";
public @java.lang.SuppressWarnings("all") AccessorsFluentOnClass fieldName(final String fieldName) {
this.fieldName = fieldName;
return this;
}
public @java.lang.SuppressWarnings("all") String fieldName() {
return this.fieldName;
}
public @java.lang.SuppressWarnings("all") String getOtherFieldWithOverride() {
return this.otherFieldWithOverride;
}
AccessorsFluentOnClass() {
super();
}
}
class AccessorsChain {
private @lombok.Setter @lombok.experimental.Accessors(chain = true) boolean isRunning;
public @java.lang.SuppressWarnings("all") AccessorsChain setRunning(final boolean isRunning) {
this.isRunning = isRunning;
return this;
}
AccessorsChain() {
super();
}
}
@lombok.experimental.Accessors(prefix = "f") class AccessorsPrefix {
private @lombok.Setter String fieldName;
private @lombok.Setter String fActualField;
public @java.lang.SuppressWarnings("all") void setActualField(final String fActualField) {
this.fActualField = fActualField;
}
AccessorsPrefix() {
super();
}
}
@lombok.experimental.Accessors(prefix = {"f", ""}) class AccessorsPrefix2 {
private @lombok.Setter String fieldName;
private @lombok.Setter String fActualField;
public @java.lang.SuppressWarnings("all") void setFieldName(final String fieldName) {
this.fieldName = fieldName;
}
public @java.lang.SuppressWarnings("all") void setActualField(final String fActualField) {
this.fActualField = fActualField;
}
AccessorsPrefix2() {
super();
}
}
@lombok.experimental.Accessors(prefix = "f") @lombok.ToString @lombok.EqualsAndHashCode class AccessorsPrefix3 {
private String fName;
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
return (("AccessorsPrefix3(fName=" + this.getName()) + ")");
}
public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) {
if ((o == this))
return true;
if ((! (o instanceof AccessorsPrefix3)))
return false;
final @java.lang.SuppressWarnings("all") AccessorsPrefix3 other = (AccessorsPrefix3) o;
if ((! other.canEqual((java.lang.Object) this)))
return false;
if (((this.getName() == null) ? (other.getName() != null) : (! this.getName().equals((java.lang.Object) other.getName()))))
return false;
return true;
}
public @java.lang.SuppressWarnings("all") boolean canEqual(final java.lang.Object other) {
return (other instanceof AccessorsPrefix3);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 31;
int result = 1;
result = ((result * PRIME) + ((this.getName() == null) ? 0 : this.getName().hashCode()));
return result;
}
AccessorsPrefix3() {
super();
}
private String getName() {
return fName;
}
}
@@ -0,0 +1,38 @@
class AccessorsFluent {
@lombok.Getter @lombok.Setter @lombok.experimental.Accessors(fluent=true)
private String fieldName = "";
}
@lombok.experimental.Accessors(fluent=true)
@lombok.Getter
class AccessorsFluentOnClass {
@lombok.Setter private String fieldName = "";
@lombok.experimental.Accessors private String otherFieldWithOverride = "";
}
class AccessorsChain {
@lombok.Setter @lombok.experimental.Accessors(chain=true) private boolean isRunning;
}
@lombok.experimental.Accessors(prefix="f")
class AccessorsPrefix {
@lombok.Setter private String fieldName;
@lombok.Setter private String fActualField;
}
@lombok.experimental.Accessors(prefix={"f", ""})
class AccessorsPrefix2 {
@lombok.Setter private String fieldName;
@lombok.Setter private String fActualField;
}
@lombok.experimental.Accessors(prefix="f")
@lombok.ToString
@lombok.EqualsAndHashCode
class AccessorsPrefix3 {
private String fName;
private String getName() {
return fName;
}
}
@@ -0,0 +1 @@
19:9 WARNING Not generating setter for this field: It does not fit your @Accessors prefix list.
@@ -0,0 +1 @@
19 warning Not generating setter for this field: It does not fit your @Accessors prefix list.