langtools/test/tools/javac/processing/OverwriteInitialInput.java
changeset 43368 cabe410a7a5c
equal deleted inserted replaced
43367:7797472a9ed5 43368:cabe410a7a5c
       
     1 /*
       
     2  * Copyright (c) 2015, 2016, 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 8067747
       
    27  * @summary Verify the correct Filer behavior w.r.t. initial inputs
       
    28  *          (should throw FilerException when overwriting initial inputs).
       
    29  * @library /tools/lib /tools/javac/lib
       
    30  * @modules jdk.compiler/com.sun.tools.javac.api
       
    31  *          jdk.compiler/com.sun.tools.javac.main
       
    32  * @build toolbox.ToolBox toolbox.JavacTask toolbox.Task
       
    33  * @build OverwriteInitialInput JavacTestingAbstractProcessor
       
    34  * @run main OverwriteInitialInput
       
    35  */
       
    36 
       
    37 import java.io.IOException;
       
    38 import java.io.OutputStream;
       
    39 import java.io.Writer;
       
    40 import java.nio.file.Files;
       
    41 import java.nio.file.Path;
       
    42 import java.nio.file.Paths;
       
    43 import java.util.Set;
       
    44 
       
    45 import javax.annotation.processing.FilerException;
       
    46 import javax.annotation.processing.RoundEnvironment;
       
    47 import javax.annotation.processing.SupportedOptions;
       
    48 import javax.lang.model.element.TypeElement;
       
    49 import javax.tools.StandardLocation;
       
    50 
       
    51 import toolbox.JavacTask;
       
    52 import toolbox.Task;
       
    53 import toolbox.ToolBox;
       
    54 
       
    55 @SupportedOptions({OverwriteInitialInput.EXPECT_EXCEPTION_OPTION,
       
    56                    OverwriteInitialInput.TEST_SOURCE
       
    57                   })
       
    58 public class OverwriteInitialInput extends JavacTestingAbstractProcessor {
       
    59 
       
    60     public static final String EXPECT_EXCEPTION_OPTION = "exception";
       
    61     public static final String TEST_SOURCE = "testSource";
       
    62 
       
    63     @Override
       
    64     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
       
    65         if (roundEnv.processingOver()) {
       
    66             if (processingEnv.getOptions().containsKey(EXPECT_EXCEPTION_OPTION)) {
       
    67                 try (Writer w = processingEnv.getFiler().createSourceFile("Test").openWriter()) {
       
    68                     throw new AssertionError("Expected IOException not seen.");
       
    69                 } catch (FilerException ex) {
       
    70                     //expected
       
    71                 } catch (IOException ex) {
       
    72                     throw new IllegalStateException(ex);
       
    73                 }
       
    74                 try (OutputStream out = processingEnv.getFiler().createClassFile("Test").openOutputStream()) {
       
    75                     throw new AssertionError("Expected IOException not seen.");
       
    76                 } catch (FilerException ex) {
       
    77                     //expected
       
    78                 } catch (IOException ex) {
       
    79                     throw new IllegalStateException(ex);
       
    80                 }
       
    81                 if (processingEnv.getOptions().containsKey(TEST_SOURCE)) {
       
    82                     try (OutputStream out = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "", "Test.java").openOutputStream()) {
       
    83                         throw new AssertionError("Expected IOException not seen.");
       
    84                     } catch (FilerException ex) {
       
    85                         //expected
       
    86                     } catch (IOException ex) {
       
    87                         throw new IllegalStateException(ex);
       
    88                     }
       
    89                 } else {
       
    90                     try (OutputStream out = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "Test2.class").openOutputStream()) {
       
    91                         throw new AssertionError("Expected IOException not seen.");
       
    92                     } catch (FilerException ex) {
       
    93                         //expected
       
    94                     } catch (IOException ex) {
       
    95                         throw new IllegalStateException(ex);
       
    96                     }
       
    97                 }
       
    98             } else {
       
    99                 try (Writer w = processingEnv.getFiler().createSourceFile("Test").openWriter()) {
       
   100                     w.append("public class Test {}");
       
   101                 } catch (IOException ex) {
       
   102                     throw new IllegalStateException(ex);
       
   103                 }
       
   104                 try (OutputStream out = processingEnv.getFiler().createClassFile("Test2").openOutputStream()) {
       
   105                     try (ToolBox.MemoryFileManager mfm = new ToolBox.MemoryFileManager()) {
       
   106                         ToolBox tb = new ToolBox();
       
   107                         new JavacTask(tb)
       
   108                           .sources("public class Test2 {}")
       
   109                           .fileManager(mfm)
       
   110                           .run()
       
   111                           .writeAll();
       
   112 
       
   113                         out.write(mfm.getFileBytes(StandardLocation.CLASS_OUTPUT, "Test2"));
       
   114                     }
       
   115                 } catch (IOException ex) {
       
   116                     throw new IllegalStateException(ex);
       
   117                 }
       
   118             }
       
   119         }
       
   120 
       
   121         return false;
       
   122     }
       
   123 
       
   124     public static void main(String... args) throws IOException {
       
   125         new OverwriteInitialInput().run();
       
   126     }
       
   127 
       
   128     void run() throws IOException {
       
   129         run(Task.Mode.API);
       
   130         run(Task.Mode.CMDLINE);
       
   131     }
       
   132 
       
   133     void run(Task.Mode mode) throws IOException {
       
   134         ToolBox tb = new ToolBox();
       
   135         Path path = Paths.get("output");
       
   136         if (Files.isDirectory(path))
       
   137             tb.cleanDirectory(path);
       
   138         Files.deleteIfExists(path);
       
   139         tb.createDirectories(path);
       
   140         Path thisSource = Paths.get(System.getProperty("test.src"), "OverwriteInitialInput.java");
       
   141         new JavacTask(tb, mode).options("-processor", "OverwriteInitialInput",
       
   142                                         "-d", path.toString(),
       
   143                                         "-XDaccessInternalAPI=true")
       
   144                                .files(thisSource)
       
   145                                .run()
       
   146                                .writeAll();
       
   147         new JavacTask(tb, mode).options("-processor", "OverwriteInitialInput",
       
   148                                         "-d", path.toString(),
       
   149                                         "-A" + EXPECT_EXCEPTION_OPTION,
       
   150                                         "-A" + TEST_SOURCE,
       
   151                                         "-XDaccessInternalAPI=true")
       
   152                                .files(thisSource, path.resolve("Test.java"))
       
   153                                .run()
       
   154                                .writeAll();
       
   155         new JavacTask(tb, mode).options("-processor", "OverwriteInitialInput",
       
   156                                         "-classpath", path.toString(),
       
   157                                         "-processorpath", System.getProperty("test.class.path"),
       
   158                                         "-d", path.toString(),
       
   159                                         "-A" + EXPECT_EXCEPTION_OPTION,
       
   160                                         "-XDaccessInternalAPI=true")
       
   161                                .classes("Test", "Test2")
       
   162                                .run()
       
   163                                .writeAll();
       
   164     }
       
   165 
       
   166 }