📄 classpool.java
字号:
* @param superclass the super class. * @throws RuntimeException if the existing class is frozen. */ public synchronized CtClass makeClass(String classname, CtClass superclass) throws RuntimeException { checkNotFrozen(classname); CtClass clazz = new CtNewClass(classname, this, false, superclass); cacheCtClass(classname, clazz, true); return clazz; } /** * Creates a new public nested class. * This method is called by CtClassType.makeNestedClass(). * * @param classname a fully-qualified class name. * @return the nested class. */ synchronized CtClass makeNestedClass(String classname) { checkNotFrozen(classname); CtClass clazz = new CtNewNestedClass(classname, this, false, null); cacheCtClass(classname, clazz, true); return clazz; } /** * Creates a new public interface. * If there already exists a class/interface with the same name, * the new interface overwrites that previous one. * * @param name a fully-qualified interface name. * @throws RuntimeException if the existing interface is frozen. */ public CtClass makeInterface(String name) throws RuntimeException { return makeInterface(name, null); } /** * Creates a new public interface. * If there already exists a class/interface with the same name, * the new interface overwrites that previous one. * * @param name a fully-qualified interface name. * @param superclass the super interface. * @throws RuntimeException if the existing interface is frozen. */ public synchronized CtClass makeInterface(String name, CtClass superclass) throws RuntimeException { checkNotFrozen(name); CtClass clazz = new CtNewClass(name, this, true, superclass); cacheCtClass(name, clazz, true); return clazz; } /** * Appends the system search path to the end of the * search path. The system search path * usually includes the platform library, extension * libraries, and the search path specified by the * <code>-classpath</code> option or the <code>CLASSPATH</code> * environment variable. * * @return the appended class path. */ public ClassPath appendSystemPath() { return source.appendSystemPath(); } /** * Insert a <code>ClassPath</code> object at the head of the * search path. * * @return the inserted class path. * @see javassist.ClassPath * @see javassist.URLClassPath * @see javassist.ByteArrayClassPath */ public ClassPath insertClassPath(ClassPath cp) { return source.insertClassPath(cp); } /** * Appends a <code>ClassPath</code> object to the end of the * search path. * * @return the appended class path. * @see javassist.ClassPath * @see javassist.URLClassPath * @see javassist.ByteArrayClassPath */ public ClassPath appendClassPath(ClassPath cp) { return source.appendClassPath(cp); } /** * Inserts a directory or a jar (or zip) file at the head of the * search path. * * @param pathname the path name of the directory or jar file. * It must not end with a path separator ("/"). * @return the inserted class path. * @throws NotFoundException if the jar file is not found. */ public ClassPath insertClassPath(String pathname) throws NotFoundException { return source.insertClassPath(pathname); } /** * Appends a directory or a jar (or zip) file to the end of the * search path. * * @param pathname the path name of the directory or jar file. * It must not end with a path separator ("/"). * @return the appended class path. * @throws NotFoundException if the jar file is not found. */ public ClassPath appendClassPath(String pathname) throws NotFoundException { return source.appendClassPath(pathname); } /** * Detatches the <code>ClassPath</code> object from the search path. * The detached <code>ClassPath</code> object cannot be added * to the pathagain. */ public void removeClassPath(ClassPath cp) { source.removeClassPath(cp); } /** * Appends directories and jar files for search. * * <p>The elements of the given path list must be separated by colons * in Unix or semi-colons in Windows. * * @param pathlist a (semi)colon-separated list of * the path names of directories and jar files. * The directory name must not end with a path * separator ("/"). * @throws NotFoundException if a jar file is not found. */ public void appendPathList(String pathlist) throws NotFoundException { char sep = File.pathSeparatorChar; int i = 0; for (;;) { int j = pathlist.indexOf(sep, i); if (j < 0) { appendClassPath(pathlist.substring(i)); break; } else { appendClassPath(pathlist.substring(i, j)); i = j + 1; } } } /** * Converts the given class to a <code>java.lang.Class</code> object. * Once this method is called, further modifications are not * allowed any more. * To load the class, this method uses the context class loader * of the current thread. It is obtained by calling * <code>getClassLoader()</code>. * * <p>This behavior can be changed by subclassing the pool and changing * the <code>getClassLoader()</code> method. * If the program is running on some application * server, the context class loader might be inappropriate to load the * class. * * <p>This method is provided for convenience. If you need more * complex functionality, you should write your own class loader. * * <p><b>Warining:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * * @see #toClass(CtClass, java.lang.ClassLoader, ProtectionDomain) * @see #getClassLoader() */ public Class toClass(CtClass clazz) throws CannotCompileException { // Some subclasses of ClassPool may override toClass(CtClass,ClassLoader). // So we should call that method instead of toClass(.., ProtectionDomain). return toClass(clazz, getClassLoader()); } /** * Get the classloader for <code>toClass()</code>, <code>getAnnotations()</code> in * <code>CtClass</code>, etc. * * <p>The default is the context class loader. * * @return the classloader for the pool * @see #toClass(CtClass) * @see CtClass#getAnnotations() */ public ClassLoader getClassLoader() { return getContextClassLoader(); } /** * Obtains a class loader that seems appropriate to look up a class * by name. */ static ClassLoader getContextClassLoader() { return Thread.currentThread().getContextClassLoader(); } /** * Converts the class to a <code>java.lang.Class</code> object. * Do not override this method any more at a subclass because * <code>toClass(CtClass)</code> never calls this method. * * <p><b>Warining:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * * @deprecated Replaced by {@link #toClass(CtClass,ClassLoader,ProtectionDomain)}. * A subclass of <code>ClassPool</code> that has been * overriding this method should be modified. It should override * {@link #toClass(CtClass,ClassLoader,ProtectionDomain)}. */ public Class toClass(CtClass ct, ClassLoader loader) throws CannotCompileException { return toClass(ct, loader, null); } /** * Converts the class to a <code>java.lang.Class</code> object. * Once this method is called, further modifications are not allowed * any more. * * <p>The class file represented by the given <code>CtClass</code> is * loaded by the given class loader to construct a * <code>java.lang.Class</code> object. Since a private method * on the class loader is invoked through the reflection API, * the caller must have permissions to do that. * * <p>An easy way to obtain <code>ProtectionDomain</code> object is * to call <code>getProtectionDomain()</code> * in <code>java.lang.Class</code>. It returns the domain that the * class belongs to. * * <p>This method is provided for convenience. If you need more * complex functionality, you should write your own class loader. * * @param loader the class loader used to load this class. * For example, the loader returned by * <code>getClassLoader()</code> can be used * for this parameter. * @param domain the protection domain for the class. * If it is null, the default domain created * by <code>java.lang.ClassLoader</code> is used. * * @see #getClassLoader() * @since 3.3 */ public Class toClass(CtClass ct, ClassLoader loader, ProtectionDomain domain) throws CannotCompileException { try { byte[] b = ct.toBytecode(); java.lang.reflect.Method method; Object[] args; if (domain == null) { method = defineClass1; args = new Object[] { ct.getName(), b, new Integer(0), new Integer(b.length)}; } else { method = defineClass2; args = new Object[] { ct.getName(), b, new Integer(0), new Integer(b.length), domain}; } return toClass2(method, loader, args); } catch (RuntimeException e) { throw e; } catch (java.lang.reflect.InvocationTargetException e) { throw new CannotCompileException(e.getTargetException()); } catch (Exception e) { throw new CannotCompileException(e); } } private static synchronized Class toClass2(Method method, ClassLoader loader, Object[] args) throws Exception { method.setAccessible(true); Class clazz = (Class)method.invoke(loader, args); method.setAccessible(false); return clazz; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -