01: /*
02: * Copyright 2004 Brian S O'Neill
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.cojen.classfile;
18:
19: import java.io.InputStream;
20:
21: /**
22: * {@link ClassFileDataLoader} implementation that loads class file data as
23: * resources from a ClassLoader.
24: *
25: * @author Brian S O'Neill
26: */
27: public class ResourceClassFileDataLoader implements ClassFileDataLoader {
28: private final ClassLoader mLoader;
29:
30: /**
31: * Loads resources using the ClassLoader that loaded this class.
32: */
33: public ResourceClassFileDataLoader() {
34: mLoader = getClass().getClassLoader();
35: }
36:
37: /**
38: * @param loader ClassLoader for finding resources; if null, use system
39: * ClassLoader.
40: */
41: public ResourceClassFileDataLoader(ClassLoader loader) {
42: mLoader = loader;
43: }
44:
45: public InputStream getClassData(String name) {
46: name = name.replace('.', '/') + ".class";
47:
48: InputStream in;
49: if (mLoader == null) {
50: in = ClassLoader.getSystemResourceAsStream(name);
51: } else {
52: in = mLoader.getResourceAsStream(name);
53: }
54:
55: return in;
56: }
57: }
|