changeset 3382:4ccabc2f6346

8153268: javac accepts enums being referenced by 'uses' statement Reviewed-by: jjg
author vromero
date Fri, 06 May 2016 16:06:27 -0400
parents eddb2c2a831d
children e043221cf018
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties test/tools/javac/diags/examples.not-yet.txt test/tools/javac/modules/UsesTest.java
diffstat 4 files changed, 33 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Thu May 05 16:36:00 2016 -0700
+++ b/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/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu May 05 16:36:00 2016 -0700
+++ b/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/test/tools/javac/diags/examples.not-yet.txt	Thu May 05 16:36:00 2016 -0700
+++ b/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/test/tools/javac/modules/UsesTest.java	Thu May 05 16:36:00 2016 -0700
+++ b/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,