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.InputStream; 20 import java.net.URL; 21 22 /** 23 * A search-path for obtaining a class file 24 * by <code>getResourceAsStream()</code> in <code>java.lang.Class</code>. 25 * 26 * <p>Try adding a <code>ClassClassPath</code> when a program is running 27 * with a user-defined class loader and any class files are not found with 28 * the default <code>ClassPool</code>. For example, 29 * 30 * <pre> 31 * ClassPool cp = ClassPool.getDefault(); 32 * cp.insertClassPath(new ClassClassPath(this.getClass())); 33 * </pre> 34 * 35 * This code snippet permanently adds a <code>ClassClassPath</code> 36 * to the default <code>ClassPool</code>. Note that the default 37 * <code>ClassPool</code> is a singleton. The added 38 * <code>ClassClassPath</code> uses a class object representing 39 * the class including the code snippet above. 40 * 41 * <p>Class files in a named module are private to that module. 42 * This method cannot obtain class files in named modules. 43 * </p> 44 * 45 * @see ClassPool#insertClassPath(ClassPath) 46 * @see ClassPool#appendClassPath(ClassPath) 47 * @see LoaderClassPath 48 */ 49 public class ClassClassPath implements ClassPath { 50 private Class<?> thisClass; 51 52 /** Creates a search path. 53 * 54 * @param c the <code>Class</code> object used to obtain a class 55 * file. <code>getResourceAsStream()</code> is called on 56 * this object. 57 */ ClassClassPath(Class<?> c)58 public ClassClassPath(Class<?> c) { 59 thisClass = c; 60 } 61 ClassClassPath()62 ClassClassPath() { 63 /* The value of thisClass was this.getClass() in early versions: 64 * 65 * thisClass = this.getClass(); 66 * 67 * However, this made openClassfile() not search all the system 68 * class paths if javassist.jar is put in jre/lib/ext/ 69 * (with JDK1.4). 70 */ 71 this(java.lang.Object.class); 72 } 73 74 /** 75 * Obtains a class file by <code>getResourceAsStream()</code>. 76 */ 77 @Override openClassfile(String classname)78 public InputStream openClassfile(String classname) throws NotFoundException { 79 String filename = '/' + classname.replace('.', '/') + ".class"; 80 return thisClass.getResourceAsStream(filename); 81 } 82 83 /** 84 * Obtains the URL of the specified class file. 85 * 86 * @return null if the class file could not be found. 87 */ 88 @Override find(String classname)89 public URL find(String classname) { 90 String filename = '/' + classname.replace('.', '/') + ".class"; 91 return thisClass.getResource(filename); 92 } 93 94 @Override toString()95 public String toString() { 96 return thisClass.getName() + ".class"; 97 } 98 } 99