/Users/lyon/j4p/src/javassist/ByteArrayClassPath.java

1    /* 
2     * Javassist, a Java-bytecode translator toolkit. 
3     * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved. 
4     * 
5     * The contents of this file are subject to the Mozilla Public License Version 
6     * 1.1 (the "License"); you may not use this file except in compliance with 
7     * the License.  Alternatively, the contents of this file may be used under 
8     * the terms of the GNU Lesser General Public License Version 2.1 or later. 
9     * 
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    
20   /** 
21    * A <code>ByteArrayClassPath</code> contains bytes that is served as 
22    * a class file to a <code>ClassPool</code>.  It is useful to convert 
23    * a byte array to a <code>CtClass</code> object. 
24    * 
25    * <p>For example, if you want to convert a byte array <code>b</code> 
26    * into a <code>CtClass</code> object representing the class with a name 
27    * <code>classname</code>, then do as following: 
28    * 
29    * <ul><pre> 
30    * ClassPool cp = ClassPool.getDefault(); 
31    * cp.insertClassPath(new ByteArrayClassPath(classname, b)); 
32    * CtClass cc = cp.get(classname); 
33    * </pre></ul> 
34    * 
35    * <p>The <code>ClassPool</code> object <code>cp</code> uses the created 
36    * <code>ByteArrayClassPath</code> object as the source of the class file. 
37    * 
38    * <p>A <code>ByteArrayClassPath</code> must be instantiated for every 
39    * class.  It contains only a single class file. 
40    * 
41    * @see javassist.ClassPath 
42    * @see ClassPool#insertClassPath(ClassPath) 
43    * @see ClassPool#appendClassPath(ClassPath) 
44    */ 
45   public class ByteArrayClassPath implements ClassPath { 
46       protected String classname; 
47       protected byte[] classfile; 
48    
49       /* 
50        * Creates a <code>ByteArrayClassPath</code> containing the given 
51        * bytes. 
52        * 
53        * @param name              a fully qualified class name 
54        * @param classfile         the contents of a class file. 
55        */ 
56       public ByteArrayClassPath(String name, byte[] classfile) { 
57           this.classname = name; 
58           this.classfile = classfile; 
59       } 
60    
61       /** 
62        * Closes this class path. 
63        */ 
64       public void close() { 
65       } 
66    
67       public String toString() { 
68           return "byte[]:" + classname; 
69       } 
70    
71       /** 
72        * Opens a class file. 
73        */ 
74       public InputStream openClassfile(String classname) { 
75           if (this.classname.equals(classname)) 
76               return new ByteArrayInputStream(classfile); 
77           else 
78               return null; 
79       } 
80   } 
81