原作者文章出处:自定义class loader
1
2
3
|
protected
Class> findClass(String name)
throws
ClassNotFoundException {
throw
new
ClassNotFoundException(name);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
protected
Class> loadClass(String name,
boolean
resolve)
throws
ClassNotFoundException
{
synchronized
(getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class> c = findLoadedClass(name);
if
(c ==
null
) {
long
t0 = System.nanoTime();
try
{
if
(parent !=
null
) {
c = parent.loadClass(name,
false
);
}
else
{
c = findBootstrapClassOrNull(name);
}
}
catch
(ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if
(c ==
null
) {
// If still not found, then invoke findClass in order
// to find the class.
long
t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if
(resolve) {
resolveClass(c);
}
return
c;
}
}
|
1
|
public
final
ClassLoader getParent()
|
1
|
protected
final
Class> findLoadedClass(String name)
|
1
|
protected
final
void
resolveClass(Class> c)
|
1
|
protected
final
Class> defineClass(String name,
byte
[] b,
int
off,
int
len)
|
1
2
3
4
5
|
package
com.stevex.app.classloader;
public
interface
Car {
public
void
run();
}
|
1
2
3
4
5
6
7
8
9
|
package
com.stevex.app.classloader;
public
class
BMW
implements
Car {
public
void
run() {
System.out.println(
"BMW"
);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package
com.stevex.app.classloader;
import
java.io.ByteArrayOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
public
class
SteveClassLoader
extends
ClassLoader {
@Override
public
Class> findClass(String name) {
byte
[] bt = loadClassData(name);
return
defineClass(name, bt,
0
, bt.length);
}
private
byte
[] loadClassData(String className) {
// read class
InputStream is = getClass().getClassLoader().getResourceAsStream(
className.replace(
"."
,
"/"
) +
".class"
);
ByteArrayOutputStream byteSt =
new
ByteArrayOutputStream();
// write into byte
int
len =
0
;
try
{
while
((len = is.read()) != -
1
) {
byteSt.write(len);
}
}
catch
(IOException e) {
e.printStackTrace();
}
// convert into byte array
return
byteSt.toByteArray();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package
com.stevex.app.classloader;
import
java.lang.reflect.InvocationTargetException;
import
java.lang.reflect.Method;
public
class
SteveClassLoaderTest {
public
static
void
main(String[] args)
throws
InstantiationException,
IllegalAccessException, NoSuchMethodException, SecurityException,
IllegalArgumentException, InvocationTargetException, ClassNotFoundException {
SteveClassLoader loader =
new
SteveClassLoader();
loadClass(loader);
findClass1(loader);
//findClass2(loader);
}
private
static
void
findClass1(SteveClassLoader loader)
throws
InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Class> c = loader.findClass(
"com.stevex.app.classloader.BMW"
);
System.out.println(
"Loaded by :"
+ c.getClassLoader());
Object ob = c.newInstance();
Method md = c.getMethod(
"run"
);
md.invoke(ob);
}
private
static
void
loadClass(SteveClassLoader loader)
throws
ClassNotFoundException, InstantiationException,
IllegalAccessException {
Class> c = loader.loadClass(
"com.stevex.app.classloader.BMW"
);
System.out.println(
"Loaded by :"
+ c.getClassLoader());
Car car = (Car) c.newInstance();
car.run();
BMW bmw = (BMW) c.newInstance();
bmw.run();
}
private
static
void
findClass2(SteveClassLoader loader)
throws
InstantiationException,
IllegalAccessException {
Class> c = loader.findClass(
"com.stevex.app.classloader.BMW"
);
System.out.println(
"Loaded by :"
+ c.getClassLoader());
Car car = (Car) c.newInstance();
car.run();
BMW bmw = (BMW) c.newInstance();
bmw.run();
}
}
|