8152616: com.sun.tools.javac.tree.Pretty generates nested comments for enum
authorsdama
Tue, 06 Feb 2018 23:49:10 +0530
changeset 48749 fd40b0b3d849
parent 48748 4d716bc7ed54
child 48750 ffbb784a8873
8152616: com.sun.tools.javac.tree.Pretty generates nested comments for enum Summary: Fixed nested comments for enum Reviewed-by: mcimadamore Contributed-by: srinivas.dama@oracle.com
src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java
test/langtools/tools/javac/T8152616.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java	Mon Feb 05 15:08:46 2018 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java	Tue Feb 06 23:49:10 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2018, 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
@@ -637,19 +637,43 @@
                 print("/*public static final*/ ");
                 print(tree.name);
                 if (tree.init != null) {
-                    if (sourceOutput && tree.init.hasTag(NEWCLASS)) {
-                        print(" /*enum*/ ");
+                    if (tree.init.hasTag(NEWCLASS)) {
                         JCNewClass init = (JCNewClass) tree.init;
-                        if (init.args != null && init.args.nonEmpty()) {
+                        if (sourceOutput) {
+                            print(" /*enum*/ ");
+                            if (init.args != null && init.args.nonEmpty()) {
+                                print("(");
+                                print(init.args);
+                                print(")");
+                            }
+                            if (init.def != null && init.def.defs != null) {
+                                print(" ");
+                                printBlock(init.def.defs);
+                            }
+                            return;
+                        }else {
+                            print(" /* = ");
+                            print("new ");
+                            if (init.def != null && init.def.mods.annotations.nonEmpty()) {
+                                printTypeAnnotations(init.def.mods.annotations);
+                            }
+                            printExpr(init.clazz);
                             print("(");
-                            print(init.args);
+                            printExprs(init.args);
                             print(")");
+                            print(" */");
+                            print(" /*enum*/ ");
+                            if (init.args != null && init.args.nonEmpty()) {
+                                print("(");
+                                printExprs(init.args);
+                                print(")");
+                            }
+                            if (init.def != null && init.def.defs != null) {
+                                print(" ");
+                                printBlock(init.def.defs);
+                            }
+                            return;
                         }
-                        if (init.def != null && init.def.defs != null) {
-                            print(" ");
-                            printBlock(init.def.defs);
-                        }
-                        return;
                     }
                     print(" /* = ");
                     printExpr(tree.init);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8152616.java	Tue Feb 06 23:49:10 2018 +0530
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2018, 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 8152616
+ * @summary Unit test for corner case of PrettyPrinting when SourceOutput is false
+ * @run compile --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED T8152616.java
+ * @run main T8152616
+ */
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.io.StringWriter;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.DiagnosticListener;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.util.JavacTask;
+import com.sun.tools.javac.api.JavacTool;
+import com.sun.tools.javac.tree.JCTree;
+import com.sun.tools.javac.tree.Pretty;
+import java.io.IOException;
+
+public class T8152616 {
+
+    public String PrettyPrint(JCTree tree){
+        StringWriter s = new StringWriter();
+        try {
+            new Pretty(s, false).printExpr(tree);
+        }
+        catch (IOException e) {
+            throw new AssertionError(e);
+        }
+        return s.toString();
+    }
+
+    public static void main(String[] args) throws Exception {
+        T8152616 obj = new T8152616();
+        JavacTool javac = JavacTool.create();
+        StandardJavaFileManager jfm = javac.getStandardFileManager(null,null,null);
+        File file = File.createTempFile("test", ".java");
+        OutputStream outputStream = new FileOutputStream(file);
+        outputStream.write("enum Foo {AA(10), BB, CC { void m() {} }; void m() {};}".getBytes());
+        JavacTask task = javac.getTask(null, jfm, null, null, null,
+                   jfm.getJavaFileObjects(file.getAbsolutePath()));
+        Iterable<? extends CompilationUnitTree> trees = task.parse();
+        CompilationUnitTree thisTree = trees.iterator().next();
+        file.delete();
+        outputStream = new FileOutputStream(file);
+        outputStream.write((obj.PrettyPrint((JCTree)thisTree)).getBytes());
+        task = javac.getTask(null, jfm, null, null, null,
+                   jfm.getJavaFileObjects(file.getAbsolutePath()));
+        if(task.parse().toString().contains("ERROR")){
+             throw new AssertionError("parsing temp file failed with errors");
+        }else{
+             System.out.println("parsing successfull");
+        }
+        file.delete();
+    }
+}