Package gnu.bytecode
Class ClassType
- java.lang.Object
-
- gnu.bytecode.Type
-
- gnu.bytecode.ObjectType
-
- gnu.bytecode.ClassType
-
- All Implemented Interfaces:
AttrContainer
,Member
,Externalizable
,Serializable
,Type
- Direct Known Subclasses:
PairClassType
public class ClassType extends ObjectType implements AttrContainer, Externalizable, Member
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class gnu.bytecode.Type
Type.NeverReturns
-
-
Field Summary
Fields Modifier and Type Field Description Method
constructor
static int
JAVA_10_VERSION
static int
JAVA_9_VERSION
static int
JDK_1_1_VERSION
static int
JDK_1_2_VERSION
static int
JDK_1_3_VERSION
static int
JDK_1_4_VERSION
static int
JDK_1_5_VERSION
static int
JDK_1_6_VERSION
static int
JDK_1_7_VERSION
static int
JDK_1_8_VERSION
static ClassType[]
noClasses
-
Fields inherited from class gnu.bytecode.ObjectType
flags
-
Fields inherited from class gnu.bytecode.Type
boolean_ctype, boolean_type, booleanType, booleanValue_method, byte_type, byteType, char_type, charType, clone_method, double_type, doubleType, doubleValue_method, errorType, float_type, floatType, floatValue_method, int_type, intType, intValue_method, java_lang_Class_type, javalangannotationAnnotationType, javalangBooleanType, javalangCharacterType, javalangClassType, javalanginvokeMethodHandleType, javalangNumberType, javalangObjectType, javalangStringType, javalangThrowableType, long_type, longType, longValue_method, neverReturnsType, nullType, number_type, objectType, pointer_type, reflectClass, short_type, shortType, string_type, throwable_type, toString_method, tostring_type, toStringType, typeArray0, void_type, voidType
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Field
addField()
Add a new field to this class.Field
addField(String name)
Add a new field to this class, and name the field.Field
addField(String name, Type type)
Field
addField(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.void
addMemberClass(ClassType member)
void
addMemberClasses()
Method
addMethod(Constructor method)
Method
addMethod(Method method)
Method
addMethod(String name)
Method
addMethod(String name, int flags)
Method
addMethod(String name, int flags, Type[] arg_types, Type return_type)
Add a method to this ClassType.Method
addMethod(String name, Type[] arg_types, Type return_type, int flags)
Method
addMethod(String name, String signature, int flags)
void
addMethods(Class clas)
Use reflection to add all the declared methods of this class.void
addModifiers(int flags)
Method
checkSingleAbstractMethod()
Check to see if this is a Single Abstract Method (SAM) type.void
cleanupAfterCompilation()
Clear various object references, to help garbage collection.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.Method[]
getAbstractMethods()
ClassType[]
getAllInterfaces()
Get all the interfaces this class implements.<T extends Annotation>
TgetAnnotation(Class<T> clas)
Attribute
getAttributes()
Get the (first) Attribute of this container.short
getClassfileMajorVersion()
short
getClassfileMinorVersion()
int
getClassfileVersion()
CpoolEntry
getConstant(int i)
ConstantPool
getConstants()
ClassType
getDeclaredClass(String simpleName)
Field
getDeclaredField(String name)
Find a field with the given name declared in this class.Method
getDeclaredMethod(String name, int argCount)
Get a method with matching name and number of arguments.Method
getDeclaredMethod(String name, Type[] arg_types)
Look for a matching method.Method
getDeclaredMethods()
Method
getDeclaredStaticMethod(String name, int argCount)
Get a static method with matching name and number of arguments.ClassType
getDeclaringClass()
Method
getDefaultConstructor()
Member
getEnclosingMember()
Field
getField(String name)
Find a field with the given name declared in this class or its ancestors.Field
getField(String name, int mask)
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(String name, Type[] paramTypes, int flags)
Method
getMethod(Method method)
Add a method to this ClassType.Method
getMethod(String name, Type[] arg_types)
Looks for a method matching the name and 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, List<Method> result)
Helper to get methods satisfying a filtering predicate.int
getModifiers()
Return the modifiers (access flags) for this class.ClassType
getOuterLinkType()
String
getPackageName()
String
getSimpleName()
boolean
getStaticFlag()
ClassType
getSuperclass()
TypeVariable[]
getTypeParameters()
boolean
hasOuterLink()
boolean
implementsInterface(ClassType iface)
True if this class/interface implements the interface iface.static boolean
isAccessible(ClassType caller, ClassType declaring, ObjectType receiver, int modifiers)
Check if a component is accessible from this class.boolean
isAccessible(Member member, ObjectType receiver)
Check if a member is accessible from this class.boolean
isAnnotation()
int
isCompatibleWithValue(Type valueType)
If this is the target type, is a given source type compatible?boolean
isFinal()
boolean
isInterface()
boolean
isSubclass(ClassType other)
boolean
isSubclass(String cname)
A more efficient version of isSubclass(ClassType.make(cname)).static ClassType
make(String name)
Find a ClassType with the given name, or create a new one.static ClassType
make(String name, ClassType superClass)
Deprecated.static String
nameToSignature(String name)
void
readExternal(ObjectInput in)
Object
readResolve()
void
removeField(Field field, Field prev)
void
setAttributes(Attribute attributes)
Set the (list of) Attributes of this container.void
setClassfileVersion(int code)
void
setClassfileVersion(int major, int minor)
void
setClassfileVersionJava5()
void
setEnclosingMember(Member member)
void
setInterface(boolean val)
void
setInterfaces(ClassType[] interfaces)
void
setModifiers(int flags)
Set the modifiers (access flags) for this class.void
setName(String name)
Sets the name of the class being defined in this classfile.Field
setOuterLink(ClassType outer)
Note that this class needs an other link ("this$0") field.void
setSourceFile(String name)
Set the name of the SourceFile associated with this class.void
setStratum(String stratum)
Create aSourceDebugExtAttr
, if needed, and set the "stratum".void
setSuper(ClassType superClass)
void
setSuper(String name)
Set the superclass of the is class.static byte[]
to_utf8(String str)
Convert a String to a Utf8 byte array.String
toString()
void
writeExternal(ObjectOutput out)
byte[]
writeToArray()
void
writeToFile()
void
writeToFile(String filename)
void
writeToStream(OutputStream stream)
-
Methods inherited from class gnu.bytecode.ObjectType
coerceFromObject, emitCoerceFromObject, getContextClass, getContextClassLoader, getInternalName, getMethods, getReflectClass, isExisting, isInstance, setExisting
-
Methods inherited from class gnu.bytecode.Type
coerceToObject, emitCoerceToObject, emitConvertFromPrimitive, emitIsInstance, getGenericSignature, getImplementationType, getMaybeGenericSignature, getName, getRawType, getRealType, getSignature, getSize, getSizeInWords, getType, hashCode, isCompatibleWithValue, isMoreSpecific, isSame, isSubtype, isValidJavaTypeName, isVoid, lookupType, lowestCommonSharedType, lowestCommonSuperType, make, make, printSignature, promote, promoteIfUnsigned, registerTypeForClass, setGenericSignature, 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
-
Methods inherited from interface java.lang.reflect.Type
getTypeName
-
-
-
-
Field Detail
-
JDK_1_1_VERSION
public static final int JDK_1_1_VERSION
- See Also:
- Constant Field Values
-
JDK_1_2_VERSION
public static final int JDK_1_2_VERSION
- See Also:
- Constant Field Values
-
JDK_1_3_VERSION
public static final int JDK_1_3_VERSION
- See Also:
- Constant Field Values
-
JDK_1_4_VERSION
public static final int JDK_1_4_VERSION
- See Also:
- Constant Field Values
-
JDK_1_5_VERSION
public static final int JDK_1_5_VERSION
- See Also:
- Constant Field Values
-
JDK_1_6_VERSION
public static final int JDK_1_6_VERSION
- See Also:
- Constant Field Values
-
JDK_1_7_VERSION
public static final int JDK_1_7_VERSION
- See Also:
- Constant Field Values
-
JDK_1_8_VERSION
public static final int JDK_1_8_VERSION
- See Also:
- Constant Field Values
-
JAVA_9_VERSION
public static final int JAVA_9_VERSION
- See Also:
- Constant Field Values
-
JAVA_10_VERSION
public static final int JAVA_10_VERSION
- See Also:
- Constant Field Values
-
noClasses
public static final ClassType[] noClasses
-
constructor
public Method constructor
-
-
Constructor Detail
-
ClassType
public ClassType()
-
ClassType
public ClassType(String class_name)
-
-
Method Detail
-
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 interfaceAttrContainer
-
setAttributes
public final void setAttributes(Attribute attributes)
Description copied from interface:AttrContainer
Set the (list of) Attributes of this container.- Specified by:
setAttributes
in interfaceAttrContainer
-
getConstants
public final ConstantPool getConstants()
- Specified by:
getConstants
in interfaceAttrContainer
-
getConstant
public final CpoolEntry getConstant(int i)
-
getModifiers
public final int getModifiers()
Return the modifiers (access flags) for this class.- Specified by:
getModifiers
in interfaceMember
-
getStaticFlag
public final boolean getStaticFlag()
- Specified by:
getStaticFlag
in interfaceMember
-
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)
-
getDeclaringClass
public ClassType getDeclaringClass()
- Specified by:
getDeclaringClass
in interfaceMember
-
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 classdeclaring
- 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.
-
setStratum
public void setStratum(String stratum)
Create aSourceDebugExtAttr
, 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 classObjectType
-
getPackageName
public String getPackageName()
-
getInterfaces
public ClassType[] getInterfaces()
- Overrides:
getInterfaces
in classObjectType
- 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 classType
-
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 classObjectType
- 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
-
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.
-
getMethods
public final Method getMethods()
Get the methods of this class.
-
getMethodCount
public final int getMethodCount()
-
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(Constructor method)
-
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 returnsearchSupers
- 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, int searchSupers)
Get methods matching a given filter.- Parameters:
filter
- to select methods to returnsearchSupers
- 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 returnsearchSupers
- 0 if only current class should be searched, 1 if superclasses should also be searched, 2 if super-interfaces should also be searchedresult
- array to place selected methods inoffset
- 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 classObjectType
- Parameters:
filter
- to select methods to returnsearchSupers
- 0 if only current class should be searched, 1 if superclasses should also be searched, 2 if super-interfaces should also be searchedresult
- 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 namearg_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 classObjectType
-
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().
-
getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> clas)
- Specified by:
getAnnotation
in interfaceMember
-
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 classType
- 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 classObjectType
-
writeExternal
public void writeExternal(ObjectOutput out) throws IOException
- Specified by:
writeExternal
in interfaceExternalizable
- Throws:
IOException
-
readExternal
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
- Specified by:
readExternal
in interfaceExternalizable
- 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.
-
-