/
PluginClassLoader.java
136 lines (119 loc) · 3.78 KB
/
PluginClassLoader.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package net.ME1312.Galaxi.Engine.Library;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
/**
* Plugin ClassLoader Class
*/
public class PluginClassLoader extends URLClassLoader {
private static HashMap<File, PluginClassLoader> loaders = new HashMap<File, PluginClassLoader>();
private Class<?> defaultClass = null;
private final ClassLoader parent;
private final File file;
/**
* Load Classes from URLs
*
* @param file File
*/
public static PluginClassLoader get(File file) {
return get(null, file);
}
/**
* Load Classes from URLs with a parent loader
*
* @param parent Parent loader
* @param file File
*/
public static PluginClassLoader get(ClassLoader parent, File file) {
if (!loaders.keySet().contains(file)) loaders.put(file, new PluginClassLoader(parent, file));
return loaders.get(file);
}
private PluginClassLoader(ClassLoader parent, File file) {
super(toSuper(file), null);
this.parent = parent;
this.file = file;
loaders.put(file, this);
}
/*
* Convert File[] to URL[] for Super
*/
private static URL[] toSuper(File file) {
URL result = null;
try {
result = file.toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return (result == null)?new URL[0]:new URL[]{result};
}
/**
* Get the file used by this ClassLoader
*
* @return Loaded File
*/
public File getFile() {
return file;
}
/**
* Set the Default Class to load when no class is found
*
* @param clazz Class
*/
public void setDefaultClass(Class<?> clazz) {
defaultClass = clazz;
}
/**
* Get the Default Class to load when no class is found
*
* @throws ClassNotFoundException when no Default Class is set
* @return Default Class
*/
public Class<?> getDefaultClass() throws ClassNotFoundException {
if (defaultClass == null) {
throw new ClassNotFoundException();
} else {
return defaultClass;
}
}
private Class<?> getDefaultClass(String name) throws ClassNotFoundException {
try {
return getDefaultClass();
} catch (ClassNotFoundException e) {
throw new ClassNotFoundException(name);
}
}
public Class<?> loadClass(String name) throws ClassNotFoundException {
return this.loadClass(name, (byte) 1);
}
private Class<?> loadClass(String name, byte level) throws ClassNotFoundException {
boolean check = level > 0;
try {
return super.loadClass(name);
} catch (NoClassDefFoundError | ClassNotFoundException e) {
if (parent != null) try {
return parent.loadClass(name);
} catch (NoClassDefFoundError | ClassNotFoundException e2) {}
if (check) {
Iterator i = new LinkedList<>(loaders.values()).iterator();
while (true) {
PluginClassLoader loader;
do {
if (!i.hasNext()) {
return getDefaultClass(name);
}
loader = (PluginClassLoader) i.next();
} while (loader == this);
try {
return loader.loadClass(name, (byte) 0);
} catch (NoClassDefFoundError | ClassNotFoundException e3) {}
}
} else {
throw new ClassNotFoundException(name);
}
}
}
}