jdk/test/java/beans/PropertyEditor/MemoryClassLoader.java
author rehn
Fri, 07 Jul 2017 23:04:06 +0200
changeset 46643 cb5f289ba033
parent 5506 202f599c92aa
permissions -rw-r--r--
8183545: Event tracing, transition hooks Reviewed-by: dcubed, egahlin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1308
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     4
 *
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     8
 *
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    13
 * accompanied this code).
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    14
 *
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1308
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1308
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1308
diff changeset
    21
 * questions.
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    22
 */
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    23
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    24
import java.io.ByteArrayOutputStream;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    25
import java.net.URI;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    26
import java.net.URISyntaxException;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    27
import java.util.ArrayList;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    28
import java.util.HashMap;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    29
import java.util.List;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    30
import java.util.Map;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    31
import javax.tools.FileObject;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    32
import javax.tools.ForwardingJavaFileManager;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    33
import javax.tools.JavaCompiler;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    34
import javax.tools.JavaFileManager;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    35
import javax.tools.JavaFileObject.Kind;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    36
import javax.tools.SimpleJavaFileObject;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    37
import javax.tools.ToolProvider;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    38
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    39
public final class MemoryClassLoader extends ClassLoader {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    40
    private final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    41
    private final MemoryFileManager manager = new MemoryFileManager(this.compiler);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    42
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    43
    public Class<?> compile(String name, String content) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    44
        compile(new Source(name, content));
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    45
        try {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    46
            return findClass(name);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    47
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    48
        catch (ClassNotFoundException exception) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    49
            throw new Error(exception);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    50
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    51
    }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    52
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    53
    public void compile(Source... sources) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    54
        List<Source> list = new ArrayList<Source>();
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    55
        if (sources != null) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    56
            for (Source source : sources) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    57
                if (source != null) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    58
                    list.add(source);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    59
                }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    60
            }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    61
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    62
        synchronized (this.manager) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    63
            this.compiler.getTask(null, this.manager, null, null, null, list).call();
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    64
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    65
    }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    66
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    67
    @Override
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    68
    protected Class<?> findClass(String name) throws ClassNotFoundException {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    69
        synchronized (this.manager) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    70
            Output mc = this.manager.map.remove(name);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    71
            if (mc != null) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    72
                byte[] array = mc.toByteArray();
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    73
                return defineClass(name, array, 0, array.length);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    74
            }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    75
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    76
        return super.findClass(name);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    77
    }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    78
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    79
    private static final class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    80
        private final Map<String, Output> map = new HashMap<String, Output>();
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    81
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    82
        MemoryFileManager(JavaCompiler compiler) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    83
            super(compiler.getStandardFileManager(null, null, null));
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    84
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    85
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    86
        @Override
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    87
        public Output getJavaFileForOutput(Location location, String name, Kind kind, FileObject source) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    88
            Output mc = this.map.get(name);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    89
            if (mc == null) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    90
                mc = new Output(name);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    91
                this.map.put(name, mc);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    92
            }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    93
            return mc;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    94
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    95
    }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    96
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    97
    private static class MemoryFileObject extends SimpleJavaFileObject {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    98
        MemoryFileObject(String name, Kind kind) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    99
            super(toURI(name, kind.extension), kind);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   100
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   101
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   102
        private static URI toURI(String name, String extension) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   103
            try {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   104
                return new URI("mfm:///" + name.replace('.', '/') + extension);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   105
            }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   106
            catch (URISyntaxException exception) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   107
                throw new Error(exception);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   108
            }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   109
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   110
    }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   111
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   112
    private static final class Output extends MemoryFileObject {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   113
        private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   114
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   115
        Output(String name) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   116
            super(name, Kind.CLASS);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   117
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   118
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   119
        byte[] toByteArray() {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   120
            return this.baos.toByteArray();
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   121
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   122
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   123
        @Override
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   124
        public ByteArrayOutputStream openOutputStream() {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   125
            this.baos.reset();
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   126
            return this.baos;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   127
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   128
    }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   129
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   130
    public static final class Source extends MemoryFileObject {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   131
        private final String content;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   132
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   133
        Source(String name, String content) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   134
            super(name, Kind.SOURCE);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   135
            this.content = content;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   136
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   137
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   138
        @Override
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   139
        public CharSequence getCharContent(boolean ignore) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   140
            return this.content;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   141
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   142
    }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
   143
}