Mon, 17 Nov 2014 12:40:43 -0800
Added tag jdk8u31-b09 for changeset 26c46688ce4a
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. |
duke@1 | 8 | * |
duke@1 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 13 | * accompanied this code). |
duke@1 | 14 | * |
duke@1 | 15 | * You should have received a copy of the GNU General Public License version |
duke@1 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 18 | * |
ohair@554 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 20 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 21 | * questions. |
duke@1 | 22 | */ |
duke@1 | 23 | |
duke@1 | 24 | package evalexpr; |
duke@1 | 25 | |
duke@1 | 26 | import java.io.ByteArrayOutputStream; |
duke@1 | 27 | import java.io.FilterOutputStream; |
duke@1 | 28 | import java.io.IOException; |
duke@1 | 29 | import java.io.InputStream; |
duke@1 | 30 | import java.io.OutputStream; |
duke@1 | 31 | import java.io.Reader; |
duke@1 | 32 | import java.io.StringReader; |
duke@1 | 33 | import java.net.URI; |
duke@1 | 34 | import java.net.URISyntaxException; |
duke@1 | 35 | import java.nio.CharBuffer; |
duke@1 | 36 | import java.util.HashMap; |
duke@1 | 37 | import java.util.Map; |
duke@1 | 38 | import javax.tools.*; |
duke@1 | 39 | import javax.tools.JavaFileObject.Kind; |
duke@1 | 40 | |
duke@1 | 41 | /** |
duke@1 | 42 | * A file manager for compiling strings to byte arrays. |
duke@1 | 43 | * This file manager delegates to another file manager |
duke@1 | 44 | * to lookup classes on boot class path. |
duke@1 | 45 | * |
jjg@581 | 46 | * <p><b>This is NOT part of any supported API. |
duke@1 | 47 | * If you write code that depends on this, you do so at your own |
duke@1 | 48 | * risk. This code and its internal interfaces are subject to change |
duke@1 | 49 | * or deletion without notice.</b></p> |
duke@1 | 50 | * @author Peter von der Ahé |
duke@1 | 51 | */ |
duke@1 | 52 | public final class MemoryFileManager extends ForwardingJavaFileManager { |
duke@1 | 53 | /** |
duke@1 | 54 | * Maps binary class names to class files stored as byte arrays. |
duke@1 | 55 | */ |
duke@1 | 56 | private Map<String, byte[]> classes; |
duke@1 | 57 | |
duke@1 | 58 | /** |
duke@1 | 59 | * Creates a JavaFileObject representing the given compilation unit. |
duke@1 | 60 | * @param name a name representing this source code, for example, the name of a class |
duke@1 | 61 | * @param code a compilation unit (source code for a Java program) |
duke@1 | 62 | * @return a JavaFileObject represtenting the given compilation unit |
duke@1 | 63 | */ |
duke@1 | 64 | public static JavaFileObject makeSource(String name, String code) { |
duke@1 | 65 | return new JavaSourceFromString(name, code); |
duke@1 | 66 | } |
duke@1 | 67 | |
duke@1 | 68 | /** |
duke@1 | 69 | * Construct a memory file manager which delegates to the specified |
duke@1 | 70 | * file manager for unknown sources. |
duke@1 | 71 | * @param fileManager a file manager used to look up class files on class path, etc. |
duke@1 | 72 | */ |
duke@1 | 73 | public MemoryFileManager(JavaFileManager fileManager) { |
duke@1 | 74 | super(fileManager); |
duke@1 | 75 | classes = new HashMap<String, byte[]>(); |
duke@1 | 76 | } |
duke@1 | 77 | |
duke@1 | 78 | /** |
duke@1 | 79 | * Get a class loader which first search the classes stored |
duke@1 | 80 | * by this file mananger. |
duke@1 | 81 | * @return a class loader for compiled files |
duke@1 | 82 | */ |
duke@1 | 83 | @Override |
duke@1 | 84 | public ClassLoader getClassLoader(Location location) { |
duke@1 | 85 | return new ByteArrayClassLoader(classes); |
duke@1 | 86 | } |
duke@1 | 87 | |
duke@1 | 88 | @Override |
duke@1 | 89 | public JavaFileObject getJavaFileForOutput(Location location, |
duke@1 | 90 | String name, |
duke@1 | 91 | Kind kind, |
duke@1 | 92 | FileObject originatingSource) |
duke@1 | 93 | throws UnsupportedOperationException |
duke@1 | 94 | { |
duke@1 | 95 | if (originatingSource instanceof JavaSourceFromString) { |
duke@1 | 96 | return new JavaClassInArray(name); |
duke@1 | 97 | } else { |
duke@1 | 98 | throw new UnsupportedOperationException(); |
duke@1 | 99 | } |
duke@1 | 100 | } |
duke@1 | 101 | |
duke@1 | 102 | protected static URI uriFromString(String uri) { |
duke@1 | 103 | try { |
duke@1 | 104 | return new URI(uri); |
duke@1 | 105 | } catch (URISyntaxException e) { |
duke@1 | 106 | throw new IllegalArgumentException(e); |
duke@1 | 107 | } |
duke@1 | 108 | } |
duke@1 | 109 | |
duke@1 | 110 | /** |
duke@1 | 111 | * A file object representing a Java class file stored in a byte array. |
duke@1 | 112 | */ |
duke@1 | 113 | private class JavaClassInArray extends SimpleJavaFileObject { |
duke@1 | 114 | |
duke@1 | 115 | private String name; |
duke@1 | 116 | |
duke@1 | 117 | /** |
duke@1 | 118 | * Constructs a JavaClassInArray object. |
duke@1 | 119 | * @param name binary name of the class to be stored in this file object |
duke@1 | 120 | */ |
duke@1 | 121 | JavaClassInArray(String name) { |
duke@1 | 122 | super(uriFromString("mfm:///" + name.replace('.','/') + Kind.CLASS.extension), |
duke@1 | 123 | Kind.CLASS); |
duke@1 | 124 | this.name = name; |
duke@1 | 125 | } |
duke@1 | 126 | |
duke@1 | 127 | public OutputStream openOutputStream() { |
duke@1 | 128 | return new FilterOutputStream(new ByteArrayOutputStream()) { |
duke@1 | 129 | public void close() throws IOException { |
duke@1 | 130 | out.close(); |
duke@1 | 131 | ByteArrayOutputStream bos = (ByteArrayOutputStream)out; |
duke@1 | 132 | classes.put(name, bos.toByteArray()); |
duke@1 | 133 | System.out.println("compiled " + name); |
duke@1 | 134 | } |
duke@1 | 135 | }; |
duke@1 | 136 | } |
duke@1 | 137 | } |
duke@1 | 138 | |
duke@1 | 139 | /** |
duke@1 | 140 | * A file object used to represent source coming from a string. |
duke@1 | 141 | */ |
duke@1 | 142 | private static class JavaSourceFromString extends SimpleJavaFileObject { |
duke@1 | 143 | /** |
duke@1 | 144 | * The source code of this "file". |
duke@1 | 145 | */ |
duke@1 | 146 | final String code; |
duke@1 | 147 | |
duke@1 | 148 | /** |
duke@1 | 149 | * Constructs a new JavaSourceFromString. |
duke@1 | 150 | * @param name the name of the compilation unit represented by this file object |
duke@1 | 151 | * @param code the source code for the compilation unit represented by this file object |
duke@1 | 152 | */ |
duke@1 | 153 | JavaSourceFromString(String name, String code) { |
duke@1 | 154 | super(uriFromString("mfm:///" + name.replace('.','/') + Kind.SOURCE.extension), |
duke@1 | 155 | Kind.SOURCE); |
duke@1 | 156 | this.code = code; |
duke@1 | 157 | } |
duke@1 | 158 | |
duke@1 | 159 | @Override |
duke@1 | 160 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
duke@1 | 161 | return code; |
duke@1 | 162 | } |
duke@1 | 163 | } |
duke@1 | 164 | |
duke@1 | 165 | } |