/** * Lock a class into the cache * * @param c * the class */ public void lockInCache(CtClass c) { super.cacheCtClass(c.getName(), c, false); }
/** * Creates a new public nested class. * This method is called by {@link 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. * @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; }
/** * Get any local copy of the class * * @param classname * the class name * @return the class * @throws NotFoundException * when the class is not found */ public synchronized CtClass getLocally(String classname) throws NotFoundException { softcache.remove(classname); CtClass clazz = (CtClass)classes.get(classname); if (clazz == null) { clazz = createCtClass(classname, true); if (clazz == null) throw new NotFoundException(classname); super.cacheCtClass(classname, clazz, false); } return clazz; }
/** * Creates a new public class. * If there already exists a class/interface with the same name, * the new class overwrites that previous class. * * <p>If no constructor is explicitly added to the created new * class, Javassist generates constructors and adds it when * the class file is generated. It generates a new constructor * for each constructor of the super class. The new constructor * takes the same set of parameters and invokes the * corresponding constructor of the super class. All the received * parameters are passed to it. * * @param classname a fully-qualified class name. * @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; }
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); }
/** * Cache a class * * @param classname * the class name * @param c * the ctClass * @param dynamic * whether the class is dynamically generated */ protected void cacheCtClass(String classname, CtClass c, boolean dynamic) { if (dynamic) { super.cacheCtClass(classname, c, dynamic); } else { if (repository.isPrune()) c.prune(); softcache.put(classname, c); } }
/** * Removes this <code>CtClass</code> object from the * <code>ClassPool</code>. * After this method is called, any method cannot be called on the * removed <code>CtClass</code> object. * * <p>If <code>get()</code> in <code>ClassPool</code> is called * with the name of the removed method, * the <code>ClassPool</code> will read the class file again * and constructs another <code>CtClass</code> object representing * the same class. */ public void detach() { ClassPool cp = getClassPool(); CtClass obj = cp.removeCached(getName()); if (obj != this) cp.cacheCtClass(getName(), obj, false); }
return found; else { cacheCtClass(classname, clazz, true); return clazz;
/** * 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. * @param ifNotFrozen throws a RuntimeException if this parameter is true * and there is a frozen class with the same name. * @see javassist.ByteArrayClassPath */ public CtClass makeClass(InputStream classfile, boolean ifNotFrozen) throws IOException, RuntimeException { compress(); classfile = new BufferedInputStream(classfile); CtClass clazz = new CtClassType(classfile, this); clazz.checkModify(); String classname = clazz.getName(); if (ifNotFrozen) checkNotFrozen(classname); cacheCtClass(classname, clazz, true); return clazz; }
/** * 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. * @param ifNotFrozen throws a RuntimeException if this parameter is true * and there is a frozen class with the same name. * @since 3.20 */ public CtClass makeClass(ClassFile classfile, boolean ifNotFrozen) throws RuntimeException { compress(); CtClass clazz = new CtClassType(classfile, this); clazz.checkModify(); String classname = clazz.getName(); if (ifNotFrozen) checkNotFrozen(classname); cacheCtClass(classname, clazz, true); return clazz; }
/** * @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; }