gnu.bytecode
Class ClassType

java.lang.Object
  extended by gnu.bytecode.Type
      extended by gnu.bytecode.ObjectType
          extended by gnu.bytecode.ClassType
All Implemented Interfaces:
AttrContainer, java.io.Externalizable, java.io.Serializable
Direct Known Subclasses:
PairClassType

public class ClassType
extends ObjectType
implements AttrContainer, java.io.Externalizable

See Also:
Serialized Form

Field Summary
 int access_flags
           
 Method constructor
           
static int major_version
           
static int minor_version
           
static ClassType[] noClasses
           
 
Fields inherited from class gnu.bytecode.ObjectType
flags
 
Fields inherited from class gnu.bytecode.Type
boolean_ctype, boolean_type, booleanValue_method, byte_type, char_type, double_type, doubleValue_method, float_type, floatValue_method, int_type, intValue_method, java_lang_Class_type, long_type, longValue_method, neverReturnsType, nullType, number_type, pointer_type, reflectClass, short_type, string_type, throwable_type, toString_method, tostring_type, typeArray0, void_type
 
Constructor Summary
ClassType()
           
ClassType(java.lang.String class_name)
           
 
Method Summary
 Field addField()
          Add a new field to this class.
 Field addField(java.lang.String name)
          Add a new field to this class, and name the field.
 Field addField(java.lang.String name, Type type)
           
 Field addField(java.lang.String name, Type type, int flags)
           
 void addFields()
          Use reflection to add all the declared fields of this class.
 void addInterface(ClassType newInterface)
          Add an interface to the list of implemented interfaces.
 Method addMethod(java.lang.String name)
           
 Method addMethod(java.lang.String name, int flags)
           
 Method addMethod(java.lang.String name, int flags, Type[] arg_types, Type return_type)
          Add a method to this ClassType.
 Method addMethod(java.lang.String name, java.lang.String signature, int flags)
           
 Method addMethod(java.lang.String name, Type[] arg_types, Type return_type, int flags)
           
 void addMethods(java.lang.Class clas)
          Use reflection to add all the declared methods of this class.
 int compare(Type other)
          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.
 int countMethods(Filter filter, int searchSupers)
          Count methods matching a given filter.
 void doFixups()
          Do various fixups after generating code but before we can write it out.
 Attribute getAttributes()
          Get the (first) Attribute of this container.
 CpoolEntry getConstant(int i)
           
 ConstantPool getConstants()
           
 Field getDeclaredField(java.lang.String name)
          Find a field with the given name declared in this class.
 Method getDeclaredMethod(java.lang.String name, int argCount)
          Get a method with matching name and number of arguments.
 Method getDeclaredMethod(java.lang.String name, Type[] arg_types)
           
 Method getDeclaredMethods()
           
 Field getField(java.lang.String name)
          Find a field with the given name declared in this class or its ancestors.
 int getFieldCount()
           
 Field getFields()
          Get the fields of this class.
 ClassType[] getInterfaces()
           
 Method[] getMatchingMethods(java.lang.String name, Type[] paramTypes, int flags)
           
 Method getMethod(java.lang.reflect.Method method)
          Add a method to this ClassType.
 Method getMethod(java.lang.String name, Type[] arg_types)
           
 int getMethodCount()
           
 Method getMethods()
          Get the methods of this class.
 Method[] getMethods(Filter filter, boolean searchSupers)
           
 Method[] getMethods(Filter filter, int searchSupers)
          Get methods matching a given filter.
 int getMethods(Filter filter, int searchSupers, Method[] result, int offset)
          Deprecated.  
 int getMethods(Filter filter, int searchSupers, java.util.Vector result, java.lang.String context)
          Helper to get methods satisfying a filtering predicate.
 int getModifiers()
          Return the modifiers (access flags) for this class.
 java.lang.String getPackageName()
           
 ClassType getSuperclass()
           
 boolean implementsInterface(ClassType iface)
          True if this class/interface implements the interface iface.
 boolean isAccessible(ClassType declaring, int modifiers)
          Check if a component is accessible from this class.
 boolean isInterface()
           
 boolean isSubclass(ClassType other)
           
static ClassType make(java.lang.String name)
          Find a ClassType with the given name, or create a new one.
static ClassType make(java.lang.String name, ClassType superClass)
           
 void readExternal(java.io.ObjectInput in)
           
 java.lang.Object readResolve()
           
 void setAttributes(Attribute attributes)
          Set the (list of) Attributes of this container.
 void setInterface(boolean val)
           
 void setInterfaces(ClassType[] interfaces)
           
 void setModifiers(int flags)
          Set the modifiers (access flags) for this class.
 void setName(java.lang.String name)
          Sets the name of the class being defined in this classfile.
 void setSourceFile(java.lang.String name)
          Set the name of the SourceFile associated with this class.
 void setStratum(java.lang.String stratum)
          Create a SourceDebugExtAttr, if needed, and set the "stratum".
 void setSuper(ClassType superClass)
           
 void setSuper(java.lang.String name)
          Set the superclass of the is class.
