edu.umd.cs.findbugs.ba.type2
Class TypeRepository

java.lang.Object
  extended by edu.umd.cs.findbugs.ba.type2.TypeRepository

public class TypeRepository
extends java.lang.Object

Factory/repository class to ensure that all abstract Java types are represented by a unique Type object. Queries on the type hierarchy can be performed on the types instantiated by the repository.

Typically, this class is used by specifying a ClassResolver that does the work of finding class representations, which will determine whether particular types are classes or interfaces, and what the superclasses and superinterfaces of class and interface types are. The isSubtype(edu.umd.cs.findbugs.ba.type2.ObjectType, edu.umd.cs.findbugs.ba.type2.ObjectType) method will automatically construct the class hierarchy using the ClassResolver to determine the hierarchy.

Another way to use TypeRepository is to explicitly mark ClassType objects as interfaces or classes, and add the subtype relationships using addSuperclassLink(edu.umd.cs.findbugs.ba.type2.ObjectType, edu.umd.cs.findbugs.ba.type2.ObjectType) and addInterfaceLink(edu.umd.cs.findbugs.ba.type2.ObjectType, edu.umd.cs.findbugs.ba.type2.ClassType) for ClassTypes. Subtype relationships for array types are always added automatically based on the class hierarchy. Note that if you use this approach, you must explicitly add java.lang.Object, java.io.Serializable, and java.lang.Cloneable to the repository.

Author:
David Hovemeyer
See Also:
Type, ClassResolver

Constructor Summary
TypeRepository(ClassResolver resolver)
          Constructor.
 
Method Summary
 void addInterfaceLink(ObjectType implementor, ClassType iface)
          Add a direct implemented interface relationship to types in the repository.
 void addSuperclassLink(ObjectType subclass, ObjectType superclass)
          Add a direct superclass relationship to types in the repository.
 ArrayType arrayTypeFromDimensionsAndBaseType(int numDimensions, Type baseType)
          Get an ArrayType from number of dimensions and base type.
 ArrayType arrayTypeFromElementType(Type elementType)
          Create a one-dimensional array type with given element type, which can be an array type.
 ArrayType arrayTypeFromSignature(java.lang.String signature)
          Get an ArrayType from an array signature, creating it if it doesn't exist.
 BasicType basicTypeFromSignature(java.lang.String signature)
          Create an BasicType from a basic type signature.
 BasicType basicTypeFromTypeCode(byte typeCode)
          Create an BasicType from a type code.
 ClassType classTypeFromDottedClassName(java.lang.String dottedClassName)
          Get a ClassType from a class or interface name using dots to separate package components, creating it if it doesn't exist.
 ClassType classTypeFromSignature(java.lang.String signature)
          Get a ClassType from a signature, e.g., JAVA_LANG_OBJECT_SIGNATURE.
 ClassType classTypeFromSlashedClassName(java.lang.String slashedClassName)
          Get a ClassType from a class or interface name using slashes to separate package components, creating it if it doesn't exist.
 BasicType getBooleanType()
          Get the boolean type.
 Type getBottomType()
          Get the instance of the special BOTTOM type.
 BasicType getByteType()
          Get the byte type.
 BasicType getCharType()
          Get the char type.
 Type getDoubleExtraType()
          Get the instance of the special double extra type.
 BasicType getDoubleType()
          Get the double type.
 ObjectType getFirstCommonSuperclass(ObjectType a, ObjectType b)
          Get the first common superclass of two object types, in the sense used by the VM Spec.
 BasicType getFloatType()
          Get the float type.
 BasicType getIntType()
          Get the int type.
 Type getLongExtraType()
          Get the instance of the special long extra type.
 BasicType getLongType()
          Get the long type.
 Type getNullType()
          Get the instance of the special NULL type.
 Type getReturnAddressType()
          Get the instance of the return address type.
 BasicType getShortType()
          Get the short type.
 ClassType getSuperclass(ClassType type)
          Get the superclass of a class type.
 Type getTopType()
          Get the instance of the special TOP type.
 BasicType getVoidType()
          Get the void type.
 boolean isSubtype(ObjectType subtype, ObjectType supertype)
          Determine if one object type is a subtype of another.
 Type specialTypeFromSignature(java.lang.String signature)
          Create a special type from a signature.
 Type typeFromSignature(java.lang.String signature)
          Get an Type object representing the type whose JVM signature is given, creating it if it doesn't exist.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TypeRepository

public TypeRepository(ClassResolver resolver)
Constructor. Creates a type repository that has basic and special types, but no class or array types.

Parameters:
resolver - the ClassResolver that will be used to find inheritance hierarchy information for classes
Method Detail

classTypeFromSignature

public ClassType classTypeFromSignature(java.lang.String signature)
Get a ClassType from a signature, e.g., JAVA_LANG_OBJECT_SIGNATURE.

Parameters:
signature - the class signature
Returns:
the ClassType representing the class

classTypeFromSlashedClassName

public ClassType classTypeFromSlashedClassName(java.lang.String slashedClassName)
Get a ClassType from a class or interface name using slashes to separate package components, creating it if it doesn't exist. (A name with components separated by slashes is the native format for bare class names in class files.)

Parameters:
slashedClassName - class name in slashed format
Returns:
the ClassType representing the class

classTypeFromDottedClassName

public ClassType classTypeFromDottedClassName(java.lang.String dottedClassName)
Get a ClassType from a class or interface name using dots to separate package components, creating it if it doesn't exist.

Parameters:
dottedClassName - the class name in dotted format
Returns:
the ClassType representing the class

arrayTypeFromSignature

