Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * 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;
 }
Exemplo n.º 3
0
 @Override
 protected void cacheCtClass(String className, CtClass c, boolean dynamic) {
   if (isSrg && !exclude(className)) {
     return;
   }
   super.cacheCtClass(className, c, dynamic);
 }
Exemplo n.º 4
0
  /*
   * 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);
  }
Exemplo n.º 5
0
  /**
   * 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;
  }
Exemplo n.º 6
0
 /**
  * Creates a new class (or interface) from the given class file. If there already exists a class
  * with the same name, this method returns the existing class; a new class is never created from
  * the given class file.
  *
  * <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 the class file.
  * @see #makeClass(InputStream)
  * @see javassist.ByteArrayClassPath
  * @since 3.9
  */
 public CtClass makeClassIfNew(InputStream classfile) throws IOException, RuntimeException {
   compress();
   classfile = new BufferedInputStream(classfile);
   CtClass clazz = new CtClassType(classfile, this);
   clazz.checkModify();
   String classname = clazz.getName();
   CtClass found = checkNotExists(classname);
   if (found != null) return found;
   else {
     cacheCtClass(classname, clazz, true);
     return clazz;
   }
 }
Exemplo n.º 7
0
  /**
   * @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) {
      // 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;
  }
Exemplo n.º 8
0
 /**
  * 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;
 }