changeset 3503:b4ca7d930f0f

Fix bug entering duplicate cass symbols Reviewed-by: jjg
author jlahoda
date Tue, 15 Dec 2015 13:00:02 -0800
parents 041dafa89238
children 088c841e9b42
files src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java test/tools/javac/modules/EdgeCases.java test/tools/lib/ModuleTestBase.java
diffstat 3 files changed, 75 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java	Thu Dec 10 15:45:59 2015 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java	Tue Dec 15 13:00:02 2015 -0800
@@ -666,31 +666,6 @@
         return () -> Iterators.createCompoundIterator(classes.values(), v -> v.values().iterator());
     }
 
-    private ClassSymbol enterClass(ModuleSymbol msym, Name flatName, JavaFileObject classFile) {
-        Assert.checkNonNull(msym);
-        ClassSymbol cs = getClass(msym, flatName);
-        if (cs != null) {
-            String msg = Log.format("%s: completer = %s; class file = %s; source file = %s",
-                                    cs.fullname,
-                                    cs.completer,
-                                    cs.classfile,
-                                    cs.sourcefile);
-            throw new AssertionError(msg);
-        }
-        Name packageName = Convert.packagePart(flatName);
-        PackageSymbol owner;
-
-        if (packageName.isEmpty()) {
-            owner = msym.unnamedPackage;
-         } else {
-           owner = lookupPackage(msym, packageName);
-        }
-        cs = defineClass(Convert.shortName(flatName), owner);
-        cs.classfile = classFile;
-        doEnterClass(msym, cs);
-        return cs;
-    }
-
     private void doEnterClass(ModuleSymbol msym, ClassSymbol cs) {
         classes.computeIfAbsent(cs.flatname, n -> new HashMap<>()).put(msym, cs);
     }
@@ -705,7 +680,9 @@
         Assert.checkNonNull(ps.modle);
         ClassSymbol c = getClass(ps.modle, flatname);
         if (c == null) {
-            return enterClass(ps.modle, flatname, (JavaFileObject)null);
+            c = defineClass(Convert.shortName(flatname), ps);
+            doEnterClass(ps.modle, c);
+            return c;
         } else
             return c;
     }
--- a/test/tools/javac/modules/EdgeCases.java	Thu Dec 10 15:45:59 2015 -0800
+++ b/test/tools/javac/modules/EdgeCases.java	Tue Dec 15 13:00:02 2015 -0800
@@ -33,6 +33,7 @@
  * @run main EdgeCases
  */
 
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.HashSet;
@@ -171,4 +172,63 @@
             throw new Exception("expected output not found");
     }
 
+    @Test
+    void testAssignClassToAutomaticModule(Path base) throws Exception {
+        //check that if a ClassSymbol belongs to an automatic module, it is properly assigned and not
+        //duplicated when being accessed through a classfile.
+        Path automaticSrc = base.resolve("automaticSrc");
+        tb.writeJavaFiles(automaticSrc, "package api1; public class Api1 {}");
+        Path automaticClasses = base.resolve("automaticClasses");
+        tb.createDirectories(automaticClasses);
+
+        String automaticLog = tb.new JavacTask()
+                                .outdir(automaticClasses)
+                                .files(findJavaFiles(automaticSrc))
+                                .run()
+                                .writeAll()
+                                .getOutput(ToolBox.OutputKind.DIRECT);
+
+        if (!automaticLog.isEmpty())
+            throw new Exception("expected output not found: " + automaticLog);
+
+        Path modulePath = base.resolve("module-path");
+
+        Files.createDirectories(modulePath);
+
+        Path automaticJar = modulePath.resolve("m1-1.0.jar");
+
+        tb.new JarTask(automaticJar)
+          .baseDir(automaticClasses)
+          .files("api1/Api1.class")
+          .run();
+
+        Path src = base.resolve("src");
+        Path src_m2 = src.resolve("m2");
+        tb.writeJavaFiles(src_m2,
+                          "module m2 { requires m1; exports api2; }",
+                          "package api2; public class Api2 { public static api1.Api1 get() { return null; } }");
+        Path src_m3 = src.resolve("m3");
+        tb.writeJavaFiles(src_m3,
+                          "module m3 { requires m1; requires m2; }",
+                          "package test; public class Test { { api2.Api2.get(); api1.Api1 a1; } }");
+        Path classes = base.resolve("classes");
+        tb.createDirectories(classes);
+
+        tb.new JavacTask()
+                .options("-modulepath", modulePath.toString(),
+                         "-modulesourcepath", src.toString())
+                .outdir(classes)
+                .files(findJavaFiles(src_m2))
+                .run()
+                .writeAll();
+
+        tb.new JavacTask()
+                .options("-modulepath", modulePath.toString(),
+                         "-modulesourcepath", src.toString())
+                .outdir(classes)
+                .files(findJavaFiles(src_m3))
+                .run()
+                .writeAll();
+    }
+
 }
--- a/test/tools/lib/ModuleTestBase.java	Thu Dec 10 15:45:59 2015 -0800
+++ b/test/tools/lib/ModuleTestBase.java	Tue Dec 15 13:00:02 2015 -0800
@@ -84,19 +84,20 @@
 
     // move to ToolBox?
     // change returntyp to List<Path> -- means updating ToolBox methods
-    Path[] findJavaFiles(Path p) throws IOException {
+    Path[] findJavaFiles(Path... paths) throws IOException {
         Set<Path> files = new TreeSet<>();
-        Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
-            @Override
-            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
-                    throws IOException {
-                if (file.getFileName().toString().endsWith(".java")) {
-                    files.add(file);
+        for (Path p : paths) {
+            Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
+                @Override
+                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
+                        throws IOException {
+                    if (file.getFileName().toString().endsWith(".java")) {
+                        files.add(file);
+                    }
+                    return FileVisitResult.CONTINUE;
                 }
-                return FileVisitResult.CONTINUE;
-            }
-
-        });
+            });
+        }
         return files.toArray(new Path[files.size()]);
     }