Merge branch 'master' of github.com:rzwitserloot/lombok

This commit is contained in:
Reinier Zwitserloot
2010-11-15 20:05:04 +01:00
8 changed files with 134 additions and 128 deletions
+4 -3
View File
@@ -34,6 +34,7 @@ import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log;
public class JavacResolution {
@@ -378,9 +379,9 @@ public class JavacResolution {
}
if (generics != null && !generics.isEmpty()) {
List<JCExpression> args = List.nil();
for (Type t : generics) args = args.append(typeToJCTree(t, maker, ast, true));
replacement = maker.TypeApply(replacement, args);
ListBuffer<JCExpression> args = ListBuffer.lb();
for (Type t : generics) args.append(typeToJCTree(t, maker, ast, true));
replacement = maker.TypeApply(replacement, args.toList());
}
return replacement;
@@ -50,6 +50,7 @@ import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCTypeCast;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
/**
@@ -93,14 +94,14 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> {
}
boolean seenDeclaration = false;
List<JCStatement> tryBlock = List.nil();
List<JCStatement> newStatements = List.nil();
ListBuffer<JCStatement> newStatements = ListBuffer.lb();
ListBuffer<JCStatement> tryBlock = ListBuffer.lb();
for (JCStatement statement : statements) {
if (!seenDeclaration) {
if (statement == decl) seenDeclaration = true;
newStatements = newStatements.append(statement);
newStatements.append(statement);
} else {
tryBlock = tryBlock.append(statement);
tryBlock.append(statement);
}
}
@@ -108,8 +109,7 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> {
annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent.");
return true;
}
doAssignmentCheck(annotationNode, tryBlock, decl.name);
doAssignmentCheck(annotationNode, tryBlock.toList(), decl.name);
TreeMaker maker = annotationNode.getTreeMaker();
JCFieldAccess cleanupMethod = maker.Select(maker.Ident(decl.name), annotationNode.toName(cleanupName));
@@ -123,14 +123,14 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> {
JCBlock finalizer = maker.Block(0, List.<JCStatement>of(ifNotNullCleanup));
newStatements = newStatements.append(maker.Try(maker.Block(0, tryBlock), List.<JCCatch>nil(), finalizer));
newStatements.append(maker.Try(maker.Block(0, tryBlock.toList()), List.<JCCatch>nil(), finalizer));
if (blockNode instanceof JCBlock) {
((JCBlock)blockNode).stats = newStatements;
((JCBlock)blockNode).stats = newStatements.toList();
} else if (blockNode instanceof JCCase) {
((JCCase)blockNode).stats = newStatements;
((JCCase)blockNode).stats = newStatements.toList();
} else if (blockNode instanceof JCMethodDecl) {
((JCMethodDecl)blockNode).body.stats = newStatements;
((JCMethodDecl)blockNode).body.stats = newStatements.toList();
} else throw new AssertionError("Should not get here");
ancestor.rebuild();
@@ -53,6 +53,7 @@ import com.sun.tools.javac.tree.JCTree.JCTypeApply;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
public class HandleConstructor {
@ProviderFor(JavacAnnotationHandler.class)
@@ -97,7 +98,7 @@ public class HandleConstructor {
}
private static List<JavacNode> findRequiredFields(JavacNode typeNode) {
List<JavacNode> fields = List.nil();
ListBuffer<JavacNode> fields = ListBuffer.lb();
for (JavacNode child : typeNode.down()) {
if (child.getKind() != Kind.FIELD) continue;
JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
@@ -108,9 +109,9 @@ public class HandleConstructor {
if ((fieldFlags & Flags.STATIC) != 0) continue;
boolean isFinal = (fieldFlags & Flags.FINAL) != 0;
boolean isNonNull = !findAnnotations(child, TransformationsUtil.NON_NULL_PATTERN).isEmpty();
if ((isFinal || isNonNull) && fieldDecl.init == null) fields = fields.append(child);
if ((isFinal || isNonNull) && fieldDecl.init == null) fields.append(child);
}
return fields;
return fields.toList();
}
@ProviderFor(JavacAnnotationHandler.class)
@@ -125,7 +126,7 @@ public class HandleConstructor {
@SuppressWarnings("deprecation")
boolean suppressConstructorProperties = ann.suppressConstructorProperties();
if (level == AccessLevel.NONE) return true;
List<JavacNode> fields = List.nil();
ListBuffer<JavacNode> fields = ListBuffer.lb();
for (JavacNode child : typeNode.down()) {
if (child.getKind() != Kind.FIELD) continue;
JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
@@ -134,9 +135,9 @@ public class HandleConstructor {
long fieldFlags = fieldDecl.mods.flags;
//Skip static fields.
if ((fieldFlags & Flags.STATIC) != 0) continue;
fields = fields.append(child);
fields.append(child);
}
new HandleConstructor().generateConstructor(typeNode, level, fields, staticName, false, suppressConstructorProperties);
new HandleConstructor().generateConstructor(typeNode, level, fields.toList(), staticName, false, suppressConstructorProperties);
return true;
}
@@ -176,11 +177,11 @@ public class HandleConstructor {
if (fields.isEmpty()) return;
TreeMaker maker = node.getTreeMaker();
JCExpression constructorPropertiesType = chainDots(maker, node, "java", "beans", "ConstructorProperties");
List<JCExpression> fieldNames = List.nil();
ListBuffer<JCExpression> fieldNames = ListBuffer.lb();
for (JavacNode field : fields) {
fieldNames = fieldNames.append(maker.Literal(field.getName()));
fieldNames.append(maker.Literal(field.getName()));
}
JCExpression fieldNamesArray = maker.NewArray(null, List.<JCExpression>nil(), fieldNames);
JCExpression fieldNamesArray = maker.NewArray(null, List.<JCExpression>nil(), fieldNames.toList());
JCAnnotation annotation = maker.Annotation(constructorPropertiesType, List.of(fieldNamesArray));
mods.annotations = mods.annotations.append(annotation);
}
@@ -188,23 +189,23 @@ public class HandleConstructor {
private JCMethodDecl createConstructor(AccessLevel level, JavacNode typeNode, List<JavacNode> fields, boolean suppressConstructorProperties) {
TreeMaker maker = typeNode.getTreeMaker();
List<JCStatement> nullChecks = List.nil();
List<JCStatement> assigns = List.nil();
List<JCVariableDecl> params = List.nil();
ListBuffer<JCStatement> nullChecks = ListBuffer.lb();
ListBuffer<JCStatement> assigns = ListBuffer.lb();
ListBuffer<JCVariableDecl> params = ListBuffer.lb();
for (JavacNode fieldNode : fields) {
JCVariableDecl field = (JCVariableDecl) fieldNode.get();
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);
params.append(param);
JCFieldAccess thisX = maker.Select(maker.Ident(fieldNode.toName("this")), field.name);
JCAssign assign = maker.Assign(thisX, maker.Ident(field.name));
assigns = assigns.append(maker.Exec(assign));
assigns.append(maker.Exec(assign));
if (!nonNulls.isEmpty()) {
JCStatement nullCheck = generateNullCheck(maker, fieldNode);
if (nullCheck != null) nullChecks = nullChecks.append(nullCheck);
if (nullCheck != null) nullChecks.append(nullCheck);
}
}
@@ -214,7 +215,7 @@ public class HandleConstructor {
}
return maker.MethodDef(mods, typeNode.toName("<init>"),
null, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), maker.Block(0L, nullChecks.appendList(assigns)), null);
null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(), maker.Block(0L, nullChecks.appendList(assigns).toList()), null);
}
private boolean isLocalType(JavacNode type) {
@@ -232,20 +233,20 @@ public class HandleConstructor {
JCExpression returnType, constructorType;
List<JCTypeParameter> typeParams = List.nil();
List<JCVariableDecl> params = List.nil();
List<JCExpression> typeArgs1 = List.nil();
List<JCExpression> typeArgs2 = List.nil();
List<JCExpression> args = List.nil();
ListBuffer<JCTypeParameter> typeParams = ListBuffer.lb();
ListBuffer<JCVariableDecl> params = ListBuffer.lb();
ListBuffer<JCExpression> typeArgs1 = ListBuffer.lb();
ListBuffer<JCExpression> typeArgs2 = ListBuffer.lb();
ListBuffer<JCExpression> args = ListBuffer.lb();
if (!type.typarams.isEmpty()) {
for (JCTypeParameter param : type.typarams) {
typeArgs1 = typeArgs1.append(maker.Ident(param.name));
typeArgs2 = typeArgs2.append(maker.Ident(param.name));
typeParams = typeParams.append(maker.TypeParameter(param.name, param.bounds));
typeArgs1.append(maker.Ident(param.name));
typeArgs2.append(maker.Ident(param.name));
typeParams.append(maker.TypeParameter(param.name, param.bounds));
}
returnType = maker.TypeApply(maker.Ident(type.name), typeArgs1);
constructorType = maker.TypeApply(maker.Ident(type.name), typeArgs2);
returnType = maker.TypeApply(maker.Ident(type.name), typeArgs1.toList());
constructorType = maker.TypeApply(maker.Ident(type.name), typeArgs2.toList());
} else {
returnType = maker.Ident(type.name);
constructorType = maker.Ident(type.name);
@@ -257,21 +258,21 @@ public class HandleConstructor {
if (field.vartype instanceof JCIdent) pType = maker.Ident(((JCIdent)field.vartype).name);
else if (field.vartype instanceof JCTypeApply) {
JCTypeApply typeApply = (JCTypeApply) field.vartype;
List<JCExpression> tArgs = List.nil();
for (JCExpression arg : typeApply.arguments) tArgs = tArgs.append(arg);
pType = maker.TypeApply(typeApply.clazz, tArgs);
ListBuffer<JCExpression> tArgs = ListBuffer.lb();
for (JCExpression arg : typeApply.arguments) tArgs.append(arg);
pType = maker.TypeApply(typeApply.clazz, tArgs.toList());
} else {
pType = field.vartype;
}
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));
params.append(param);
args.append(maker.Ident(field.name));
}
JCReturn returnStatement = maker.Return(maker.NewClass(null, List.<JCExpression>nil(), constructorType, args, null));
JCReturn returnStatement = maker.Return(maker.NewClass(null, List.<JCExpression>nil(), constructorType, args.toList(), null));
JCBlock body = maker.Block(0, List.<JCStatement>of(returnStatement));
return maker.MethodDef(mods, typeNode.toName(name), returnType, typeParams, params, List.<JCExpression>nil(), body, null);
return maker.MethodDef(mods, typeNode.toName(name), returnType, typeParams.toList(), params.toList(), List.<JCExpression>nil(), body, null);
}
}
@@ -51,6 +51,7 @@ import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCUnary;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
/**
@@ -146,12 +147,12 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
errorNode.addWarning("Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type.");
}
List<JavacNode> nodesForEquality = List.nil();
ListBuffer<JavacNode> nodesForEquality = ListBuffer.lb();
if (includes != null) {
for (JavacNode child : typeNode.down()) {
if (child.getKind() != Kind.FIELD) continue;
JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
if (includes.contains(fieldDecl.name.toString())) nodesForEquality = nodesForEquality.append(child);
if (includes.contains(fieldDecl.name.toString())) nodesForEquality.append(child);
}
} else {
for (JavacNode child : typeNode.down()) {
@@ -165,7 +166,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
if (excludes != null && excludes.contains(fieldDecl.name.toString())) continue;
//Skip fields that start with $
if (fieldDecl.name.toString().startsWith("$")) continue;
nodesForEquality = nodesForEquality.append(child);
nodesForEquality.append(child);
}
}
@@ -175,7 +176,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
boolean isFinal = (((JCClassDecl)typeNode.get()).mods.flags & Flags.FINAL) != 0;
needsCanEqual = !isFinal || !isDirectDescendantOfObject;
JCMethodDecl method = createEquals(typeNode, nodesForEquality, callSuper, fieldAccess, needsCanEqual);
JCMethodDecl method = createEquals(typeNode, nodesForEquality.toList(), callSuper, fieldAccess, needsCanEqual);
injectMethod(typeNode, method);
break;
case EXISTS_BY_LOMBOK:
@@ -202,7 +203,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
}
switch (methodExists("hashCode", typeNode)) {
case NOT_EXISTS:
JCMethodDecl method = createHashCode(typeNode, nodesForEquality, callSuper, fieldAccess);
JCMethodDecl method = createHashCode(typeNode, nodesForEquality.toList(), callSuper, fieldAccess);
injectMethod(typeNode, method);
break;
case EXISTS_BY_LOMBOK:
@@ -223,28 +224,28 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
JCAnnotation overrideAnnotation = maker.Annotation(chainDots(maker, typeNode, "java", "lang", "Override"), List.<JCExpression>nil());
JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation));
JCExpression returnType = maker.TypeIdent(TypeTags.INT);
List<JCStatement> statements = List.nil();
ListBuffer<JCStatement> statements = ListBuffer.lb();
Name primeName = typeNode.toName("PRIME");
Name resultName = typeNode.toName("result");
/* final int PRIME = 31; */ {
if (!fields.isEmpty() || callSuper) {
statements = statements.append(maker.VarDef(maker.Modifiers(Flags.FINAL),
statements.append(maker.VarDef(maker.Modifiers(Flags.FINAL),
primeName, maker.TypeIdent(TypeTags.INT), maker.Literal(31)));
}
}
/* int result = 1; */ {
statements = statements.append(maker.VarDef(maker.Modifiers(0), resultName, maker.TypeIdent(TypeTags.INT), maker.Literal(1)));
statements.append(maker.VarDef(maker.Modifiers(0), resultName, maker.TypeIdent(TypeTags.INT), maker.Literal(1)));
}
List<JCExpression> intoResult = List.nil();
ListBuffer<JCExpression> intoResult = ListBuffer.lb();
if (callSuper) {
JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(),
maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("hashCode")),
List.<JCExpression>nil());
intoResult = intoResult.append(callToSuper);
intoResult.append(callToSuper);
}
int tempCounter = 0;
@@ -255,14 +256,14 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
switch (((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind()) {
case BOOLEAN:
/* this.fieldName ? 1231 : 1237 */
intoResult = intoResult.append(maker.Conditional(fieldAccessor, maker.Literal(1231), maker.Literal(1237)));
intoResult.append(maker.Conditional(fieldAccessor, maker.Literal(1231), maker.Literal(1237)));
break;
case LONG:
intoResult = intoResult.append(longToIntForHashCode(maker, fieldAccessor, createFieldAccessor(maker, fieldNode, fieldAccess)));
intoResult.append(longToIntForHashCode(maker, fieldAccessor, createFieldAccessor(maker, fieldNode, fieldAccess)));
break;
case FLOAT:
/* Float.floatToIntBits(this.fieldName) */
intoResult = intoResult.append(maker.Apply(
intoResult.append(maker.Apply(
List.<JCExpression>nil(),
chainDots(maker, typeNode, "java", "lang", "Float", "floatToIntBits"),
List.of(fieldAccessor)));
@@ -274,9 +275,9 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
List.<JCExpression>nil(),
chainDots(maker, typeNode, "java", "lang", "Double", "doubleToLongBits"),
List.of(fieldAccessor));
statements = statements.append(
statements.append(
maker.VarDef(maker.Modifiers(Flags.FINAL), tempVar, maker.TypeIdent(TypeTags.LONG), init));
intoResult = intoResult.append(longToIntForHashCode(maker, maker.Ident(tempVar), maker.Ident(tempVar)));
intoResult.append(longToIntForHashCode(maker, maker.Ident(tempVar), maker.Ident(tempVar)));
break;
default:
case BYTE:
@@ -284,7 +285,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
case INT:
case CHAR:
/* just the field */
intoResult = intoResult.append(fieldAccessor);
intoResult.append(fieldAccessor);
break;
}
} else if (fType instanceof JCArrayTypeTree) {
@@ -294,14 +295,14 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
boolean useDeepHC = multiDim || !primitiveArray;
JCExpression hcMethod = chainDots(maker, typeNode, "java", "util", "Arrays", useDeepHC ? "deepHashCode" : "hashCode");
intoResult = intoResult.append(
intoResult.append(
maker.Apply(List.<JCExpression>nil(), hcMethod, List.of(fieldAccessor)));
} else /* objects */ {
/* this.fieldName == null ? 0 : this.fieldName.hashCode() */
JCExpression hcCall = maker.Apply(List.<JCExpression>nil(), maker.Select(createFieldAccessor(maker, fieldNode, fieldAccess), typeNode.toName("hashCode")),
List.<JCExpression>nil());
JCExpression thisEqualsNull = maker.Binary(JCTree.EQ, fieldAccessor, maker.Literal(TypeTags.BOT, null));
intoResult = intoResult.append(
intoResult.append(
maker.Conditional(thisEqualsNull, maker.Literal(0), hcCall));
}
}
@@ -311,14 +312,14 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
for (JCExpression expr : intoResult) {
JCExpression mult = maker.Binary(JCTree.MUL, maker.Ident(resultName), maker.Ident(primeName));
JCExpression add = maker.Binary(JCTree.PLUS, mult, expr);
statements = statements.append(maker.Exec(maker.Assign(maker.Ident(resultName), add)));
statements.append(maker.Exec(maker.Assign(maker.Ident(resultName), add)));
}
/* return result; */ {
statements = statements.append(maker.Return(maker.Ident(resultName)));
statements.append(maker.Return(maker.Ident(resultName)));
}
JCBlock body = maker.Block(0, statements);
JCBlock body = maker.Block(0, statements.toList());
return maker.MethodDef(mods, typeNode.toName("hashCode"), returnType,
List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
}
@@ -344,38 +345,38 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
JCExpression objectType = chainDots(maker, typeNode, "java", "lang", "Object");
JCExpression returnType = maker.TypeIdent(TypeTags.BOOLEAN);
List<JCStatement> statements = List.nil();
List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(Flags.FINAL), oName, objectType, null));
ListBuffer<JCStatement> statements = ListBuffer.lb();
final List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(Flags.FINAL), oName, objectType, null));
/* if (o == this) return true; */ {
statements = statements.append(maker.If(maker.Binary(JCTree.EQ, maker.Ident(oName),
statements.append(maker.If(maker.Binary(JCTree.EQ, maker.Ident(oName),
maker.Ident(thisName)), returnBool(maker, true), null));
}
/* if (!(o instanceof MyType) return false; */ {
JCUnary notInstanceOf = maker.Unary(JCTree.NOT, maker.TypeTest(maker.Ident(oName), maker.Ident(type.name)));
statements = statements.append(maker.If(notInstanceOf, returnBool(maker, false), null));
statements.append(maker.If(notInstanceOf, returnBool(maker, false), null));
}
/* MyType<?> other = (MyType<?>) o; */ {
if (!fields.isEmpty() || needsCanEqual) {
final JCExpression selfType1, selfType2;
List<JCExpression> wildcards1 = List.nil();
List<JCExpression> wildcards2 = List.nil();
ListBuffer<JCExpression> wildcards1 = ListBuffer.lb();
ListBuffer<JCExpression> wildcards2 = ListBuffer.lb();
for (int i = 0 ; i < type.typarams.length() ; i++) {
wildcards1 = wildcards1.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
wildcards2 = wildcards2.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
wildcards1.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
wildcards2.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
}
if (type.typarams.isEmpty()) {
selfType1 = maker.Ident(type.name);
selfType2 = maker.Ident(type.name);
} else {
selfType1 = maker.TypeApply(maker.Ident(type.name), wildcards1);
selfType2 = maker.TypeApply(maker.Ident(type.name), wildcards2);
selfType1 = maker.TypeApply(maker.Ident(type.name), wildcards1.toList());
selfType2 = maker.TypeApply(maker.Ident(type.name), wildcards2.toList());
}
statements = statements.append(
statements.append(
maker.VarDef(maker.Modifiers(Flags.FINAL), otherName, selfType1, maker.TypeCast(selfType2, maker.Ident(oName))));
}
}
@@ -386,7 +387,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
JCExpression equalityCheck = maker.Apply(exprNil,
maker.Select(maker.Ident(otherName), typeNode.toName("canEqual")),
List.<JCExpression>of(maker.Ident(thisName)));
statements = statements.append(maker.If(maker.Unary(JCTree.NOT, equalityCheck), returnBool(maker, false), null));
statements.append(maker.If(maker.Unary(JCTree.NOT, equalityCheck), returnBool(maker, false), null));
}
}
@@ -396,7 +397,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("equals")),
List.<JCExpression>of(maker.Ident(oName)));
JCUnary superNotEqual = maker.Unary(JCTree.NOT, callToSuper);
statements = statements.append(maker.If(superNotEqual, returnBool(maker, false), null));
statements.append(maker.If(superNotEqual, returnBool(maker, false), null));
}
for (JavacNode fieldNode : fields) {
@@ -407,15 +408,15 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
switch (((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind()) {
case FLOAT:
/* if (Float.compare(this.fieldName, other.fieldName) != 0) return false; */
statements = statements.append(generateCompareFloatOrDouble(thisFieldAccessor, otherFieldAccessor, maker, typeNode, false));
statements.append(generateCompareFloatOrDouble(thisFieldAccessor, otherFieldAccessor, maker, typeNode, false));
break;
case DOUBLE:
/* if (Double(this.fieldName, other.fieldName) != 0) return false; */
statements = statements.append(generateCompareFloatOrDouble(thisFieldAccessor, otherFieldAccessor, maker, typeNode, true));
statements.append(generateCompareFloatOrDouble(thisFieldAccessor, otherFieldAccessor, maker, typeNode, true));
break;
default:
/* if (this.fieldName != other.fieldName) return false; */
statements = statements.append(
statements.append(
maker.If(maker.Binary(JCTree.NE, thisFieldAccessor, otherFieldAccessor), returnBool(maker, false), null));
break;
}
@@ -427,7 +428,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
JCExpression eqMethod = chainDots(maker, typeNode, "java", "util", "Arrays", useDeepEquals ? "deepEquals" : "equals");
List<JCExpression> args = List.of(thisFieldAccessor, otherFieldAccessor);
statements = statements.append(maker.If(maker.Unary(JCTree.NOT,
statements.append(maker.If(maker.Unary(JCTree.NOT,
maker.Apply(List.<JCExpression>nil(), eqMethod, args)), returnBool(maker, false), null));
} else /* objects */ {
/* if (this.fieldName == null ? other.fieldName != null : !this.fieldName.equals(other.fieldName)) return false; */
@@ -437,15 +438,15 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
maker.Select(createFieldAccessor(maker, fieldNode, fieldAccess), typeNode.toName("equals")),
List.of(createFieldAccessor(maker, fieldNode, fieldAccess, maker.Ident(otherName))));
JCExpression fieldsAreNotEqual = maker.Conditional(thisEqualsNull, otherNotEqualsNull, maker.Unary(JCTree.NOT, thisEqualsThat));
statements = statements.append(maker.If(fieldsAreNotEqual, returnBool(maker, false), null));
statements.append(maker.If(fieldsAreNotEqual, returnBool(maker, false), null));
}
}
/* return true; */ {
statements = statements.append(returnBool(maker, true));
statements.append(returnBool(maker, true));
}
JCBlock body = maker.Block(0, statements);
JCBlock body = maker.Block(0, statements.toList());
return maker.MethodDef(mods, typeNode.toName("equals"), returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null);
}
@@ -61,6 +61,7 @@ import com.sun.tools.javac.tree.JCTree.JCTypeApply;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
@@ -287,7 +288,7 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
return value.get();
*/
List<JCStatement> statements = List.nil();
ListBuffer<JCStatement> statements = ListBuffer.lb();
JCVariableDecl field = (JCVariableDecl) fieldNode.get();
field.type = null;
@@ -302,46 +303,46 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
/* java.util.concurrent.atomic.AtomicReference<ValueType> value = this.fieldName.get();*/ {
JCTypeApply valueVarType = maker.TypeApply(chainDotsString(maker, fieldNode, AR), List.of(copyType(maker, field)));
statements = statements.append(maker.VarDef(maker.Modifiers(0), valueName, valueVarType, callGet(fieldNode, createFieldAccessor(maker, fieldNode, FieldAccess.ALWAYS_FIELD))));
statements.append(maker.VarDef(maker.Modifiers(0), valueName, valueVarType, callGet(fieldNode, createFieldAccessor(maker, fieldNode, FieldAccess.ALWAYS_FIELD))));
}
/* if (value == null) { */ {
JCSynchronized synchronizedStatement;
/* synchronized (this.fieldName) { */ {
List<JCStatement> synchronizedStatements = List.nil();
ListBuffer<JCStatement> synchronizedStatements = ListBuffer.lb();
/* value = this.fieldName.get(); */ {
JCExpressionStatement newAssign = maker.Exec(maker.Assign(maker.Ident(valueName), callGet(fieldNode, createFieldAccessor(maker, fieldNode, FieldAccess.ALWAYS_FIELD))));
synchronizedStatements = synchronizedStatements.append(newAssign);
synchronizedStatements.append(newAssign);
}
/* if (value == null) { */ {
List<JCStatement> innerIfStatements = List.nil();
ListBuffer<JCStatement> innerIfStatements = ListBuffer.lb();
/* value = new java.util.concurrent.atomic.AtomicReference<ValueType>(new ValueType());*/ {
JCTypeApply valueVarType = maker.TypeApply(chainDotsString(maker, fieldNode, AR), List.of(copyType(maker, field)));
JCNewClass newInstance = maker.NewClass(null, NIL_EXPRESSION, valueVarType, List.<JCExpression>of(field.init), null);
JCStatement statement = maker.Exec(maker.Assign(maker.Ident(valueName), newInstance));
innerIfStatements = innerIfStatements.append(statement);
innerIfStatements.append(statement);
}
/* this.fieldName.set(value); */ {
JCStatement statement = callSet(fieldNode, createFieldAccessor(maker, fieldNode, FieldAccess.ALWAYS_FIELD), maker.Ident(valueName));
innerIfStatements = innerIfStatements.append(statement);
innerIfStatements.append(statement);
}
JCBinary isNull = maker.Binary(JCTree.EQ, maker.Ident(valueName), maker.Literal(TypeTags.BOT, null));
JCIf ifStatement = maker.If(isNull, maker.Block(0, innerIfStatements), null);
synchronizedStatements = synchronizedStatements.append(ifStatement);
JCIf ifStatement = maker.If(isNull, maker.Block(0, innerIfStatements.toList()), null);
synchronizedStatements.append(ifStatement);
}
synchronizedStatement = maker.Synchronized(createFieldAccessor(maker, fieldNode, FieldAccess.ALWAYS_FIELD), maker.Block(0, synchronizedStatements));
synchronizedStatement = maker.Synchronized(createFieldAccessor(maker, fieldNode, FieldAccess.ALWAYS_FIELD), maker.Block(0, synchronizedStatements.toList()));
}
JCBinary isNull = maker.Binary(JCTree.EQ, maker.Ident(valueName), maker.Literal(TypeTags.BOT, null));
JCIf ifStatement = maker.If(isNull, maker.Block(0, List.<JCStatement>of(synchronizedStatement)), null);
statements = statements.append(ifStatement);
statements.append(ifStatement);
}
/* return value.get(); */
statements = statements.append(maker.Return(callGet(fieldNode, maker.Ident(valueName))));
statements.append(maker.Return(callGet(fieldNode, maker.Ident(valueName))));
// update the field type and init last
@@ -350,7 +351,7 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
field.init = maker.NewClass(null, NIL_EXPRESSION, copyType(maker, field), NIL_EXPRESSION, null);
}
return statements;
return statements.toList();
}
private JCMethodInvocation callGet(JavacNode source, JCExpression receiver) {
@@ -49,6 +49,7 @@ 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.util.List;
import com.sun.tools.javac.util.ListBuffer;
/**
* Handles the {@code ToString} annotation for javac.
@@ -130,12 +131,12 @@ public class HandleToString implements JavacAnnotationHandler<ToString> {
return false;
}
List<JavacNode> nodesForToString = List.nil();
ListBuffer<JavacNode> nodesForToString = ListBuffer.lb();
if (includes != null) {
for (JavacNode child : typeNode.down()) {
if (child.getKind() != Kind.FIELD) continue;
JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
if (includes.contains(fieldDecl.name.toString())) nodesForToString = nodesForToString.append(child);
if (includes.contains(fieldDecl.name.toString())) nodesForToString.append(child);
}
} else {
for (JavacNode child : typeNode.down()) {
@@ -147,13 +148,13 @@ public class HandleToString implements JavacAnnotationHandler<ToString> {
if (excludes != null && excludes.contains(fieldDecl.name.toString())) continue;
//Skip fields that start with $.
if (fieldDecl.name.toString().startsWith("$")) continue;
nodesForToString = nodesForToString.append(child);
nodesForToString.append(child);
}
}
switch (methodExists("toString", typeNode)) {
case NOT_EXISTS:
JCMethodDecl method = createToString(typeNode, nodesForToString, includeFieldNames, callSuper, fieldAccess);
JCMethodDecl method = createToString(typeNode, nodesForToString.toList(), includeFieldNames, callSuper, fieldAccess);
injectMethod(typeNode, method);
return true;
case EXISTS_BY_LOMBOK:
@@ -54,6 +54,7 @@ import com.sun.tools.javac.tree.JCTree.JCNewArray;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
/**
@@ -109,7 +110,7 @@ public class JavacHandlerUtil {
public static void deleteImportFromCompilationUnit(JavacNode node, String name) {
if (!node.shouldDeleteLombokAnnotations()) return;
List<JCTree> newDefs = List.nil();
ListBuffer<JCTree> newDefs = ListBuffer.lb();
JCCompilationUnit unit = (JCCompilationUnit) node.top().get();
@@ -119,17 +120,17 @@ public class JavacHandlerUtil {
JCImport imp0rt = (JCImport)def;
delete = (!imp0rt.staticImport && imp0rt.qualid.toString().equals(name));
}
if (!delete) newDefs = newDefs.append(def);
if (!delete) newDefs.append(def);
}
unit.defs = newDefs;
unit.defs = newDefs.toList();
}
private static List<JCAnnotation> filterList(List<JCAnnotation> annotations, JCTree jcTree) {
List<JCAnnotation> newAnnotations = List.nil();
ListBuffer<JCAnnotation> newAnnotations = ListBuffer.lb();
for (JCAnnotation ann : annotations) {
if (jcTree != ann) newAnnotations = newAnnotations.append(ann);
if (jcTree != ann) newAnnotations.append(ann);
}
return newAnnotations;
return newAnnotations.toList();
}
/**
@@ -486,12 +487,12 @@ public class JavacHandlerUtil {
}
private static List<JCTree> addAllButOne(List<JCTree> defs, int idx) {
List<JCTree> out = List.nil();
ListBuffer<JCTree> out = ListBuffer.lb();
int i = 0;
for (JCTree def : defs) {
if (i++ != idx) out = out.append(def);
if (i++ != idx) out.append(def);
}
return out;
return out.toList();
}
/**
@@ -537,7 +538,7 @@ public class JavacHandlerUtil {
* Only the simple name is checked - the package and any containing class are ignored.
*/
public static List<JCAnnotation> findAnnotations(JavacNode fieldNode, Pattern namePattern) {
List<JCAnnotation> result = List.nil();
ListBuffer<JCAnnotation> result = ListBuffer.lb();
for (JavacNode child : fieldNode.down()) {
if (child.getKind() == Kind.ANNOTATION) {
JCAnnotation annotation = (JCAnnotation) child.get();
@@ -545,11 +546,11 @@ public class JavacHandlerUtil {
int idx = name.lastIndexOf(".");
String suspect = idx == -1 ? name : name.substring(idx + 1);
if (namePattern.matcher(suspect).matches()) {
result = result.append(annotation);
result.append(annotation);
}
}
}
return result;
return result.toList();
}
/**
@@ -586,16 +587,16 @@ public class JavacHandlerUtil {
if (idx > -1) matched[idx] = true;
}
List<Integer> problematic = List.nil();
ListBuffer<Integer> problematic = ListBuffer.lb();
for (int i = 0 ; i < list.size() ; i++) {
if (!matched[i]) problematic = problematic.append(i);
if (!matched[i]) problematic.append(i);
}
return problematic;
return problematic.toList();
}
static List<JCExpression> getAndRemoveAnnotationParameter(JCAnnotation ast, String parameterName) {
List<JCExpression> params = List.nil();
ListBuffer<JCExpression> params = ListBuffer.lb();
List<JCExpression> result = List.nil();
for (JCExpression param : ast.args) {
@@ -614,18 +615,18 @@ public class JavacHandlerUtil {
}
}
params = params.append(param);
params.append(param);
}
ast.args = params;
ast.args = params.toList();
return result;
}
static List<JCAnnotation> copyAnnotations(List<JCExpression> in) {
List<JCAnnotation> out = List.nil();
ListBuffer<JCAnnotation> out = ListBuffer.lb();
for (JCExpression expr : in) {
if (!(expr instanceof JCAnnotation)) continue;
out = out.append((JCAnnotation) expr.clone());
out.append((JCAnnotation) expr.clone());
}
return out;
return out.toList();
}
}
+3 -3
View File
@@ -389,7 +389,7 @@ public class Delombok {
JavaCompiler delegate = compiler.processAnnotations(compiler.enterTrees(toJavacList(roots)));
for (JCCompilationUnit unit : roots) {
DelombokResult result = new DelombokResult(commentsMap.get(unit).comments, unit, force || tca.changed.contains(unit));
DelombokResult result = new DelombokResult(commentsMap.get(unit).comments.toList(), unit, force || tca.changed.contains(unit));
if (verbose) feedback.printf("File: %s [%s]\n", unit.sourcefile.getName(), result.isChanged() ? "delomboked" : "unchanged");
Writer rawWriter;
if (presetWriter != null) rawWriter = presetWriter;
@@ -408,10 +408,10 @@ public class Delombok {
}
public static class Comments {
public com.sun.tools.javac.util.List<Comment> comments = com.sun.tools.javac.util.List.nil();
public com.sun.tools.javac.util.ListBuffer<Comment> comments = com.sun.tools.javac.util.ListBuffer.lb();
void add(Comment comment) {
comments = comments.append(comment);
comments.append(comment);
}
}