-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileClassLoader.java
More file actions
143 lines (129 loc) · 4.51 KB
/
FileClassLoader.java
File metadata and controls
143 lines (129 loc) · 4.51 KB
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
137
138
139
140
141
142
143
package api.cli;
/**
* @(#)FileClassLoader.java 1.0 12/09/2008
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
/**
* FileClassLoader extends URLClassLoader to provide functionality for loading a class from a file or byte array.
*
* @author Darryl
* @see URLClassLoader
*/
public class FileClassLoader extends URLClassLoader {
/**
* Constructs a new FileClassLoader with the default delegation parent ClassLoader. URLs can be added to the
* FileClassLoader using the addURL(URL) method.
* <P>
* This constructor invokes FileClassLoader(URL[]) with an empty URL array.
*
* @see #addURL(URL)
* @see #FileClassLoader(URL[])
*/
public FileClassLoader() {
this(new URL[] {});
}
/**
* Constructs a new FileClassLoader for the specified URLs with the default delegation parent ClassLoader.
* <P>
* This constructor invokes FileClassLoader(URL[], ClassLoader) with the URLs and a null parent.
*
* @param urls
* the URLs from which to load classes and resources
* @see #FileClassLoader(URL[] urls, ClassLoader)
*/
public FileClassLoader(URL[] urls) {
this(urls, null);
}
/**
* Constructs a new FileClassLoader using the specified delegation parent ClassLoader. URLs can be added to the
* FileClassLoader using the addURL(URL) method.
* <P>
* When loading resources, this ClassLoader will be searched after first searching in the specified parent class
* loader.
* <P>
* This constructor invokes FileClassLoader(URL[], ClassLoader) with an empty URL array and the specified parent.
*
* @param parent
* the parent class loader for delegation
* @see #addURL(URL)
* @see #FileClassLoader(URL[], ClassLoader)
*/
public FileClassLoader(ClassLoader parent) {
this(new URL[] {}, parent);
}
/**
* Constructs a new FileClassLoader for the specified URLs using the specified delegation parent ClassLoader. The
* URLs will be searched in the order specified for classes and resources after first searching in the specified
* parent class loader. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is
* assumed to refer to a JAR file which will be downloaded and opened as needed.
*
* @param urls
* the URLs from which to load classes and resources
* @param parent
* the parent class loader for delegation
* @see URLClassLoader#URLClassLoader(URL[], ClassLoader)
*/
public FileClassLoader(URL[] urls, ClassLoader parent) {
super(urls);
}
/**
* Appends the specified URL to the list of URLs to search for classes and resources. Overrides the super method to
* provide public access.
*
* @param url
* the URL to be added to the search path of URLs
* @see URLClassLoader#addURL(URL)
*/
@Override
public void addURL(URL url) {
super.addURL(url);
}
/**
* Converts a file into an instance of class Class and resolves the Class so it can be used. This method extracts
* the file content as a byte array and invokes createClass(byte[]).
* <P>
* The file content should have the format of a valid class file as defined by the <a
* href="http://java.sun.com/docs/books/vmspec/"> Java Virtual Machine Specification</a>.
*
* @param file
* the file to be loaded as a class
* @return The Class object that was created from the file, or null if any exception was encountered
* @throws IOException
* Signals that an I/O exception has occurred.
* @see #createClass(byte[])
*/
public Class<?> createClass(File file) throws IOException {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] bytes = new byte[fis.available()];
int read = fis.read(bytes);
if (read != bytes.length)
return null;
return createClass(bytes);
} finally {
fis.close();
}
}
/**
* Converts an array of bytes into an instance of class Class and resolves the Cla return null;used.
* <P>
* The byte array should have the format of a valid class file as defined by the <a
* href="http://java.sun.com/docs/books/vmspec/"> Java Virtual Machine Specification</a>.
*
* @param bytes
* The bytes that make up the class data
* @return The Class object that was created from the byte array
* @see ClassLoader#defineClass(String, byte[], int, int)
* @see ClassLoader#resolveClass(Class)
*/
public Class<?> createClass(byte[] bytes) {
Class<?> clazz = defineClass(null, bytes, 0, bytes.length);
resolveClass(clazz);
return clazz;
}
}