author | joehw |
Mon, 17 Apr 2017 16:24:10 -0700 | |
changeset 44797 | 8b3b3b911b8a |
parent 25868 | 686eef1e7a79 |
permissions | -rw-r--r-- |
6 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
5 |
/* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
7 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
10 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
11 |
* the License. You may obtain a copy of the License at |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
12 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
14 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
15 |
* Unless required by applicable law or agreed to in writing, software |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
18 |
* See the License for the specific language governing permissions and |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
19 |
* limitations under the License. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
20 |
*/ |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
21 |
|
6 | 22 |
package com.sun.org.apache.bcel.internal.util; |
23 |
||
24 |
||
25 |
import java.util.Hashtable; |
|
26 |
import java.io.*; |
|
27 |
import com.sun.org.apache.bcel.internal.*; |
|
28 |
import com.sun.org.apache.bcel.internal.classfile.*; |
|
29 |
||
30 |
/** |
|
31 |
* <p>Drop in replacement for the standard class loader of the JVM. You can use it |
|
32 |
* in conjunction with the JavaWrapper to dynamically modify/create classes |
|
33 |
* as they're requested.</p> |
|
34 |
* |
|
35 |
* <p>This class loader recognizes special requests in a distinct |
|
36 |
* format, i.e., when the name of the requested class contains with |
|
37 |
* "$$BCEL$$" it calls the createClass() method with that name |
|
38 |
* (everything bevor the $$BCEL$$ is considered to be the package |
|
39 |
* name. You can subclass the class loader and override that |
|
40 |
* method. "Normal" classes class can be modified by overriding the |
|
41 |
* modifyClass() method which is called just before defineClass().</p> |
|
42 |
* |
|
43 |
* <p>There may be a number of packages where you have to use the default |
|
44 |
* class loader (which may also be faster). You can define the set of packages |
|
45 |
* where to use the system class loader in the constructor. The default value contains |
|
46 |
* "java.", "sun.", "javax."</p> |
|
47 |
* |
|
48 |
* @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> |
|
49 |
* @see JavaWrapper |
|
50 |
* @see ClassPath |
|
51 |
*/ |
|
52 |
public class ClassLoader extends java.lang.ClassLoader { |
|
53 |
private Hashtable classes = new Hashtable(); // Hashtable is synchronized thus thread-safe |
|
54 |
private String[] ignored_packages = { |
|
55 |
"java.", "javax.", "sun." |
|
56 |
}; |
|
57 |
private Repository repository = SyntheticRepository.getInstance(); |
|
58 |
private java.lang.ClassLoader deferTo = ClassLoader.getSystemClassLoader(); |
|
59 |
||
60 |
public ClassLoader() { |
|
61 |
} |
|
62 |
||
63 |
public ClassLoader(java.lang.ClassLoader deferTo) { |
|
64 |
this.deferTo = deferTo; |
|
65 |
this.repository = new ClassLoaderRepository(deferTo); |
|
66 |
} |
|
67 |
||
68 |
/** @param ignored_packages classes contained in these packages will be loaded |
|
69 |
* with the system class loader |
|
70 |
*/ |
|
71 |
public ClassLoader(String[] ignored_packages) { |
|
72 |
addIgnoredPkgs(ignored_packages); |
|
73 |
} |
|
74 |
||
75 |
public ClassLoader(java.lang.ClassLoader deferTo, String [] ignored_packages) { |
|
76 |
this.deferTo = deferTo; |
|
77 |
this.repository = new ClassLoaderRepository(deferTo); |
|
78 |
||
79 |
addIgnoredPkgs(ignored_packages); |
|
80 |
} |
|
81 |
||
82 |
private void addIgnoredPkgs(String[] ignored_packages) { |
|
83 |
String[] new_p = new String[ignored_packages.length + this.ignored_packages.length]; |
|
84 |
||
85 |
System.arraycopy(this.ignored_packages, 0, new_p, 0, this.ignored_packages.length); |
|
86 |
System.arraycopy(ignored_packages, 0, new_p, this.ignored_packages.length, |
|
87 |
ignored_packages.length); |
|
88 |
||
89 |
this.ignored_packages = new_p; |
|
90 |
} |
|
91 |
||
92 |
protected Class loadClass(String class_name, boolean resolve) |
|
93 |
throws ClassNotFoundException |
|
94 |
{ |
|
95 |
Class cl = null; |
|
96 |
||
97 |
/* First try: lookup hash table. |
|
98 |
*/ |
|
99 |
if((cl=(Class)classes.get(class_name)) == null) { |
|
100 |
/* Second try: Load system class using system class loader. You better |
|
101 |
* don't mess around with them. |
|
102 |
*/ |
|
103 |
for(int i=0; i < ignored_packages.length; i++) { |
|
104 |
if(class_name.startsWith(ignored_packages[i])) { |
|
105 |
cl = deferTo.loadClass(class_name); |
|
106 |
break; |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
if(cl == null) { |
|
111 |
JavaClass clazz = null; |
|
112 |
||
113 |
/* Third try: Special request? |
|
114 |
*/ |
|
115 |
if(class_name.indexOf("$$BCEL$$") >= 0) |
|
116 |
clazz = createClass(class_name); |
|
117 |
else { // Fourth try: Load classes via repository |
|
118 |
if ((clazz = repository.loadClass(class_name)) != null) { |
|
119 |
clazz = modifyClass(clazz); |
|
120 |
} |
|
121 |
else |
|
122 |
throw new ClassNotFoundException(class_name); |
|
123 |
} |
|
124 |
||
125 |
if(clazz != null) { |
|
126 |
byte[] bytes = clazz.getBytes(); |
|
127 |
cl = defineClass(class_name, bytes, 0, bytes.length); |
|
128 |
} else // Fourth try: Use default class loader |
|
129 |
cl = Class.forName(class_name); |
|
130 |
} |
|
131 |
||
132 |
if(resolve) |
|
133 |
resolveClass(cl); |
|
134 |
} |
|
135 |
||
136 |
classes.put(class_name, cl); |
|
137 |
||
138 |
return cl; |
|
139 |
} |
|
140 |
||
141 |
/** Override this method if you want to alter a class before it gets actually |
|
142 |
* loaded. Does nothing by default. |
|
143 |
*/ |
|
144 |
protected JavaClass modifyClass(JavaClass clazz) { |
|
145 |
return clazz; |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Override this method to create you own classes on the fly. The |
|
150 |
* name contains the special token $$BCEL$$. Everything before that |
|
151 |
* token is consddered to be a package name. You can encode you own |
|
152 |
* arguments into the subsequent string. You must regard however not |
|
153 |
* to use any "illegal" characters, i.e., characters that may not |
|
154 |
* appear in a Java class name too<br> |
|
155 |
* |
|
156 |
* The default implementation interprets the string as a encoded compressed |
|
157 |
* Java class, unpacks and decodes it with the Utility.decode() method, and |
|
158 |
* parses the resulting byte array and returns the resulting JavaClass object. |
|
159 |
* |
|
160 |
* @param class_name compressed byte code with "$$BCEL$$" in it |
|
161 |
*/ |
|
162 |
protected JavaClass createClass(String class_name) { |
|
163 |
int index = class_name.indexOf("$$BCEL$$"); |
|
164 |
String real_name = class_name.substring(index + 8); |
|
165 |
||
166 |
JavaClass clazz = null; |
|
167 |
try { |
|
168 |
byte[] bytes = Utility.decode(real_name, true); |
|
169 |
ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes), "foo"); |
|
170 |
||
171 |
clazz = parser.parse(); |
|
172 |
} catch(Throwable e) { |
|
173 |
e.printStackTrace(); |
|
174 |
return null; |
|
175 |
} |
|
176 |
||
177 |
// Adapt the class name to the passed value |
|
178 |
ConstantPool cp = clazz.getConstantPool(); |
|
179 |
||
180 |
ConstantClass cl = (ConstantClass)cp.getConstant(clazz.getClassNameIndex(), |
|
181 |
Constants.CONSTANT_Class); |
|
182 |
ConstantUtf8 name = (ConstantUtf8)cp.getConstant(cl.getNameIndex(), |
|
183 |
Constants.CONSTANT_Utf8); |
|
184 |
name.setBytes(class_name.replace('.', '/')); |
|
185 |
||
186 |
return clazz; |
|
187 |
} |
|
188 |
} |