Home | History | Annotate | Download | only in javassist
      1 /*
      2  * Javassist, a Java-bytecode translator toolkit.
      3  * Copyright (C) 1999-2007 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 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     public String toString() {
     70         return "byte[]:" + classname;
     71     }
     72 
     73     /**
     74      * Opens the class file.
     75      */
     76     public InputStream openClassfile(String classname) {
     77         if(this.classname.equals(classname))
     78             return new ByteArrayInputStream(classfile);
     79         else
     80             return null;
     81     }
     82 
     83     /**
     84      * Obtains the URL.
     85      */
     86     public URL find(String classname) {
     87         if(this.classname.equals(classname)) {
     88             String cname = classname.replace('.', '/') + ".class";
     89             try {
     90                 // return new File(cname).toURL();
     91                 return new URL("file:/ByteArrayClassPath/" + cname);
     92             }
     93             catch (MalformedURLException e) {}
     94         }
     95 
     96         return null;
     97     }
     98 }
     99