Package gnu.bytecode

Class ClassType

All Implemented Interfaces:
AttrContainer, Member, Externalizable, Serializable, Type
Direct Known Subclasses:
PairClassType

public class ClassType extends ObjectType implements AttrContainer, Externalizable, Member
See Also:
  • Field Details

  • Constructor Details

    • ClassType

      public ClassType()
    • ClassType

      public ClassType(String class_name)
  • Method Details

    • getClassfileMajorVersion

      public short getClassfileMajorVersion()
    • getClassfileMinorVersion

      public short getClassfileMinorVersion()
    • setClassfileVersion

      public void setClassfileVersion(int major, int minor)
    • setClassfileVersion

      public void setClassfileVersion(int code)
    • getClassfileVersion

      public int getClassfileVersion()
    • setClassfileVersionJava5

      public void setClassfileVersionJava5()
    • make

      public static ClassType make(String name)
      Find a ClassType with the given name, or create a new one. Use this for "library classes", where you need the field/method types, but not one where you are about to generate code for.
      Parameters:
      name - the name of the class (e..g. "java.lang.String").
    • make

      @Deprecated public static ClassType make(String name, ClassType superClass)
      Deprecated.
    • getAttributes

      public final Attribute getAttributes()
      Description copied from interface: AttrContainer
      Get the (first) Attribute of this container.
      Specified by:
      getAttributes in interface AttrContainer
    • setAttributes

      public final void setAttributes(Attribute attributes)
      Description copied from interface: AttrContainer
      Set the (list of) Attributes of this container.
      Specified by:
      setAttributes in interface AttrContainer
    • getConstants

      public final ConstantPool getConstants()
      Specified by:
      getConstants in interface AttrContainer
    • getConstant

      public final CpoolEntry getConstant(int i)
    • getModifiers

      public final int getModifiers()
      Return the modifiers (access flags) for this class.
      Specified by:
      getModifiers in interface Member
    • getStaticFlag

      public final boolean getStaticFlag()
      Specified by:
      getStaticFlag in interface Member
    • setModifiers

      public final void setModifiers(int flags)
      Set the modifiers (access flags) for this class.
    • addModifiers

      public final void addModifiers(int flags)
    • getSimpleName

      public String getSimpleName()
    • addMemberClass

      public void addMemberClass(ClassType member)
    • getDeclaredClass

      public ClassType getDeclaredClass(String simpleName)
    • getDeclaringClass

      public ClassType getDeclaringClass()
      Specified by:
      getDeclaringClass in interface Member
    • getEnclosingMember

      public Member getEnclosingMember()
    • setEnclosingMember

      public void setEnclosingMember(Member member)
    • addMemberClasses

      public void addMemberClasses()
    • hasOuterLink

      public final boolean hasOuterLink()
    • getOuterLinkType

      public ClassType getOuterLinkType()
    • setOuterLink

      public final Field setOuterLink(ClassType outer)
      Note that this class needs an other link ("this$0") field. This is only allowed if !isExisting(). Adjust any existing "<init>" methods to take the extra implicit parameter.
      Parameters:
      outer - the outer class
    • isAccessible

      public boolean isAccessible(Member member, ObjectType receiver)
      Check if a member is accessible from this class.
      Parameters:
      member - the member (Field, Method) we're trting to access.
      receiver - the type of the receiver object, if applicable.
      Returns:
      true if the specified component can be accessed from this class.
    • isAccessible

      public static boolean isAccessible(ClassType caller, ClassType declaring, ObjectType receiver, int modifiers)
      Check if a component is accessible from this class.
      Parameters:
      caller - the class containing the calling method, or null if unspecified class
      declaring - the class containing the component (a field, method, or inner class)
      receiver - the type of the receiver object, if applicable.
      modifiers - the access flags of the component
      Returns:
      true if the specified component can be accessed from this class.
    • setName

      public void setName(String name)
      Sets the name of the class being defined in this classfile.
      Specified by:
      setName in interface Member
      Overrides:
      setName in class Type
      Parameters:
      name - the name to give to the class
    • nameToSignature

      public static String nameToSignature(String name)
    • setStratum

      public void setStratum(String stratum)
      Create a SourceDebugExtAttr, if needed, and set the "stratum". The stratum is typically a programming language such as "JSP", "Scheme", or "Java" (the default).
    • setSourceFile

      public void setSourceFile(String name)
      Set the name of the SourceFile associated with this class.
    • getTypeParameters

      public TypeVariable[] getTypeParameters()
    • setSuper

      public void setSuper(String name)
      Set the superclass of the is class.
      Parameters:
      name - name of super class, or null if this is "Object".
    • setSuper

      public void setSuper(ClassType superClass)
    • getSuperclass

      public ClassType getSuperclass()
      Overrides:
      getSuperclass in class ObjectType
    • getPackageName

      public String getPackageName()
    • getInterfaces

      public ClassType[] getInterfaces()
      Overrides:
      getInterfaces in class ObjectType
      Returns:
      the interfaces this class is declared to implement (not those inherited from its superclass/superinterfaces).
    • getAllInterfaces

      public ClassType[] getAllInterfaces()
      Get all the interfaces this class implements. Includes those inherited from its superclass/superinterfaces.
    • setInterfaces

      public void setInterfaces(ClassType[] interfaces)
    • addInterface

      public void addInterface(ClassType newInterface)
      Add an interface to the list of implemented interfaces.
    • isInterface

      public final boolean isInterface()
      Overrides:
      isInterface in class Type
    • setInterface

      public final void setInterface(boolean val)
    • isFinal

      public final boolean isFinal()
    • isAnnotation

      public final boolean isAnnotation()
    • getFields

      public final Field getFields()
      Get the fields of this class.
    • getFieldCount

      public final int getFieldCount()
    • getDeclaredField

      public Field getDeclaredField(String name)
      Find a field with the given name declared in this class.
      Returns:
      the matching field, or null if there is no such field.
    • getField

      public Field getField(String name, int mask)
      Find a field with the given name declared in this class or its ancestors.
      Overrides:
      getField in class ObjectType
      Parameters:
      name - the name of the field.
      mask - of match a field whose modifiers has one of these bits set. Howeve, if mask is -1, ignore the access flags.
      Returns:
      the matching field, or null if there is no such field.
    • getField

      public Field getField(String name)
      Find a field with the given name declared in this class or its ancestors.
      Returns:
      the matching field, or null if there is no such field.
    • addField

      public Field addField()
      Add a new field to this class.
    • addField

      public Field addField(String name)
      Add a new field to this class, and name the field.
      Parameters:
      name - the name of the new field
    • addField

      public final Field addField(String name, Type type)
    • addField

      public final Field addField(String name, Type type, int flags)
    • addFields

      public void addFields()
      Use reflection to add all the declared fields of this class. Does not add private fields. Does not check for duplicate (already-known) fields. Is not thread-safe if another thread may access this ClassType.
    • removeField

      public void removeField(Field field, Field prev)
    • getMethods

      public final Method getMethods()
      Get the methods of this class.
    • getMethodCount

      public final int getMethodCount()
    • addMethod

      public Method addMethod(String name)
    • addMethod

      public Method addMethod(String name, int flags)
    • addMethod

      public Method addMethod(String name, Type[] arg_types, Type return_type, int flags)
    • addMethod

      public Method addMethod(String name, int flags, Type[] arg_types, Type return_type)
      Add a method to this ClassType. If an existing method matches, return that. Otherwise, create a new one. In contrast, the other addMethod methods always create new Methods.
    • addMethod

      public Method addMethod(Method method)
    • addMethod

      public Method addMethod(Constructor method)
    • addMethod

      public Method addMethod(String name, String signature, int flags)
    • getMethod

      public Method getMethod(Method method)
      Add a method to this ClassType. If an existing method matches, return that. Otherwise, create a new one.
    • getDeclaredMethods

      public final Method getDeclaredMethods()
    • countMethods

      public final int countMethods(Filter filter, int searchSupers)
      Count methods matching a given filter.
      Parameters:
      filter - to select methods to return
      searchSupers - 0 if only current class should be searched, 1 if superclasses should also be searched, 2 if super-interfaces should also be searched
      Returns:
      number of methods that match
    • getMethods

      public Method[] getMethods(Filter filter, boolean searchSupers)
    • getMethods

      public Method[] getMethods(Filter filter, int searchSupers)
      Get methods matching a given filter.
      Parameters:
      filter - to select methods to return
      searchSupers - 0 if only current class should be searched, 1 if superclasses should also be searched, 2 if super-interfaces should also be searched
      Returns:
      a fresh array containing the methods satisfying the filter
    • getMethods

      @Deprecated public int getMethods(Filter filter, int searchSupers, Method[] result, int offset)
      Deprecated.
      Helper to get methods satisfying a filtering predicate.
      Parameters:
      filter - to select methods to return
      searchSupers - 0 if only current class should be searched, 1 if superclasses should also be searched, 2 if super-interfaces should also be searched
      result - array to place selected methods in
      offset - start of where in result to place result
      Returns:
      number of methods placed in result array
    • getMethods

      public int getMethods(Filter filter, int searchSupers, List<Method> result)
      Helper to get methods satisfying a filtering predicate.
      Overrides:
      getMethods in class ObjectType
      Parameters:
      filter - to select methods to return
      searchSupers - 0 if only current class should be searched, 1 if superclasses should also be searched, 2 if super-interfaces should also be searched
      result - List to add selected methods in
      Returns:
      number of methods placed in result list
    • getAbstractMethods

      public Method[] getAbstractMethods()
    • getDeclaredMethod

      public Method getDeclaredMethod(String name, Type[] arg_types)
      Look for a matching method.
      Parameters:
      name - method name
      arg_types - parameter types that must match. Can also be null, to match any parameter type list. Otherwise, an element of arg_types must be the same type (equals), though a null element of arg_types is a wildcard that matches any type.
    • getDeclaredMethod

      public Method getDeclaredMethod(String name, int argCount)
      Get a method with matching name and number of arguments.
    • getDeclaredStaticMethod

      public Method getDeclaredStaticMethod(String name, int argCount)
      Get a static method with matching name and number of arguments.
    • getMethod

      public Method getMethod(String name, Type[] arg_types)
      Looks for a method matching the name and types. Note looks for an exact match, unless a type is null, not necessarily the best match.
      Overrides:
      getMethod in class ObjectType
    • getDefaultConstructor

      public Method getDefaultConstructor()
    • addMethods

      public void addMethods(Class clas)
      Use reflection to add all the declared methods of this class. Does not add constructors nor private or package-private methods. Does not check for duplicate (already-known) methods.
      Parameters:
      clas - should be the same as getReflectClass().
    • getMatchingMethods

      public Method[] getMatchingMethods(String name, Type[] paramTypes, int flags)
    • getAnnotation

      public <T extends Annotation> T getAnnotation(Class<T> clas)
      Specified by:
      getAnnotation in interface Member
    • doFixups

      public void doFixups()
      Do various fixups after generating code but before we can write it out. This includes assigning constant pool indexes where needed, finalizing labels, etc.
    • writeToStream

      public void writeToStream(OutputStream stream) throws IOException
      Throws:
      IOException
    • writeToFile

      public void writeToFile(String filename) throws IOException
      Throws:
      IOException
    • writeToFile

      public void writeToFile() throws IOException
      Throws:
      IOException
    • writeToArray

      public byte[] writeToArray()
    • to_utf8

      public static byte[] to_utf8(String str)
      Convert a String to a Utf8 byte array.
      Parameters:
      str - the input String.
      Returns:
      the input encoded as a utf8 byte array.
    • implementsInterface

      public final boolean implementsInterface(ClassType iface)
      True if this class/interface implements the interface iface.
    • isSubclass

      public final boolean isSubclass(String cname)
      A more efficient version of isSubclass(ClassType.make(cname)). Does not cause the named class be loaded if it hasn't been.
      Parameters:
      cname - a class name - cannot be an interface name
    • isSubclass

      public final boolean isSubclass(ClassType other)
    • isCompatibleWithValue

      public int isCompatibleWithValue(Type valueType)
      Description copied from class: Type
      If this is the target type, is a given source type compatible?
      Overrides:
      isCompatibleWithValue in class Type
      Returns:
      -1 if not compatible; 0 if need to check at run-time; 1 if compatible; 2 if compatible and no conversion or cast needed. We also return 0 for some "narrowing" conversions even if we know they will always succeed, so as to make such conversions less preferred when doing method overloading.
    • compare

      public int compare(Type other)
      Description copied from class: Type
      Return a numeric code showing "subtype" relationship: 1: if other is a pure subtype of this; 0: if has the same values; -1: if this is a pure subtype of other; -2: if they have values in common but neither is a subtype of the other; -3: if the types have no values in common. "Same values" is rather loose; by "A is a subtype of B" we mean that all instance of A can be "widened" to B. More formally, A.compare(B) returns: 1: all B values can be converted to A without a coercion failure (i.e. a ClassCastException or overflow or major loss of information), but not vice versa. 0: all A values can be converted to B without a coercion failure and vice versa; -1: all A values can be converted to B without a coercion failure but not vice versa; -2: there are (potentially) some A values that can be converted to B, and some B values can be converted to A; -3: there are no A values that can be converted to B, and neither are there any B values that can be converted to A.
      Overrides:
      compare in class ObjectType
    • toString

      public String toString()
      Overrides:
      toString in class Type
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Specified by:
      writeExternal in interface Externalizable
      Throws:
      IOException
    • readExternal

      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
      Specified by:
      readExternal in interface Externalizable
      Throws:
      IOException
      ClassNotFoundException
    • readResolve

      public Object readResolve() throws ObjectStreamException
      Throws:
      ObjectStreamException
    • cleanupAfterCompilation

      public void cleanupAfterCompilation()
      Clear various object references, to help garbage collection.
    • checkSingleAbstractMethod

      public Method checkSingleAbstractMethod()
      Check to see if this is a Single Abstract Method (SAM) type. I.e. an interface or abstract class that has one and only one abstract method. (One way that lambdas/closures are useful is that when given a lambda in a context that requires a SAM, create an implementing class using the lambda for the abstract method.)
      Returns:
      the single abstract Method, or null if this is not a SAM type.