8022322: Reject default and static methods in annotation
Summary: Causes javac to reject static and default method declarations inside an annotation
Reviewed-by: jjg
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Flags.java Mon Sep 09 16:32:08 2013 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Flags.java Mon Sep 09 16:26:55 2013 -0400
@@ -97,7 +97,6 @@
public static final int MANDATED = 1<<15;
public static final int StandardFlags = 0x0fff;
- public static final int ModifierFlags = StandardFlags & ~INTERFACE;
// Because the following access flags are overloaded with other
// bit positions, we translate them when reading and writing class
@@ -287,7 +286,9 @@
SYNCHRONIZED | FINAL | STRICTFP;
public static final long
ExtendedStandardFlags = (long)StandardFlags | DEFAULT,
+ ModifierFlags = ((long)StandardFlags & ~INTERFACE) | DEFAULT,
InterfaceMethodMask = ABSTRACT | STATIC | PUBLIC | STRICTFP | DEFAULT,
+ AnnotationTypeElementMask = FINAL | ABSTRACT | PUBLIC | STRICTFP,
LocalVarFlags = FINAL | PARAMETER;
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java Mon Sep 09 16:32:08 2013 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java Mon Sep 09 16:26:55 2013 -0400
@@ -1050,6 +1050,7 @@
long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) {
long mask;
long implicit = 0;
+
switch (sym.kind) {
case VAR:
if (sym.owner.kind != TYP)
@@ -1070,7 +1071,10 @@
} else
mask = ConstructorFlags;
} else if ((sym.owner.flags_field & INTERFACE) != 0) {
- if ((flags & (DEFAULT | STATIC)) != 0) {
+ if ((sym.owner.flags_field & ANNOTATION) != 0) {
+ mask = AnnotationTypeElementMask;
+ implicit = PUBLIC | ABSTRACT;
+ } else if ((flags & (DEFAULT | STATIC)) != 0) {
mask = InterfaceMethodMask;
implicit = PUBLIC;
if ((flags & DEFAULT) != 0) {
@@ -1079,8 +1083,7 @@
} else {
mask = implicit = InterfaceMethodFlags;
}
- }
- else {
+ } else {
mask = MethodFlags;
}
// Imply STRICTFP if owner has STRICTFP set.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/annotations/neg/NoDefault.java Mon Sep 09 16:26:55 2013 -0400
@@ -0,0 +1,9 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8022322
+ * @summary Default methods are not allowed in an annotation.
+ * @compile/fail/ref=NoDefault.out -XDrawDiagnostics NoDefault.java
+ */
+@interface NoDefault {
+ default int m() {return 0;}
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/annotations/neg/NoDefault.out Mon Sep 09 16:26:55 2013 -0400
@@ -0,0 +1,3 @@
+NoDefault.java:8:17: compiler.err.mod.not.allowed.here: default
+NoDefault.java:8:21: compiler.err.intf.meth.cant.have.body
+2 errors
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/annotations/neg/NoDefaultAbstract.java Mon Sep 09 16:26:55 2013 -0400
@@ -0,0 +1,9 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8022322
+ * @summary Default methods are not allowed in an annotation.
+ * @compile/fail/ref=NoDefaultAbstract.out -XDrawDiagnostics NoDefaultAbstract.java
+ */
+@interface NoDefaultAbstract {
+ default int m();
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/annotations/neg/NoDefaultAbstract.out Mon Sep 09 16:26:55 2013 -0400
@@ -0,0 +1,2 @@
+NoDefaultAbstract.java:8:17: compiler.err.mod.not.allowed.here: default
+1 error
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/annotations/neg/NoStatic.java Mon Sep 09 16:26:55 2013 -0400
@@ -0,0 +1,10 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8022322
+ * @summary Static methods are not allowed in an annotation.
+ * @compile/fail/ref=NoStatic.out -XDrawDiagnostics NoStatic.java
+ */
+
+@interface NoStatic {
+ static int m() {return 0;}
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/annotations/neg/NoStatic.out Mon Sep 09 16:26:55 2013 -0400
@@ -0,0 +1,3 @@
+NoStatic.java:9:16: compiler.err.mod.not.allowed.here: static
+NoStatic.java:9:20: compiler.err.intf.meth.cant.have.body
+2 errors
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/annotations/neg/NoStaticAbstract.java Mon Sep 09 16:26:55 2013 -0400
@@ -0,0 +1,10 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8022322
+ * @summary Static methods are not allowed in an annotation.
+ * @compile/fail/ref=NoStaticAbstract.out -XDrawDiagnostics NoStaticAbstract.java
+ */
+
+@interface NoStaticAbstract {
+ static int m();
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/annotations/neg/NoStaticAbstract.out Mon Sep 09 16:26:55 2013 -0400
@@ -0,0 +1,2 @@
+NoStaticAbstract.java:9:16: compiler.err.mod.not.allowed.here: static
+1 error