test/tools/javac/lambdaShapes/org/openjdk/tests/separate/Compiler.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package org.openjdk.tests.separate;
aoqi@0 27
aoqi@0 28 import java.util.*;
aoqi@0 29 import java.util.concurrent.atomic.AtomicInteger;
aoqi@0 30 import java.util.concurrent.ConcurrentHashMap;
aoqi@0 31 import java.io.*;
aoqi@0 32 import java.net.URI;
aoqi@0 33 import javax.tools.*;
aoqi@0 34
aoqi@0 35 import com.sun.source.util.JavacTask;
aoqi@0 36
aoqi@0 37 import static org.openjdk.tests.separate.SourceModel.Type;
aoqi@0 38 import static org.openjdk.tests.separate.SourceModel.Class;
aoqi@0 39 import static org.openjdk.tests.separate.SourceModel.Extends;
aoqi@0 40 import static org.openjdk.tests.separate.SourceModel.SourceProcessor;
aoqi@0 41
aoqi@0 42 public class Compiler {
aoqi@0 43
aoqi@0 44 public enum Flags {
aoqi@0 45 VERBOSE, // Prints out files as they are compiled
aoqi@0 46 USECACHE // Keeps results around for reuse. Only use this is
aoqi@0 47 // you're sure that each compilation name maps to the
aoqi@0 48 // same source code
aoqi@0 49 }
aoqi@0 50
aoqi@0 51 private static final AtomicInteger counter = new AtomicInteger();
aoqi@0 52 private static final String targetDir = "gen-separate";
aoqi@0 53 private static final File root = new File(targetDir);
aoqi@0 54 private static ConcurrentHashMap<String,File> cache =
aoqi@0 55 new ConcurrentHashMap<>();
aoqi@0 56
aoqi@0 57 Set<Flags> flags;
aoqi@0 58
aoqi@0 59 private JavaCompiler systemJavaCompiler;
aoqi@0 60 private StandardJavaFileManager fm;
aoqi@0 61 private List<File> tempDirs;
aoqi@0 62 private List<ClassFilePreprocessor> postprocessors;
aoqi@0 63
aoqi@0 64 private static class SourceFile extends SimpleJavaFileObject {
aoqi@0 65 private final String content;
aoqi@0 66
aoqi@0 67 public SourceFile(String name, String content) {
aoqi@0 68 super(URI.create("myfo:/" + name + ".java"), Kind.SOURCE);
aoqi@0 69 this.content = content;
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 73 return toString();
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 public String toString() { return this.content; }
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 public Compiler(Flags ... flags) {
aoqi@0 80 setFlags(flags);
aoqi@0 81 this.tempDirs = new ArrayList<>();
aoqi@0 82 this.postprocessors = new ArrayList<>();
aoqi@0 83 this.systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
aoqi@0 84 this.fm = systemJavaCompiler.getStandardFileManager(null, null, null);
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 public void setFlags(Flags ... flags) {
aoqi@0 88 this.flags = new HashSet<>(Arrays.asList(flags));
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 public void addPostprocessor(ClassFilePreprocessor cfp) {
aoqi@0 92 this.postprocessors.add(cfp);
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 /**
aoqi@0 96 * Compile hierarchies starting with each of the 'types' and return
aoqi@0 97 * a ClassLoader that can be used to load the compiled classes.
aoqi@0 98 */
aoqi@0 99 public ClassLoader compile(Type ... types) {
aoqi@0 100 ClassFilePreprocessor[] cfps = this.postprocessors.toArray(
aoqi@0 101 new ClassFilePreprocessor[0]);
aoqi@0 102
aoqi@0 103 DirectedClassLoader dcl = new DirectedClassLoader(cfps);
aoqi@0 104
aoqi@0 105 for (Type t : types) {
aoqi@0 106 for (Map.Entry<String,File> each : compileHierarchy(t).entrySet()) {
aoqi@0 107 dcl.setLocationFor(each.getKey(), each.getValue());
aoqi@0 108 }
aoqi@0 109 }
aoqi@0 110 return dcl;
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 /**
aoqi@0 114 * Compiles and loads a hierarchy, starting at 'type'
aoqi@0 115 */
aoqi@0 116 public java.lang.Class<?> compileAndLoad(Type type)
aoqi@0 117 throws ClassNotFoundException {
aoqi@0 118
aoqi@0 119 ClassLoader loader = compile(type);
aoqi@0 120 return java.lang.Class.forName(type.getName(), false, loader);
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 /**
aoqi@0 124 * Compiles a hierarchy, starting at 'type' and return a mapping of the
aoqi@0 125 * name to the location where the classfile for that type resides.
aoqi@0 126 */
aoqi@0 127 private Map<String,File> compileHierarchy(Type type) {
aoqi@0 128 HashMap<String,File> outputDirs = new HashMap<>();
aoqi@0 129
aoqi@0 130 File outDir = compileOne(type);
aoqi@0 131 outputDirs.put(type.getName(), outDir);
aoqi@0 132
aoqi@0 133 Class superClass = type.getSuperclass();
aoqi@0 134 if (superClass != null)
aoqi@0 135 outputDirs.putAll(compileHierarchy(superClass));
aoqi@0 136 for (Extends ext : type.getSupertypes())
aoqi@0 137 outputDirs.putAll(compileHierarchy(ext.getType()));
aoqi@0 138
aoqi@0 139 return outputDirs;
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 private File compileOne(Type type) {
aoqi@0 143 if (this.flags.contains(Flags.USECACHE)) {
aoqi@0 144 File dir = cache.get(type.getName());
aoqi@0 145 if (dir != null) {
aoqi@0 146 return dir;
aoqi@0 147 }
aoqi@0 148 }
aoqi@0 149 List<JavaFileObject> files = new ArrayList<>();
aoqi@0 150 SourceProcessor accum =
aoqi@0 151 (name, src) -> { files.add(new SourceFile(name, src)); };
aoqi@0 152
aoqi@0 153 Collection<Type> deps = type.typeDependencies(type.isFullCompilation());
aoqi@0 154 for (Type dep : deps) {
aoqi@0 155 if (type.isFullCompilation())
aoqi@0 156 dep.generate(accum);
aoqi@0 157 else
aoqi@0 158 dep.generateAsDependency(accum, type.methodDependencies());
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 type.generate(accum);
aoqi@0 162
aoqi@0 163 JavacTask ct = (JavacTask)this.systemJavaCompiler.getTask(
aoqi@0 164 null, this.fm, null, null, null, files);
aoqi@0 165 File destDir = null;
aoqi@0 166 do {
aoqi@0 167 int value = counter.incrementAndGet();
aoqi@0 168 destDir = new File(root, Integer.toString(value));
aoqi@0 169 } while (destDir.exists());
aoqi@0 170
aoqi@0 171 if (this.flags.contains(Flags.VERBOSE)) {
aoqi@0 172 System.out.println("Compilation unit for " + type.getName() +
aoqi@0 173 " : compiled into " + destDir);
aoqi@0 174 for (JavaFileObject jfo : files) {
aoqi@0 175 System.out.println(jfo.toString());
aoqi@0 176 }
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 try {
aoqi@0 180 destDir.mkdirs();
aoqi@0 181 this.fm.setLocation(
aoqi@0 182 StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
aoqi@0 183 } catch (IOException e) {
aoqi@0 184 throw new RuntimeException(
aoqi@0 185 "IOException encountered during compilation", e);
aoqi@0 186 }
aoqi@0 187 Boolean result = ct.call();
aoqi@0 188 if (result == Boolean.FALSE) {
aoqi@0 189 throw new RuntimeException(
aoqi@0 190 "Compilation failure in " + type.getName() + " unit");
aoqi@0 191 }
aoqi@0 192 if (this.flags.contains(Flags.USECACHE)) {
aoqi@0 193 File existing = cache.putIfAbsent(type.getName(), destDir);
aoqi@0 194 if (existing != null) {
aoqi@0 195 deleteDir(destDir);
aoqi@0 196 return existing;
aoqi@0 197 }
aoqi@0 198 } else {
aoqi@0 199 this.tempDirs.add(destDir);
aoqi@0 200 }
aoqi@0 201 return destDir;
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 private static void deleteDir(File dir) {
aoqi@0 205 for (File f : dir.listFiles()) {
aoqi@0 206 f.delete();
aoqi@0 207 };
aoqi@0 208 dir.delete();
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 public void cleanup() {
aoqi@0 212 if (!this.flags.contains(Flags.USECACHE)) {
aoqi@0 213 for (File d : tempDirs) {
aoqi@0 214 deleteDir(d);
aoqi@0 215 };
aoqi@0 216 tempDirs = new ArrayList<>();
aoqi@0 217 }
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 // Removes all of the elements in the cache and deletes the associated
aoqi@0 221 // output directories. This may not actually empty the cache if there
aoqi@0 222 // are concurrent users of it.
aoqi@0 223 public static void purgeCache() {
aoqi@0 224 for (Map.Entry<String,File> entry : cache.entrySet()) {
aoqi@0 225 cache.remove(entry.getKey());
aoqi@0 226 deleteDir(entry.getValue());
aoqi@0 227 }
aoqi@0 228 }
aoqi@0 229 }

mercurial