8153268: javac accepts enums being referenced by 'uses' statement
authorvromero
Fri, 06 May 2016 16:06:27 -0400
changeset 37854 a76a06106d02
parent 37853 b4ea8806ad1a
child 37855 d0b5e74a1136
8153268: javac accepts enums being referenced by 'uses' statement Reviewed-by: jjg
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/diags/examples.not-yet.txt
langtools/test/tools/javac/modules/UsesTest.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Thu May 05 16:36:00 2016 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Fri May 06 16:06:27 2016 -0400
@@ -102,6 +102,7 @@
 import com.sun.tools.javac.tree.JCTree.Tag;
 
 import static com.sun.tools.javac.code.Flags.ABSTRACT;
+import static com.sun.tools.javac.code.Flags.ENUM;
 import static com.sun.tools.javac.code.Flags.PUBLIC;
 import static com.sun.tools.javac.tree.JCTree.Tag.MODULEDEF;
 
@@ -753,7 +754,10 @@
         @Override
         public void visitUses(JCUses tree) {
             Type st = attr.attribType(tree.qualid, env, syms.objectType);
-            if (st.hasTag(CLASS)) {
+            Symbol sym = TreeInfo.symbol(tree.qualid);
+            if ((sym.flags() & ENUM) != 0) {
+                log.error(tree.qualid.pos(), Errors.ServiceDefinitionIsEnum(st.tsym));
+            } else if (st.hasTag(CLASS)) {
                 ClassSymbol service = (ClassSymbol) st.tsym;
                 Directive.UsesDirective d = new Directive.UsesDirective(service);
                 if (!allUses.add(d)) {
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu May 05 16:36:00 2016 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri May 06 16:06:27 2016 -0400
@@ -2719,6 +2719,10 @@
     the service definition is an inner class: {0}
 
 # 0: symbol
+compiler.err.service.definition.is.enum=\
+    the service definition is an enum: {0}
+
+# 0: symbol
 compiler.err.service.implementation.doesnt.have.a.no.args.constructor=\
     the service implementation does not have a default constructor: {0}
 
--- a/langtools/test/tools/javac/diags/examples.not-yet.txt	Thu May 05 16:36:00 2016 -0700
+++ b/langtools/test/tools/javac/diags/examples.not-yet.txt	Fri May 06 16:06:27 2016 -0400
@@ -146,6 +146,7 @@
 compiler.err.package.in.other.module
 compiler.err.processorpath.no.processormodulepath
 compiler.err.service.definition.is.inner
+compiler.err.service.definition.is.enum
 compiler.err.service.implementation.doesnt.have.a.no.args.constructor
 compiler.err.service.implementation.is.abstract
 compiler.err.service.implementation.is.inner
--- a/langtools/test/tools/javac/modules/UsesTest.java	Thu May 05 16:36:00 2016 -0700
+++ b/langtools/test/tools/javac/modules/UsesTest.java	Fri May 06 16:06:27 2016 -0400
@@ -82,6 +82,29 @@
     }
 
     @Test
+    public void testEnumAsAService(Path base) throws Exception {
+        Path src = base.resolve("src");
+        tb.writeJavaFiles(src,
+                "module m { uses pkg.EnumST; }",
+                "package pkg; public enum EnumST {A, B}");
+        Path classes = base.resolve("classes");
+        Files.createDirectories(classes);
+
+        List<String> output = new JavacTask(tb)
+                .options("-XDrawDiagnostics")
+                .outdir(classes)
+                .files(tb.findJavaFiles(src))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+        List<String> expected = Arrays.asList("module-info.java:1:20: compiler.err.service.definition.is.enum: pkg.EnumST",
+                "1 error");
+        if (!output.containsAll(expected)) {
+            throw new Exception("Expected output not found");
+        }
+    }
+
+    @Test
     public void testSimpleAnnotation(Path base) throws Exception {
         Path src = base.resolve("src");
         tb.writeJavaFiles(src,