public ArrayType arrayTypeFromSignature(java.lang.String signature)
                                 throws InvalidSignatureException
Get an ArrayType from an array signature, creating it if it doesn't exist.

Parameters:
signature - the array signature
Returns:
the ArrayType representing the array type
Throws:
InvalidSignatureException

arrayTypeFromDimensionsAndBaseType

public ArrayType arrayTypeFromDimensionsAndBaseType(int numDimensions,
                                                    Type baseType)
Get an ArrayType from number of dimensions and base type. The base type must not be an array type.

Parameters:
numDimensions - the number of dimensions
baseType - the base type (e.g, "Object" in the array type "Object[][]"): must be created from this type repository
Returns:
the array type

arrayTypeFromElementType

public ArrayType arrayTypeFromElementType(Type elementType)
Create a one-dimensional array type with given element type, which can be an array type. Sometimes it is easier to think of all arrays as being one dimensional.

Parameters:
elementType - the element type
Returns:
an array type with the given element type

basicTypeFromTypeCode

public BasicType basicTypeFromTypeCode(byte typeCode)
Create an BasicType from a type code.

Parameters:
typeCode - the basic type code (T_BOOLEAN, etc.)
Returns:
the BasicType representing the basic type

basicTypeFromSignature

public BasicType basicTypeFromSignature(java.lang.String signature)
                                 throws InvalidSignatureException
Create an BasicType from a basic type signature.

Parameters:
signature - the signature
Returns:
the BasicType representing the basic type
Throws:
InvalidSignatureException

specialTypeFromSignature

public Type specialTypeFromSignature(java.lang.String signature)
                              throws InvalidSignatureException
Create a special type from a signature. The signature must be one of the constants defined in SpecialTypeSignatures.

Parameters:
signature - special type signature
Returns:
the special Type
Throws:
InvalidSignatureException

typeFromSignature

public Type typeFromSignature(java.lang.String signature)
                       throws InvalidSignatureException
Get an Type object representing the type whose JVM signature is given, creating it if it doesn't exist.

Parameters:
signature - the JVM signature of the type: something like "B" (the byte basic type), "Ljava/lang/String;" (the type of a reference to java.lang.String), or "[Ljava/lang/Object;" (the type of a reference to an array of java.lang.Object references).
Returns:
the Type object representing the type
Throws:
InvalidSignatureException

getVoidType

public BasicType getVoidType()
Get the void type.

Returns:
the void type

getBooleanType

public BasicType getBooleanType()
Get the boolean type.

Returns:
the boolean type

getByteType

public BasicType getByteType()
Get the byte type.

Returns:
the byte type

getCharType

public BasicType getCharType()
Get the char type.

Returns:
the char type

getShortType

public BasicType getShortType()
Get the short type.

Returns:
the short type

getIntType

public BasicType getIntType()
Get the int type.

Returns:
the int type

getLongType

public BasicType getLongType()
Get the long type.

Returns:
the long type

getFloatType

public BasicType getFloatType()
Get the float type.

Returns:
the float type

getDoubleType

public BasicType getDoubleType()
Get the double type.

Returns:
the double type

getTopType

public Type getTopType()
Get the instance of the special TOP type.

Returns:
the TOP instance

getBottomType

public Type getBottomType()
Get the instance of the special BOTTOM type.

Returns:
the BOTTOM instance

getNullType

public Type getNullType()
Get the instance of the special NULL type.

Returns:
the NULL type

getLongExtraType

public Type getLongExtraType()
Get the instance of the special long extra type.

Returns:
the long extra type

getDoubleExtraType

public Type getDoubleExtraType()
Get the instance of the special double extra type.

Returns:
the double extra type

getReturnAddressType

public Type getReturnAddressType()
Get the instance of the return address type.

Returns:
the return address type

addSuperclassLink

public void addSuperclassLink(ObjectType subclass,
                              ObjectType superclass)
Add a direct superclass relationship to types in the repository.

Parameters:
subclass - the subclass
superclass - the superclass

addInterfaceLink

public void addInterfaceLink(ObjectType implementor,
                             ClassType iface)
Add a direct implemented interface relationship to types in the repository.

Parameters:
implementor - the class or interface directly implementing the interface (i.e., the subtype)
iface - the implemented interface (i.e., the supertype)

isSubtype

public boolean isSubtype(ObjectType subtype,
                         ObjectType supertype)
                  throws java.lang.ClassNotFoundException
Determine if one object type is a subtype of another.

Parameters:
subtype - the potential subtype
supertype - the potential supertype
Returns:
true if subtype is really a subtype of supertype, false otherwise
Throws:
java.lang.ClassNotFoundException

getSuperclass

public ClassType getSuperclass(ClassType type)
                        throws java.lang.ClassNotFoundException
Get the superclass of a class type.

Parameters:
type - the class type
Returns:
the ClassType representing the class's superclass, or null if the type has no superclass (i.e., is java.lang.Object)
Throws:
java.lang.ClassNotFoundException

getFirstCommonSuperclass

public ObjectType getFirstCommonSuperclass(ObjectType a,
                                           ObjectType b)
                                    throws java.lang.ClassNotFoundException
Get the first common superclass of two object types, in the sense used by the VM Spec. This means that interfaces are always considered to have java.lang.Object as their common superclass, even if a more accurate interface type could be used. Similarly, arrays of interface types of same dimensionality are considered to have an array of java.lang.Object as their common superclass.

This operation is commutative.

Parameters:
a - an ObjectType
b - another ObjectType
Returns:
the first common superclass of a and b
Throws:
java.lang.ClassNotFoundException