1 /* 2 * Javassist, a Java-bytecode translator toolkit. 3 * Copyright (C) 1999- 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 * or the Apache License Version 2.0. 10 * 11 * Software distributed under the License is distributed on an "AS IS" basis, 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 * for the specific language governing rights and limitations under the 14 * License. 15 */ 16 17 package javassist; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.net.MalformedURLException; 23 import java.net.URL; 24 import java.net.URLConnection; 25 import java.net.URLStreamHandler; 26 27 /** 28 * A <code>ByteArrayClassPath</code> contains bytes that is served as 29 * a class file to a <code>ClassPool</code>. It is useful to convert 30 * a byte array to a <code>CtClass</code> object. 31 * 32 * <p>For example, if you want to convert a byte array <code>b</code> 33 * into a <code>CtClass</code> object representing the class with a name 34 * <code>classname</code>, then do as following: 35 * 36 * <pre> 37 * ClassPool cp = ClassPool.getDefault(); 38 * cp.insertClassPath(new ByteArrayClassPath(classname, b)); 39 * CtClass cc = cp.get(classname); 40 * </pre> 41 * 42 * <p>The <code>ClassPool</code> object <code>cp</code> uses the created 43 * <code>ByteArrayClassPath</code> object as the source of the class file. 44 * 45 * <p>A <code>ByteArrayClassPath</code> must be instantiated for every 46 * class. It contains only a single class file. 47 * 48 * @see javassist.ClassPath 49 * @see ClassPool#insertClassPath(ClassPath) 50 * @see ClassPool#appendClassPath(ClassPath) 51 * @see ClassPool#makeClass(InputStream) 52 */ 53 public class ByteArrayClassPath implements ClassPath { 54 protected String classname; 55 protected byte[] classfile; 56 57 /* 58 * Creates a <code>ByteArrayClassPath</code> containing the given 59 * bytes. 60 * 61 * @param name a fully qualified class name 62 * @param classfile the contents of a class file. 63 */ ByteArrayClassPath(String name, byte[] classfile)64 public ByteArrayClassPath(String name, byte[] classfile) { 65 this.classname = name; 66 this.classfile = classfile; 67 } 68 69 @Override toString()70 public String toString() { 71 return "byte[]:" + classname; 72 } 73 74 /** 75 * Opens the class file. 76 */ 77 @Override openClassfile(String classname)78 public InputStream openClassfile(String classname) { 79 if(this.classname.equals(classname)) 80 return new ByteArrayInputStream(classfile); 81 return null; 82 } 83 84 /** 85 * Obtains the URL. 86 */ 87 @Override find(String classname)88 public URL find(String classname) { 89 if(this.classname.equals(classname)) { 90 String cname = classname.replace('.', '/') + ".class"; 91 try { 92 return new URL(null, "file:/ByteArrayClassPath/" + cname, new BytecodeURLStreamHandler()); 93 } 94 catch (MalformedURLException e) {} 95 } 96 97 return null; 98 } 99 100 private class BytecodeURLStreamHandler extends URLStreamHandler { openConnection(final URL u)101 protected URLConnection openConnection(final URL u) { 102 return new BytecodeURLConnection(u); 103 } 104 } 105 106 private class BytecodeURLConnection extends URLConnection { BytecodeURLConnection(URL url)107 protected BytecodeURLConnection(URL url) { 108 super(url); 109 } 110 connect()111 public void connect() throws IOException { 112 } 113 getInputStream()114 public InputStream getInputStream() throws IOException { 115 return new ByteArrayInputStream(classfile); 116 } 117 getContentLength()118 public int getContentLength() { 119 return classfile.length; 120 } 121 } 122 } 123