< prev index next >

test/jdk/lambda/separate/Compiler.java

Print this page
rev 14566 : 8044051: Test jdk/lambda/vm/InterfaceAccessFlagsTest.java gets IOException during compilation
Summary: create the gen-separate directory in the current directory to improve robustness
Reviewed-by: darcy
   1 /*
   2  * Copyright (c) 2012, 2013, 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  */


  37 import java.nio.file.SimpleFileVisitor;
  38 import java.nio.file.attribute.BasicFileAttributes;
  39 
  40 import static separate.SourceModel.Type;
  41 import static separate.SourceModel.Class;
  42 import static separate.SourceModel.Extends;
  43 import static separate.SourceModel.SourceProcessor;
  44 
  45 public class Compiler {
  46 
  47     public enum Flags {
  48         VERBOSE, // Prints out files as they are compiled
  49         USECACHE // Keeps results around for reuse.  Only use this is
  50                  // you're sure that each compilation name maps to the
  51                  // same source code
  52     };
  53 
  54     private static final AtomicInteger counter = new AtomicInteger();
  55     private static final String targetDir =
  56         System.getProperty("lambda.separate.targetDirectory",
  57             System.getProperty("java.io.tmpdir") + File.separator + "gen-separate");
  58     private static final File root = new File(targetDir);
  59     private static ConcurrentHashMap<String,File> cache =
  60             new ConcurrentHashMap<>();
  61 
  62     Set<Flags> flags;
  63 
  64     private JavaCompiler systemJavaCompiler;
  65     private StandardJavaFileManager fm;
  66     private List<File> tempDirs;
  67     private List<ClassFilePreprocessor> postprocessors;
  68 
  69     private static class SourceFile extends SimpleJavaFileObject {
  70         private final String content;
  71 
  72         public SourceFile(String name, String content) {
  73             super(URI.create("myfo:/" + name + ".java"), Kind.SOURCE);
  74             this.content = content;
  75         }
  76 
  77         public CharSequence getCharContent(boolean ignoreEncodingErrors) {


 172         File destDir = null;
 173         do {
 174             int value = counter.incrementAndGet();
 175             destDir = new File(root, Integer.toString(value));
 176         } while (destDir.exists());
 177 
 178         if (this.flags.contains(Flags.VERBOSE)) {
 179             System.out.println("Compilation unit for " + type.getName() +
 180                 " : compiled into " + destDir);
 181             for (JavaFileObject jfo : files) {
 182                 System.out.println(jfo.toString());
 183             }
 184         }
 185 
 186         try {
 187             destDir.mkdirs();
 188             this.fm.setLocation(
 189                 StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
 190         } catch (IOException e) {
 191             throw new RuntimeException(
 192                 "IOException encountered during compilation");
 193         }
 194         Boolean result = ct.call();
 195         if (result == Boolean.FALSE) {
 196             throw new RuntimeException(
 197                 "Compilation failure in " + type.getName() + " unit");
 198         }
 199         if (this.flags.contains(Flags.USECACHE)) {
 200             File existing = cache.putIfAbsent(type.getName(), destDir);
 201             if (existing != null) {
 202                 deleteDir(destDir);
 203                 return existing;
 204             }
 205         } else {
 206         this.tempDirs.add(destDir);
 207         }
 208         return destDir;
 209     }
 210 
 211     private static void deleteDir(File dir) {
 212         if(!dir.exists()) {


   1 /*
   2  * Copyright (c) 2012, 2021, 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  */


  37 import java.nio.file.SimpleFileVisitor;
  38 import java.nio.file.attribute.BasicFileAttributes;
  39 
  40 import static separate.SourceModel.Type;
  41 import static separate.SourceModel.Class;
  42 import static separate.SourceModel.Extends;
  43 import static separate.SourceModel.SourceProcessor;
  44 
  45 public class Compiler {
  46 
  47     public enum Flags {
  48         VERBOSE, // Prints out files as they are compiled
  49         USECACHE // Keeps results around for reuse.  Only use this is
  50                  // you're sure that each compilation name maps to the
  51                  // same source code
  52     };
  53 
  54     private static final AtomicInteger counter = new AtomicInteger();
  55     private static final String targetDir =
  56         System.getProperty("lambda.separate.targetDirectory",
  57             "." + File.separator + "gen-separate");
  58     private static final File root = new File(targetDir);
  59     private static ConcurrentHashMap<String,File> cache =
  60             new ConcurrentHashMap<>();
  61 
  62     Set<Flags> flags;
  63 
  64     private JavaCompiler systemJavaCompiler;
  65     private StandardJavaFileManager fm;
  66     private List<File> tempDirs;
  67     private List<ClassFilePreprocessor> postprocessors;
  68 
  69     private static class SourceFile extends SimpleJavaFileObject {
  70         private final String content;
  71 
  72         public SourceFile(String name, String content) {
  73             super(URI.create("myfo:/" + name + ".java"), Kind.SOURCE);
  74             this.content = content;
  75         }
  76 
  77         public CharSequence getCharContent(boolean ignoreEncodingErrors) {


 172         File destDir = null;
 173         do {
 174             int value = counter.incrementAndGet();
 175             destDir = new File(root, Integer.toString(value));
 176         } while (destDir.exists());
 177 
 178         if (this.flags.contains(Flags.VERBOSE)) {
 179             System.out.println("Compilation unit for " + type.getName() +
 180                 " : compiled into " + destDir);
 181             for (JavaFileObject jfo : files) {
 182                 System.out.println(jfo.toString());
 183             }
 184         }
 185 
 186         try {
 187             destDir.mkdirs();
 188             this.fm.setLocation(
 189                 StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
 190         } catch (IOException e) {
 191             throw new RuntimeException(
 192                 "IOException encountered during compilation: " + e.getMessage(), e);
 193         }
 194         Boolean result = ct.call();
 195         if (result == Boolean.FALSE) {
 196             throw new RuntimeException(
 197                 "Compilation failure in " + type.getName() + " unit");
 198         }
 199         if (this.flags.contains(Flags.USECACHE)) {
 200             File existing = cache.putIfAbsent(type.getName(), destDir);
 201             if (existing != null) {
 202                 deleteDir(destDir);
 203                 return existing;
 204             }
 205         } else {
 206         this.tempDirs.add(destDir);
 207         }
 208         return destDir;
 209     }
 210 
 211     private static void deleteDir(File dir) {
 212         if(!dir.exists()) {


< prev index next >