8150096: Cleanup synthetic JCCompilationUnit for html files
authorksrini
Thu, 18 Feb 2016 12:48:11 -0800
changeset 36045 9643bffe2105
parent 36044 8d48ff7e2cf2
child 36046 d43a092d6be5
8150096: Cleanup synthetic JCCompilationUnit for html files Reviewed-by: jjg
langtools/src/jdk.compiler/share/classes/com/sun/tools/doclint/Checker.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/JavadocClassFinder.java
langtools/test/jdk/javadoc/doclet/testPackageHtml/TestPackageHtml.java
langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg1/X.java
langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg1/package.html
langtools/test/jdk/javadoc/doclet/testWarnings/TestWarnings.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/doclint/Checker.java	Thu Feb 18 13:42:59 2016 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/doclint/Checker.java	Thu Feb 18 12:48:11 2016 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -149,13 +149,13 @@
         env.setCurrent(p, tree);
 
         boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty();
+        JavaFileObject fo = p.getCompilationUnit().getSourceFile();
 
         if (p.getLeaf().getKind() == Tree.Kind.PACKAGE) {
             // If p points to a package, the implied declaration is the
             // package declaration (if any) for the compilation unit.
             // Handle this case specially, because doc comments are only
             // expected in package-info files.
-            JavaFileObject fo = p.getCompilationUnit().getSourceFile();
             boolean isPkgInfo = fo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
             if (tree == null) {
                 if (isPkgInfo)
@@ -165,6 +165,12 @@
                 if (!isPkgInfo)
                     reportReference("dc.unexpected.comment");
             }
+        } else if (tree != null && fo.isNameCompatible("package", JavaFileObject.Kind.HTML)) {
+            // a package.html file with a DocCommentTree
+            if (tree.getFullBody().isEmpty()) {
+                reportMissing("dc.missing.comment");
+                return null;
+            }
         } else {
             if (tree == null) {
                 if (!isSynthetic() && !isOverridingMethod)
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java	Thu Feb 18 13:42:59 2016 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java	Thu Feb 18 12:48:11 2016 -0800
@@ -28,7 +28,9 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.text.BreakIterator;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -68,12 +70,16 @@
 import com.sun.source.util.JavacTask;
 import com.sun.source.util.TreePath;
 import com.sun.tools.javac.code.Flags;
+import com.sun.tools.javac.code.Scope.NamedImportScope;
+import com.sun.tools.javac.code.Scope.StarImportScope;
+import com.sun.tools.javac.code.Scope.WriteableScope;
 import com.sun.tools.javac.code.Symbol;
 import com.sun.tools.javac.code.Symbol.ClassSymbol;
 import com.sun.tools.javac.code.Symbol.MethodSymbol;
 import com.sun.tools.javac.code.Symbol.PackageSymbol;
 import com.sun.tools.javac.code.Symbol.TypeSymbol;
 import com.sun.tools.javac.code.Symbol.VarSymbol;
+import com.sun.tools.javac.code.Symtab;
 import com.sun.tools.javac.code.Type;
 import com.sun.tools.javac.code.Type.ArrayType;
 import com.sun.tools.javac.code.Type.ClassType;
@@ -103,6 +109,7 @@
 import com.sun.tools.javac.tree.DCTree.DCParam;
 import com.sun.tools.javac.tree.DCTree.DCReference;
 import com.sun.tools.javac.tree.DCTree.DCText;
+import com.sun.tools.javac.tree.DocCommentTable;
 import com.sun.tools.javac.tree.DocTreeMaker;
 import com.sun.tools.javac.tree.EndPosTable;
 import com.sun.tools.javac.tree.JCTree;
@@ -163,6 +170,8 @@
     private BreakIterator breakIterator;
     private JavaFileManager fileManager;
     private ParserFactory parser;
+    private Symtab syms;
+    private Map<JavaFileObject, PackageSymbol> javaFileObjectToPackageMap;
 
     // called reflectively from Trees.instance(CompilationTask task)
     public static JavacTrees instance(JavaCompiler.CompilationTask task) {
@@ -186,6 +195,7 @@
     }
 
     protected JavacTrees(Context context) {
+        javaFileObjectToPackageMap = new HashMap<>();
         this.breakIterator = null;
         context.put(JavacTrees.class, this);
         init(context);
@@ -207,6 +217,7 @@
         types = Types.instance(context);
         docTreeMaker = DocTreeMaker.instance(context);
         parser = ParserFactory.instance(context);
+        syms = Symtab.instance(context);
         fileManager = context.get(JavaFileManager.class);
         JavacTask t = context.get(JavacTask.class);
         if (t instanceof JavacTaskImpl)
@@ -1018,7 +1029,8 @@
     @Override @DefinedBy(Api.COMPILER_TREE)
     public DocTreePath getDocTreePath(FileObject fileObject) {
         JavaFileObject jfo = asJavaFileObject(fileObject);
-        return new DocTreePath(makeTreePath(jfo), getDocCommentTree(jfo));
+        DocCommentTree docCommentTree = getDocCommentTree(jfo);
+        return new DocTreePath(makeTreePath(jfo, docCommentTree), docCommentTree);
     }
 
     @Override @DefinedBy(Api.COMPILER_TREE)
@@ -1136,7 +1148,17 @@
         }
     }
 
-    private TreePath makeTreePath(final JavaFileObject jfo) {
+    /**
+     * Register a file object, such as for a package.html, that provides
+     * doc comments for a package.
+     * @param psym the PackageSymbol representing the package.
+     * @param jfo  the JavaFileObject for the given package.
+     */
+    public void putJavaFileObject(PackageSymbol psym, JavaFileObject jfo) {
+        javaFileObjectToPackageMap.putIfAbsent(jfo, psym);
+    }
+
+    private TreePath makeTreePath(final JavaFileObject jfo, DocCommentTree dcTree) {
         JCCompilationUnit jcCompilationUnit = new JCCompilationUnit(List.nil()) {
             public int getPos() {
                 return Position.FIRSTPOS;
@@ -1156,8 +1178,44 @@
                 return null;
             }
         };
+
+        PackageSymbol psym = javaFileObjectToPackageMap.getOrDefault(jfo, syms.unnamedPackage);
+
+        jcCompilationUnit.docComments = new DocCommentTable() {
+            @Override
+            public boolean hasComment(JCTree tree) {
+                return false;
+            }
+
+            @Override
+            public Comment getComment(JCTree tree) {
+                throw new UnsupportedOperationException();
+            }
+
+            @Override
+            public String getCommentText(JCTree tree) {
+                throw new UnsupportedOperationException();
+            }
+
+            @Override
+            public DCDocComment getCommentTree(JCTree tree) {
+                return (DCDocComment)dcTree;
+            }
+
+            @Override
+            public void putComment(JCTree tree, Comment c) {
+                throw new UnsupportedOperationException();
+            }
+
+        };
+        jcCompilationUnit.lineMap = jcCompilationUnit.getLineMap();
+        jcCompilationUnit.namedImportScope = new NamedImportScope(psym, jcCompilationUnit.toplevelScope);
+        jcCompilationUnit.packge = psym;
+        jcCompilationUnit.starImportScope = null;
         jcCompilationUnit.sourcefile = jfo;
-        enter.main(List.of(jcCompilationUnit));
+        jcCompilationUnit.starImportScope = new StarImportScope(psym);
+        jcCompilationUnit.toplevelScope = WriteableScope.create(psym);
+
         return new TreePath(jcCompilationUnit);
     }
 }
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/JavadocClassFinder.java	Thu Feb 18 13:42:59 2016 -0800
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/JavadocClassFinder.java	Thu Feb 18 12:48:11 2016 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
 
 import javax.tools.JavaFileObject;
 
+import com.sun.tools.javac.api.JavacTrees;
 import com.sun.tools.javac.code.Symbol.PackageSymbol;
 import com.sun.tools.javac.code.ClassFinder;
 import com.sun.tools.javac.util.Context;
@@ -66,10 +67,13 @@
     private EnumSet<JavaFileObject.Kind> noSource = EnumSet.of(JavaFileObject.Kind.CLASS,
                                                                JavaFileObject.Kind.HTML);
 
+    private final JavacTrees trees;
+
     public JavadocClassFinder(Context context) {
         super(context);
         docenv = DocEnv.instance(context);
         preferSource = true;
+        trees = JavacTrees.instance(context);
     }
 
     /**
@@ -85,7 +89,9 @@
      */
     @Override
     protected void extraFileActions(PackageSymbol pack, JavaFileObject fo) {
-        if (fo.isNameCompatible("package", JavaFileObject.Kind.HTML))
+        if (fo.isNameCompatible("package", JavaFileObject.Kind.HTML)) {
             docenv.pkgToJavaFOMap.put(pack, fo);
+            trees.putJavaFileObject(pack, fo);
+        }
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/javadoc/doclet/testPackageHtml/TestPackageHtml.java	Thu Feb 18 12:48:11 2016 -0800
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug      8150096
+ * @summary  Make sure package.html is recognized by doclint
+ * @library  ../lib
+ * @modules jdk.javadoc/jdk.javadoc.internal.tool
+ * @build    JavadocTester
+ * @run main TestPackageHtml
+ */
+
+public class TestPackageHtml extends JavadocTester {
+    public static void main(String... args) throws Exception  {
+        TestPackageHtml tester = new TestPackageHtml();
+        tester.runTests();
+    }
+
+    @Test
+    void testPackageHtml() {
+        javadoc("-d", "out-pkg-html",
+                "-sourcepath", testSrc,
+                "pkg1");
+        checkExit(Exit.FAILED);
+        checkOutput(Output.OUT, true, "package.html:10: error: bad use of '>'");
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg1/X.java	Thu Feb 18 12:48:11 2016 -0800
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package pkg1;
+
+/**
+ * An empty class.
+ */
+public class X {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg1/package.html	Thu Feb 18 12:48:11 2016 -0800
@@ -0,0 +1,9 @@
+<HTML>
+    <BODY>
+        <!-- generate a syntax error to verify that the file is parsed -->
+        <pre>
+            &lt;opaque value="TRUE"/>
+        </pre>
+    </BODY>
+</HTML>
+
--- a/langtools/test/jdk/javadoc/doclet/testWarnings/TestWarnings.java	Thu Feb 18 13:42:59 2016 -0800
+++ b/langtools/test/jdk/javadoc/doclet/testWarnings/TestWarnings.java	Thu Feb 18 12:48:11 2016 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -31,7 +31,7 @@
  *           Make sure error message starts with "error -".
  * @author   jamieh
  * @library  ../lib
- * @modules jdk.javadoc
+ * @modules jdk.javadoc/jdk.javadoc.internal.tool
  * @build    JavadocTester
  * @run main TestWarnings
  */
@@ -61,11 +61,6 @@
         checkOutput(Output.OUT, true,
                 "X.java:26: error: self-closing element not allowed");
 
-        /* DCErroneous
-        checkOutput(Output.OUT, true,
-                "package.html: error - Body tag missing from HTML");
-        */
-
         checkOutput("pkg/X.html", false,
                 "can't find m()");
         checkOutput("pkg/X.html", false,