test/langtools/tools/javac/T8152616.java
changeset 48749 fd40b0b3d849
child 52019 d63efc278e93
equal deleted inserted replaced
48748:4d716bc7ed54 48749:fd40b0b3d849
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8152616
       
    27  * @summary Unit test for corner case of PrettyPrinting when SourceOutput is false
       
    28  * @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
       
    29  * @run main T8152616
       
    30  */
       
    31 
       
    32 import java.io.File;
       
    33 import java.io.FileOutputStream;
       
    34 import java.io.OutputStream;
       
    35 import java.io.StringWriter;
       
    36 import javax.tools.StandardJavaFileManager;
       
    37 import javax.tools.DiagnosticListener;
       
    38 import com.sun.source.tree.CompilationUnitTree;
       
    39 import com.sun.source.util.JavacTask;
       
    40 import com.sun.tools.javac.api.JavacTool;
       
    41 import com.sun.tools.javac.tree.JCTree;
       
    42 import com.sun.tools.javac.tree.Pretty;
       
    43 import java.io.IOException;
       
    44 
       
    45 public class T8152616 {
       
    46 
       
    47     public String PrettyPrint(JCTree tree){
       
    48         StringWriter s = new StringWriter();
       
    49         try {
       
    50             new Pretty(s, false).printExpr(tree);
       
    51         }
       
    52         catch (IOException e) {
       
    53             throw new AssertionError(e);
       
    54         }
       
    55         return s.toString();
       
    56     }
       
    57 
       
    58     public static void main(String[] args) throws Exception {
       
    59         T8152616 obj = new T8152616();
       
    60         JavacTool javac = JavacTool.create();
       
    61         StandardJavaFileManager jfm = javac.getStandardFileManager(null,null,null);
       
    62         File file = File.createTempFile("test", ".java");
       
    63         OutputStream outputStream = new FileOutputStream(file);
       
    64         outputStream.write("enum Foo {AA(10), BB, CC { void m() {} }; void m() {};}".getBytes());
       
    65         JavacTask task = javac.getTask(null, jfm, null, null, null,
       
    66                    jfm.getJavaFileObjects(file.getAbsolutePath()));
       
    67         Iterable<? extends CompilationUnitTree> trees = task.parse();
       
    68         CompilationUnitTree thisTree = trees.iterator().next();
       
    69         file.delete();
       
    70         outputStream = new FileOutputStream(file);
       
    71         outputStream.write((obj.PrettyPrint((JCTree)thisTree)).getBytes());
       
    72         task = javac.getTask(null, jfm, null, null, null,
       
    73                    jfm.getJavaFileObjects(file.getAbsolutePath()));
       
    74         if(task.parse().toString().contains("ERROR")){
       
    75              throw new AssertionError("parsing temp file failed with errors");
       
    76         }else{
       
    77              System.out.println("parsing successfull");
       
    78         }
       
    79         file.delete();
       
    80     }
       
    81 }