test/langtools/tools/javac/lib/InMemoryFileManager.java
author darcy
Wed, 27 Jun 2018 21:01:12 -0700
changeset 50892 a5557f24b4d4
parent 47216 71c04702a3d5
permissions -rw-r--r--
8205615: Start of release updates for JDK 12 8205621: Increment JDK version for JDK 12 8193292: Add SourceVersion.RELEASE_12 8193290: Add source 12 and target 12 to javac 8205619: Bump maximum recognized class file version to 56 for JDK 12 Reviewed-by: alanb, smarks, jjg, mr, erikj, psandoz, dholmes Contributed-by: erik.joelsson@oracle.com, mikael.vidstedt@oracle.com, joe.darcy@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     1
/*
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     4
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     8
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    13
 * accompanied this code).
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    14
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    18
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    21
 * questions.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    22
 */
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    23
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    24
import java.io.*;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    25
import java.net.URI;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    26
import java.util.HashMap;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    27
import java.util.Map;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    28
import javax.tools.*;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    29
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    30
import static java.util.Collections.unmodifiableMap;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    31
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    32
/**
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    33
 * class for storing source/byte code in memory.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    34
 */
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    35
public class InMemoryFileManager extends ForwardingJavaFileManager {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    36
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    37
    private final Map<String, InMemoryJavaFile> classes = new HashMap<>();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    38
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    39
    public InMemoryFileManager(JavaFileManager fileManager) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    40
        super(fileManager);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    41
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    42
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    43
    @Override
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    44
    public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    45
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    46
        InMemoryJavaFile javaFile = new InMemoryJavaFile(className);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    47
        classes.put(className, javaFile);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    48
        return javaFile;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    49
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    50
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    51
    @Override
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    52
    public ClassLoader getClassLoader(Location location) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    53
        return new ClassLoader(this.getClass().getClassLoader()) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    54
            @Override
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    55
            protected Class<?> findClass(String name) throws ClassNotFoundException {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    56
                InMemoryJavaFile classData = classes.get(name);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    57
                if (classData == null) throw new ClassNotFoundException(name);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    58
                byte[] byteCode = classData.bos.toByteArray();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    59
                return defineClass(name, byteCode, 0, byteCode.length);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    60
            }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    61
        };
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    62
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    63
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    64
    public Map<String, ? extends JavaFileObject> getClasses() {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    65
        return unmodifiableMap(classes);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    66
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    67
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    68
    private static class InMemoryJavaFile extends SimpleJavaFileObject {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    69
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    70
        private final ByteArrayOutputStream bos =
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    71
                new ByteArrayOutputStream();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    72
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    73
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    74
        protected InMemoryJavaFile(String name) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    75
            super(URI.create("mfm:///" + name.replace('.', '/') + Kind.CLASS.extension), Kind.CLASS);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    76
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    77
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    78
        @Override
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    79
        public OutputStream openOutputStream() throws IOException {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    80
            return bos;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    81
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    82
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    83
        @Override
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    84
        public InputStream openInputStream() throws IOException {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    85
            return new ByteArrayInputStream(bos.toByteArray());
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    86
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    87
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    88
}