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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambdaShapes/org/openjdk/tests/separate/Compiler.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,229 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package org.openjdk.tests.separate;
    1.30 +
    1.31 +import java.util.*;
    1.32 +import java.util.concurrent.atomic.AtomicInteger;
    1.33 +import java.util.concurrent.ConcurrentHashMap;
    1.34 +import java.io.*;
    1.35 +import java.net.URI;
    1.36 +import javax.tools.*;
    1.37 +
    1.38 +import com.sun.source.util.JavacTask;
    1.39 +
    1.40 +import static org.openjdk.tests.separate.SourceModel.Type;
    1.41 +import static org.openjdk.tests.separate.SourceModel.Class;
    1.42 +import static org.openjdk.tests.separate.SourceModel.Extends;
    1.43 +import static org.openjdk.tests.separate.SourceModel.SourceProcessor;
    1.44 +
    1.45 +public class Compiler {
    1.46 +
    1.47 +    public enum Flags {
    1.48 +        VERBOSE, // Prints out files as they are compiled
    1.49 +        USECACHE // Keeps results around for reuse.  Only use this is
    1.50 +                 // you're sure that each compilation name maps to the
    1.51 +                 // same source code
    1.52 +    }
    1.53 +
    1.54 +    private static final AtomicInteger counter = new AtomicInteger();
    1.55 +    private static final String targetDir = "gen-separate";
    1.56 +    private static final File root = new File(targetDir);
    1.57 +    private static ConcurrentHashMap<String,File> cache =
    1.58 +            new ConcurrentHashMap<>();
    1.59 +
    1.60 +    Set<Flags> flags;
    1.61 +
    1.62 +    private JavaCompiler systemJavaCompiler;
    1.63 +    private StandardJavaFileManager fm;
    1.64 +    private List<File> tempDirs;
    1.65 +    private List<ClassFilePreprocessor> postprocessors;
    1.66 +
    1.67 +    private static class SourceFile extends SimpleJavaFileObject {
    1.68 +        private final String content;
    1.69 +
    1.70 +        public SourceFile(String name, String content) {
    1.71 +            super(URI.create("myfo:/" + name + ".java"), Kind.SOURCE);
    1.72 +            this.content = content;
    1.73 +        }
    1.74 +
    1.75 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    1.76 +            return toString();
    1.77 +        }
    1.78 +
    1.79 +        public String toString() { return this.content; }
    1.80 +    }
    1.81 +
    1.82 +    public Compiler(Flags ... flags) {
    1.83 +        setFlags(flags);
    1.84 +        this.tempDirs = new ArrayList<>();
    1.85 +        this.postprocessors = new ArrayList<>();
    1.86 +        this.systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
    1.87 +        this.fm = systemJavaCompiler.getStandardFileManager(null, null, null);
    1.88 +    }
    1.89 +
    1.90 +    public void setFlags(Flags ... flags) {
    1.91 +        this.flags = new HashSet<>(Arrays.asList(flags));
    1.92 +    }
    1.93 +
    1.94 +    public void addPostprocessor(ClassFilePreprocessor cfp) {
    1.95 +        this.postprocessors.add(cfp);
    1.96 +    }
    1.97 +
    1.98 +    /**
    1.99 +     * Compile hierarchies starting with each of the 'types' and return
   1.100 +     * a ClassLoader that can be used to load the compiled classes.
   1.101 +     */
   1.102 +    public ClassLoader compile(Type ... types) {
   1.103 +        ClassFilePreprocessor[] cfps = this.postprocessors.toArray(
   1.104 +            new ClassFilePreprocessor[0]);
   1.105 +
   1.106 +        DirectedClassLoader dcl = new DirectedClassLoader(cfps);
   1.107 +
   1.108 +        for (Type t : types) {
   1.109 +            for (Map.Entry<String,File> each : compileHierarchy(t).entrySet()) {
   1.110 +                dcl.setLocationFor(each.getKey(), each.getValue());
   1.111 +            }
   1.112 +        }
   1.113 +        return dcl;
   1.114 +    }
   1.115 +
   1.116 +    /**
   1.117 +     * Compiles and loads a hierarchy, starting at 'type'
   1.118 +     */
   1.119 +    public java.lang.Class<?> compileAndLoad(Type type)
   1.120 +            throws ClassNotFoundException {
   1.121 +
   1.122 +        ClassLoader loader = compile(type);
   1.123 +        return java.lang.Class.forName(type.getName(), false, loader);
   1.124 +    }
   1.125 +
   1.126 +    /**
   1.127 +     * Compiles a hierarchy, starting at 'type' and return a mapping of the
   1.128 +     * name to the location where the classfile for that type resides.
   1.129 +     */
   1.130 +    private Map<String,File> compileHierarchy(Type type) {
   1.131 +        HashMap<String,File> outputDirs = new HashMap<>();
   1.132 +
   1.133 +        File outDir = compileOne(type);
   1.134 +        outputDirs.put(type.getName(), outDir);
   1.135 +
   1.136 +        Class superClass = type.getSuperclass();
   1.137 +        if (superClass != null)
   1.138 +            outputDirs.putAll(compileHierarchy(superClass));
   1.139 +        for (Extends ext : type.getSupertypes())
   1.140 +            outputDirs.putAll(compileHierarchy(ext.getType()));
   1.141 +
   1.142 +        return outputDirs;
   1.143 +    }
   1.144 +
   1.145 +    private File compileOne(Type type) {
   1.146 +        if (this.flags.contains(Flags.USECACHE)) {
   1.147 +            File dir = cache.get(type.getName());
   1.148 +            if (dir != null) {
   1.149 +                return dir;
   1.150 +            }
   1.151 +        }
   1.152 +        List<JavaFileObject> files = new ArrayList<>();
   1.153 +        SourceProcessor accum =
   1.154 +            (name, src) -> { files.add(new SourceFile(name, src)); };
   1.155 +
   1.156 +        Collection<Type> deps = type.typeDependencies(type.isFullCompilation());
   1.157 +        for (Type dep : deps) {
   1.158 +            if (type.isFullCompilation())
   1.159 +                dep.generate(accum);
   1.160 +            else
   1.161 +                dep.generateAsDependency(accum, type.methodDependencies());
   1.162 +        }
   1.163 +
   1.164 +        type.generate(accum);
   1.165 +
   1.166 +        JavacTask ct = (JavacTask)this.systemJavaCompiler.getTask(
   1.167 +            null, this.fm, null, null, null, files);
   1.168 +        File destDir = null;
   1.169 +        do {
   1.170 +            int value = counter.incrementAndGet();
   1.171 +            destDir = new File(root, Integer.toString(value));
   1.172 +        } while (destDir.exists());
   1.173 +
   1.174 +        if (this.flags.contains(Flags.VERBOSE)) {
   1.175 +            System.out.println("Compilation unit for " + type.getName() +
   1.176 +                " : compiled into " + destDir);
   1.177 +            for (JavaFileObject jfo : files) {
   1.178 +                System.out.println(jfo.toString());
   1.179 +            }
   1.180 +        }
   1.181 +
   1.182 +        try {
   1.183 +            destDir.mkdirs();
   1.184 +            this.fm.setLocation(
   1.185 +                StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
   1.186 +        } catch (IOException e) {
   1.187 +            throw new RuntimeException(
   1.188 +                "IOException encountered during compilation", e);
   1.189 +        }
   1.190 +        Boolean result = ct.call();
   1.191 +        if (result == Boolean.FALSE) {
   1.192 +            throw new RuntimeException(
   1.193 +                "Compilation failure in " + type.getName() + " unit");
   1.194 +        }
   1.195 +        if (this.flags.contains(Flags.USECACHE)) {
   1.196 +            File existing = cache.putIfAbsent(type.getName(), destDir);
   1.197 +            if (existing != null) {
   1.198 +                deleteDir(destDir);
   1.199 +                return existing;
   1.200 +            }
   1.201 +        } else {
   1.202 +        this.tempDirs.add(destDir);
   1.203 +        }
   1.204 +        return destDir;
   1.205 +    }
   1.206 +
   1.207 +    private static void deleteDir(File dir) {
   1.208 +        for (File f : dir.listFiles()) {
   1.209 +            f.delete();
   1.210 +        };
   1.211 +        dir.delete();
   1.212 +    }
   1.213 +
   1.214 +    public void cleanup() {
   1.215 +        if (!this.flags.contains(Flags.USECACHE)) {
   1.216 +            for (File d : tempDirs) {
   1.217 +                deleteDir(d);
   1.218 +            };
   1.219 +            tempDirs = new ArrayList<>();
   1.220 +        }
   1.221 +    }
   1.222 +
   1.223 +    // Removes all of the elements in the cache and deletes the associated
   1.224 +    // output directories.  This may not actually empty the cache if there
   1.225 +    // are concurrent users of it.
   1.226 +    public static void purgeCache() {
   1.227 +        for (Map.Entry<String,File> entry : cache.entrySet()) {
   1.228 +            cache.remove(entry.getKey());
   1.229 +            deleteDir(entry.getValue());
   1.230 +        }
   1.231 +    }
   1.232 +}

mercurial