diff options
Diffstat (limited to 'src/core/lombok/javac/handlers')
15 files changed, 161 insertions, 118 deletions
diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index fc578f0f..b382395e 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -220,7 +220,7 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { } } - private JCMethodDecl generateBuildMethod(String name, Name staticName, JCExpression returnType, java.util.List<Name> fieldNames, JavacNode type, List<JCExpression> thrownExceptions) { + public JCMethodDecl generateBuildMethod(String name, Name staticName, JCExpression returnType, java.util.List<Name> fieldNames, JavacNode type, List<JCExpression> thrownExceptions) { JavacTreeMaker maker = type.getTreeMaker(); JCExpression call; @@ -254,7 +254,7 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { return maker.MethodDef(maker.Modifiers(Flags.PUBLIC), type.toName(name), returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), thrownExceptions, body, null); } - private JCMethodDecl generateBuilderMethod(String builderMethodName, String builderClassName, JavacNode type, List<JCTypeParameter> typeParams) { + public JCMethodDecl generateBuilderMethod(String builderMethodName, String builderClassName, JavacNode type, List<JCTypeParameter> typeParams) { JavacTreeMaker maker = type.getTreeMaker(); ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>(); @@ -269,7 +269,7 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { return maker.MethodDef(maker.Modifiers(Flags.STATIC | Flags.PUBLIC), type.toName(builderMethodName), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), copyTypeParams(maker, typeParams), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null); } - private java.util.List<JavacNode> addFieldsToBuilder(JavacNode builderType, java.util.List<Name> namesOfParameters, java.util.List<JCExpression> typesOfParameters, JCTree source) { + public java.util.List<JavacNode> addFieldsToBuilder(JavacNode builderType, java.util.List<Name> namesOfParameters, java.util.List<JCExpression> typesOfParameters, JCTree source) { int len = namesOfParameters.size(); java.util.List<JavacNode> existing = new ArrayList<JavacNode>(); for (JavacNode child : builderType.down()) { @@ -299,7 +299,7 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { } - private JCMethodDecl makeSetterMethodForBuilder(JavacNode builderType, JavacNode fieldNode, JCTree source, boolean fluent, boolean chain) { + public JCMethodDecl makeSetterMethodForBuilder(JavacNode builderType, JavacNode fieldNode, JCTree source, boolean fluent, boolean chain) { Name fieldName = ((JCVariableDecl) fieldNode.get()).name; for (JavacNode child : builderType.down()) { @@ -315,7 +315,7 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { return HandleSetter.createSetter(Flags.PUBLIC, fieldNode, maker, setterName, chain, source, List.<JCAnnotation>nil(), List.<JCAnnotation>nil()); } - private JavacNode findInnerClass(JavacNode parent, String name) { + public JavacNode findInnerClass(JavacNode parent, String name) { for (JavacNode child : parent.down()) { if (child.getKind() != Kind.TYPE) continue; JCClassDecl td = (JCClassDecl) child.get(); @@ -324,7 +324,7 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { return null; } - private JavacNode makeBuilderClass(JavacNode tdParent, String builderClassName, List<JCTypeParameter> typeParams, JCAnnotation ast) { + public JavacNode makeBuilderClass(JavacNode tdParent, String builderClassName, List<JCTypeParameter> typeParams, JCAnnotation ast) { JavacTreeMaker maker = tdParent.getTreeMaker(); JCModifiers mods = maker.Modifiers(Flags.PUBLIC | Flags.STATIC); JCClassDecl builder = maker.ClassDef(mods, tdParent.toName(builderClassName), copyTypeParams(maker, typeParams), null, List.<JCExpression>nil(), List.<JCTree>nil()); diff --git a/src/core/lombok/javac/handlers/HandleCleanup.java b/src/core/lombok/javac/handlers/HandleCleanup.java index 55a96df7..4aa61764 100644 --- a/src/core/lombok/javac/handlers/HandleCleanup.java +++ b/src/core/lombok/javac/handlers/HandleCleanup.java @@ -145,7 +145,7 @@ public class HandleCleanup extends JavacAnnotationHandler<Cleanup> { ancestor.rebuild(); } - private JCExpression preventNullAnalysis(JavacTreeMaker maker, JavacNode node, JCExpression expression) { + public 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))); @@ -155,11 +155,11 @@ public class HandleCleanup extends JavacAnnotationHandler<Cleanup> { } } - private void doAssignmentCheck(JavacNode node, List<JCStatement> statements, Name name) { + public void doAssignmentCheck(JavacNode node, List<JCStatement> statements, Name name) { for (JCStatement statement : statements) doAssignmentCheck0(node, statement, name); } - private void doAssignmentCheck0(JavacNode node, JCTree statement, Name name) { + public void doAssignmentCheck0(JavacNode node, JCTree statement, Name name) { if (statement instanceof JCAssign) doAssignmentCheck0(node, ((JCAssign)statement).rhs, name); if (statement instanceof JCExpressionStatement) doAssignmentCheck0(node, ((JCExpressionStatement)statement).expr, name); diff --git a/src/core/lombok/javac/handlers/HandleConstructor.java b/src/core/lombok/javac/handlers/HandleConstructor.java index 8fc01ffe..adfa253f 100644 --- a/src/core/lombok/javac/handlers/HandleConstructor.java +++ b/src/core/lombok/javac/handlers/HandleConstructor.java @@ -96,7 +96,7 @@ public class HandleConstructor { } } - private static List<JavacNode> findRequiredFields(JavacNode typeNode) { + public static List<JavacNode> findRequiredFields(JavacNode typeNode) { ListBuffer<JavacNode> fields = new ListBuffer<JavacNode>(); for (JavacNode child : typeNode.down()) { if (child.getKind() != Kind.FIELD) continue; @@ -133,7 +133,7 @@ public class HandleConstructor { } } - static List<JavacNode> findAllFields(JavacNode typeNode) { + public static List<JavacNode> findAllFields(JavacNode typeNode) { ListBuffer<JavacNode> fields = new ListBuffer<JavacNode>(); for (JavacNode child : typeNode.down()) { if (child.getKind() != Kind.FIELD) continue; @@ -150,7 +150,7 @@ public class HandleConstructor { return fields.toList(); } - static boolean checkLegality(JavacNode typeNode, JavacNode errorNode, String name) { + public static boolean checkLegality(JavacNode typeNode, JavacNode errorNode, String name) { JCClassDecl typeDecl = null; if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) typeNode.get(); long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags; @@ -213,7 +213,7 @@ public class HandleConstructor { } } - private static void addConstructorProperties(JCModifiers mods, JavacNode node, List<JavacNode> fields) { + public static void addConstructorProperties(JCModifiers mods, JavacNode node, List<JavacNode> fields) { if (fields.isEmpty()) return; JavacTreeMaker maker = node.getTreeMaker(); JCExpression constructorPropertiesType = chainDots(node, "java", "beans", "ConstructorProperties"); @@ -227,7 +227,7 @@ public class HandleConstructor { mods.annotations = mods.annotations.append(annotation); } - static JCMethodDecl createConstructor(AccessLevel level, List<JCAnnotation> onConstructor, JavacNode typeNode, List<JavacNode> fields, boolean suppressConstructorProperties, JCTree source) { + public static JCMethodDecl createConstructor(AccessLevel level, List<JCAnnotation> onConstructor, JavacNode typeNode, List<JavacNode> fields, boolean suppressConstructorProperties, JCTree source) { JavacTreeMaker maker = typeNode.getTreeMaker(); boolean isEnum = (((JCClassDecl) typeNode.get()).mods.flags & Flags.ENUM) != 0; @@ -266,14 +266,14 @@ public class HandleConstructor { null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(), maker.Block(0L, nullChecks.appendList(assigns).toList()), null), source, typeNode.getContext()); } - private static boolean isLocalType(JavacNode type) { + public static boolean isLocalType(JavacNode type) { Kind kind = type.up().getKind(); if (kind == Kind.COMPILATION_UNIT) return false; if (kind == Kind.TYPE) return isLocalType(type.up()); return true; } - private JCMethodDecl createStaticConstructor(String name, AccessLevel level, JavacNode typeNode, List<JavacNode> fields, JCTree source) { + public JCMethodDecl createStaticConstructor(String name, AccessLevel level, JavacNode typeNode, List<JavacNode> fields, JCTree source) { JavacTreeMaker maker = typeNode.getTreeMaker(); JCClassDecl type = (JCClassDecl) typeNode.get(); diff --git a/src/core/lombok/javac/handlers/HandleDelegate.java b/src/core/lombok/javac/handlers/HandleDelegate.java index 6f7eaa0a..ec6ea20c 100644 --- a/src/core/lombok/javac/handlers/HandleDelegate.java +++ b/src/core/lombok/javac/handlers/HandleDelegate.java @@ -64,6 +64,7 @@ import com.sun.tools.javac.code.Type.ClassType; import com.sun.tools.javac.code.Type.TypeVar; import com.sun.tools.javac.code.Types; import com.sun.tools.javac.model.JavacTypes; +import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCBlock; import com.sun.tools.javac.tree.JCTree.JCClassDecl; @@ -95,6 +96,8 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> { "clone()", "finalize()")); + private static final String LEGALITY_OF_DELEGATE = "@Delegate is legal only on instance fields or no-argument instance methods."; + @Override public void handle(AnnotationValues<Delegate> annotation, JCAnnotation ast, JavacNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.DELEGATE_FLAG_USAGE, "@Delegate"); @@ -104,19 +107,24 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> { Name delegateName = annotationNode.toName(annotationNode.up().getName()); DelegateReceiver delegateReceiver; JavacResolution reso = new JavacResolution(annotationNode.getContext()); + JCTree member = annotationNode.up().get(); if (annotationNode.up().getKind() == Kind.FIELD) { + if ((((JCVariableDecl) member).mods.flags & Flags.STATIC) != 0) { + annotationNode.addError(LEGALITY_OF_DELEGATE); + return; + } delegateReceiver = DelegateReceiver.FIELD; - delegateType = annotationNode.up().get().type; + delegateType = member.type; if (delegateType == null) reso.resolveClassMember(annotationNode.up()); - delegateType = annotationNode.up().get().type; + delegateType = member.type; } else if (annotationNode.up().getKind() == Kind.METHOD) { - if (!(annotationNode.up().get() instanceof JCMethodDecl)) { - annotationNode.addError("@Delegate is legal only on no-argument methods."); + if (!(member instanceof JCMethodDecl)) { + annotationNode.addError(LEGALITY_OF_DELEGATE); return; } - JCMethodDecl methodDecl = (JCMethodDecl) annotationNode.up().get(); - if (!methodDecl.params.isEmpty()) { - annotationNode.addError("@Delegate is legal only on no-argument methods."); + JCMethodDecl methodDecl = (JCMethodDecl) member; + if (!methodDecl.params.isEmpty() || (methodDecl.mods.flags & Flags.STATIC) != 0) { + annotationNode.addError(LEGALITY_OF_DELEGATE); return; } delegateReceiver = DelegateReceiver.METHOD; @@ -194,7 +202,7 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> { for (MethodSig sig : signaturesToDelegate) generateAndAdd(sig, annotationNode, delegateName, delegateReceiver); } - private void generateAndAdd(MethodSig sig, JavacNode annotation, Name delegateName, DelegateReceiver delegateReceiver) { + public void generateAndAdd(MethodSig sig, JavacNode annotation, Name delegateName, DelegateReceiver delegateReceiver) { List<JCMethodDecl> toAdd = new ArrayList<JCMethodDecl>(); try { toAdd.add(createDelegateMethod(sig, annotation, delegateName, delegateReceiver)); @@ -211,7 +219,7 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> { } } - private static class CantMakeDelegates extends Exception { + public static class CantMakeDelegates extends Exception { Set<String> conflicted; } @@ -222,7 +230,7 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> { * * @throws CantMakeDelegates If there's a conflict. Conflict list is in ex.conflicted. */ - private void checkConflictOfTypeVarNames(MethodSig sig, JavacNode annotation) throws CantMakeDelegates { + public void checkConflictOfTypeVarNames(MethodSig sig, JavacNode annotation) throws CantMakeDelegates { // As first step, we check if there's a conflict between the delegate method's type vars and our own class. if (sig.elem.getTypeParameters().isEmpty()) return; @@ -262,7 +270,7 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> { } } - private JCMethodDecl createDelegateMethod(MethodSig sig, JavacNode annotation, Name delegateName, DelegateReceiver delegateReceiver) throws TypeNotConvertibleException, CantMakeDelegates { + public JCMethodDecl createDelegateMethod(MethodSig sig, JavacNode annotation, Name delegateName, DelegateReceiver delegateReceiver) throws TypeNotConvertibleException, CantMakeDelegates { /* public <T, U, ...> ReturnType methodName(ParamType1 name1, ParamType2 name2, ...) throws T1, T2, ... { * (return) delegate.<T, U>methodName(name1, name2); * } @@ -324,11 +332,11 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> { return recursiveSetGeneratedBy(maker.MethodDef(mods, sig.name, returnType, toList(typeParams), toList(params), toList(thrown), bodyBlock, null), annotation.get(), annotation.getContext()); } - private static <T> com.sun.tools.javac.util.List<T> toList(ListBuffer<T> collection) { + public static <T> com.sun.tools.javac.util.List<T> toList(ListBuffer<T> collection) { return collection == null ? com.sun.tools.javac.util.List.<T>nil() : collection.toList(); } - private void addMethodBindings(List<MethodSig> signatures, ClassType ct, JavacTypes types, Set<String> banList) { + public void addMethodBindings(List<MethodSig> signatures, ClassType ct, JavacTypes types, Set<String> banList) { TypeSymbol tsym = ct.asElement(); if (tsym == null) return; @@ -351,7 +359,7 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> { } } - private static class MethodSig { + public static class MethodSig { final Name name; final ExecutableType type; final boolean isDeprecated; @@ -378,7 +386,7 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> { } } - private static String printSig(ExecutableType method, Name name, JavacTypes types) { + public static String printSig(ExecutableType method, Name name, JavacTypes types) { StringBuilder sb = new StringBuilder(); sb.append(name.toString()).append("("); boolean first = true; @@ -390,12 +398,12 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> { return sb.append(")").toString(); } - private static String typeBindingToSignature(TypeMirror binding, JavacTypes types) { + public static String typeBindingToSignature(TypeMirror binding, JavacTypes types) { binding = types.erasure(binding); return binding.toString(); } - private enum DelegateReceiver { + public enum DelegateReceiver { METHOD { public JCExpression get(final JavacNode node, final Name name) { com.sun.tools.javac.util.List<JCExpression> nilExprs = 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 f61b48ad..2c998f48 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -33,6 +33,7 @@ import lombok.ConfigurationKeys; import lombok.EqualsAndHashCode; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; +import lombok.core.handlers.HandlerUtil; import lombok.javac.Javac; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; @@ -72,7 +73,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas private static final String RESULT_NAME = "result"; private static final String PRIME_NAME = "PRIME"; - private void checkForBogusFieldNames(JavacNode type, AnnotationValues<EqualsAndHashCode> annotation) { + public void checkForBogusFieldNames(JavacNode type, AnnotationValues<EqualsAndHashCode> annotation) { if (annotation.isExplicit("exclude")) { for (int i : createListOfNonExistentFields(List.from(annotation.getInstance().exclude()), type, true, true)) { annotation.setWarning("exclude", "This field does not exist, or would have been excluded anyway.", i); @@ -222,7 +223,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas injectMethod(typeNode, hashCodeMethod); } - private JCMethodDecl createHashCode(JavacNode typeNode, List<JavacNode> fields, boolean callSuper, FieldAccess fieldAccess, JCTree source) { + public JCMethodDecl createHashCode(JavacNode typeNode, List<JavacNode> fields, boolean callSuper, FieldAccess fieldAccess, JCTree source) { JavacTreeMaker maker = typeNode.getTreeMaker(); JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(typeNode, "Override"), List.<JCExpression>nil()); @@ -234,9 +235,9 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas Name resultName = typeNode.toName(RESULT_NAME); long finalFlag = JavacHandlerUtil.addFinalIfNeeded(0L, typeNode.getContext()); - /* final int PRIME = 31; */ { + /* final int PRIME = X; */ { if (!fields.isEmpty() || callSuper) { - statements.append(maker.VarDef(maker.Modifiers(finalFlag), primeName, maker.TypeIdent(CTC_INT), maker.Literal(31))); + statements.append(maker.VarDef(maker.Modifiers(finalFlag), primeName, maker.TypeIdent(CTC_INT), maker.Literal(HandlerUtil.primeForHashcode()))); } } @@ -258,8 +259,9 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas if (fType instanceof JCPrimitiveTypeTree) { switch (((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind()) { case BOOLEAN: - /* this.fieldName ? 1231 : 1237 */ - statements.append(createResultCalculation(typeNode, maker.Conditional(fieldAccessor, maker.Literal(1231), maker.Literal(1237)))); + /* this.fieldName ? X : Y */ + statements.append(createResultCalculation(typeNode, maker.Conditional(fieldAccessor, + maker.Literal(HandlerUtil.primeForTrue()), maker.Literal(HandlerUtil.primeForFalse())))); break; case LONG: { Name dollarFieldName = dollar.append(((JCVariableDecl)fieldNode.get()).name); @@ -325,7 +327,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null), source, typeNode.getContext()); } - private JCExpressionStatement createResultCalculation(JavacNode typeNode, JCExpression expr) { + public JCExpressionStatement createResultCalculation(JavacNode typeNode, JCExpression expr) { /* result = result * PRIME + (expr); */ JavacTreeMaker maker = typeNode.getTreeMaker(); Name resultName = typeNode.toName(RESULT_NAME); @@ -335,14 +337,14 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas } /** The 2 references must be clones of each other. */ - private JCExpression longToIntForHashCode(JavacTreeMaker maker, JCExpression ref1, JCExpression ref2) { + public JCExpression longToIntForHashCode(JavacTreeMaker maker, JCExpression ref1, JCExpression ref2) { /* (int)(ref >>> 32 ^ ref) */ JCExpression shift = maker.Binary(CTC_UNSIGNED_SHIFT_RIGHT, ref1, maker.Literal(32)); JCExpression xorBits = maker.Binary(CTC_BITXOR, shift, ref2); return maker.TypeCast(maker.TypeIdent(CTC_INT), xorBits); } - private JCExpression createTypeReference(JavacNode type) { + public JCExpression createTypeReference(JavacNode type) { java.util.List<String> list = new ArrayList<String>(); list.add(type.getName()); JavacNode tNode = type.up(); @@ -362,7 +364,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas return chain; } - private JCMethodDecl createEquals(JavacNode typeNode, List<JavacNode> fields, boolean callSuper, FieldAccess fieldAccess, boolean needsCanEqual, JCTree source) { + public JCMethodDecl createEquals(JavacNode typeNode, List<JavacNode> fields, boolean callSuper, FieldAccess fieldAccess, boolean needsCanEqual, JCTree source) { JavacTreeMaker maker = typeNode.getTreeMaker(); JCClassDecl type = (JCClassDecl) typeNode.get(); @@ -496,7 +498,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("equals"), returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source, typeNode.getContext()); } - private JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source) { + public JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source) { /* public boolean canEqual(final java.lang.Object other) { * return other instanceof Outer.Inner.MyType; * } @@ -517,7 +519,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source, typeNode.getContext()); } - private JCStatement generateCompareFloatOrDouble(JCExpression thisDotField, JCExpression otherDotField, + public JCStatement generateCompareFloatOrDouble(JCExpression thisDotField, JCExpression otherDotField, JavacTreeMaker maker, JavacNode node, boolean isDouble) { /* if (Float.compare(fieldName, other.fieldName) != 0) return false; */ JCExpression clazz = genJavaLangTypeRef(node, isDouble ? "Double" : "Float"); @@ -527,7 +529,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas return maker.If(compareCallEquals0, returnBool(maker, false), null); } - private JCStatement returnBool(JavacTreeMaker maker, boolean bool) { + public JCStatement returnBool(JavacTreeMaker maker, boolean bool) { return maker.Return(maker.Literal(CTC_BOOLEAN, bool ? 1 : 0)); } } diff --git a/src/core/lombok/javac/handlers/HandleExtensionMethod.java b/src/core/lombok/javac/handlers/HandleExtensionMethod.java index 91ecd4d0..345c5f8e 100644 --- a/src/core/lombok/javac/handlers/HandleExtensionMethod.java +++ b/src/core/lombok/javac/handlers/HandleExtensionMethod.java @@ -91,9 +91,9 @@ public class HandleExtensionMethod extends JavacAnnotationHandler<ExtensionMetho annotationNode.rebuild(); } - - - private List<Extension> getExtensions(final JavacNode typeNode, final List<Object> extensionProviders) { + + + public List<Extension> getExtensions(final JavacNode typeNode, final List<Object> extensionProviders) { List<Extension> extensions = new ArrayList<Extension>(); for (Object extensionProvider : extensionProviders) { if (!(extensionProvider instanceof JCFieldAccess)) continue; @@ -108,7 +108,7 @@ public class HandleExtensionMethod extends JavacAnnotationHandler<ExtensionMetho return extensions; } - private Extension getExtension(final JavacNode typeNode, final ClassType extensionMethodProviderType) { + public Extension getExtension(final JavacNode typeNode, final ClassType extensionMethodProviderType) { List<MethodSymbol> extensionMethods = new ArrayList<MethodSymbol>(); TypeSymbol tsym = extensionMethodProviderType.asElement(); if (tsym != null) for (Symbol member : tsym.getEnclosedElements()) { diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 4414ae24..48a13bde 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -162,13 +162,13 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { } } - private void createGetterForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists, boolean lazy, List<JCAnnotation> onMethod) { + public void createGetterForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists, boolean lazy, List<JCAnnotation> onMethod) { for (JavacNode fieldNode : fieldNodes) { createGetterForField(level, fieldNode, errorNode, whineIfExists, lazy, onMethod); } } - private void createGetterForField(AccessLevel level, + public void createGetterForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean whineIfExists, boolean lazy, List<JCAnnotation> onMethod) { if (fieldNode.getKind() != Kind.FIELD) { source.addError("@Getter is only supported on a class or a field."); @@ -218,7 +218,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { injectMethod(fieldNode.up(), createGetter(access, fieldNode, fieldNode.getTreeMaker(), source.get(), lazy, onMethod)); } - private JCMethodDecl createGetter(long access, JavacNode field, JavacTreeMaker treeMaker, JCTree source, boolean lazy, List<JCAnnotation> onMethod) { + public JCMethodDecl createGetter(long access, JavacNode field, JavacTreeMaker treeMaker, JCTree source, boolean lazy, List<JCAnnotation> onMethod) { JCVariableDecl fieldNode = (JCVariableDecl) field.get(); // Remember the type; lazy will change it @@ -228,7 +228,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { List<JCStatement> statements; JCTree toClearOfMarkers = null; - if (lazy) { + if (lazy && !inNetbeansEditor(field)) { toClearOfMarkers = fieldNode.init; statements = createLazyGetterBody(treeMaker, field, source); } else { @@ -262,7 +262,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { return decl; } - private static List<JCAnnotation> findDelegatesAndRemoveFromField(JavacNode field) { + public static List<JCAnnotation> findDelegatesAndRemoveFromField(JavacNode field) { JCVariableDecl fieldNode = (JCVariableDecl) field.get(); List<JCAnnotation> delegates = List.nil(); @@ -285,14 +285,14 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { return delegates; } - private List<JCStatement> createSimpleGetterBody(JavacTreeMaker treeMaker, JavacNode field) { + public List<JCStatement> createSimpleGetterBody(JavacTreeMaker treeMaker, JavacNode field) { return List.<JCStatement>of(treeMaker.Return(createFieldAccessor(treeMaker, field, FieldAccess.ALWAYS_FIELD))); } private static final String AR = "java.util.concurrent.atomic.AtomicReference"; private static final List<JCExpression> NIL_EXPRESSION = List.nil(); - private static final java.util.Map<TypeTag, String> TYPE_MAP; + public static final java.util.Map<TypeTag, String> TYPE_MAP; static { Map<TypeTag, String> m = new HashMap<TypeTag, String>(); m.put(CTC_INT, "Integer"); @@ -306,7 +306,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { TYPE_MAP = Collections.unmodifiableMap(m); } - private List<JCStatement> createLazyGetterBody(JavacTreeMaker maker, JavacNode fieldNode, JCTree source) { + public List<JCStatement> createLazyGetterBody(JavacTreeMaker maker, JavacNode fieldNode, JCTree source) { /* java.lang.Object value = this.fieldName.get(); if (value == null) { @@ -426,17 +426,17 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { return statements.toList(); } - private JCMethodInvocation callGet(JavacNode source, JCExpression receiver) { + public JCMethodInvocation callGet(JavacNode source, JCExpression receiver) { JavacTreeMaker maker = source.getTreeMaker(); return maker.Apply(NIL_EXPRESSION, maker.Select(receiver, source.toName("get")), NIL_EXPRESSION); } - private JCStatement callSet(JavacNode source, JCExpression receiver, JCExpression value) { + public JCStatement callSet(JavacNode source, JCExpression receiver, JCExpression value) { JavacTreeMaker maker = source.getTreeMaker(); return maker.Exec(maker.Apply(NIL_EXPRESSION, maker.Select(receiver, source.toName("set")), List.<JCExpression>of(value))); } - private JCExpression copyType(JavacTreeMaker treeMaker, JCVariableDecl fieldNode) { + public JCExpression copyType(JavacTreeMaker treeMaker, JCVariableDecl fieldNode) { return fieldNode.type != null ? treeMaker.Type(fieldNode.type) : fieldNode.vartype; } } diff --git a/src/core/lombok/javac/handlers/HandleLog.java b/src/core/lombok/javac/handlers/HandleLog.java index 1b384847..ee7268f7 100644 --- a/src/core/lombok/javac/handlers/HandleLog.java +++ b/src/core/lombok/javac/handlers/HandleLog.java @@ -49,10 +49,10 @@ public class HandleLog { private HandleLog() { throw new UnsupportedOperationException(); } - - public static void processAnnotation(LoggingFramework framework, AnnotationValues<?> annotation, JavacNode annotationNode) { + + public static void processAnnotation(LoggingFramework framework, AnnotationValues<?> annotation, JavacNode annotationNode, String loggerTopic) { deleteAnnotationIfNeccessary(annotationNode, framework.getAnnotationClass()); - + JavacNode typeNode = annotationNode.up(); switch (typeNode.getKind()) { case TYPE: @@ -65,14 +65,13 @@ public class HandleLog { annotationNode.addError("@Log is legal only on classes and enums."); return; } - - if (fieldExists(logFieldName, typeNode)!= MemberExistsResult.NOT_EXISTS) { + if (fieldExists(logFieldName, typeNode) != MemberExistsResult.NOT_EXISTS) { annotationNode.addWarning("Field '" + logFieldName + "' already exists."); return; } - + JCFieldAccess loggingType = selfType(typeNode); - createField(framework, typeNode, loggingType, annotationNode.get(), logFieldName, useStatic); + createField(framework, typeNode, loggingType, annotationNode.get(), logFieldName, useStatic, loggerTopic); break; default: annotationNode.addError("@Log is legal only on types."); @@ -80,22 +79,28 @@ public class HandleLog { } } - private static JCFieldAccess selfType(JavacNode typeNode) { + public static JCFieldAccess selfType(JavacNode typeNode) { JavacTreeMaker maker = typeNode.getTreeMaker(); Name name = ((JCClassDecl) typeNode.get()).name; return maker.Select(maker.Ident(name), typeNode.toName("class")); } - private static boolean createField(LoggingFramework framework, JavacNode typeNode, JCFieldAccess loggingType, JCTree source, String logFieldName, boolean useStatic) { + private static boolean createField(LoggingFramework framework, JavacNode typeNode, JCFieldAccess loggingType, JCTree source, String logFieldName, boolean useStatic, String loggerTopic) { JavacTreeMaker maker = typeNode.getTreeMaker(); // private static final <loggerType> log = <factoryMethod>(<parameter>); JCExpression loggerType = chainDotsString(typeNode, framework.getLoggerTypeName()); JCExpression factoryMethod = chainDotsString(typeNode, framework.getLoggerFactoryMethodName()); - - JCExpression loggerName = framework.createFactoryParameter(typeNode, loggingType); + + JCExpression loggerName; + if (loggerTopic == null || loggerTopic.trim().length() == 0) { + loggerName = framework.createFactoryParameter(typeNode, loggingType); + } else { + loggerName = maker.Literal(loggerTopic); + } + JCMethodInvocation factoryMethodCall = maker.Apply(List.<JCExpression>nil(), factoryMethod, List.<JCExpression>of(loggerName)); - + JCVariableDecl fieldDecl = recursiveSetGeneratedBy(maker.VarDef( maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (useStatic ? Flags.STATIC : 0)), typeNode.toName(logFieldName), loggerType, factoryMethodCall), source, typeNode.getContext()); @@ -111,8 +116,7 @@ public class HandleLog { public static class HandleCommonsLog extends JavacAnnotationHandler<lombok.extern.apachecommons.CommonsLog> { @Override public void handle(AnnotationValues<lombok.extern.apachecommons.CommonsLog> annotation, JCAnnotation ast, JavacNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.LOG_COMMONS_FLAG_USAGE, "@apachecommons.CommonsLog", ConfigurationKeys.LOG_ANY_FLAG_USAGE, "any @Log"); - - processAnnotation(LoggingFramework.COMMONS, annotation, annotationNode); + processAnnotation(LoggingFramework.COMMONS, annotation, annotationNode, annotation.getInstance().topic()); } } @@ -123,8 +127,7 @@ public class HandleLog { public static class HandleJulLog extends JavacAnnotationHandler<lombok.extern.java.Log> { @Override public void handle(AnnotationValues<lombok.extern.java.Log> annotation, JCAnnotation ast, JavacNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.LOG_JUL_FLAG_USAGE, "@java.Log", ConfigurationKeys.LOG_ANY_FLAG_USAGE, "any @Log"); - - processAnnotation(LoggingFramework.JUL, annotation, annotationNode); + processAnnotation(LoggingFramework.JUL, annotation, annotationNode, annotation.getInstance().topic()); } } @@ -135,8 +138,7 @@ public class HandleLog { public static class HandleLog4jLog extends JavacAnnotationHandler<lombok.extern.log4j.Log4j> { @Override public void handle(AnnotationValues<lombok.extern.log4j.Log4j> annotation, JCAnnotation ast, JavacNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.LOG_LOG4J_FLAG_USAGE, "@Log4j", ConfigurationKeys.LOG_ANY_FLAG_USAGE, "any @Log"); - - processAnnotation(LoggingFramework.LOG4J, annotation, annotationNode); + processAnnotation(LoggingFramework.LOG4J, annotation, annotationNode, annotation.getInstance().topic()); } } @@ -147,8 +149,7 @@ public class HandleLog { public static class HandleLog4j2Log extends JavacAnnotationHandler<lombok.extern.log4j.Log4j2> { @Override public void handle(AnnotationValues<lombok.extern.log4j.Log4j2> annotation, JCAnnotation ast, JavacNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.LOG_LOG4J2_FLAG_USAGE, "@Log4j2", ConfigurationKeys.LOG_ANY_FLAG_USAGE, "any @Log"); - - processAnnotation(LoggingFramework.LOG4J2, annotation, annotationNode); + processAnnotation(LoggingFramework.LOG4J2, annotation, annotationNode, annotation.getInstance().topic()); } } @@ -159,8 +160,7 @@ public class HandleLog { public static class HandleSlf4jLog extends JavacAnnotationHandler<lombok.extern.slf4j.Slf4j> { @Override public void handle(AnnotationValues<lombok.extern.slf4j.Slf4j> annotation, JCAnnotation ast, JavacNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.LOG_SLF4J_FLAG_USAGE, "@Slf4j", ConfigurationKeys.LOG_ANY_FLAG_USAGE, "any @Log"); - - processAnnotation(LoggingFramework.SLF4J, annotation, annotationNode); + processAnnotation(LoggingFramework.SLF4J, annotation, annotationNode, annotation.getInstance().topic()); } } @@ -171,8 +171,7 @@ public class HandleLog { public static class HandleXSlf4jLog extends JavacAnnotationHandler<lombok.extern.slf4j.XSlf4j> { @Override public void handle(AnnotationValues<lombok.extern.slf4j.XSlf4j> annotation, JCAnnotation ast, JavacNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.LOG_XSLF4J_FLAG_USAGE, "@XSlf4j", ConfigurationKeys.LOG_ANY_FLAG_USAGE, "any @Log"); - - processAnnotation(LoggingFramework.XSLF4J, annotation, annotationNode); + processAnnotation(LoggingFramework.XSLF4J, annotation, annotationNode, annotation.getInstance().topic()); } } diff --git a/src/core/lombok/javac/handlers/HandleNonNull.java b/src/core/lombok/javac/handlers/HandleNonNull.java index e57c63dd..172e70b3 100644 --- a/src/core/lombok/javac/handlers/HandleNonNull.java +++ b/src/core/lombok/javac/handlers/HandleNonNull.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 The Project Lombok Authors. + * Copyright (C) 2013-2014 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -145,7 +145,7 @@ public class HandleNonNull extends JavacAnnotationHandler<NonNull> { declaration.body.stats = newList; } - private boolean isNullCheck(JCStatement stat) { + public boolean isNullCheck(JCStatement stat) { return returnVarNameIfNullCheck(stat) != null; } @@ -154,7 +154,7 @@ public class HandleNonNull extends JavacAnnotationHandler<NonNull> { * where the block braces are optional. If it is of this form, returns "x". * If it is not of this form, returns null. */ - private String returnVarNameIfNullCheck(JCStatement stat) { + public String returnVarNameIfNullCheck(JCStatement stat) { if (!(stat instanceof JCIf)) return null; /* Check that the if's statement is a throw statement, possibly in a block. */ { diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index d73dce7c..fbc9ef46 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -144,13 +144,13 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { } } - private void createSetterForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { + public void createSetterForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { for (JavacNode fieldNode : fieldNodes) { createSetterForField(level, fieldNode, errorNode, whineIfExists, onMethod, onParam); } } - private void createSetterForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { + public void createSetterForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { if (fieldNode.getKind() != Kind.FIELD) { fieldNode.addError("@Setter is only supported on a class or a field."); return; @@ -193,13 +193,13 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { injectMethod(fieldNode.up(), createdSetter); } - static JCMethodDecl createSetter(long access, JavacNode field, JavacTreeMaker treeMaker, JCTree source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { + public static JCMethodDecl createSetter(long access, JavacNode field, JavacTreeMaker treeMaker, JCTree source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { String setterName = toSetterName(field); boolean returnThis = shouldReturnThis(field); return createSetter(access, field, treeMaker, setterName, returnThis, source, onMethod, onParam); } - static JCMethodDecl createSetter(long access, JavacNode field, JavacTreeMaker treeMaker, String setterName, boolean shouldReturnThis, JCTree source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { + public static JCMethodDecl createSetter(long access, JavacNode field, JavacTreeMaker treeMaker, String setterName, boolean shouldReturnThis, JCTree source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { if (setterName == null) return null; JCVariableDecl fieldDecl = (JCVariableDecl) field.get(); @@ -214,7 +214,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { Name methodName = field.toName(setterName); List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables); - long flags = JavacHandlerUtil.addFinalIfNeeded(0L, field.getContext()); + long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext()); JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(flags, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); if (nonNulls.isEmpty()) { diff --git a/src/core/lombok/javac/handlers/HandleSneakyThrows.java b/src/core/lombok/javac/handlers/HandleSneakyThrows.java index 4bd08bbb..ffe37a4c 100644 --- a/src/core/lombok/javac/handlers/HandleSneakyThrows.java +++ b/src/core/lombok/javac/handlers/HandleSneakyThrows.java @@ -42,12 +42,15 @@ import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCBlock; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCExpression; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCStatement; +import com.sun.tools.javac.tree.JCTree.JCTry; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.List; +import lombok.javac.Javac; /** * Handles the {@code lombok.SneakyThrows} annotation for javac. @@ -81,7 +84,7 @@ public class HandleSneakyThrows extends JavacAnnotationHandler<SneakyThrows> { } } - private void handleMethod(JavacNode annotation, JCMethodDecl method, Collection<String> exceptions) { + public void handleMethod(JavacNode annotation, JCMethodDecl method, Collection<String> exceptions) { JavacNode methodNode = annotation.up(); if ( (method.mods.flags & Flags.ABSTRACT) != 0) { @@ -111,28 +114,38 @@ public class HandleSneakyThrows extends JavacAnnotationHandler<SneakyThrows> { methodNode.rebuild(); } - private void generateEmptyBlockWarning(JavacNode methodNode, JavacNode annotation, boolean hasConstructorCall) { + public void generateEmptyBlockWarning(JavacNode methodNode, JavacNode annotation, boolean hasConstructorCall) { if (hasConstructorCall) { annotation.addWarning("Calls to sibling / super constructors are always excluded from @SneakyThrows; @SneakyThrows has been ignored because there is no other code in this constructor."); } else { annotation.addWarning("This method or constructor is empty; @SneakyThrows has been ignored."); } } - - private JCStatement buildTryCatchBlock(JavacNode node, List<JCStatement> contents, String exception, JCTree source) { + + public JCStatement buildTryCatchBlock(JavacNode node, List<JCStatement> contents, String exception, JCTree source) { JavacTreeMaker maker = node.getTreeMaker(); Context context = node.getContext(); JCBlock tryBlock = setGeneratedBy(maker.Block(0, contents), source, context); - JCExpression varType = chainDots(node, exception.split("\\.")); - JCVariableDecl catchParam = maker.VarDef(maker.Modifiers(Flags.FINAL), node.toName("$ex"), varType, null); + JCVariableDecl catchParam = maker.VarDef(maker.Modifiers(Flags.FINAL | Flags.PARAMETER), node.toName("$ex"), varType, null); JCExpression lombokLombokSneakyThrowNameRef = chainDots(node, "lombok", "Lombok", "sneakyThrow"); JCBlock catchBody = maker.Block(0, List.<JCStatement>of(maker.Throw(maker.Apply( List.<JCExpression>nil(), lombokLombokSneakyThrowNameRef, List.<JCExpression>of(maker.Ident(node.toName("$ex"))))))); - - return setGeneratedBy(maker.Try(tryBlock, List.of(recursiveSetGeneratedBy(maker.Catch(catchParam, catchBody), source, context)), null), source, context); + JCTry tryStatement = maker.Try(tryBlock, List.of(recursiveSetGeneratedBy(maker.Catch(catchParam, catchBody), source, context)), null); + if (JavacHandlerUtil.inNetbeansEditor(node)) { + //set span (start and end position) of the try statement and the main block + //this allows NetBeans to dive into the statement correctly: + JCCompilationUnit top = (JCCompilationUnit) node.top().get(); + int startPos = contents.head.pos; + int endPos = Javac.getEndPosition(contents.last().pos(), top); + tryBlock.pos = startPos; + tryStatement.pos = startPos; + Javac.storeEnd(tryBlock, endPos, top); + Javac.storeEnd(tryStatement, endPos, top); + } + return setGeneratedBy(tryStatement, source, context); } } diff --git a/src/core/lombok/javac/handlers/HandleToString.java b/src/core/lombok/javac/handlers/HandleToString.java index 98c72645..743e7b26 100644 --- a/src/core/lombok/javac/handlers/HandleToString.java +++ b/src/core/lombok/javac/handlers/HandleToString.java @@ -59,7 +59,7 @@ import com.sun.tools.javac.util.ListBuffer; */ @ProviderFor(JavacAnnotationHandler.class) public class HandleToString extends JavacAnnotationHandler<ToString> { - private void checkForBogusFieldNames(JavacNode type, AnnotationValues<ToString> annotation) { + public void checkForBogusFieldNames(JavacNode type, AnnotationValues<ToString> annotation) { if (annotation.isExplicit("exclude")) { for (int i : createListOfNonExistentFields(List.from(annotation.getInstance().exclude()), type, true, false)) { annotation.setWarning("exclude", "This field does not exist, or would have been excluded anyway.", i); @@ -253,7 +253,7 @@ public class HandleToString extends JavacAnnotationHandler<ToString> { List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null), source, typeNode.getContext()); } - private static String getTypeName(JavacNode typeNode) { + public static String getTypeName(JavacNode typeNode) { String typeName = ((JCClassDecl) typeNode.get()).name.toString(); JavacNode upType = typeNode.up(); while (upType.getKind() == Kind.TYPE) { diff --git a/src/core/lombok/javac/handlers/HandleWither.java b/src/core/lombok/javac/handlers/HandleWither.java index 746f0a2e..7b55b671 100644 --- a/src/core/lombok/javac/handlers/HandleWither.java +++ b/src/core/lombok/javac/handlers/HandleWither.java @@ -145,13 +145,13 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { } } - private void createWitherForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { + public void createWitherForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { for (JavacNode fieldNode : fieldNodes) { createWitherForField(level, fieldNode, errorNode, whineIfExists, onMethod, onParam); } } - private void createWitherForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { + public void createWitherForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { if (fieldNode.getKind() != Kind.FIELD) { fieldNode.addError("@Wither is only supported on a class or a field."); return; @@ -204,7 +204,7 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { injectMethod(fieldNode.up(), createdWither); } - private JCMethodDecl createWither(long access, JavacNode field, JavacTreeMaker maker, JCTree source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { + public JCMethodDecl createWither(long access, JavacNode field, JavacTreeMaker maker, JCTree source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { String witherName = toWitherName(field); if (witherName == null) return null; @@ -217,7 +217,7 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { Name methodName = field.toName(witherName); List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables); - long flags = JavacHandlerUtil.addFinalIfNeeded(0L, field.getContext()); + long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext()); JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); JCExpression selfType = cloneSelfType(field); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 2d859103..37164a6c 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -151,7 +151,7 @@ public class JavacHandlerUtil { if (source == null) generatedNodes.remove(node); else generatedNodes.put(node, new WeakReference<JCTree>(source)); } - if (source != null && !inNetbeansEditor(context)) node.pos = source.pos; + if (source != null && (!inNetbeansEditor(context) || (node instanceof JCVariableDecl && (((JCVariableDecl) node).mods.flags & Flags.PARAMETER) != 0))) node.pos = source.pos; return node; } @@ -845,7 +845,7 @@ public class JavacHandlerUtil { return typeNode.add(field, Kind.FIELD); } - private static boolean isEnumConstant(final JCVariableDecl field) { + public static boolean isEnumConstant(final JCVariableDecl field) { return (field.mods.flags & Flags.ENUM) != 0; } @@ -920,8 +920,16 @@ public class JavacHandlerUtil { } } - private static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) { + public static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) { if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateSuppressWarnings()) return; + for (JCAnnotation ann : mods.annotations) { + JCTree annType = ann.getAnnotationType(); + Name lastPart = null; + if (annType instanceof JCIdent) lastPart = ((JCIdent) annType).name; + else if (annType instanceof JCFieldAccess) lastPart = ((JCFieldAccess) annType).name; + + if (lastPart != null && lastPart.contentEquals("SuppressWarnings")) return; + } JavacTreeMaker maker = node.getTreeMaker(); JCExpression suppressWarningsType = genJavaLangTypeRef(node, "SuppressWarnings"); JCLiteral allLiteral = maker.Literal("all"); @@ -1211,7 +1219,7 @@ public class JavacHandlerUtil { return isClassAndDoesNotHaveFlags(typeNode, Flags.INTERFACE | Flags.ANNOTATION); } - private static boolean isClassAndDoesNotHaveFlags(JavacNode typeNode, int flags) { + public static boolean isClassAndDoesNotHaveFlags(JavacNode typeNode, int flags) { JCClassDecl typeDecl = null; if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl)typeNode.get(); else return false; @@ -1296,13 +1304,20 @@ public class JavacHandlerUtil { private static final Pattern SECTION_FINDER = Pattern.compile("^\\s*\\**\\s*[-*][-*]+\\s*([GS]ETTER|WITHER)\\s*[-*][-*]+\\s*\\**\\s*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); - private static String stripLinesWithTagFromJavadoc(String javadoc, String regexpFragment) { + public static String stripLinesWithTagFromJavadoc(String javadoc, String regexpFragment) { Pattern p = Pattern.compile("^\\s*\\**\\s*" + regexpFragment + "\\s*\\**\\s*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(javadoc); return m.replaceAll(""); } - private static String[] splitJavadocOnSectionIfPresent(String javadoc, String sectionName) { + public static String stripSectionsFromJavadoc(String javadoc) { + Matcher m = SECTION_FINDER.matcher(javadoc); + if (!m.find()) return javadoc; + + return javadoc.substring(0, m.start()); + } + + public static String[] splitJavadocOnSectionIfPresent(String javadoc, String sectionName) { Matcher m = SECTION_FINDER.matcher(javadoc); int getterSectionHeaderStart = -1; int getterSectionStart = -1; @@ -1328,15 +1343,17 @@ public class JavacHandlerUtil { } public static enum CopyJavadoc { - VERBATIM, GETTER { + VERBATIM, + GETTER { @Override public String[] split(String javadoc) { // step 1: Check if there is a 'GETTER' section. If yes, that becomes the new method's javadoc and we strip that from the original. String[] out = splitJavadocOnSectionIfPresent(javadoc, "GETTER"); if (out != null) return out; - // failing that, create a copy, but strip @return from the original and @param from the copy. + // failing that, create a copy, but strip @return from the original and @param from the copy, as well as other sections. String copy = javadoc; javadoc = stripLinesWithTagFromJavadoc(javadoc, "@returns?\\s+.*"); copy = stripLinesWithTagFromJavadoc(copy, "@param(?:eter)?\\s+.*"); + copy = stripSectionsFromJavadoc(copy); return new String[] {copy, javadoc}; } }, @@ -1359,6 +1376,7 @@ public class JavacHandlerUtil { String copy = javadoc; javadoc = stripLinesWithTagFromJavadoc(javadoc, "@param(?:eter)?\\s+.*"); copy = stripLinesWithTagFromJavadoc(copy, "@returns?\\s+.*"); + copy = stripSectionsFromJavadoc(copy); return new String[] {copy, javadoc}; } @@ -1372,8 +1390,8 @@ public class JavacHandlerUtil { * Copies javadoc on one node to the other. * * in 'GETTER' copyMode, first a 'GETTER' segment is searched for. If it exists, that will become the javadoc for the 'to' node, and this section is - * stripped out of the 'from' node. If no 'GETTER' segment is found, then the entire javadoc is taken minus any {@code @param} lines. any {@code @return} lines - * are stripped from 'from'. + * stripped out of the 'from' node. If no 'GETTER' segment is found, then the entire javadoc is taken minus any {@code @param} lines and other sections. + * any {@code @return} lines are stripped from 'from'. * * in 'SETTER' mode, stripping works similarly to 'GETTER' mode, except {@code param} are copied and stripped from the original and {@code @return} are skipped. */ diff --git a/src/core/lombok/javac/handlers/package-info.java b/src/core/lombok/javac/handlers/package-info.java index 100a1b96..338feb9b 100644 --- a/src/core/lombok/javac/handlers/package-info.java +++ b/src/core/lombok/javac/handlers/package-info.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Project Lombok Authors. + * Copyright (C) 2009-2014 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,5 +22,8 @@ /** * Contains the classes that implement the transformations for all of lombok's various features on the javac v1.6 platform. + * + * <strong>NB: This package is not public API in the sense that contents of this package, + * even public classes / methods / etc, may change in point releases.</strong> */ package lombok.javac.handlers; |