1 /*
   2  * Copyright (c) 2014, 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 8047183
  27  * @summary JDK build fails with sjavac enabled
  28  *
  29  * @modules jdk.compiler
  30  * @build Wrapper
  31  * @run main Wrapper IgnoreSymbolFile
  32  */
  33 
  34 import java.io.File;
  35 import java.io.FileWriter;
  36 import java.io.IOException;
  37 import java.io.PrintStream;
  38 import java.lang.reflect.Method;
  39 import java.util.Arrays;
  40 
  41 public class IgnoreSymbolFile {
  42     public static void main(String... args) throws Exception {
  43         IgnoreSymbolFile test = new IgnoreSymbolFile();
  44         test.run();
  45     }
  46 
  47     void run() throws Exception {
  48         String body =
  49                 "package p;\n"
  50                 + "import sun.reflect.annotation.*;\n"
  51                 + "class X {\n"
  52                 + "    ExceptionProxy proxy;"
  53                 + "}";
  54         writeFile("src/p/X.java", body);
  55 
  56         new File("classes").mkdirs();
  57 
  58         int rc1 = compile("-d", "classes",
  59                           "--state-dir=classes",
  60                           "-Werror",
  61                           "src");
  62         if (rc1 == 0)
  63             error("compilation succeeded unexpectedly");
  64 
  65         int rc2 = compile("-d", "classes",
  66                           "--state-dir=classes",
  67                           "-Werror",
  68                           "-XDignore.symbol.file=true",
  69                           "src");
  70         if (rc2 != 0)
  71             error("compilation failed unexpectedly: rc=" + rc2);
  72 
  73         if (errors > 0)
  74             throw new Exception(errors + " errors occurred");
  75     }
  76 
  77     int compile(String... args) throws ReflectiveOperationException {
  78         // Use reflection to avoid a compile-time dependency on sjavac Main
  79         System.err.println("compile: " + Arrays.toString(args));
  80         Class<?> c = Class.forName("com.sun.tools.sjavac.Main");
  81         Method m = c.getDeclaredMethod("go", String[].class);
  82         int rc = (Integer) m.invoke(null, (Object) args);
  83         System.err.println("rc=" + rc);
  84         return rc;
  85     }
  86 
  87     void writeFile(String path, String body) throws IOException {
  88         File f = new File(path);
  89         if (f.getParentFile() != null)
  90             f.getParentFile().mkdirs();
  91         try (FileWriter w = new FileWriter(f)) {
  92             w.write(body);
  93         }
  94     }
  95 
  96     void error(String msg) {
  97         System.err.println("Error: " + msg);
  98         errors++;
  99     }
 100 
 101     int errors;
 102 }