8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
authorsadayapalam
Fri, 12 Aug 2016 11:49:18 +0530
changeset 40313 a85f92c9a8ab
parent 40312 4c7bf578577e
child 40314 52f8470ce3e4
8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op Reviewed-by: mcimadamore, ksrini
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/src/jdk.javadoc/share/classes/com/sun/javadoc/package-info.java
langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/package-info.java
langtools/test/tools/javac/T6404756.out
langtools/test/tools/javac/annotations/neg/DeprecatedAnnotationTest/DeprecatedAnnotationTest.out
langtools/test/tools/javac/annotations/neg/DeprecatedAnnotationTest/package-info.java
langtools/test/tools/javac/diags/examples/DeprecatedAnnotationHasNoEffect/DeprecatedAnnotationHasNoEffect.java
langtools/test/tools/javac/warnings/suppress/T6480588.out
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Aug 11 17:02:00 2016 +0000
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Aug 12 11:49:18 2016 +0530
@@ -4282,6 +4282,9 @@
             case TOPLEVEL:
                 attribTopLevel(env);
                 break;
+            case PACKAGEDEF:
+                attribPackage(env.tree.pos(), ((JCPackageDecl) env.tree).packge);
+                break;
             default:
                 attribClass(env.tree.pos(), env.enclClass.sym);
         }
@@ -4300,6 +4303,20 @@
         }
     }
 
