CtClass checkNotExists(String classname) { CtClass clazz = getCached(classname); if (clazz == null) if (!childFirstLookup && parent != null) { try { clazz = parent.get0(classname, true); } catch (NotFoundException e) {} } return clazz; }
clazz = get0(classname, true);
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)"); }
/** * @param useCache false if the cached CtClass must be ignored. * @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) { // clazz.getName() != classname if classname is "[L<name>;". if (useCache) cacheCtClass(clazz.getName(), clazz, false); return clazz; } if (childFirstLookup && parent != null) clazz = parent.get0(classname, useCache); return clazz; }
/** * 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; } }
/** * 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, * * <pre> * getAndRename("Point", "Pair") * </pre> * * 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; }