Package javassist

Class CtClassType

java.lang.Object
javassist.CtClass
javassist.CtClassType
Direct Known Subclasses:
CtNewClass

class CtClassType extends CtClass
Classinvalid input: '<'?> types.
  • Field Details

    • classPool

      ClassPool classPool
    • wasChanged

      boolean wasChanged
    • wasFrozen

      private boolean wasFrozen
    • wasPruned

      boolean wasPruned
    • gcConstPool

      boolean gcConstPool
    • classfile

      ClassFile classfile
    • rawClassfile

      byte[] rawClassfile
    • memberCache

      private Reference<CtMember.Cache> memberCache
    • accessors

      private AccessorMaker accessors
    • fieldInitializers

      private FieldInitLink fieldInitializers
    • hiddenMethods

      private Map<CtMethod,String> hiddenMethods
    • uniqueNumberSeed

      private int uniqueNumberSeed
    • doPruning

      private boolean doPruning
    • getCount

      private int getCount
    • GET_THRESHOLD

      private static final int GET_THRESHOLD
      See Also:
  • Constructor Details

  • Method Details

    • extendToString

      protected void extendToString(StringBuilder buffer)
      Description copied from class: CtClass
      Implemented in subclasses to add to the CtClass.toString() result. Subclasses should put a space before each token added to the buffer.
      Overrides:
      extendToString in class CtClass
    • exToString

      private void exToString(StringBuilder buffer, String msg, CtMember head, CtMember tail)
    • getAccessorMaker

      public AccessorMaker getAccessorMaker()
      Description copied from class: CtClass
      Undocumented method. Do not use; internal-use only.
      Overrides:
      getAccessorMaker in class CtClass
    • getClassFile2

      public ClassFile getClassFile2()
      Description copied from class: CtClass
      Returns a class file for this class (read only). Normal applications do not need calling this method. Use getClassFile().

      The ClassFile object obtained by this method is read only. Changes to this object might not be reflected on a class file generated by toBytecode(), toClass(), etc.

      This method is available even if isFrozen() is true. However, if the class is frozen, it might be also pruned.

      Overrides:
      getClassFile2 in class CtClass
      See Also:
    • getClassFile3

      public ClassFile getClassFile3(boolean doCompress)
    • incGetCounter

      final void incGetCounter()
      Overrides:
      incGetCounter in class CtClass
    • compress

      void compress()
      Invoked from ClassPool#compress(). It releases the class files that have not been recently used if they are unmodified.
      Overrides:
      compress in class CtClass
    • saveClassFile

      private void saveClassFile()
      Converts a ClassFile object into a byte array for saving memory space.
    • removeClassFile

      private void removeClassFile()
    • setClassFile

      private ClassFile setClassFile(ClassFile cf)
      Updates classfile if it is null.
    • getClassPool

      public ClassPool getClassPool()
      Description copied from class: CtClass
      Returns a ClassPool for this class.
      Overrides:
      getClassPool in class CtClass
    • setClassPool

      void setClassPool(ClassPool cp)
    • getURL

      public URL getURL() throws NotFoundException
      Description copied from class: CtClass
      Returns the uniform resource locator (URL) of the class file.
      Overrides:
      getURL in class CtClass
      Throws:
      NotFoundException
    • isModified

      public boolean isModified()
      Description copied from class: CtClass
      Returns true if the definition of the class has been modified.
      Overrides:
      isModified in class CtClass
    • isFrozen

      public boolean isFrozen()
      Description copied from class: CtClass
      Returns true if the class has been loaded or written out and thus it cannot be modified any more.
      Overrides:
      isFrozen in class CtClass
      See Also:
    • freeze

      public void freeze()
      Description copied from class: CtClass
      Makes the class frozen.
      Overrides:
      freeze in class CtClass
      See Also:
    • checkModify

      void checkModify() throws RuntimeException
      Overrides:
      checkModify in class CtClass
      Throws:
      RuntimeException
    • defrost

      public void defrost()
      Description copied from class: CtClass
      Defrosts the class so that the class can be modified again.

      To avoid changes that will be never reflected, the class is frozen to be unmodifiable if it is loaded or written out. This method should be called only in a case that the class will be reloaded or written out later again.

      If defrost() will be called later, pruning must be disallowed in advance.

      Overrides:
      defrost in class CtClass
      See Also:
    • subtypeOf

      public boolean subtypeOf(CtClass clazz) throws NotFoundException
      Description copied from class: CtClass
      Returns true if this class extends or implements clazz. It also returns true if this class is the same as clazz.
      Overrides:
      subtypeOf in class CtClass
      Throws:
      NotFoundException
    • setName

      public void setName(String name) throws RuntimeException
      Description copied from class: CtClass
      Sets the class name
      Overrides:
      setName in class CtClass
      Parameters:
      name - fully-qualified name
      Throws:
      RuntimeException
    • getGenericSignature

      public String getGenericSignature()
      Description copied from class: CtClass
      Returns the generic signature of the class.

      The generics of Java is implemented by the erasure technique. After compilation, all type parameters are dropped off from the main part of a class file. However, for reflection, the type parameters are encoded into generic signatures and attached to a class file.

      Overrides:
      getGenericSignature in class CtClass
      Returns:
      null if the generic signature is not included.
      See Also:
    • setGenericSignature

      public void setGenericSignature(String sig)
      Description copied from class: CtClass
      Sets the generic signature of the class.

      The generics of Java is implemented by the erasure technique. After compilation, all type parameters are dropped off from the main part of a class file. However, for reflection, the type parameters must be encoded into generic signatures and attached to a class file.

      For example,

      class List<T> {
           T value;
           T get() { return value; }
           void set(T v) { value = v; }
       }
       

      this class is generated by the following code:

       ClassPool pool = ClassPool.getDefault();
       CtClass cc = pool.makeClass("List");
       CtClass objectClass = pool.get(CtClass.javaLangObject);
       ClassSignature cs = new ClassSignature(
                               new TypeParameter[] { new TypeParameter("T") });
       cc.setGenericSignature(cs.encode());    // <T:Ljava/lang/Object;>Ljava/lang/Object;
      
       CtField f = new CtField(objClass, "value", cc);
       TypeVariable tvar = new TypeVariable("T");
       f.setGenericSignature(tvar.encode());   // TT;
       cc.addField(f);
      
       CtMethod m = CtNewMethod.make("public Object get(){return value;}", cc);
       MethodSignature ms = new MethodSignature(null, null, tvar, null);
       m.setGenericSignature(ms.encode());     // ()TT;
       cc.addMethod(m);
      
       CtMethod m2 = CtNewMethod.make("public void set(Object v){value = v;}", cc);
       MethodSignature ms2 = new MethodSignature(null, new Type[] { tvar },
                                                 new BaseType("void"), null);
       m2.setGenericSignature(ms2.encode());   // (TT;)V;
       cc.addMethod(m2);
      
       cc.writeFile();
       

      The generated class file is equivalent to the following:

      class List {
           Object value;
           Object get() { return value; }
           void set(Object v) { value = v; }
       }

      but it includes generic signatures for the class, the field, and the methods so that the type variable T can be accessible through reflection.

      MethodSignature is a utility class. You can directly pass the signature string to the setGenericSignature method. For the specification of the signatures, see Section 4.7.9.1 Signatures of The Java Virtual Machine Specification (Java SE 8).

      Overrides:
      setGenericSignature in class CtClass
      Parameters:
      sig - a generic signature.
      See Also:
    • replaceClassName

      public void replaceClassName(ClassMap classnames) throws RuntimeException
      Description copied from class: CtClass
      Changes class names appearing in the class file according to the given map.

      All the class names appearing in the class file are tested with map to determine whether each class name is replaced or not. Thus this method can be used for collecting all the class names in the class file. To do that, first define a subclass of ClassMap so that get() records all the given parameters. Then, make an instance of that subclass as an empty hash-table. Finally, pass that instance to this method. After this method finishes, that instance would contain all the class names appearing in the class file.

      Overrides:
      replaceClassName in class CtClass
      Parameters:
      classnames - the hashtable associating replaced class names with substituted names.
      Throws:
      RuntimeException
    • replaceClassName

      public void replaceClassName(String oldname, String newname) throws RuntimeException
      Description copied from class: CtClass
      Substitutes newName for all occurrences of a class name oldName in the class file.
      Overrides:
      replaceClassName in class CtClass
      Parameters:
      oldname - replaced class name
      newname - substituted class name
      Throws:
      RuntimeException
    • isInterface

      public boolean isInterface()
      Description copied from class: CtClass
      Determines whether this object represents a class or an interface. It returns true if this object represents an interface.
      Overrides:
      isInterface in class CtClass
    • isAnnotation

      public boolean isAnnotation()
      Description copied from class: CtClass
      Determines whether this object represents an annotation type. It returns true if this object represents an annotation type.
      Overrides:
      isAnnotation in class CtClass
    • isEnum

      public boolean isEnum()
      Description copied from class: CtClass
      Determines whether this object represents an enum. It returns true if this object represents an enum.
      Overrides:
      isEnum in class CtClass
    • getModifiers

      public int getModifiers()
      Description copied from class: CtClass
      Returns the modifiers for this class, encoded in an integer. For decoding, use javassist.Modifier.

      If the class is a static nested class (a.k.a. static inner class), the returned modifiers include Modifier.STATIC.

      Overrides:
      getModifiers in class CtClass
      See Also:
    • getNestedClasses

      public CtClass[] getNestedClasses() throws NotFoundException
      Description copied from class: CtClass
      Returns an array of nested classes declared in the class. Nested classes are inner classes, anonymous classes, local classes, and static nested classes.
      Overrides:
      getNestedClasses in class CtClass
      Throws:
      NotFoundException
    • setModifiers

      public void setModifiers(int mod)
      Description copied from class: CtClass
      Sets the modifiers.

      If the class is a nested class, this method also modifies the class declaring that nested class (i.e. the enclosing class is modified).

      Overrides:
      setModifiers in class CtClass
      Parameters:
      mod - modifiers encoded by javassist.Modifier
      See Also:
    • updateInnerEntry

      private static void updateInnerEntry(int newMod, String name, CtClass clazz, boolean outer)
    • hasAnnotation

      public boolean hasAnnotation(String annotationName)
      Description copied from class: CtClass
      Returns true if the class has the specified annotation type.
      Overrides:
      hasAnnotation in class CtClass
      Parameters:
      annotationName - the name of annotation type.
      Returns:
      true if the annotation is found, otherwise false.
    • hasAnnotationType

      @Deprecated static boolean hasAnnotationType(Class<?> clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2)
      Deprecated.
    • hasAnnotationType

      static boolean hasAnnotationType(String annotationTypeName, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2)
    • getAnnotation

      public Object getAnnotation(Class<?> clz) throws ClassNotFoundException
      Description copied from class: CtClass
      Returns the annotation if the class has the specified annotation type. For example, if an annotation @Author is associated with this class, an Author object is returned. The member values can be obtained by calling methods on the Author object.
      Overrides:
      getAnnotation in class CtClass
      Parameters:
      clz - the annotation type.
      Returns:
      the annotation if found, otherwise null.
      Throws:
      ClassNotFoundException
    • getAnnotationType

      static Object getAnnotationType(Class<?> clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • getAnnotations

      public Object[] getAnnotations() throws ClassNotFoundException
      Description copied from class: CtClass
      Returns the annotations associated with this class. For example, if an annotation @Author is associated with this class, the returned array contains an Author object. The member values can be obtained by calling methods on the Author object.
      Overrides:
      getAnnotations in class CtClass
      Returns:
      an array of annotation-type objects.
      Throws:
      ClassNotFoundException
      See Also:
    • getAvailableAnnotations

      public Object[] getAvailableAnnotations()
      Description copied from class: CtClass
      Returns the annotations associated with this class. This method is equivalent to getAnnotations() except that, if any annotations are not on the classpath, they are not included in the returned array.
      Overrides:
      getAvailableAnnotations in class CtClass
      Returns:
      an array of annotation-type objects.
      See Also:
    • getAnnotations

      private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • toAnnotationType

      static Object[] toAnnotationType(boolean ignoreNotFound, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • toAnnotationType

      static Object[][] toAnnotationType(boolean ignoreNotFound, ClassPool cp, ParameterAnnotationsAttribute a1, ParameterAnnotationsAttribute a2, MethodInfo minfo) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • toAnnoType

      private static Object toAnnoType(Annotation anno, ClassPool cp) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • subclassOf

      public boolean subclassOf(CtClass superclass)
      Description copied from class: CtClass
      Determines whether the class directly or indirectly extends the given class. If this class extends a class A and the class A extends a class B, then subclassof(B) returns true.

      This method returns true if the given class is identical to the class represented by this object.

      Overrides:
      subclassOf in class CtClass
    • getSuperclass

      public CtClass getSuperclass() throws NotFoundException
      Description copied from class: CtClass
      Obtains the class object representing the superclass of the class. It returns null if this object represents the java.lang.Object class and thus it does not have the super class.

      If this object represents an interface, this method always returns the java.lang.Object class. To obtain the super interfaces extended by that interface, call getInterfaces().

      Overrides:
      getSuperclass in class CtClass
      Throws:
      NotFoundException
    • setSuperclass

      public void setSuperclass(CtClass clazz) throws CannotCompileException
      Description copied from class: CtClass
      Changes a super class unless this object represents an interface. The new super class must be compatible with the old one; for example, it should inherit from the old super class.

      If this object represents an interface, this method is equivalent to addInterface(); it appends clazz to the list of the super interfaces extended by that interface. Note that an interface can extend multiple super interfaces.

      Overrides:
      setSuperclass in class CtClass
      Throws:
      CannotCompileException
      See Also:
    • getInterfaces

      public CtClass[] getInterfaces() throws NotFoundException
      Description copied from class: CtClass
      Obtains the class objects representing the interfaces implemented by the class or, if this object represents an interface, the interfaces extended by that interface.
      Overrides:
      getInterfaces in class CtClass
      Throws:
      NotFoundException
    • setInterfaces

      public void setInterfaces(CtClass[] list)
      Description copied from class: CtClass
      Sets implemented interfaces. If this object represents an interface, this method sets the interfaces extended by that interface.
      Overrides:
      setInterfaces in class CtClass
      Parameters:
      list - a list of the CtClass objects representing interfaces, or null if the class implements no interfaces.
    • addInterface

      public void addInterface(CtClass anInterface)
      Description copied from class: CtClass
      Adds an interface.
      Overrides:
      addInterface in class CtClass
      Parameters:
      anInterface - the added interface.
    • getDeclaringClass

      public CtClass getDeclaringClass() throws NotFoundException
      Description copied from class: CtClass
      If this class is a member class or interface of another class, then the class enclosing this class is returned.
      Overrides:
      getDeclaringClass in class CtClass
      Returns:
      null if this class is a top-level class or an anonymous class.
      Throws:
      NotFoundException
    • getEnclosingBehavior

      public CtBehavior getEnclosingBehavior() throws NotFoundException
      Description copied from class: CtClass
      Returns the immediately enclosing method of this class. It might be not a method but a constructor. This method works only with JDK 1.5 or later.
      Overrides:
      getEnclosingBehavior in class CtClass
      Returns:
      null if this class is not a local class or an anonymous class.
      Throws:
      NotFoundException
    • makeNestedClass

      public CtClass makeNestedClass(String name, boolean isStatic)
      Description copied from class: CtClass
      Makes a new public nested class. If this method is called, the CtClass, which encloses the nested class, is modified since a class file includes a list of nested classes.

      The current implementation only supports a static nested class. isStatic must be true.

      Overrides:
      makeNestedClass in class CtClass
      Parameters:
      name - the simple name of the nested class.
      isStatic - true if the nested class is static.
    • nameReplaced

      private void nameReplaced()
    • hasMemberCache

      protected CtMember.Cache hasMemberCache()
      Returns null if members are not cached.
    • getMembers

      protected CtMember.Cache getMembers()
    • makeFieldCache

      private void makeFieldCache(CtMember.Cache cache)
    • makeBehaviorCache

      private void makeBehaviorCache(CtMember.Cache cache)
    • getFields

      public CtField[] getFields()
      Description copied from class: CtClass
      Returns an array containing CtField objects representing all the non-private fields of the class. That array includes non-private fields inherited from the superclasses.
      Overrides:
      getFields in class CtClass
    • getFields

      private static void getFields(List<CtMember> alist, CtClass cc)
    • getField

      public CtField getField(String name, String desc) throws NotFoundException
      Description copied from class: CtClass
      Returns the field with the specified name and type. The returned field may be a private field declared in a super class or interface. Unlike Java, the JVM allows a class to have multiple fields with the same name but different types.
      Overrides:
      getField in class CtClass
      Parameters:
      name - the field name.
      desc - the type descriptor of the field. It is available by CtField.getSignature().
      Throws:
      NotFoundException
      See Also:
    • checkGetField

      private CtField checkGetField(CtField f, String name, String desc) throws NotFoundException
      Throws:
      NotFoundException
    • getField2

      CtField getField2(String name, String desc)
      Overrides:
      getField2 in class CtClass
      Returns:
      null if the specified field is not found.
    • getDeclaredFields

      public CtField[] getDeclaredFields()
      Description copied from class: CtClass
      Gets all the fields declared in the class. The inherited fields are not included.

      Note: the result does not include inherited fields.

      Overrides:
      getDeclaredFields in class CtClass
    • getDeclaredField

      public CtField getDeclaredField(String name) throws NotFoundException
      Description copied from class: CtClass
      Retrieves the field with the specified name among the fields declared in the class.

      Note: this method does not search the super classes.

      Overrides:
      getDeclaredField in class CtClass
      Throws:
      NotFoundException
    • getDeclaredField

      public CtField getDeclaredField(String name, String desc) throws NotFoundException
      Description copied from class: CtClass
      Retrieves the field with the specified name and type among the fields declared in the class. Unlike Java, the JVM allows a class to have multiple fields with the same name but different types.

      Note: this method does not search the super classes.

      Overrides:
      getDeclaredField in class CtClass
      Parameters:
      name - the field name.
      desc - the type descriptor of the field. It is available by CtField.getSignature().
      Throws:
      NotFoundException
      See Also:
    • getDeclaredField2

      private CtField getDeclaredField2(String name, String desc)
    • getDeclaredBehaviors

      public CtBehavior[] getDeclaredBehaviors()
      Description copied from class: CtClass
      Gets all the constructors and methods declared in the class.
      Overrides:
      getDeclaredBehaviors in class CtClass
    • getConstructors

      public CtConstructor[] getConstructors()
      Description copied from class: CtClass
      Returns an array containing CtConstructor objects representing all the non-private constructors of the class.
      Overrides:
      getConstructors in class CtClass
    • isPubCons

      private static boolean isPubCons(CtConstructor cons)
    • getConstructor

      public CtConstructor getConstructor(String desc) throws NotFoundException
      Description copied from class: CtClass
      Returns the constructor with the given signature, which is represented by a character string called method descriptor. For details of the method descriptor, see the JVM specification or javassist.bytecode.Descriptor.
      Overrides:
      getConstructor in class CtClass
      Parameters:
      desc - method descriptor
      Throws:
      NotFoundException
      See Also:
    • getDeclaredConstructors

      public CtConstructor[] getDeclaredConstructors()
      Description copied from class: CtClass
      Gets all the constructors declared in the class.
      Overrides:
      getDeclaredConstructors in class CtClass
      See Also:
    • getClassInitializer

      public CtConstructor getClassInitializer()
      Description copied from class: CtClass
      Gets the class initializer (static constructor) declared in the class. This method returns null if no class initializer is not declared.
      Overrides:
      getClassInitializer in class CtClass
      See Also:
    • getMethods

      public CtMethod[] getMethods()
      Description copied from class: CtClass
      Returns an array containing CtMethod objects representing all the non-private methods of the class. That array includes non-private methods inherited from the superclasses.
      Overrides:
      getMethods in class CtClass
    • getMethods0

      private static void getMethods0(Map<String,CtMember> h, CtClass cc)
    • getMethod

      public CtMethod getMethod(String name, String desc) throws NotFoundException
      Description copied from class: CtClass
      Returns the method with the given name and signature. The returned method may be declared in a super class. The method signature is represented by a character string called method descriptor, which is defined in the JVM specification.
      Overrides:
      getMethod in class CtClass
      Parameters:
      name - method name
      desc - method descriptor
      Throws:
      NotFoundException
      See Also:
    • getMethod0

      private static CtMethod getMethod0(CtClass cc, String name, String desc)
    • getDeclaredMethods

      public CtMethod[] getDeclaredMethods()
      Description copied from class: CtClass
      Gets all methods declared in the class. The inherited methods are not included.
      Overrides:
      getDeclaredMethods in class CtClass
      See Also:
    • getDeclaredMethods

      public CtMethod[] getDeclaredMethods(String name) throws NotFoundException
      Description copied from class: CtClass
      Retrieves methods with the specified name among the methods declared in the class. Multiple methods with different parameters may be returned.

      Note: this method does not search the superclasses.

      Overrides:
      getDeclaredMethods in class CtClass
      Parameters:
      name - method name.
      Throws:
      NotFoundException
    • getDeclaredMethod

      public CtMethod getDeclaredMethod(String name) throws NotFoundException
      Description copied from class: CtClass
      Retrieves the method with the specified name among the methods declared in the class. If there are multiple methods with the specified name, then this method returns one of them.

      Note: this method does not search the superclasses.

      Overrides:
      getDeclaredMethod in class CtClass
      Throws:
      NotFoundException
      See Also:
    • getDeclaredMethod

      public CtMethod getDeclaredMethod(String name, CtClass[] params) throws NotFoundException
      Description copied from class: CtClass
      Retrieves the method with the specified name and parameter types among the methods declared in the class.

      Note: this method does not search the superclasses.

      Overrides:
      getDeclaredMethod in class CtClass
      Parameters:
      name - method name
      params - parameter types
      Throws:
      NotFoundException
      See Also:
    • addField

      public void addField(CtField f, String init) throws CannotCompileException
      Description copied from class: CtClass
      Adds a field with an initial value.

      The CtField belonging to another CtClass cannot be directly added to this class. Only a field created for this class can be added.

      The initial value is given as an expression written in Java. Any regular Java expression can be used for specifying the initial value. The followings are examples.

       cc.addField(f, "0")               // the initial value is 0.
       cc.addField(f, "i + 1")           // i + 1.
       cc.addField(f, "new Point()");    // a Point object.
       

      Here, the type of variable cc is CtClass. The type of f is CtField.

      Note: do not change the modifier of the field (in particular, do not add or remove static to/from the modifier) after it is added to the class by addField().

      Overrides:
      addField in class CtClass
      Parameters:
      init - an expression for the initial value.
      Throws:
      CannotCompileException
      See Also:
    • addField

      public void addField(CtField f, CtField.Initializer init) throws CannotCompileException
      Description copied from class: CtClass
      Adds a field with an initial value.

      The CtField belonging to another CtClass cannot be directly added to this class. Only a field created for this class can be added.

      For example,

       CtClass cc = ...;
       addField(new CtField(CtClass.intType, "i", cc),
                CtField.Initializer.constant(1));
       

      This code adds an int field named "i". The initial value of this field is 1.

      Overrides:
      addField in class CtClass
      Parameters:
      init - specifies the initial value of the field.
      Throws:
      CannotCompileException
      See Also:
    • removeField

      public void removeField(CtField f) throws NotFoundException
      Description copied from class: CtClass
      Removes a field declared in this class.
      Overrides:
      removeField in class CtClass
      Parameters:
      f - removed field.
      Throws:
      NotFoundException - if the field is not found.
    • makeClassInitializer

      public CtConstructor makeClassInitializer() throws CannotCompileException
      Description copied from class: CtClass
      Makes an empty class initializer (static constructor). If the class already includes a class initializer, this method returns it.
      Overrides:
      makeClassInitializer in class CtClass
      Throws:
      CannotCompileException
      See Also:
    • addConstructor

      public void addConstructor(CtConstructor c) throws CannotCompileException
      Description copied from class: CtClass
      Adds a constructor. To add a class initializer (static constructor), call makeClassInitializer().
      Overrides:
      addConstructor in class CtClass
      Throws:
      CannotCompileException
      See Also:
    • removeConstructor

      public void removeConstructor(CtConstructor m) throws NotFoundException
      Description copied from class: CtClass
      Removes a constructor declared in this class.
      Overrides:
      removeConstructor in class CtClass
      Parameters:
      m - removed constructor.
      Throws:
      NotFoundException - if the constructor is not found.
    • addMethod

      public void addMethod(CtMethod m) throws CannotCompileException
      Description copied from class: CtClass
      Adds a method.
      Overrides:
      addMethod in class CtClass
      Throws:
      CannotCompileException
    • removeMethod

      public void removeMethod(CtMethod m) throws NotFoundException
      Description copied from class: CtClass
      Removes a method declared in this class.
      Overrides:
      removeMethod in class CtClass
      Parameters:
      m - removed method.
      Throws:
      NotFoundException - if the method is not found.
    • getAttribute

      public byte[] getAttribute(String name)
      Description copied from class: CtClass
      Obtains an attribute with the given name. If that attribute is not found in the class file, this method returns null.

      This is a convenient method mainly for obtaining a user-defined attribute. For dealing with attributes, see the javassist.bytecode package. For example, the following expression returns all the attributes of a class file.

       getClassFile().getAttributes()
       
      Overrides:
      getAttribute in class CtClass
      Parameters:
      name - attribute name
      See Also:
    • setAttribute

      public void setAttribute(String name, byte[] data)
      Description copied from class: CtClass
      Adds a named attribute. An arbitrary data (smaller than 64Kb) can be saved in the class file. Some attribute name are reserved by the JVM. The attributes with the non-reserved names are ignored when a class file is loaded into the JVM. If there is already an attribute with the same name, this method substitutes the new one for it.

      This is a convenient method mainly for adding a user-defined attribute. For dealing with attributes, see the javassist.bytecode package. For example, the following expression adds an attribute info to a class file.

       getClassFile().addAttribute(info)
       
      Overrides:
      setAttribute in class CtClass
      Parameters:
      name - attribute name
      data - attribute value
      See Also:
    • instrument

      public void instrument(CodeConverter converter) throws CannotCompileException
      Description copied from class: CtClass
      Applies the given converter to all methods and constructors declared in the class. This method calls instrument() on every CtMethod and CtConstructor object in the class.
      Overrides:
      instrument in class CtClass
      Parameters:
      converter - specifies how to modify.
      Throws:
      CannotCompileException
    • instrument

      public void instrument(ExprEditor editor) throws CannotCompileException
      Description copied from class: CtClass
      Modifies the bodies of all methods and constructors declared in the class. This method calls instrument() on every CtMethod and CtConstructor object in the class.
      Overrides:
      instrument in class CtClass
      Parameters:
      editor - specifies how to modify.
      Throws:
      CannotCompileException
    • prune

      public void prune()
      Description copied from class: CtClass
      Discards unnecessary attributes, in particular, CodeAttributes (method bodies) of the class, to minimize the memory footprint. After calling this method, the class is read only. It cannot be modified any more. Furthermore, toBytecode(), writeFile(), toClass(), or instrument() cannot be called. However, the method names and signatures in the class etc. are still accessible.

      toBytecode(), writeFile(), and toClass() internally call this method if automatic pruning is on.

      According to some experiments, pruning does not really reduce memory consumption. Only about 20%. Since pruning takes time, it might not pay off. So the automatic pruning is off by default.

      Overrides:
      prune in class CtClass
      See Also:
    • rebuildClassFile

      public void rebuildClassFile()
      Description copied from class: CtClass
      If this method is called, the class file will be rebuilt when it is finally generated by toBytecode() and writeFile(). For a performance reason, the symbol table of the class file may contain unused entries, for example, after a method or a filed is deleted. This method removes those unused entries. This removal will minimize the size of the class file.
      Overrides:
      rebuildClassFile in class CtClass
    • toBytecode

      public void toBytecode(DataOutputStream out) throws CannotCompileException, IOException
      Description copied from class: CtClass
      Converts this class to a class file. Once this method is called, further modifications are not possible any more.

      This method dose not close the output stream in the end.

      Overrides:
      toBytecode in class CtClass
      Parameters:
      out - the output stream that a class file is written to.
      Throws:
      CannotCompileException
      IOException
    • dumpClassFile

      private void dumpClassFile(ClassFile cf) throws IOException
      Throws:
      IOException
    • checkPruned

      private void checkPruned(String method)
    • stopPruning

      public boolean stopPruning(boolean stop)
      Description copied from class: CtClass
      Disallows (or allows) automatically pruning this CtClass object.

      Javassist can automatically prune a CtClass object when toBytecode() (or toClass(), writeFile()) is called. Since a ClassPool holds all instances of CtClass even after toBytecode() (or toClass(), writeFile()) is called, pruning may significantly save memory consumption.

      If ClassPool.doPruning is true, the automatic pruning is on by default. Otherwise, it is off. The default value of ClassPool.doPruning is false.

      Overrides:
      stopPruning in class CtClass
      Parameters:
      stop - disallow pruning if true. Otherwise, allow.
      Returns:
      the previous status of pruning. true if pruning is already stopped.
      See Also:
    • modifyClassConstructor

      private void modifyClassConstructor(ClassFile cf) throws CannotCompileException, NotFoundException
      Throws:
      CannotCompileException
      NotFoundException
    • modifyClassConstructor

      private void modifyClassConstructor(ClassFile cf, Bytecode code, int stacksize, int localsize) throws CannotCompileException
      Throws:
      CannotCompileException
    • modifyConstructors

      private void modifyConstructors(ClassFile cf) throws CannotCompileException, NotFoundException
      Throws:
      CannotCompileException
      NotFoundException
    • insertAuxInitializer

      private static void insertAuxInitializer(CodeAttribute codeAttr, Bytecode initializer, int stacksize) throws BadBytecode
      Throws:
      BadBytecode
    • makeFieldInitializer

      private int makeFieldInitializer(Bytecode code, CtClass[] parameters) throws CannotCompileException, NotFoundException
      Throws:
      CannotCompileException
      NotFoundException
    • getHiddenMethods

      Map<CtMethod,String> getHiddenMethods()
    • getUniqueNumber

      int getUniqueNumber()
    • makeUniqueName

      public String makeUniqueName(String prefix)
      Description copied from class: CtClass
      Makes a unique member name. This method guarantees that the returned name is not used as a prefix of any methods or fields visible in this class. If the returned name is XYZ, then any method or field names in this class do not start with XYZ.
      Overrides:
      makeUniqueName in class CtClass
      Parameters:
      prefix - the prefix of the member name.
    • notFindInArray

      private static boolean notFindInArray(String prefix, String[] values)
    • makeMemberList

      private void makeMemberList(Map<Object,CtClassType> table)