diff options
13 files changed, 150 insertions, 17 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index f795197b..67a2d07c 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -245,6 +245,27 @@ public class EclipseHandlerUtil { return node; } + public static MarkerAnnotation generateDeprecatedAnnotation(ASTNode source) { + QualifiedTypeReference qtr = new QualifiedTypeReference(new char[][] { + {'j', 'a', 'v', 'a'}, {'l', 'a', 'n', 'g'}, {'D', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd'}}, poss(source, 3)); + setGeneratedBy(qtr, source); + return new MarkerAnnotation(qtr, source.sourceStart); + } + + public static boolean isFieldDeprecated(EclipseNode fieldNode) { + FieldDeclaration field = (FieldDeclaration) fieldNode.get(); + if ((field.modifiers & ClassFileConstants.AccDeprecated) != 0) { + return true; + } + if (field.annotations == null) return false; + for (Annotation annotation : field.annotations) { + if (typeMatches(Deprecated.class, fieldNode, annotation.type)) { + return true; + } + } + return false; + } + /** * Checks if the given TypeReference node is likely to be a reference to the provided class. * diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index 92f1177f..3bdba74e 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -212,14 +212,20 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> { } MethodDeclaration method = generateGetter((TypeDeclaration) fieldNode.up().get(), fieldNode, getterName, modifier, source, lazy); - Annotation[] copiedAnnotations = copyAnnotations(source, findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN), findDelegatesAndMarkAsHandled(fieldNode)); + + Annotation[] deprecated = null; + if (isFieldDeprecated(fieldNode)) { + deprecated = new Annotation[] { generateDeprecatedAnnotation(source) }; + } + + Annotation[] copiedAnnotations = copyAnnotations(source, findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN), findDelegatesAndMarkAsHandled(fieldNode), deprecated); if (copiedAnnotations.length != 0) { method.annotations = copiedAnnotations; } injectMethod(fieldNode.up(), method); } - + private static Annotation[] findDelegatesAndMarkAsHandled(EclipseNode fieldNode) { List<Annotation> delegates = new ArrayList<Annotation>(); for (EclipseNode child : fieldNode.down()) { diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index ea81965b..8599fca7 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -184,7 +184,9 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0); method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE; setGeneratedBy(method.returnType, source); - method.annotations = null; + if (isFieldDeprecated(fieldNode)) { + method.annotations = new Annotation[] { generateDeprecatedAnnotation(source) }; + } Argument param = new Argument(field.name, p, copyType(field.type, source), Modifier.FINAL); param.sourceStart = pS; param.sourceEnd = pE; setGeneratedBy(param, source); diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index c286ed24..b3421f86 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -74,11 +74,9 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { public void generateGetterForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelGetter) { if (checkForTypeLevelGetter) { if (typeNode != null) for (JavacNode child : typeNode.down()) { - if (child.getKind() == Kind.ANNOTATION) { - if (annotationTypeMatches(Getter.class, child)) { - //The annotation will make it happen, so we can skip it. - return; - } + if (annotationTypeMatches(Getter.class, child)) { + //The annotation will make it happen, so we can skip it. + return; } } } @@ -244,6 +242,9 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { List<JCAnnotation> delegates = findDelegatesAndRemoveFromField(field); List<JCAnnotation> annsOnMethod = nonNulls.appendList(nullables); + if (isFieldDeprecated(field)) { + annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.<JCExpression>nil())); + } JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source); diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index db1fa724..0298311e 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -208,6 +208,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { JCBlock methodBody = treeMaker.Block(0, statements); Name methodName = field.toName(toSetterName(fieldDecl)); List<JCAnnotation> annsOnParam = nonNulls.appendList(nullables); + JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); //WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6. JCExpression methodType = treeMaker.Type(new JCNoType(getCtcInt(TypeTags.class, "VOID"))); @@ -217,7 +218,11 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { List<JCExpression> throwsClauses = List.nil(); JCExpression annotationMethodDefaultValue = null; - return recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, List.<JCAnnotation>nil()), methodName, methodType, + List<JCAnnotation> annsOnMethod = List.nil(); + if (isFieldDeprecated(field)) { + annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.<JCExpression>nil())); + } + return recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source); } diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 244f7d38..32b17322 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2011 The Project Lombok Authors. + * Copyright (C) 2009-2012 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 @@ -142,6 +142,24 @@ public class JavacHandlerUtil { } /** + * Returns if a field is marked deprecated, either by {@code @Deprecated} or in javadoc + * @param field the field to check + * @return {@code true} if a field is marked deprecated, either by {@code @Deprecated} or in javadoc, otherwise {@code false} + */ + public static boolean isFieldDeprecated(JavacNode field) { + JCVariableDecl fieldNode = (JCVariableDecl) field.get(); + if ((fieldNode.mods.flags & Flags.DEPRECATED) != 0) { + return true; + } + for (JavacNode child : field.down()) { + if (annotationTypeMatches(Deprecated.class, child)) { + return true; + } + } + return false; + } + + /** * Creates an instance of {@code AnnotationValues} for the provided AST Node. * * @param type An annotation class type, such as {@code lombok.Getter.class}. diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java index acf1589d..7c73b465 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java @@ -51,11 +51,9 @@ import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.FieldReference; -import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation; import org.eclipse.jdt.internal.compiler.ast.MemberValuePair; import org.eclipse.jdt.internal.compiler.ast.MessageSend; import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; -import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference; import org.eclipse.jdt.internal.compiler.ast.ReturnStatement; import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; @@ -613,11 +611,7 @@ public class PatchDelegate { } if (isDeprecated) { - QualifiedTypeReference qtr = new QualifiedTypeReference(new char[][] { - {'j', 'a', 'v', 'a'}, {'l', 'a', 'n', 'g'}, {'D', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd'}}, poss(source, 3)); - setGeneratedBy(qtr, source); - MarkerAnnotation ann = new MarkerAnnotation(qtr, pS); - method.annotations = new Annotation[] {ann}; + method.annotations = new Annotation[] { generateDeprecatedAnnotation(source) }; } method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; diff --git a/test/transform/resource/after-delombok/GetterDeprecated.java b/test/transform/resource/after-delombok/GetterDeprecated.java new file mode 100644 index 00000000..3387540f --- /dev/null +++ b/test/transform/resource/after-delombok/GetterDeprecated.java @@ -0,0 +1,18 @@ +class GetterDeprecated { + @Deprecated + int annotation; + /** + * @deprecated + */ + int javadoc; + @java.lang.Deprecated + @java.lang.SuppressWarnings("all") + public int getAnnotation() { + return this.annotation; + } + @java.lang.Deprecated + @java.lang.SuppressWarnings("all") + public int getJavadoc() { + return this.javadoc; + } +}
\ No newline at end of file diff --git a/test/transform/resource/after-delombok/SetterDeprecated.java b/test/transform/resource/after-delombok/SetterDeprecated.java new file mode 100644 index 00000000..5a6cf9f3 --- /dev/null +++ b/test/transform/resource/after-delombok/SetterDeprecated.java @@ -0,0 +1,18 @@ +class SetterDeprecated { + @Deprecated + int annotation; + /** + * @deprecated + */ + int javadoc; + @java.lang.Deprecated + @java.lang.SuppressWarnings("all") + public void setAnnotation(final int annotation) { + this.annotation = annotation; + } + @java.lang.Deprecated + @java.lang.SuppressWarnings("all") + public void setJavadoc(final int javadoc) { + this.javadoc = javadoc; + } +}
\ No newline at end of file diff --git a/test/transform/resource/after-ecj/GetterDeprecated.java b/test/transform/resource/after-ecj/GetterDeprecated.java new file mode 100644 index 00000000..c19198dd --- /dev/null +++ b/test/transform/resource/after-ecj/GetterDeprecated.java @@ -0,0 +1,14 @@ +import lombok.Getter; +class GetterDeprecated { + @Deprecated @Getter int annotation; + @Getter int javadoc; + public @java.lang.Deprecated @java.lang.SuppressWarnings("all") int getAnnotation() { + return this.annotation; + } + public @java.lang.Deprecated @java.lang.SuppressWarnings("all") int getJavadoc() { + return this.javadoc; + } + GetterDeprecated() { + super(); + } +} diff --git a/test/transform/resource/after-ecj/SetterDeprecated.java b/test/transform/resource/after-ecj/SetterDeprecated.java new file mode 100644 index 00000000..b12ae34d --- /dev/null +++ b/test/transform/resource/after-ecj/SetterDeprecated.java @@ -0,0 +1,14 @@ +import lombok.Setter; +class SetterDeprecated { + @Deprecated @Setter int annotation; + @Setter int javadoc; + public @java.lang.Deprecated @java.lang.SuppressWarnings("all") void setAnnotation(final int annotation) { + this.annotation = annotation; + } + public @java.lang.Deprecated @java.lang.SuppressWarnings("all") void setJavadoc(final int javadoc) { + this.javadoc = javadoc; + } + SetterDeprecated() { + super(); + } +} diff --git a/test/transform/resource/before/GetterDeprecated.java b/test/transform/resource/before/GetterDeprecated.java new file mode 100644 index 00000000..01b66bca --- /dev/null +++ b/test/transform/resource/before/GetterDeprecated.java @@ -0,0 +1,11 @@ +import lombok.Getter; +class GetterDeprecated { + + @Deprecated + @Getter int annotation; + + /** + * @deprecated + */ + @Getter int javadoc; +}
\ No newline at end of file diff --git a/test/transform/resource/before/SetterDeprecated.java b/test/transform/resource/before/SetterDeprecated.java new file mode 100644 index 00000000..e655622f --- /dev/null +++ b/test/transform/resource/before/SetterDeprecated.java @@ -0,0 +1,11 @@ +import lombok.Setter; +class SetterDeprecated { + + @Deprecated + @Setter int annotation; + + /** + * @deprecated + */ + @Setter int javadoc; +}
\ No newline at end of file |