From 1af5bd623247340cdc3c6fbbbb145da67ed8a066 Mon Sep 17 00:00:00 2001 From: emc Date: Mon, 9 Sep 2013 16:26:55 -0400 Subject: [PATCH] 8022322: Reject default and static methods in annotation Summary: Causes javac to reject static and default method declarations inside an annotation Reviewed-by: jjg --- src/share/classes/com/sun/tools/javac/code/Flags.java | 3 ++- src/share/classes/com/sun/tools/javac/comp/Check.java | 9 ++++++--- test/tools/javac/annotations/neg/NoDefault.java | 9 +++++++++ test/tools/javac/annotations/neg/NoDefault.out | 3 +++ .../tools/javac/annotations/neg/NoDefaultAbstract.java | 9 +++++++++ test/tools/javac/annotations/neg/NoDefaultAbstract.out | 2 ++ test/tools/javac/annotations/neg/NoStatic.java | 10 ++++++++++ test/tools/javac/annotations/neg/NoStatic.out | 3 +++ test/tools/javac/annotations/neg/NoStaticAbstract.java | 10 ++++++++++ test/tools/javac/annotations/neg/NoStaticAbstract.out | 2 ++ 10 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 test/tools/javac/annotations/neg/NoDefault.java create mode 100644 test/tools/javac/annotations/neg/NoDefault.out create mode 100644 test/tools/javac/annotations/neg/NoDefaultAbstract.java create mode 100644 test/tools/javac/annotations/neg/NoDefaultAbstract.out create mode 100644 test/tools/javac/annotations/neg/NoStatic.java create mode 100644 test/tools/javac/annotations/neg/NoStatic.out create mode 100644 test/tools/javac/annotations/neg/NoStaticAbstract.java create mode 100644 test/tools/javac/annotations/neg/NoStaticAbstract.out diff --git a/src/share/classes/com/sun/tools/javac/code/Flags.java b/src/share/classes/com/sun/tools/javac/code/Flags.java index b2da2807..3a4084ed 100644 --- a/src/share/classes/com/sun/tools/javac/code/Flags.java +++ b/src/share/classes/com/sun/tools/javac/code/Flags.java @@ -97,7 +97,6 @@ public class Flags { 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 @@ public class Flags { 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; diff --git a/src/share/classes/com/sun/tools/javac/comp/Check.java b/src/share/classes/com/sun/tools/javac/comp/Check.java index 5529627e..59d6c7b4 100644 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java @@ -1050,6 +1050,7 @@ public class Check { 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 @@ public class Check { } 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 @@ public class Check { } else { mask = implicit = InterfaceMethodFlags; } - } - else { + } else { mask = MethodFlags; } // Imply STRICTFP if owner has STRICTFP set. diff --git a/test/tools/javac/annotations/neg/NoDefault.java b/test/tools/javac/annotations/neg/NoDefault.java new file mode 100644 index 00000000..282bc98b --- /dev/null +++ b/test/tools/javac/annotations/neg/NoDefault.java @@ -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;} +} diff --git a/test/tools/javac/annotations/neg/NoDefault.out b/test/tools/javac/annotations/neg/NoDefault.out new file mode 100644 index 00000000..ffcbf823 --- /dev/null +++ b/test/tools/javac/annotations/neg/NoDefault.out @@ -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 diff --git a/test/tools/javac/annotations/neg/NoDefaultAbstract.java b/test/tools/javac/annotations/neg/NoDefaultAbstract.java new file mode 100644 index 00000000..6c288810 --- /dev/null +++ b/test/tools/javac/annotations/neg/NoDefaultAbstract.java @@ -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(); +} diff --git a/test/tools/javac/annotations/neg/NoDefaultAbstract.out b/test/tools/javac/annotations/neg/NoDefaultAbstract.out new file mode 100644 index 00000000..ea445c68 --- /dev/null +++ b/test/tools/javac/annotations/neg/NoDefaultAbstract.out @@ -0,0 +1,2 @@ +NoDefaultAbstract.java:8:17: compiler.err.mod.not.allowed.here: default +1 error diff --git a/test/tools/javac/annotations/neg/NoStatic.java b/test/tools/javac/annotations/neg/NoStatic.java new file mode 100644 index 00000000..3a62dc7c --- /dev/null +++ b/test/tools/javac/annotations/neg/NoStatic.java @@ -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;} +} diff --git a/test/tools/javac/annotations/neg/NoStatic.out b/test/tools/javac/annotations/neg/NoStatic.out new file mode 100644 index 00000000..8d9d3131 --- /dev/null +++ b/test/tools/javac/annotations/neg/NoStatic.out @@ -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 diff --git a/test/tools/javac/annotations/neg/NoStaticAbstract.java b/test/tools/javac/annotations/neg/NoStaticAbstract.java new file mode 100644 index 00000000..af27fa75 --- /dev/null +++ b/test/tools/javac/annotations/neg/NoStaticAbstract.java @@ -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(); +} diff --git a/test/tools/javac/annotations/neg/NoStaticAbstract.out b/test/tools/javac/annotations/neg/NoStaticAbstract.out new file mode 100644 index 00000000..694cea17 --- /dev/null +++ b/test/tools/javac/annotations/neg/NoStaticAbstract.out @@ -0,0 +1,2 @@ +NoStaticAbstract.java:9:16: compiler.err.mod.not.allowed.here: static +1 error -- GitLab