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