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 Methodconstructorstatic intJAVA_10_VERSIONstatic intJAVA_9_VERSIONstatic intJDK_1_1_VERSIONstatic intJDK_1_2_VERSIONstatic intJDK_1_3_VERSIONstatic intJDK_1_4_VERSIONstatic intJDK_1_5_VERSIONstatic intJDK_1_6_VERSIONstatic intJDK_1_7_VERSIONstatic intJDK_1_8_VERSIONstatic 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 FieldaddField()Add a new field to this class.FieldaddField(String name)Add a new field to this class, and name the field.FieldaddField(String name, Type type)FieldaddField(String name, Type type, int flags)voidaddFields()Use reflection to add all the declared fields of this class.voidaddInterface(ClassType newInterface)Add an interface to the list of implemented interfaces.voidaddMemberClass(ClassType member)voidaddMemberClasses()MethodaddMethod(Constructor method)MethodaddMethod(Method method)MethodaddMethod(String name)MethodaddMethod(String name, int flags)MethodaddMethod(String name, int flags, Type[] arg_types, Type return_type)Add a method to this ClassType.MethodaddMethod(String name, Type[] arg_types, Type return_type, int flags)MethodaddMethod(String name, String signature, int flags)voidaddMethods(Class clas)Use reflection to add all the declared methods of this class.voidaddModifiers(int flags)MethodcheckSingleAbstractMethod()Check to see if this is a Single Abstract Method (SAM) type.voidcleanupAfterCompilation()Clear various object references, to help garbage collection.intcompare(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.intcountMethods(Filter filter, int searchSupers)Count methods matching a given filter.voiddoFixups()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)AttributegetAttributes()Get the (first) Attribute of this container.shortgetClassfileMajorVersion()shortgetClassfileMinorVersion()intgetClassfileVersion()CpoolEntrygetConstant(int i)ConstantPoolgetConstants()ClassTypegetDeclaredClass(String simpleName)FieldgetDeclaredField(String name)Find a field with the given name declared in this class.MethodgetDeclaredMethod(String name, int argCount)Get a method with matching name and number of arguments.MethodgetDeclaredMethod(String name, Type[] arg_types)Look for a matching method.MethodgetDeclaredMethods()MethodgetDeclaredStaticMethod(String name, int argCount)Get a static method with matching name and number of arguments.ClassTypegetDeclaringClass()MethodgetDefaultConstructor()MembergetEnclosingMember()FieldgetField(String name)Find a field with the given name declared in this class or its ancestors.FieldgetField(String name, int mask)Find a field with the given name declared in this class or its ancestors.intgetFieldCount()FieldgetFields()Get the fields of this class.ClassType[]getInterfaces()Method[]getMatchingMethods(String name, Type[] paramTypes, int flags)MethodgetMethod(Method method)Add a method to this ClassType.MethodgetMethod(String name, Type[] arg_types)Looks for a method matching the name and types.intgetMethodCount()MethodgetMethods()Get the methods of this class.Method[]getMethods(Filter filter, boolean searchSupers)Method[]getMethods(Filter filter, int searchSupers)Get methods matching a given filter.intgetMethods(Filter filter, int searchSupers, Method[] result, int offset)Deprecated.intgetMethods(Filter filter, int searchSupers, List<Method> result)Helper to get methods satisfying a filtering predicate.intgetModifiers()Return the modifiers (access flags) for this class.ClassTypegetOuterLinkType()StringgetPackageName()StringgetSimpleName()booleangetStaticFlag()ClassTypegetSuperclass()TypeVariable[]getTypeParameters()booleanhasOuterLink()booleanimplementsInterface(ClassType iface)True if this class/interface implements the interface iface.static booleanisAccessible(ClassType caller, ClassType declaring, ObjectType receiver, int modifiers)Check if a component is accessible from this class.booleanisAccessible(Member member, ObjectType receiver)Check if a member is accessible from this class.booleanisAnnotation()intisCompatibleWithValue(Type valueType)If this is the target type, is a given source type compatible?booleanisFinal()booleanisInterface()booleanisSubclass(ClassType other)booleanisSubclass(String cname)A more efficient version of isSubclass(ClassType.make(cname)).static ClassTypemake(String name)Find a ClassType with the given name, or create a new one.static ClassTypemake(String name, ClassType superClass)Deprecated.static StringnameToSignature(String name)voidreadExternal(ObjectInput in)ObjectreadResolve()voidremoveField(Field field, Field prev)voidsetAttributes(Attribute attributes)Set the (list of) Attributes of this container.voidsetClassfileVersion(int code)voidsetClassfileVersion(int major, int minor)voidsetClassfileVersionJava5()voidsetEnclosingMember(Member member)voidsetInterface(boolean val)voidsetInterfaces(ClassType[] interfaces)voidsetModifiers(int flags)Set the modifiers (access flags) for this class.voidsetName(String name)Sets the name of the class being defined in this classfile.FieldsetOuterLink(ClassType outer)Note that this class needs an other link ("this$0") field.voidsetSourceFile(String name)Set the name of the SourceFile associated with this class.voidsetStratum(String stratum)Create aSourceDebugExtAttr, if needed, and set the "stratum".voidsetSuper(ClassType superClass)voidsetSuper(String name)Set the superclass of the is class.static byte[]to_utf8(String str)Convert a String to a Utf8 byte array.StringtoString()voidwriteExternal(ObjectOutput out)byte[]writeToArray()voidwriteToFile()voidwriteToFile(String filename)voidwriteToStream(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:AttrContainerGet the (first) Attribute of this container.- Specified by:
getAttributesin interfaceAttrContainer
-
setAttributes
public final void setAttributes(Attribute attributes)
Description copied from interface:AttrContainerSet the (list of) Attributes of this container.- Specified by:
setAttributesin interfaceAttrContainer
-
getConstants
public final ConstantPool getConstants()
- Specified by:
getConstantsin interfaceAttrContainer
-
getConstant
public final CpoolEntry getConstant(int i)
-
getModifiers
public final int getModifiers()
Return the modifiers (access flags) for this class.- Specified by:
getModifiersin interfaceMember
-
getStaticFlag
public final boolean getStaticFlag()
- Specified by:
getStaticFlagin 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:
getDeclaringClassin 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:
getSuperclassin classObjectType
-
getPackageName
public String getPackageName()
-
getInterfaces
public ClassType[] getInterfaces()
- Overrides:
getInterfacesin 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:
isInterfacein 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:
getFieldin 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:
getMethodsin 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:
getMethodin 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:
getAnnotationin 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:TypeIf this is the target type, is a given source type compatible?- Overrides:
isCompatibleWithValuein 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:TypeReturn 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:
comparein classObjectType
-
writeExternal
public void writeExternal(ObjectOutput out) throws IOException
- Specified by:
writeExternalin interfaceExternalizable- Throws:
IOException
-
readExternal
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
- Specified by:
readExternalin interfaceExternalizable- Throws:
IOExceptionClassNotFoundException
-
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.
-
-