static byte[] to_utf8(java.lang.String str)
          Convert a String to a Utf8 byte array.
 java.lang.String toString()
           
 void writeExternal(java.io.ObjectOutput out)
           
 byte[] writeToArray()
           
 void writeToFile()
           
 void writeToFile(java.lang.String filename)
           
 void writeToStream(java.io.OutputStream stream)
           
 
Methods inherited from class gnu.bytecode.ObjectType
coerceFromObject, emitCoerceFromObject, getImplementationType, getInternalName, getReflectClass, isExisting, promote, setExisting
 
Methods inherited from class gnu.bytecode.Type
coerceToObject, emitCoerceToObject, emitIsInstance, getName, getSignature, getSize, getType, hashCode, isInstance, isMoreSpecific, isSubtype, isValidJavaTypeName, isVoid, lookupType, lowestCommonSuperType, make, registerTypeForClass, setReflectClass, setSignature, signatureLength, signatureLength, signatureToName, signatureToPrimitive, signatureToType, signatureToType, swappedCompareResult
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

minor_version

public static final int minor_version
See Also:
Constant Field Values

major_version

public static final int major_version
See Also:
Constant Field Values

access_flags

public int access_flags

noClasses

public static final ClassType[] noClasses

constructor

public Method constructor
Constructor Detail

ClassType

public ClassType()

ClassType

public ClassType(java.lang.String class_name)
Method Detail

make

public static ClassType make(java.lang.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

public static ClassType make(java.lang.String name,
                             ClassType superClass)

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()

getConstant

public final CpoolEntry getConstant(int i)

getModifiers

public final int getModifiers()
Return the modifiers (access flags) for this class.


setModifiers

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


isAccessible

public boolean isAccessible(ClassType declaring,
                            int modifiers)
Check if a component is accessible from this class.

Parameters:
declaring - the class containing the component (a field, method, or inner class)
modifiers - the access flags of the component
Returns:
true if the specified component can be accessed from this class.

setName

public void setName(java.lang.String name)
Sets the name of the class being defined in this classfile.

Overrides:
setName in class Type
Parameters:
name - the name to give to the class

setStratum

public void setStratum(java.lang.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(java.lang.String name)
Set the name of the SourceFile associated with this class.


setSuper

public void setSuper(java.lang.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()

getPackageName

public java.lang.String getPackageName()

getInterfaces

public ClassType[] getInterfaces()
Returns:
the interfaces this class is declared to implement (not 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()

setInterface

public final void setInterface(boolean val)

getFields

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


getFieldCount

public final int getFieldCount()

getDeclaredField

public Field getDeclaredField(java.lang.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(java.lang.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(java.lang.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(java.lang.String name,
                            Type type)

addField

public final Field addField(java.lang.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 or package-private fields. Does not check for duplicate (already-known) fields. Is not thread-safe if another thread may access this ClassType.


getMethods

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


getMethodCount

public final int getMethodCount()

addMethod

public Method addMethod(java.lang.String name)

addMethod

public Method addMethod(java.lang.String name,
                        int flags)

addMethod

public Method addMethod(java.lang.String name,
                        Type[] arg_types,
                        Type return_type,
                        int flags)

addMethod

public Method addMethod(java.lang.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(java.lang.String name,
                        java.lang.String signature,
                        int flags)

getMethod

public Method getMethod(java.lang.reflect.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 search
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

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 search
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,
                      java.util.Vector result,
                      java.lang.String context)
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 search
result - Vector to add selected methods in
context - If non-null, skip if class not visible in named package.
Returns:
number of methods placed in result array

getDeclaredMethod

public Method getDeclaredMethod(java.lang.String name,
                                Type[] arg_types)

getDeclaredMethod

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


getMethod

public Method getMethod(java.lang.String name,
                        Type[] arg_types)

addMethods

public void addMethods(java.lang.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(java.lang.String name,
                                   Type[] paramTypes,
                                   int flags)

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(java.io.OutputStream stream)
                   throws java.io.IOException
Throws:
java.io.IOException

writeToFile

public void writeToFile(java.lang.String filename)
                 throws java.io.IOException
Throws:
java.io.IOException

writeToFile

public void writeToFile()
                 throws java.io.IOException
Throws:
java.io.IOException

writeToArray

public byte[] writeToArray()

to_utf8

public static byte[] to_utf8(java.lang.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(ClassType other)

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 member" 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 not 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 java.lang.String toString()
Overrides:
toString in class Type

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Specified by:
writeExternal in interface java.io.Externalizable
Throws:
java.io.IOException

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Specified by:
readExternal in interface java.io.Externalizable
Throws:
java.io.IOException
java.lang.ClassNotFoundException

readResolve

public java.lang.Object readResolve()
                             throws java.io.ObjectStreamException
Throws:
java.io.ObjectStreamException