01: /*
02: * Javassist, a Java-bytecode translator toolkit.
03: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
04: *
05: * The contents of this file are subject to the Mozilla Public License Version
06: * 1.1 (the "License"); you may not use this file except in compliance with
07: * the License. Alternatively, the contents of this file may be used under
08: * the terms of the GNU Lesser General Public License Version 2.1 or later.
09: *
10: * Software distributed under the License is distributed on an "AS IS" basis,
11: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12: * for the specific language governing rights and limitations under the
13: * License.
14: */
15:
16: package javassist;
17:
18: import java.io.*;
19: import java.net.URL;
20: import java.net.MalformedURLException;
21:
22: /**
23: * A <code>ByteArrayClassPath</code> contains bytes that is served as
24: * a class file to a <code>ClassPool</code>. It is useful to convert
25: * a byte array to a <code>CtClass</code> object.
26: *
27: * <p>For example, if you want to convert a byte array <code>b</code>
28: * into a <code>CtClass</code> object representing the class with a name
29: * <code>classname</code>, then do as following:
30: *
31: * <ul><pre>
32: * ClassPool cp = ClassPool.getDefault();
33: * cp.insertClassPath(new ByteArrayClassPath(classname, b));
34: * CtClass cc = cp.get(classname);
35: * </pre></ul>
36: *
37: * <p>The <code>ClassPool</code> object <code>cp</code> uses the created
38: * <code>ByteArrayClassPath</code> object as the source of the class file.
39: *
40: * <p>A <code>ByteArrayClassPath</code> must be instantiated for every
41: * class. It contains only a single class file.
42: *
43: * @see javassist.ClassPath
44: * @see ClassPool#insertClassPath(ClassPath)
45: * @see ClassPool#appendClassPath(ClassPath)
46: * @see ClassPool#makeClass(InputStream)
47: */
48: public class ByteArrayClassPath implements ClassPath {
49: protected String classname;
50: protected byte[] classfile;
51:
52: /*
53: * Creates a <code>ByteArrayClassPath</code> containing the given
54: * bytes.
55: *
56: * @param name a fully qualified class name
57: * @param classfile the contents of a class file.
58: */
59: public ByteArrayClassPath(String name, byte[] classfile) {
60: this .classname = name;
61: this .classfile = classfile;
62: }
63:
64: /**
65: * Closes this class path.
66: */
67: public void close() {
68: }
69:
70: public String toString() {
71: return "byte[]:" + classname;
72: }
73:
74: /**
75: * Opens the class file.
76: */
77: public InputStream openClassfile(String classname) {
78: if (this .classname.equals(classname))
79: return new ByteArrayInputStream(classfile);
80: else
81: return null;
82: }
83:
84: /**
85: * Obtains the URL.
86: */
87: public URL find(String classname) {
88: if (this .classname.equals(classname)) {
89: String cname = classname.replace('.', '/') + ".class";
90: try {
91: // return new File(cname).toURL();
92: return new URL("file:/ByteArrayClassPath/" + cname);
93: } catch (MalformedURLException e) {
94: }
95: }
96:
97: return null;
98: }
99: }
|