diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2013-12-11 22:00:12 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2013-12-11 22:40:48 +0100 |
commit | a19b99cba2fdee4495bafce558a28af49e8e36f0 (patch) | |
tree | 73f77fdae6f5e1ba3407157fec1273f51e5532b6 /src/core/lombok | |
parent | 9ac86c8a234f2ecd280f27f0dbda15440b1bf155 (diff) | |
download | lombok-a19b99cba2fdee4495bafce558a28af49e8e36f0.tar.gz lombok-a19b99cba2fdee4495bafce558a28af49e8e36f0.tar.bz2 lombok-a19b99cba2fdee4495bafce558a28af49e8e36f0.zip |
Added complete support for delombok's 'pretty' format options; covering all of the issue #608.
Diffstat (limited to 'src/core/lombok')
10 files changed, 76 insertions, 48 deletions
diff --git a/src/core/lombok/javac/handlers/HandleCleanup.java b/src/core/lombok/javac/handlers/HandleCleanup.java index 39fedd28..7aae6303 100644 --- a/src/core/lombok/javac/handlers/HandleCleanup.java +++ b/src/core/lombok/javac/handlers/HandleCleanup.java @@ -26,6 +26,7 @@ import static lombok.javac.Javac.*; import lombok.Cleanup; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; +import lombok.delombok.LombokOptionsFactory; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; import lombok.javac.JavacTreeMaker; @@ -119,7 +120,7 @@ public class HandleCleanup extends JavacAnnotationHandler<Cleanup> { List<JCStatement> cleanupCall = List.<JCStatement>of(maker.Exec( maker.Apply(List.<JCExpression>nil(), cleanupMethod, List.<JCExpression>nil()))); - JCMethodInvocation preventNullAnalysis = preventNullAnalysis(maker, annotationNode, maker.Ident(decl.name)); + JCExpression preventNullAnalysis = preventNullAnalysis(maker, annotationNode, maker.Ident(decl.name)); JCBinary isNull = maker.Binary(CTC_NOT_EQUAL, preventNullAnalysis, maker.Literal(CTC_BOT, null)); JCIf ifNotNullCleanup = maker.If(isNull, maker.Block(0, cleanupCall), null); @@ -140,10 +141,14 @@ public class HandleCleanup extends JavacAnnotationHandler<Cleanup> { ancestor.rebuild(); } - private JCMethodInvocation preventNullAnalysis(JavacTreeMaker maker, JavacNode node, JCExpression expression) { - JCMethodInvocation singletonList = maker.Apply(List.<JCExpression>nil(), chainDotsString(node, "java.util.Collections.singletonList"), List.of(expression)); - JCMethodInvocation cleanedExpr = maker.Apply(List.<JCExpression>nil(), maker.Select(singletonList, node.toName("get")) , List.<JCExpression>of(maker.Literal(CTC_INT, 0))); - return cleanedExpr; + private JCExpression preventNullAnalysis(JavacTreeMaker maker, JavacNode node, JCExpression expression) { + if (LombokOptionsFactory.getDelombokOptions(node.getContext()).getFormatPreferences().danceAroundIdeChecks()) { + JCMethodInvocation singletonList = maker.Apply(List.<JCExpression>nil(), chainDotsString(node, "java.util.Collections.singletonList"), List.of(expression)); + JCMethodInvocation cleanedExpr = maker.Apply(List.<JCExpression>nil(), maker.Select(singletonList, node.toName("get")) , List.<JCExpression>of(maker.Literal(CTC_INT, 0))); + return cleanedExpr; + } else { + return expression; + } } private void doAssignmentCheck(JavacNode node, List<JCStatement> statements, Name name) { diff --git a/src/core/lombok/javac/handlers/HandleConstructor.java b/src/core/lombok/javac/handlers/HandleConstructor.java index 0caba524..adde3093 100644 --- a/src/core/lombok/javac/handlers/HandleConstructor.java +++ b/src/core/lombok/javac/handlers/HandleConstructor.java @@ -29,6 +29,7 @@ import lombok.RequiredArgsConstructor; import lombok.core.AnnotationValues; import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; +import lombok.delombok.LombokOptionsFactory; import lombok.experimental.Builder; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; @@ -249,7 +250,7 @@ public class HandleConstructor { } JCModifiers mods = maker.Modifiers(toJavacModifier(level), List.<JCAnnotation>nil()); - if (!suppressConstructorProperties && level != AccessLevel.PRIVATE && !isLocalType(typeNode)) { + if (!suppressConstructorProperties && level != AccessLevel.PRIVATE && !isLocalType(typeNode) && LombokOptionsFactory.getDelombokOptions(typeNode.getContext()).getFormatPreferences().generateConstructorProperties()) { addConstructorProperties(mods, typeNode, fields); } if (onConstructor != null) mods.annotations = mods.annotations.appendList(copyAnnotations(onConstructor)); diff --git a/src/core/lombok/javac/handlers/HandleDelegate.java b/src/core/lombok/javac/handlers/HandleDelegate.java index e306460f..89ab97e4 100644 --- a/src/core/lombok/javac/handlers/HandleDelegate.java +++ b/src/core/lombok/javac/handlers/HandleDelegate.java @@ -271,7 +271,7 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> { com.sun.tools.javac.util.List<JCAnnotation> annotations; if (sig.isDeprecated) { annotations = com.sun.tools.javac.util.List.of(maker.Annotation( - chainDots(annotation, "java", "lang", "Deprecated"), + genJavaLangTypeRef(annotation, "Deprecated"), com.sun.tools.javac.util.List.<JCExpression>nil())); } else { annotations = com.sun.tools.javac.util.List.nil(); diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index 64aff6d6..f3641f7f 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -221,7 +221,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas private JCMethodDecl createHashCode(JavacNode typeNode, List<JavacNode> fields, boolean callSuper, FieldAccess fieldAccess, JCTree source) { JavacTreeMaker maker = typeNode.getTreeMaker(); - JCAnnotation overrideAnnotation = maker.Annotation(chainDots(typeNode, "java", "lang", "Override"), List.<JCExpression>nil()); + JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(typeNode, "Override"), List.<JCExpression>nil()); JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation)); JCExpression returnType = maker.TypeIdent(CTC_INT); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); @@ -267,7 +267,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas /* Float.floatToIntBits(this.fieldName) */ statements.append(createResultCalculation(typeNode, maker.Apply( List.<JCExpression>nil(), - chainDots(typeNode, "java", "lang", "Float", "floatToIntBits"), + genJavaLangTypeRef(typeNode, "Float", "floatToIntBits"), List.of(fieldAccessor)))); break; case DOUBLE: { @@ -275,7 +275,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas Name dollarFieldName = dollar.append(((JCVariableDecl)fieldNode.get()).name); JCExpression init = maker.Apply( List.<JCExpression>nil(), - chainDots(typeNode, "java", "lang", "Double", "doubleToLongBits"), + genJavaLangTypeRef(typeNode, "Double", "doubleToLongBits"), List.of(fieldAccessor)); statements.append(maker.VarDef(maker.Modifiers(finalFlag), dollarFieldName, maker.TypeIdent(CTC_LONG), init)); statements.append(createResultCalculation(typeNode, longToIntForHashCode(maker, maker.Ident(dollarFieldName), maker.Ident(dollarFieldName)))); @@ -303,7 +303,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas /* $fieldName == null ? 0 : $fieldName.hashCode() */ Name dollarFieldName = dollar.append(((JCVariableDecl)fieldNode.get()).name); - statements.append(maker.VarDef(maker.Modifiers(finalFlag), dollarFieldName, chainDots(typeNode, "java", "lang", "Object"), fieldAccessor)); + statements.append(maker.VarDef(maker.Modifiers(finalFlag), dollarFieldName, genJavaLangTypeRef(typeNode, "Object"), fieldAccessor)); JCExpression hcCall = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(dollarFieldName), typeNode.toName("hashCode")), List.<JCExpression>nil()); @@ -366,9 +366,9 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas Name otherName = typeNode.toName("other"); Name thisName = typeNode.toName("this"); - JCAnnotation overrideAnnotation = maker.Annotation(chainDots(typeNode, "java", "lang", "Override"), List.<JCExpression>nil()); + JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(typeNode, "Override"), List.<JCExpression>nil()); JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation)); - JCExpression objectType = chainDots(typeNode, "java", "lang", "Object"); + JCExpression objectType = genJavaLangTypeRef(typeNode, "Object"); JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN); long finalFlag = JavacHandlerUtil.addFinalIfNeeded(0L, typeNode.getContext()); @@ -414,7 +414,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas if (needsCanEqual) { List<JCExpression> exprNil = List.nil(); JCExpression thisRef = maker.Ident(thisName); - JCExpression castThisRef = maker.TypeCast(chainDots(typeNode, "java", "lang", "Object"), thisRef); + JCExpression castThisRef = maker.TypeCast(genJavaLangTypeRef(typeNode, "Object"), thisRef); JCExpression equalityCheck = maker.Apply(exprNil, maker.Select(maker.Ident(otherName), typeNode.toName("canEqual")), List.of(castThisRef)); @@ -471,8 +471,8 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas Name thisDollarFieldName = thisDollar.append(fieldName); Name otherDollarFieldName = otherDollar.append(fieldName); - statements.append(maker.VarDef(maker.Modifiers(finalFlag), thisDollarFieldName, chainDots(typeNode, "java", "lang", "Object"), thisFieldAccessor)); - statements.append(maker.VarDef(maker.Modifiers(finalFlag), otherDollarFieldName, chainDots(typeNode, "java", "lang", "Object"), otherFieldAccessor)); + statements.append(maker.VarDef(maker.Modifiers(finalFlag), thisDollarFieldName, genJavaLangTypeRef(typeNode, "Object"), thisFieldAccessor)); + statements.append(maker.VarDef(maker.Modifiers(finalFlag), otherDollarFieldName, genJavaLangTypeRef(typeNode, "Object"), otherFieldAccessor)); JCExpression thisEqualsNull = maker.Binary(CTC_EQUAL, maker.Ident(thisDollarFieldName), maker.Literal(CTC_BOT, null)); JCExpression otherNotEqualsNull = maker.Binary(CTC_NOT_EQUAL, maker.Ident(otherDollarFieldName), maker.Literal(CTC_BOT, null)); @@ -502,7 +502,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.<JCAnnotation>nil()); JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN); Name canEqualName = typeNode.toName("canEqual"); - JCExpression objectType = chainDots(typeNode, "java", "lang", "Object"); + JCExpression objectType = genJavaLangTypeRef(typeNode, "Object"); Name otherName = typeNode.toName("other"); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(flags), otherName, objectType, null)); @@ -516,7 +516,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas private JCStatement generateCompareFloatOrDouble(JCExpression thisDotField, JCExpression otherDotField, JavacTreeMaker maker, JavacNode node, boolean isDouble) { /* if (Float.compare(fieldName, other.fieldName) != 0) return false; */ - JCExpression clazz = chainDots(node, "java", "lang", isDouble ? "Double" : "Float"); + JCExpression clazz = genJavaLangTypeRef(node, isDouble ? "Double" : "Float"); List<JCExpression> args = List.of(thisDotField, otherDotField); JCBinary compareCallEquals0 = maker.Binary(CTC_NOT_EQUAL, maker.Apply( List.<JCExpression>nil(), maker.Select(clazz, node.toName("compare")), args), maker.Literal(0)); diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 5f7e808c..ac0336ad 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -246,7 +246,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod).appendList(nonNulls).appendList(nullables); if (isFieldDeprecated(field)) { - annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.<JCExpression>nil())); + annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil())); } JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, @@ -287,20 +287,19 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { } private static final String AR = "java.util.concurrent.atomic.AtomicReference"; - private static final String JLO = "java.lang.Object"; private static final List<JCExpression> NIL_EXPRESSION = List.nil(); private static final java.util.Map<TypeTag, String> TYPE_MAP; static { Map<TypeTag, String> m = new HashMap<TypeTag, String>(); - m.put(CTC_INT, "java.lang.Integer"); - m.put(CTC_DOUBLE, "java.lang.Double"); - m.put(CTC_FLOAT, "java.lang.Float"); - m.put(CTC_SHORT, "java.lang.Short"); - m.put(CTC_BYTE, "java.lang.Byte"); - m.put(CTC_LONG, "java.lang.Long"); - m.put(CTC_BOOLEAN, "java.lang.Boolean"); - m.put(CTC_CHAR, "java.lang.Character"); + m.put(CTC_INT, "Integer"); + m.put(CTC_DOUBLE, "Double"); + m.put(CTC_FLOAT, "Float"); + m.put(CTC_SHORT, "Short"); + m.put(CTC_BYTE, "Byte"); + m.put(CTC_LONG, "Long"); + m.put(CTC_BOOLEAN, "Boolean"); + m.put(CTC_CHAR, "Character"); TYPE_MAP = Collections.unmodifiableMap(m); } @@ -339,8 +338,8 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { String boxed = TYPE_MAP.get(typeTag(field.vartype)); if (boxed != null) { isPrimitive = true; - field.vartype = chainDotsString(fieldNode, boxed); - copyOfBoxedFieldType = chainDotsString(fieldNode, boxed); + field.vartype = genJavaLangTypeRef(fieldNode, boxed); + copyOfBoxedFieldType = genJavaLangTypeRef(fieldNode, boxed); } } if (copyOfBoxedFieldType == null) copyOfBoxedFieldType = copyType(maker, field); @@ -349,7 +348,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { Name actualValueName = fieldNode.toName("actualValue"); /* java.lang.Object value = this.fieldName.get();*/ { - JCExpression valueVarType = chainDotsString(fieldNode, JLO); + JCExpression valueVarType = genJavaLangTypeRef(fieldNode, "Object"); statements.append(maker.VarDef(maker.Modifiers(0), valueName, valueVarType, callGet(fieldNode, createFieldAccessor(maker, fieldNode, FieldAccess.ALWAYS_FIELD)))); } @@ -417,7 +416,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { /* private final java.util.concurrent.atomic.AtomicReference<Object> fieldName = new java.util.concurrent.atomic.AtomicReference<Object>(); */ { field.vartype = recursiveSetGeneratedBy( - maker.TypeApply(chainDotsString(fieldNode, AR), List.<JCExpression>of(chainDotsString(fieldNode, JLO))), source, fieldNode.getContext()); + maker.TypeApply(chainDotsString(fieldNode, AR), List.<JCExpression>of(genJavaLangTypeRef(fieldNode, "Object"))), source, fieldNode.getContext()); field.init = recursiveSetGeneratedBy(maker.NewClass(null, NIL_EXPRESSION, copyType(maker, field), NIL_EXPRESSION, null), source, fieldNode.getContext()); } diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 5dd6ec69..444c2be9 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -246,7 +246,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod); if (isFieldDeprecated(field)) { - annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.<JCExpression>nil())); + annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil())); } JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, diff --git a/src/core/lombok/javac/handlers/HandleSynchronized.java b/src/core/lombok/javac/handlers/HandleSynchronized.java index 9de2c809..13b8ffcb 100644 --- a/src/core/lombok/javac/handlers/HandleSynchronized.java +++ b/src/core/lombok/javac/handlers/HandleSynchronized.java @@ -88,9 +88,9 @@ public class HandleSynchronized extends JavacAnnotationHandler<Synchronized> { annotationNode.addError("The field " + lockName + " does not exist."); return; } - JCExpression objectType = chainDots(methodNode, ast.pos, "java", "lang", "Object"); + JCExpression objectType = genJavaLangTypeRef(methodNode, ast.pos, "Object"); //We use 'new Object[0];' because unlike 'new Object();', empty arrays *ARE* serializable! - JCNewArray newObjectArray = maker.NewArray(chainDots(methodNode, ast.pos, "java", "lang", "Object"), + JCNewArray newObjectArray = maker.NewArray(genJavaLangTypeRef(methodNode, ast.pos, "Object"), List.<JCExpression>of(maker.Literal(CTC_INT, 0)), null); JCVariableDecl fieldDecl = recursiveSetGeneratedBy(maker.VarDef( maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (isStatic ? Flags.STATIC : 0)), diff --git a/src/core/lombok/javac/handlers/HandleToString.java b/src/core/lombok/javac/handlers/HandleToString.java index 19f91fe5..af65202a 100644 --- a/src/core/lombok/javac/handlers/HandleToString.java +++ b/src/core/lombok/javac/handlers/HandleToString.java @@ -169,9 +169,9 @@ public class HandleToString extends JavacAnnotationHandler<ToString> { static JCMethodDecl createToString(JavacNode typeNode, Collection<JavacNode> fields, boolean includeFieldNames, boolean callSuper, FieldAccess fieldAccess, JCTree source) { JavacTreeMaker maker = typeNode.getTreeMaker(); - JCAnnotation overrideAnnotation = maker.Annotation(chainDots(typeNode, "java", "lang", "Override"), List.<JCExpression>nil()); + JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(typeNode, "Override"), List.<JCExpression>nil()); JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation)); - JCExpression returnType = chainDots(typeNode, "java", "lang", "String"); + JCExpression returnType = genJavaLangTypeRef(typeNode, "String"); boolean first = true; diff --git a/src/core/lombok/javac/handlers/HandleWither.java b/src/core/lombok/javac/handlers/HandleWither.java index f2e51473..85ac3e37 100644 --- a/src/core/lombok/javac/handlers/HandleWither.java +++ b/src/core/lombok/javac/handlers/HandleWither.java @@ -262,7 +262,7 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod); if (isFieldDeprecated(field)) { - annsOnMethod = annsOnMethod.prepend(maker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.<JCExpression>nil())); + annsOnMethod = annsOnMethod.prepend(maker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil())); } JCMethodDecl decl = recursiveSetGeneratedBy(maker.MethodDef(maker.Modifiers(access, annsOnMethod), methodName, returnType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source, field.getContext()); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 8a125fb6..ef2a936a 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -900,10 +900,26 @@ public class JavacHandlerUtil { return flags; } + public static JCExpression genJavaLangTypeRef(JavacNode node, String... simpleNames) { + if (LombokOptionsFactory.getDelombokOptions(node.getContext()).getFormatPreferences().javaLangAsFqn()) { + return chainDots(node, "java", "lang", simpleNames); + } else { + return chainDots(node, null, null, simpleNames); + } + } + + public static JCExpression genJavaLangTypeRef(JavacNode node, int pos, String... simpleNames) { + if (LombokOptionsFactory.getDelombokOptions(node.getContext()).getFormatPreferences().javaLangAsFqn()) { + return chainDots(node, pos, "java", "lang", simpleNames); + } else { + return chainDots(node, pos, null, null, simpleNames); + } + } + private static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) { if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateSuppressWarnings()) return; JavacTreeMaker maker = node.getTreeMaker(); - JCExpression suppressWarningsType = chainDots(node, "java", "lang", "SuppressWarnings"); + JCExpression suppressWarningsType = genJavaLangTypeRef(node, "SuppressWarnings"); JCLiteral allLiteral = maker.Literal("all"); suppressWarningsType.pos = pos; allLiteral.pos = pos; @@ -933,8 +949,12 @@ public class JavacHandlerUtil { * @see com.sun.tools.javac.tree.JCTree.JCIdent * @see com.sun.tools.javac.tree.JCTree.JCFieldAccess */ - public static JCExpression chainDots(JavacNode node, String... elems) { - return chainDots(node, -1, elems); + public static JCExpression chainDots(JavacNode node, String elem1, String elem2, String... elems) { + return chainDots(node, -1, elem1, elem2, elems); + } + + public static JCExpression chainDots(JavacNode node, String[] elems) { + return chainDots(node, -1, null, null, elems); } /** @@ -949,17 +969,20 @@ public class JavacHandlerUtil { * @see com.sun.tools.javac.tree.JCTree.JCIdent * @see com.sun.tools.javac.tree.JCTree.JCFieldAccess */ - public static JCExpression chainDots(JavacNode node, int pos, String... elems) { + public static JCExpression chainDots(JavacNode node, int pos, String elem1, String elem2, String... elems) { assert elems != null; - assert elems.length > 0; JavacTreeMaker maker = node.getTreeMaker(); if (pos != -1) maker = maker.at(pos); - JCExpression e = maker.Ident(node.toName(elems[0])); - for (int i = 1 ; i < elems.length ; i++) { - e = maker.Select(e, node.toName(elems[i])); + JCExpression e = null; + if (elem1 != null) e = maker.Ident(node.toName(elem1)); + if (elem2 != null) e = e == null ? maker.Ident(node.toName(elem2)) : maker.Select(e, node.toName(elem2)); + for (int i = 0 ; i < elems.length ; i++) { + e = e == null ? maker.Ident(node.toName(elems[i])) : maker.Select(e, node.toName(elems[i])); } + assert e != null; + return e; } @@ -975,7 +998,7 @@ public class JavacHandlerUtil { * @see com.sun.tools.javac.tree.JCTree.JCFieldAccess */ public static JCExpression chainDotsString(JavacNode node, String elems) { - return chainDots(node, elems.split("\\.")); + return chainDots(node, null, null, elems.split("\\.")); } /** @@ -1007,7 +1030,7 @@ public class JavacHandlerUtil { JCVariableDecl varDecl = (JCVariableDecl) variable.get(); if (isPrimitive(varDecl.vartype)) return null; Name fieldName = varDecl.name; - JCExpression npe = chainDots(variable, "java", "lang", "NullPointerException"); + JCExpression npe = genJavaLangTypeRef(variable, "NullPointerException"); JCExpression exception = maker.NewClass(null, List.<JCExpression>nil(), npe, List.<JCExpression>of(maker.Literal(fieldName.toString())), null); JCStatement throwStatement = maker.Throw(exception); JCBlock throwBlock = maker.Block(0, List.of(throwStatement)); |