test/tools/javac/api/evalexpr/MemoryFileManager.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/api/evalexpr/MemoryFileManager.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,165 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2006, 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.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +package evalexpr;
    1.28 +
    1.29 +import java.io.ByteArrayOutputStream;
    1.30 +import java.io.FilterOutputStream;
    1.31 +import java.io.IOException;
    1.32 +import java.io.InputStream;
    1.33 +import java.io.OutputStream;
    1.34 +import java.io.Reader;
    1.35 +import java.io.StringReader;
    1.36 +import java.net.URI;
    1.37 +import java.net.URISyntaxException;
    1.38 +import java.nio.CharBuffer;
    1.39 +import java.util.HashMap;
    1.40 +import java.util.Map;
    1.41 +import javax.tools.*;
    1.42 +import javax.tools.JavaFileObject.Kind;
    1.43 +
    1.44 +/**
    1.45 + * A file manager for compiling strings to byte arrays.
    1.46 + * This file manager delegates to another file manager
    1.47 + * to lookup classes on boot class path.
    1.48 + *
    1.49 + * <p><b>This is NOT part of any supported API.
    1.50 + * If you write code that depends on this, you do so at your own
    1.51 + * risk.  This code and its internal interfaces are subject to change
    1.52 + * or deletion without notice.</b></p>
    1.53 + * @author Peter von der Ah&eacute;
    1.54 + */
    1.55 +public final class MemoryFileManager extends ForwardingJavaFileManager {
    1.56 +    /**
    1.57 +     * Maps binary class names to class files stored as byte arrays.
    1.58 +     */
    1.59 +    private Map<String, byte[]> classes;
    1.60 +
    1.61 +    /**
    1.62 +     * Creates a JavaFileObject representing the given compilation unit.
    1.63 +     * @param name a name representing this source code, for example, the name of a class
    1.64 +     * @param code a compilation unit (source code for a Java program)
    1.65 +     * @return a JavaFileObject represtenting the given compilation unit
    1.66 +     */
    1.67 +    public static JavaFileObject makeSource(String name, String code) {
    1.68 +        return new JavaSourceFromString(name, code);
    1.69 +    }
    1.70 +
    1.71 +    /**
    1.72 +     * Construct a memory file manager which delegates to the specified
    1.73 +     * file manager for unknown sources.
    1.74 +     * @param fileManager a file manager used to look up class files on class path, etc.
    1.75 +     */
    1.76 +    public MemoryFileManager(JavaFileManager fileManager) {
    1.77 +        super(fileManager);
    1.78 +        classes = new HashMap<String, byte[]>();
    1.79 +    }
    1.80 +
    1.81 +    /**
    1.82 +     * Get a class loader which first search the classes stored
    1.83 +     * by this file mananger.
    1.84 +     * @return a class loader for compiled files
    1.85 +     */
    1.86 +    @Override
    1.87 +    public ClassLoader getClassLoader(Location location) {
    1.88 +        return new ByteArrayClassLoader(classes);
    1.89 +    }
    1.90 +
    1.91 +    @Override
    1.92 +    public JavaFileObject getJavaFileForOutput(Location location,
    1.93 +                                               String name,
    1.94 +                                               Kind kind,
    1.95 +                                               FileObject originatingSource)
    1.96 +        throws UnsupportedOperationException
    1.97 +    {
    1.98 +        if (originatingSource instanceof JavaSourceFromString) {
    1.99 +            return new JavaClassInArray(name);
   1.100 +        } else {
   1.101 +            throw new UnsupportedOperationException();
   1.102 +        }
   1.103 +    }
   1.104 +
   1.105 +    protected static URI uriFromString(String uri) {
   1.106 +        try {
   1.107 +            return new URI(uri);
   1.108 +        } catch (URISyntaxException e) {
   1.109 +            throw new IllegalArgumentException(e);
   1.110 +        }
   1.111 +    }
   1.112 +
   1.113 +    /**
   1.114 +     * A file object representing a Java class file stored in a byte array.
   1.115 +     */
   1.116 +    private class JavaClassInArray extends SimpleJavaFileObject {
   1.117 +
   1.118 +        private String name;
   1.119 +
   1.120 +        /**
   1.121 +         * Constructs a JavaClassInArray object.
   1.122 +         * @param name binary name of the class to be stored in this file object
   1.123 +         */
   1.124 +        JavaClassInArray(String name) {
   1.125 +            super(uriFromString("mfm:///" + name.replace('.','/') + Kind.CLASS.extension),
   1.126 +                  Kind.CLASS);
   1.127 +            this.name = name;
   1.128 +        }
   1.129 +
   1.130 +        public OutputStream openOutputStream() {
   1.131 +            return new FilterOutputStream(new ByteArrayOutputStream()) {
   1.132 +                public void close() throws IOException {
   1.133 +                    out.close();
   1.134 +                    ByteArrayOutputStream bos = (ByteArrayOutputStream)out;
   1.135 +                    classes.put(name, bos.toByteArray());
   1.136 +                    System.out.println("compiled " + name);
   1.137 +                }
   1.138 +            };
   1.139 +        }
   1.140 +    }
   1.141 +
   1.142 +    /**
   1.143 +     * A file object used to represent source coming from a string.
   1.144 +     */
   1.145 +    private static class JavaSourceFromString extends SimpleJavaFileObject {
   1.146 +        /**
   1.147 +         * The source code of this "file".
   1.148 +         */
   1.149 +        final String code;
   1.150 +
   1.151 +        /**
   1.152 +         * Constructs a new JavaSourceFromString.
   1.153 +         * @param name the name of the compilation unit represented by this file object
   1.154 +         * @param code the source code for the compilation unit represented by this file object
   1.155 +         */
   1.156 +        JavaSourceFromString(String name, String code) {
   1.157 +            super(uriFromString("mfm:///" + name.replace('.','/') + Kind.SOURCE.extension),
   1.158 +                  Kind.SOURCE);
   1.159 +            this.code = code;
   1.160 +        }
   1.161 +
   1.162 +        @Override
   1.163 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.164 +            return code;
   1.165 +        }
   1.166 +    }
   1.167 +
   1.168 +}

mercurial