+    public void attribPackage(DiagnosticPosition pos, PackageSymbol p) {
+        try {
+            annotate.flush();
+            attribPackage(p);
+        } catch (CompletionFailure ex) {
+            chk.completionError(pos, ex);
+        }
+    }
+
+    void attribPackage(PackageSymbol p) {
+        Env<AttrContext> env = typeEnvs.get(p);
+        chk.checkDeprecatedAnnotation(((JCPackageDecl) env.tree).pid.pos(), p);
+    }
+
     public void attribModule(DiagnosticPosition pos, ModuleSymbol m) {
         try {
             annotate.flush();
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Thu Aug 11 17:02:00 2016 +0000
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Fri Aug 12 11:49:18 2016 +0530
@@ -3197,6 +3197,21 @@
             log.warning(LintCategory.DEP_ANN,
                     pos, "missing.deprecated.annotation");
         }
+        // Note: @Deprecated has no effect on local variables, parameters and package decls.
+        if (lint.isEnabled(LintCategory.DEPRECATION)) {
+            if (!syms.deprecatedType.isErroneous() && s.attribute(syms.deprecatedType.tsym) != null) {
+                switch (s.getKind()) {
+                    case LOCAL_VARIABLE:
+                    case PACKAGE:
+                    case PARAMETER:
+                    case RESOURCE_VARIABLE:
+                    case EXCEPTION_PARAMETER:
+                        log.warning(LintCategory.DEPRECATION, pos,
+                                "deprecated.annotation.has.no.effect", Kinds.kindName(s));
+                        break;
+                }
+            }
+        }
     }
 
     void checkDeprecated(final DiagnosticPosition pos, final Symbol other, final Symbol s) {
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Aug 11 17:02:00 2016 +0000
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Aug 12 11:49:18 2016 +0530
@@ -1654,6 +1654,10 @@
 compiler.warn.missing.deprecated.annotation=\
     deprecated item is not annotated with @Deprecated
 
+# 0: symbol kind
+compiler.warn.deprecated.annotation.has.no.effect=\
+    @Deprecated annotation has no effect on this {0} declaration
+
 compiler.warn.invalid.archive.file=\
     Unexpected file on path: {0}
 
--- a/langtools/src/jdk.javadoc/share/classes/com/sun/javadoc/package-info.java	Thu Aug 11 17:02:00 2016 +0000
+++ b/langtools/src/jdk.javadoc/share/classes/com/sun/javadoc/package-info.java	Fri Aug 12 11:49:18 2016 +0530
@@ -150,5 +150,4 @@
 @see com.sun.javadoc.Doclet
 @see com.sun.javadoc.RootDoc
 */
-@Deprecated
 package com.sun.javadoc;
--- a/langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/package-info.java	Thu Aug 11 17:02:00 2016 +0000
+++ b/langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/package-info.java	Fri Aug 12 11:49:18 2016 +0530
@@ -32,5 +32,4 @@
  * {@code javax.tools.DocumentationTool}
  * for replacement functionality.
  */
-@Deprecated
 package com.sun.tools.javadoc;
--- a/langtools/test/tools/javac/T6404756.out	Thu Aug 11 17:02:00 2016 +0000
+++ b/langtools/test/tools/javac/T6404756.out	Fri Aug 12 11:49:18 2016 +0530
@@ -1,4 +1,5 @@
+T6404756.java:10:28: compiler.warn.deprecated.annotation.has.no.effect: kindname.variable
 T6404756.java:10:34: compiler.warn.has.been.deprecated: foo, Foo
 - compiler.err.warnings.and.werror
 1 error
-1 warning
+2 warnings
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/annotations/neg/DeprecatedAnnotationTest/DeprecatedAnnotationTest.out	Fri Aug 12 11:49:18 2016 +0530
@@ -0,0 +1,8 @@
+package-info.java:10:9: compiler.warn.deprecated.annotation.has.no.effect: kindname.package
+- compiler.err.warnings.and.werror
+package-info.java:14:30: compiler.warn.deprecated.annotation.has.no.effect: kindname.variable
+package-info.java:16:25: compiler.warn.deprecated.annotation.has.no.effect: kindname.variable
+package-info.java:18:51: compiler.warn.deprecated.annotation.has.no.effect: kindname.variable
+package-info.java:20:40: compiler.warn.deprecated.annotation.has.no.effect: kindname.variable
+1 error
+5 warnings
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/annotations/neg/DeprecatedAnnotationTest/package-info.java	Fri Aug 12 11:49:18 2016 +0530
@@ -0,0 +1,30 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8068626
+ * @summary Add javac lint warning when the Deprecated annotation is used where it is a no-op
+ *
+ * @compile/fail/ref=DeprecatedAnnotationTest.out -Werror -Xlint:deprecation -XDrawDiagnostics package-info.java
+ */
+
+@Deprecated
+package p;
+
+class DeprecatedAnnotationTest implements AutoCloseable {
+
+    void foo(@Deprecated int p) {
+
+        @Deprecated int l;
+
+        try (@Deprecated DeprecatedAnnotationTest r = new DeprecatedAnnotationTest()) {
+            // ...
+        } catch (@Deprecated Exception e) {
+
+        }
+    }
+
+    @Override
+    public void close() throws Exception {
+        @SuppressWarnings("deprecation")  // verify that we are able to suppress.
+        @Deprecated int x;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/DeprecatedAnnotationHasNoEffect/DeprecatedAnnotationHasNoEffect.java	Fri Aug 12 11:49:18 2016 +0530
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// options: -Xlint:deprecation
+// key: compiler.warn.deprecated.annotation.has.no.effect
+
+class DeprecatedAnnotationHasNoEffect {
+    void foo(@Deprecated int p) {}
+}
--- a/langtools/test/tools/javac/warnings/suppress/T6480588.out	Thu Aug 11 17:02:00 2016 +0000
+++ b/langtools/test/tools/javac/warnings/suppress/T6480588.out	Fri Aug 12 11:49:18 2016 +0530
@@ -8,6 +8,7 @@
 T6480588.java:19:9: compiler.warn.has.been.deprecated: DeprecatedClass, compiler.misc.unnamed.package
 T6480588.java:19:34: compiler.warn.has.been.deprecated: DeprecatedClass, compiler.misc.unnamed.package
 T6480588.java:21:9: compiler.warn.has.been.deprecated: DeprecatedClass, compiler.misc.unnamed.package
+T6480588.java:21:25: compiler.warn.deprecated.annotation.has.no.effect: kindname.variable
 T6480588.java:21:35: compiler.warn.has.been.deprecated: DeprecatedClass, compiler.misc.unnamed.package
 T6480588.java:30:5: compiler.warn.has.been.deprecated: DeprecatedClass, compiler.misc.unnamed.package
 T6480588.java:29:6: compiler.warn.has.been.deprecated: DeprecatedAnnotation, compiler.misc.unnamed.package
@@ -15,4 +16,4 @@
 T6480588.java:33:25: compiler.warn.has.been.deprecated: DeprecatedClass, compiler.misc.unnamed.package
 T6480588.java:33:52: compiler.warn.has.been.deprecated: DeprecatedInterface, compiler.misc.unnamed.package
 T6480588.java:32:6: compiler.warn.has.been.deprecated: DeprecatedAnnotation, compiler.misc.unnamed.package
-17 warnings
+18 warnings