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
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class gnu.bytecode.Type
Type.NeverReturns
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final ClassType[]
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
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddField()
Add a new field to this class.Add a new field to this class, and name the field.final Field
final Field
void
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
addMethod
(Constructor method) Add a method to this ClassType.void
addMethods
(Class clas) Use reflection to add all the declared methods of this class.final void
addModifiers
(int flags) Check to see if this is a Single Abstract Method (SAM) type.void
Clear various object references, to help garbage collection.int
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.final 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[]
Get all the interfaces this class implements.<T extends Annotation>
TgetAnnotation
(Class<T> clas) final Attribute
Get the (first) Attribute of this container.short
short
int
final CpoolEntry
getConstant
(int i) final ConstantPool
getDeclaredClass
(String simpleName) getDeclaredField
(String name) Find a field with the given name declared in this class.getDeclaredMethod
(String name, int argCount) Get a method with matching name and number of arguments.getDeclaredMethod
(String name, Type[] arg_types) Look for a matching method.final Method
getDeclaredStaticMethod
(String name, int argCount) Get a static method with matching name and number of arguments.Find a field with the given name declared in this class or its ancestors.Find a field with the given name declared in this class or its ancestors.final int
final Field
Get the fields of this class.Method[]
getMatchingMethods
(String name, Type[] paramTypes, int flags) Add a method to this ClassType.Looks for a method matching the name and types.final int
final Method
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.final int
Return the modifiers (access flags) for this class.final boolean
final boolean
final 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.final boolean
int
isCompatibleWithValue
(Type valueType) If this is the target type, is a given source type compatible?final boolean
isFinal()
final boolean
final boolean
isSubclass
(ClassType other) final boolean
isSubclass
(String cname) A more efficient version of isSubclass(ClassType.make(cname)).static ClassType
Find a ClassType with the given name, or create a new one.static ClassType
Deprecated.static String
nameToSignature
(String name) void
void
removeField
(Field field, Field prev) final void
setAttributes
(Attribute attributes) Set the (list of) Attributes of this container.void
setClassfileVersion
(int code) void
setClassfileVersion
(int major, int minor) void
void
setEnclosingMember
(Member member) final void
setInterface
(boolean val) void
setInterfaces
(ClassType[] interfaces) final void
setModifiers
(int flags) Set the modifiers (access flags) for this class.void
Sets the name of the class being defined in this classfile.final 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
void
Set the superclass of the is class.static byte[]
Convert a String to a Utf8 byte array.toString()
void
byte[]
void
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 Details
-
JDK_1_1_VERSION
public static final int JDK_1_1_VERSION- See Also:
-
JDK_1_2_VERSION
public static final int JDK_1_2_VERSION- See Also:
-
JDK_1_3_VERSION
public static final int JDK_1_3_VERSION- See Also:
-
JDK_1_4_VERSION
public static final int JDK_1_4_VERSION- See Also:
-
JDK_1_5_VERSION
public static final int JDK_1_5_VERSION- See Also:
-
JDK_1_6_VERSION
public static final int JDK_1_6_VERSION- See Also:
-
JDK_1_7_VERSION
public static final int JDK_1_7_VERSION- See Also:
-
JDK_1_8_VERSION
public static final int JDK_1_8_VERSION- See Also:
-
JAVA_9_VERSION
public static final int JAVA_9_VERSION- See Also:
-
JAVA_10_VERSION
public static final int JAVA_10_VERSION- See Also:
-
noClasses
-
constructor
-
-
Constructor Details
-
ClassType
public ClassType() -
ClassType
-
-
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
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. -
getAttributes
Description copied from interface:AttrContainer
Get the (first) Attribute of this container.- Specified by:
getAttributes
in interfaceAttrContainer
-
setAttributes
Description copied from interface:AttrContainer
Set the (list of) Attributes of this container.- Specified by:
setAttributes
in interfaceAttrContainer
-
getConstants
- Specified by:
getConstants
in interfaceAttrContainer
-
getConstant
-
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
-
addMemberClass
-
getDeclaredClass
-
getDeclaringClass
- Specified by:
getDeclaringClass
in interfaceMember
-
getEnclosingMember
-
setEnclosingMember
-
addMemberClasses
public void addMemberClasses() -
hasOuterLink
public final boolean hasOuterLink() -
getOuterLinkType
-
setOuterLink
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
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
Sets the name of the class being defined in this classfile. -
nameToSignature
-
setStratum
Create aSourceDebugExtAttr
, if needed, and set the "stratum". The stratum is typically a programming language such as "JSP", "Scheme", or "Java" (the default). -
setSourceFile
Set the name of the SourceFile associated with this class. -
getTypeParameters
-
setSuper
Set the superclass of the is class.- Parameters:
name
- name of super class, or null if this is "Object".
-
setSuper
-
getSuperclass
- Overrides:
getSuperclass
in classObjectType
-
getPackageName
-
getInterfaces
- Overrides:
getInterfaces
in classObjectType
- Returns:
- the interfaces this class is declared to implement (not those inherited from its superclass/superinterfaces).
-
getAllInterfaces
Get all the interfaces this class implements. Includes those inherited from its superclass/superinterfaces. -
setInterfaces
-
addInterface
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
Get the fields of this class. -
getFieldCount
public final int getFieldCount() -
getDeclaredField
Find a field with the given name declared in this class.- Returns:
- the matching field, or null if there is no such field.
-
getField
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
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
Add a new field to this class. -
addField
Add a new field to this class, and name the field.- Parameters:
name
- the name of the new field
-
addField
-
addField
-
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
-
getMethods
Get the methods of this class. -
getMethodCount
public final int getMethodCount() -
addMethod
-
addMethod
-
addMethod
-
addMethod
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
-
addMethod
-
addMethod
-
getMethod
Add a method to this ClassType. If an existing method matches, return that. Otherwise, create a new one. -
getDeclaredMethods
-
countMethods
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
-
getMethods
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.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
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
-
getDeclaredMethod
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
Get a method with matching name and number of arguments. -
getDeclaredStaticMethod
Get a static method with matching name and number of arguments. -
getMethod
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
-
addMethods
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
-
getAnnotation
- 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
- Throws:
IOException
-
writeToFile
- Throws:
IOException
-
writeToFile
- Throws:
IOException
-
writeToArray
public byte[] writeToArray() -
to_utf8
Convert a String to a Utf8 byte array.- Parameters:
str
- the input String.- Returns:
- the input encoded as a utf8 byte array.
-
implementsInterface
True if this class/interface implements the interface iface. -
isSubclass
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
-
isCompatibleWithValue
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
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
-
toString
-
writeExternal
- Specified by:
writeExternal
in interfaceExternalizable
- Throws:
IOException
-
readExternal
- Specified by:
readExternal
in interfaceExternalizable
- Throws:
IOException
ClassNotFoundException
-
readResolve
- Throws:
ObjectStreamException
-
cleanupAfterCompilation
public void cleanupAfterCompilation()Clear various object references, to help garbage collection. -
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.
-