632N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
632N/A *
632N/A * This code is free software; you can redistribute it and/or modify it
632N/A * under the terms of the GNU General Public License version 2 only, as
632N/A * published by the Free Software Foundation.
632N/A *
632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
632N/A * version 2 for more details (a copy is included in the LICENSE file that
632N/A * accompanied this code).
632N/A *
632N/A * You should have received a copy of the GNU General Public License version
632N/A * 2 along with this work; if not, write to the Free Software Foundation,
632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
632N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
632N/A */
632N/A
632N/Aimport java.io.ByteArrayOutputStream;
632N/Aimport java.net.URI;
632N/Aimport java.net.URISyntaxException;
632N/Aimport java.util.ArrayList;
632N/Aimport java.util.HashMap;
632N/Aimport java.util.List;
632N/Aimport java.util.Map;
632N/Aimport javax.tools.FileObject;
632N/Aimport javax.tools.ForwardingJavaFileManager;
632N/Aimport javax.tools.JavaCompiler;
632N/Aimport javax.tools.JavaFileManager;
632N/Aimport javax.tools.JavaFileObject.Kind;
632N/Aimport javax.tools.SimpleJavaFileObject;
632N/Aimport javax.tools.ToolProvider;
632N/A
632N/Apublic final class MemoryClassLoader extends ClassLoader {
632N/A private final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
632N/A private final MemoryFileManager manager = new MemoryFileManager(this.compiler);
632N/A
632N/A public Class<?> compile(String name, String content) {
632N/A compile(new Source(name, content));
632N/A try {
632N/A return findClass(name);
632N/A }
632N/A catch (ClassNotFoundException exception) {
632N/A throw new Error(exception);
632N/A }
632N/A }
632N/A
632N/A public void compile(Source... sources) {
632N/A List<Source> list = new ArrayList<Source>();
632N/A if (sources != null) {
632N/A for (Source source : sources) {
632N/A if (source != null) {
632N/A list.add(source);
632N/A }
632N/A }
632N/A }
632N/A synchronized (this.manager) {
632N/A this.compiler.getTask(null, this.manager, null, null, null, list).call();
632N/A }
632N/A }
632N/A
632N/A @Override
632N/A protected Class<?> findClass(String name) throws ClassNotFoundException {
632N/A synchronized (this.manager) {
632N/A Output mc = this.manager.map.remove(name);
632N/A if (mc != null) {
632N/A byte[] array = mc.toByteArray();
632N/A return defineClass(name, array, 0, array.length);
632N/A }
632N/A }
632N/A return super.findClass(name);
632N/A }
632N/A
632N/A private static final class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> {
632N/A private final Map<String, Output> map = new HashMap<String, Output>();
632N/A
632N/A MemoryFileManager(JavaCompiler compiler) {
632N/A super(compiler.getStandardFileManager(null, null, null));
632N/A }
632N/A
632N/A @Override
632N/A public Output getJavaFileForOutput(Location location, String name, Kind kind, FileObject source) {
632N/A Output mc = this.map.get(name);
632N/A if (mc == null) {
632N/A mc = new Output(name);
632N/A this.map.put(name, mc);
632N/A }
632N/A return mc;
632N/A }
632N/A }
632N/A
632N/A private static class MemoryFileObject extends SimpleJavaFileObject {
632N/A MemoryFileObject(String name, Kind kind) {
632N/A super(toURI(name, kind.extension), kind);
632N/A }
632N/A
632N/A private static URI toURI(String name, String extension) {
632N/A try {
632N/A return new URI("mfm:///" + name.replace('.', '/') + extension);
632N/A }
632N/A catch (URISyntaxException exception) {
632N/A throw new Error(exception);
632N/A }
632N/A }
632N/A }
632N/A
632N/A private static final class Output extends MemoryFileObject {
632N/A private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
632N/A
632N/A Output(String name) {
632N/A super(name, Kind.CLASS);
632N/A }
632N/A
632N/A byte[] toByteArray() {
632N/A return this.baos.toByteArray();
632N/A }
632N/A
632N/A @Override
632N/A public ByteArrayOutputStream openOutputStream() {
632N/A this.baos.reset();
632N/A return this.baos;
632N/A }
632N/A }
632N/A
632N/A public static final class Source extends MemoryFileObject {
632N/A private final String content;
632N/A
632N/A Source(String name, String content) {
632N/A super(name, Kind.SOURCE);
632N/A this.content = content;
632N/A }
632N/A
632N/A @Override
632N/A public CharSequence getCharContent(boolean ignore) {
632N/A return this.content;
632N/A }
632N/A }
632N/A}