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