📄 classpool.java
字号:
* since <code>get()</code> does not search the class path at all * if the given name is an invalid name recorded by this method. * Note that searching the class path takes relatively long time. * * @param name a class name (separeted by dot). */ public void recordInvalidClassName(String name) { source.recordInvalidClassName(name); } /** * Records the <code>$cflow</code> variable for the field specified * by <code>cname</code> and <code>fname</code>. * * @param name variable name * @param cname class name * @param fname field name */ void recordCflow(String name, String cname, String fname) { if (cflow == null) cflow = new Hashtable(); cflow.put(name, new Object[] { cname, fname }); } /** * Undocumented method. Do not use; internal-use only. * * @param name the name of <code>$cflow</code> variable */ public Object[] lookupCflow(String name) { if (cflow == null) cflow = new Hashtable(); return (Object[])cflow.get(name); } /** * Reads a class file and constructs a <code>CtClass</code> * object with a new name. * This method is useful if you want to generate a new class as a copy * of another class (except the class name). For example, * * <ul><pre> * getAndRename("Point", "Pair") * </pre></ul> * * returns a <code>CtClass</code> object representing <code>Pair</code> * class. The definition of <code>Pair</code> is the same as that of * <code>Point</code> class except the class name since <code>Pair</code> * is defined by reading <code>Point.class</code>. * * @param orgName the original (fully-qualified) class name * @param newName the new class name */ public CtClass getAndRename(String orgName, String newName) throws NotFoundException { CtClass clazz = get0(orgName, false); if (clazz == null) throw new NotFoundException(orgName); if (clazz instanceof CtClassType) ((CtClassType)clazz).setClassPool(this); clazz.setName(newName); // indirectly calls // classNameChanged() in this class return clazz; } /* * This method is invoked by CtClassType.setName(). It removes a * CtClass object from the hash table and inserts it with the new * name. Don't delegate to the parent. */ synchronized void classNameChanged(String oldname, CtClass clazz) { CtClass c = (CtClass)getCached(oldname); if (c == clazz) // must check this equation. removeCached(oldname); // see getAndRename(). String newName = clazz.getName(); checkNotFrozen(newName); cacheCtClass(newName, clazz, false); } /** * Reads a class file from the source and returns a reference * to the <code>CtClass</code> * object representing that class file. If that class file has been * already read, this method returns a reference to the * <code>CtClass</code> created when that class file was read at the * first time. * * <p>If <code>classname</code> ends with "[]", then this method * returns a <code>CtClass</code> object for that array type. * * <p>To obtain an inner class, use "$" instead of "." for separating * the enclosing class name and the inner class name. * * @param classname a fully-qualified class name. */ public CtClass get(String classname) throws NotFoundException { CtClass clazz; if (classname == null) clazz = null; else clazz = get0(classname, true); if (clazz == null) throw new NotFoundException(classname); else { clazz.incGetCounter(); return clazz; } } /** * @param useCache false if the cached CtClass must be ignored. * @param searchParent false if the parent class pool is not searched. * @return null if the class could not be found. */ protected synchronized CtClass get0(String classname, boolean useCache) throws NotFoundException { CtClass clazz = null; if (useCache) { clazz = getCached(classname); if (clazz != null) return clazz; } if (!childFirstLookup && parent != null) { clazz = parent.get0(classname, useCache); if (clazz != null) return clazz; } clazz = createCtClass(classname, useCache); if (clazz != null) { if (useCache) cacheCtClass(classname, clazz, false); return clazz; } if (childFirstLookup && parent != null) clazz = parent.get0(classname, useCache); return clazz; } /** * Creates a CtClass object representing the specified class. * It first examines whether or not the corresponding class * file exists. If yes, it creates a CtClass object. * * @return null if the class file could not be found. */ protected CtClass createCtClass(String classname, boolean useCache) { // accept "[L<class name>;" as a class name. if (classname.charAt(0) == '[') classname = Descriptor.toClassName(classname); if (classname.endsWith("[]")) { String base = classname.substring(0, classname.indexOf('[')); if ((!useCache || getCached(base) == null) && find(base) == null) return null; else return new CtArray(classname, this); } else if (find(classname) == null) return null; else return new CtClassType(classname, this); } /** * Searches the class path to obtain the URL of the class file * specified by classname. It is also used to determine whether * the class file exists. * * @param classname a fully-qualified class name. * @return null if the class file could not be found. * @see CtClass#getURL() */ public URL find(String classname) { return source.find(classname); } /* * Is invoked by CtClassType.setName() and methods in this class. * This method throws an exception if the class is already frozen or * if this class pool cannot edit the class since it is in a parent * class pool. */ void checkNotFrozen(String classname) throws RuntimeException { CtClass clazz = getCached(classname); if (clazz == null) { if (!childFirstLookup && parent != null) { try { clazz = parent.get0(classname, true); } catch (NotFoundException e) {} if (clazz != null) throw new RuntimeException(classname + " is in a parent ClassPool. Use the parent."); } } else if (clazz.isFrozen()) throw new RuntimeException(classname + ": frozen class (cannot edit)"); } /* for CtClassType.getClassFile2(). Don't delegate to the parent. */ InputStream openClassfile(String classname) throws NotFoundException { return source.openClassfile(classname); } void writeClassfile(String classname, OutputStream out) throws NotFoundException, IOException, CannotCompileException { source.writeClassfile(classname, out); } /** * Reads class files from the source and returns an array of * <code>CtClass</code> * objects representing those class files. * * <p>If an element of <code>classnames</code> ends with "[]", * then this method * returns a <code>CtClass</code> object for that array type. * * @param classnames an array of fully-qualified class name. */ public CtClass[] get(String[] classnames) throws NotFoundException { if (classnames == null) return new CtClass[0]; int num = classnames.length; CtClass[] result = new CtClass[num]; for (int i = 0; i < num; ++i) result[i] = get(classnames[i]); return result; } /** * Reads a class file and obtains a compile-time method. * * @param classname the class name * @param methodname the method name * @see CtClass#getDeclaredMethod(String) */ public CtMethod getMethod(String classname, String methodname) throws NotFoundException { CtClass c = get(classname); return c.getDeclaredMethod(methodname); } /** * Creates a new class (or interface) from the given class file. * If there already exists a class with the same name, the new class * overwrites that previous class. * * <p>This method is used for creating a <code>CtClass</code> object * directly from a class file. The qualified class name is obtained * from the class file; you do not have to explicitly give the name. * * @param classfile class file. * @throws RuntimeException if there is a frozen class with the * the same name. * @see javassist.ByteArrayClassPath */ public CtClass makeClass(InputStream classfile) throws IOException, RuntimeException { classfile = new BufferedInputStream(classfile); CtClass clazz = new CtClassType(classfile, this); clazz.checkModify(); String classname = clazz.getName(); checkNotFrozen(classname); cacheCtClass(classname, clazz, true); return clazz; } /** * Creates a new public class. * If there already exists a class with the same name, the new class * overwrites that previous class. * * @param classname a fully-qualified class name. * @throws RuntimeException if the existing class is frozen. */ public CtClass makeClass(String classname) throws RuntimeException { return makeClass(classname, null); } /** * Creates a new public class. * If there already exists a class/interface with the same name, * the new class overwrites that previous class. * * @param classname a fully-qualified class name.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -