changeset 2992:7f25537496ff

8073534: Confusing / incorrect error message regarding annotations on non-declarations Summary: Adjusted error message for annotations on non-declarations. Reviewed-by: jlahoda, dlsmith
author alundblad
date Fri, 17 Apr 2015 11:52:10 +0200
parents 5cd4dba2e742
children 4348bf94591c
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties test/tools/javac/annotations/typeAnnotations/DeclVsUseErrorMessage.java test/tools/javac/annotations/typeAnnotations/DeclVsUseErrorMessage.out test/tools/javac/annotations/typeAnnotations/failures/TypeOnAnonClass.out test/tools/javac/annotations/typeAnnotations/failures/common/arrays/DeclarationAnnotation.out test/tools/javac/annotations/typeAnnotations/failures/common/arrays/InvalidLocation.out test/tools/javac/annotations/typeAnnotations/failures/common/innertypeparams/InvalidLocation.out test/tools/javac/annotations/typeAnnotations/failures/common/newarray/InvalidLocation.out test/tools/javac/annotations/typeAnnotations/failures/common/parambounds/BrokenAnnotation.out test/tools/javac/annotations/typeAnnotations/failures/common/parambounds/InvalidLocation.out test/tools/javac/annotations/typeAnnotations/failures/common/receiver/DeclarationAnnotation.out test/tools/javac/annotations/typeAnnotations/failures/common/rest/InvalidLocation.out test/tools/javac/annotations/typeAnnotations/failures/common/typeArgs/InvalidLocation.out test/tools/javac/annotations/typeAnnotations/failures/common/typeparams/InvalidLocation.out test/tools/javac/annotations/typeAnnotations/failures/common/wildcards/DeclarationAnnotation.out test/tools/javac/annotations/typeAnnotations/failures/common/wildcards/InvalidLocation.out test/tools/javac/diags/examples/TypeAnnoNotApplicableInTypeContext.java
diffstat 19 files changed, 100 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Apr 16 17:20:20 2015 -0600
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Apr 17 11:52:10 2015 +0200
@@ -4657,7 +4657,7 @@
             for (JCAnnotation ai : annotations) {
                 if (!ai.type.isErroneous() &&
                         typeAnnotations.annotationTargetType(ai.attribute, sym) == TypeAnnotations.AnnotationType.DECLARATION) {
-                    log.error(ai.pos(), "annotation.type.not.applicable");
+                    log.error(ai.pos(), Errors.AnnotationTypeNotApplicableToType(ai.type));
                 }
             }
         }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Thu Apr 16 17:20:20 2015 -0600
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Fri Apr 17 11:52:10 2015 +0200
@@ -2783,7 +2783,7 @@
         if (a.hasTag(TYPE_ANNOTATION) &&
                 !a.annotationType.type.isErroneous() &&
                 !isTypeAnnotation(a, isTypeParameter)) {
-            log.error(a.pos(), "annotation.type.not.applicable");
+            log.error(a.pos(), Errors.AnnotationTypeNotApplicableToType(a.type));
         }
     }
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Apr 16 17:20:20 2015 -0600
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Apr 17 11:52:10 2015 +0200
@@ -123,6 +123,10 @@
 compiler.err.annotation.type.not.applicable=\
     annotation type not applicable to this kind of declaration
 
+# 0: type
+compiler.err.annotation.type.not.applicable.to.type=\
+    annotation @{0} not applicable in this type context
+
 compiler.err.annotation.value.must.be.annotation=\
     annotation value must be an annotation
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/annotations/typeAnnotations/DeclVsUseErrorMessage.java	Fri Apr 17 11:52:10 2015 +0200
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2008, 2015, 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.
+ */
+
+/*
+ * @test
+ * @bug 8073534
+ * @summary Check for correct type annotation error messages.
+ * @compile/fail/ref=DeclVsUseErrorMessage.out -XDrawDiagnostics DeclVsUseErrorMessage.java
+ */
+
+import java.lang.annotation.*;
+import java.util.ArrayList;
+
+class DeclVsUseErrorMessage {
+
+    @Target(ElementType.METHOD)
+    @interface A {}
+
+    // Should trigger a "decl" warning
+    @A int i;
+
+    // Should trigger a "use" warning
+    {
+        new ArrayList<@A String>();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/annotations/typeAnnotations/DeclVsUseErrorMessage.out	Fri Apr 17 11:52:10 2015 +0200
@@ -0,0 +1,3 @@
+DeclVsUseErrorMessage.java:40:5: compiler.err.annotation.type.not.applicable
+DeclVsUseErrorMessage.java:44:23: compiler.err.annotation.type.not.applicable.to.type: DeclVsUseErrorMessage.A
+2 errors
--- a/test/tools/javac/annotations/typeAnnotations/failures/TypeOnAnonClass.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/TypeOnAnonClass.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,2 +1,2 @@
-TypeOnAnonClass.java:13:40: compiler.err.annotation.type.not.applicable
+TypeOnAnonClass.java:13:40: compiler.err.annotation.type.not.applicable.to.type: X
 1 error
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/arrays/DeclarationAnnotation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/arrays/DeclarationAnnotation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,5 +1,5 @@
-DeclarationAnnotation.java:13:21: compiler.err.annotation.type.not.applicable
-DeclarationAnnotation.java:10:21: compiler.err.annotation.type.not.applicable
-DeclarationAnnotation.java:11:21: compiler.err.annotation.type.not.applicable
-DeclarationAnnotation.java:12:21: compiler.err.annotation.type.not.applicable
+DeclarationAnnotation.java:13:21: compiler.err.annotation.type.not.applicable.to.type: DA
+DeclarationAnnotation.java:10:21: compiler.err.annotation.type.not.applicable.to.type: DA
+DeclarationAnnotation.java:11:21: compiler.err.annotation.type.not.applicable.to.type: DA
+DeclarationAnnotation.java:12:21: compiler.err.annotation.type.not.applicable.to.type: DA
 4 errors
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/arrays/InvalidLocation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/arrays/InvalidLocation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,2 +1,2 @@
-InvalidLocation.java:11:12: compiler.err.annotation.type.not.applicable
+InvalidLocation.java:11:12: compiler.err.annotation.type.not.applicable.to.type: A
 1 error
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/innertypeparams/InvalidLocation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/innertypeparams/InvalidLocation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,2 +1,2 @@
-InvalidLocation.java:10:17: compiler.err.annotation.type.not.applicable
+InvalidLocation.java:10:17: compiler.err.annotation.type.not.applicable.to.type: A
 1 error
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/newarray/InvalidLocation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/newarray/InvalidLocation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,2 +1,2 @@
-InvalidLocation.java:11:29: compiler.err.annotation.type.not.applicable
+InvalidLocation.java:11:29: compiler.err.annotation.type.not.applicable.to.type: A
 1 error
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/parambounds/BrokenAnnotation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/parambounds/BrokenAnnotation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,5 +1,5 @@
 BrokenAnnotation.java:16:6: compiler.err.cant.resolve.location: kindname.class, Target, , , (compiler.misc.location: kindname.class, BrokenAnnotation<T>, null)
 BrokenAnnotation.java:16:14: compiler.err.cant.resolve.location: kindname.variable, ElementType, , , (compiler.misc.location: kindname.class, BrokenAnnotation<T>, null)
 BrokenAnnotation.java:16:36: compiler.err.cant.resolve.location: kindname.variable, ElementType, , , (compiler.misc.location: kindname.class, BrokenAnnotation<T>, null)
-BrokenAnnotation.java:15:34: compiler.err.annotation.type.not.applicable
+BrokenAnnotation.java:15:34: compiler.err.annotation.type.not.applicable.to.type: BrokenAnnotation.A
 4 errors
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/parambounds/InvalidLocation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/parambounds/InvalidLocation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,2 +1,2 @@
-InvalidLocation.java:9:33: compiler.err.annotation.type.not.applicable
+InvalidLocation.java:9:33: compiler.err.annotation.type.not.applicable.to.type: A
 1 error
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/receiver/DeclarationAnnotation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/receiver/DeclarationAnnotation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,2 +1,2 @@
-DeclarationAnnotation.java:14:14: compiler.err.annotation.type.not.applicable
+DeclarationAnnotation.java:14:14: compiler.err.annotation.type.not.applicable.to.type: DA
 1 error
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/rest/InvalidLocation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/rest/InvalidLocation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,2 +1,2 @@
-InvalidLocation.java:11:9: compiler.err.annotation.type.not.applicable
+InvalidLocation.java:11:9: compiler.err.annotation.type.not.applicable.to.type: A
 1 error
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/typeArgs/InvalidLocation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/typeArgs/InvalidLocation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,2 +1,2 @@
-InvalidLocation.java:10:19: compiler.err.annotation.type.not.applicable
+InvalidLocation.java:10:19: compiler.err.annotation.type.not.applicable.to.type: A
 1 error
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/typeparams/InvalidLocation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/typeparams/InvalidLocation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,2 +1,2 @@
-InvalidLocation.java:9:23: compiler.err.annotation.type.not.applicable
+InvalidLocation.java:9:23: compiler.err.annotation.type.not.applicable.to.type: A
 1 error
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/wildcards/DeclarationAnnotation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/wildcards/DeclarationAnnotation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,2 +1,2 @@
-DeclarationAnnotation.java:15:10: compiler.err.annotation.type.not.applicable
+DeclarationAnnotation.java:15:10: compiler.err.annotation.type.not.applicable.to.type: DA
 1 error
--- a/test/tools/javac/annotations/typeAnnotations/failures/common/wildcards/InvalidLocation.out	Thu Apr 16 17:20:20 2015 -0600
+++ b/test/tools/javac/annotations/typeAnnotations/failures/common/wildcards/InvalidLocation.out	Fri Apr 17 11:52:10 2015 +0200
@@ -1,2 +1,2 @@
-InvalidLocation.java:10:19: compiler.err.annotation.type.not.applicable
+InvalidLocation.java:10:19: compiler.err.annotation.type.not.applicable.to.type: A
 1 error
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/TypeAnnoNotApplicableInTypeContext.java	Fri Apr 17 11:52:10 2015 +0200
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2015, 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.
+ */
+
+// key: compiler.err.annotation.type.not.applicable.to.type
+
+class TypeAnnoNotApplicableInTypeContext<T> {
+    @interface A { }
+    TypeAnnoNotApplicableInTypeContext<@A String> m;
+}