jparse
Class Type

java.lang.Object
  extended byjparse.Type
Direct Known Subclasses:
CompiledType, SourceType

public abstract class Type
extends Object

Information on a Java type. This is either a SourceType object from the Java parser or a CompiledType object representing a compiled class. The static members of this class provide a type caching and lookup service.

Author:
Jerry James

Field Summary
static CompiledType booleanType
          The boolean type
static CompiledType byteType
          The byte type
static CompiledType charType
          The char type
private static String[] classPath
          The classpath to use for finding classes
static CompiledType doubleType
          The double type
static CompiledType floatType
          The float type
static CompiledType intType
          The int type
static CompiledType longType
          The long type
protected static HashMap map
          A mapping from fully qualified names to Type objects
static CompiledType objectType
          The object type
private static HashMap parsedMap
          A mapping from File objects to FileAST objects
protected static HashMap pkgMap
          A mapping from package names to File objects
static CompiledType shortType
          The short type
static CompiledType stringType
          The string type
static CompiledType voidType
          The void type
 
Constructor Summary
Type()
           
 
Method Summary
static Type arithType(Type t1, Type t2)
          Determine the type of the result of arithmetic on two types, using the rules for Java type promotion
protected static String demangle(String name)
          Demangle an internal JVM fully qualified name to a Java source fully qualified name
abstract  void dump()
          Dump information about this type to standard error
static boolean exists(String className)
          Determine whether a fully qualified name corresponds to a class
private static void explodeString(String s, char c, ArrayList list)
          Break up a string by finding the location of a certain character, and add the constituent parts to a list, if they are not already there
private static File findFile(String name, boolean source)
          Find a file in the classpath.
static Type forClass(Class theClass)
          Find a type based on a class.
static Type forName(String className)
          Find a type based on its name.
abstract  Type getArrayType()
          Get the type that corresponds to an array of this type
abstract  Type[] getClasses()
          Returns an array containing Type objects representing all the classes and interfaces that are members of this type.
abstract  Type getComponentType()
          Get the component type of an array, or return null if this type does not represent an array
abstract  Constructor getConstructor(Type[] params, Type caller)
          Get an object representing the constructor for this class with the specified parameter types
abstract  Type getDeclaringClass()
          If this is an inner class, return the outer class.
abstract  Type getInner(String name)
          Get an inner class with a specified name.
abstract  Type[] getInterfaces()
          Get the interfaces implemented by this type (if it is a class) or extended by this type (if it is an interface)
abstract  Method getMethod(String methName, Type[] paramTypes, Type caller)
          Get an object representing a method in this class with the specified parameter types
abstract  Method[] getMethods()
          Returns an array containing Method objects representing all the methods that are members of this type.
abstract  Method[] getMeths(String name, Type[] params, Type caller)
          Retrieve matching methods
abstract  int getModifiers()
          Get the modifiers for this class, encoded as per The Java Virtual Machine Specification, table 4.1.
abstract  String getName()
          Get the fully qualified name of a type
abstract  String getPackage()
          Get the package in which the type definition resides
abstract  Type getSuperclass()
          Get the supertype of this Type object
 boolean implementsInterface(Type type)
          Determine whether this type implements an interface
abstract  boolean isArray()
          Determine whether this type represents an array type
abstract  boolean isAssignableFrom(Type type)
          Determine whether this type, as a formal parameter, can have a value of type type assigned to it as an actual parameter.
abstract  boolean isInner()
          Determine whether this type is an inner class
abstract  boolean isInterface()
          Determine whether this type represents an interface
abstract  boolean isPrimitive()
          Determine whether this type represents a Java primitive type
protected static String mangle(String name)
          Mangle a Java source fully qualified name to an internal JVM fully qualified name.
static Type[] mergeTypeLists(Type[] list1, Type[] list2)
          Merge two lists of types, removing duplicates and subclasses
static FileAST parseFile(File file)
          Parse a Java input file and build an AST representing it
static FileAST parseFile(String name)
          Parse a Java input file and build an AST representing it
 boolean superClassOf(Type type)
          Determine whether this type is a superclass of another type
 boolean superInterfaceOf(Type type)
          Determine whether this type is a superinterface of another type
abstract  Type varType(String varName)
          Determine the type of a (static or instance) variable
static Type varType(String className, String varName)
          Determine the type of a variable in some class
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

booleanType

public static final CompiledType booleanType
The boolean type


byteType

public static final CompiledType byteType
The byte type


charType

public static final CompiledType charType
The char type


doubleType

public static final CompiledType doubleType
The double type


floatType

public static final CompiledType floatType
The float type


intType

public static final CompiledType intType
The int type


longType

public static final CompiledType longType
The long type


shortType

public static final CompiledType shortType
The short type


voidType

public static final CompiledType voidType
The void type


objectType

public static final CompiledType objectType
The object type


stringType

public static final CompiledType stringType
The string type


map

protected static final HashMap map
A mapping from fully qualified names to Type objects


pkgMap

protected static final HashMap pkgMap
A mapping from package names to File objects


parsedMap

private static final HashMap parsedMap
A mapping from File objects to FileAST objects


classPath

private static final String[] classPath
The classpath to use for finding classes

Constructor Detail

Type

public Type()
Method Detail

explodeString

private static void explodeString(String s,
                                  char c,
                                  ArrayList list)
Break up a string by finding the location of a certain character, and add the constituent parts to a list, if they are not already there

Parameters:
s - the string to break up
c - the delimiting character
list - the list to add the parts to

findFile

private static File findFile(String name,
                             boolean source)
Find a file in the classpath. This routine can find either a source or a compiled file. It returns null if the file cannot be found.

Parameters:
name - the fully qualified name of the class to find
source - true to find a source file, false to find a class file
Returns:
a File object representing the class, or null if it could not be found

parseFile

public static FileAST parseFile(String name)
                         throws IOException
Parse a Java input file and build an AST representing it

Parameters:
name - the full pathname of the file
Returns:
an AST representing the contents of the file
Throws:
IOException - if anything goes wrong with reading the file

parseFile

public static FileAST parseFile(File file)
                         throws IOException
Parse a Java input file and build an AST representing it

Parameters:
file - the file to parse
Returns:
an AST representing the contents of the file
Throws:
IOException - if anything goes wrong with reading the file

forName

public static Type forName(String className)
                    throws ClassNotFoundException
Find a type based on its name. If no such type exists, try to look up the class and create a Type object for it.

Parameters:
className - the name of the class to look up
Returns:
a Type object representing the class, if it was found
Throws:
ClassNotFoundException - if the given class cannot be found

forClass

public static Type forClass(Class theClass)
Find a type based on a class. If no such type exists, create a Type object for it.

Parameters:
theClass - the class to look up
Returns:
a Type object representing the class

demangle

protected static String demangle(String name)
Demangle an internal JVM fully qualified name to a Java source fully qualified name

Parameters:
name - the internal JVM name
Returns:
the equivalent Java source name

mangle

protected static String mangle(String name)
Mangle a Java source fully qualified name to an internal JVM fully qualified name.

Parameters:
name - the JVM source name
Returns:
the equivalent internal Java name

exists

public static boolean exists(String className)
Determine whether a fully qualified name corresponds to a class

Parameters:
className - the name of the class to test
Returns:
true if the class exists

varType

public static Type varType(String className,
                           String varName)
Determine the type of a variable in some class

Parameters:
className - the name of the class to look up
varName - the name of the variable to look up
Returns:
the type of the variable

arithType

public static Type arithType(Type t1,
                             Type t2)
Determine the type of the result of arithmetic on two types, using the rules for Java type promotion

Parameters:
t1 - the first type involved in the arithmetic
t2 - the second type involved in the arithmetic
Returns:
the type of the result of arithmetic with t1 and t1

mergeTypeLists

public static final Type[] mergeTypeLists(Type[] list1,
                                          Type[] list2)
Merge two lists of types, removing duplicates and subclasses

Parameters:
list1 - the first list of types
list2 - the second list of types
Returns:
the merged list, with duplicates and exceptions that are subclasses of other exceptions in the list removed

isAssignableFrom

public abstract boolean isAssignableFrom(Type type)
Determine whether this type, as a formal parameter, can have a value of type type assigned to it as an actual parameter. Note that this differs from the version in java.lang.Class when type is null. It throws a NullPointerException, but this checks whether this type is not primitive (i.e., is able to hold a value of null).

Parameters:
type - the type to check against this type
Returns:
true if type can be the type of an actual parameter passed into a method declared as taking this type as a formal parameter; false otherwise

isInterface

public abstract boolean isInterface()
Determine whether this type represents an interface

Returns:
true if this type is an interface

isArray

public abstract boolean isArray()
Determine whether this type represents an array type

Returns:
true if this type is an array type

isPrimitive

public abstract boolean isPrimitive()
Determine whether this type represents a Java primitive type

Returns:
true if this type is a Java primitive type

isInner

public abstract boolean isInner()
Determine whether this type is an inner class

Returns:
true if this type is an inner class

getName

public abstract String getName()
Get the fully qualified name of a type

Returns:
the fully qualified name of this type

getSuperclass

public abstract Type getSuperclass()
                            throws ClassNotFoundException
Get the supertype of this Type object

Returns:
the supertype of this Type object
Throws:
ClassNotFoundException - if the superclass definition cannot be found

getPackage

public abstract String getPackage()
Get the package in which the type definition resides

Returns:
the package name of this type

getInterfaces

public abstract Type[] getInterfaces()
Get the interfaces implemented by this type (if it is a class) or extended by this type (if it is an interface)

Returns:
an array holding the interfaces

getComponentType

public abstract Type getComponentType()
Get the component type of an array, or return null if this type does not represent an array

Returns:
the component type of an array

getModifiers

public abstract int getModifiers()
Get the modifiers for this class, encoded as per The Java Virtual Machine Specification, table 4.1.

Returns:
the modifiers for this class
See Also:
Modifier

getDeclaringClass

public abstract Type getDeclaringClass()
If this is an inner class, return the outer class. Otherwise, return null

Returns:
the outer class, or null if none

getClasses

public abstract Type[] getClasses()
Returns an array containing Type objects representing all the classes and interfaces that are members of this type. This includes class and interface members inherited from superclasses and class and interface members declared by the class. This method returns an array of length 0 if this type has no member classes or interfaces. This method also returns an array of length 0 if this is a primitive type, an array class, or void.

Returns:
an array of member classes and interfaces

getMethods

public abstract Method[] getMethods()
Returns an array containing Method objects representing all the methods that are members of this type. This includes methods inherited from superclasses, abstract methods defined by interfaces (for abstract types only), and methods declared by this class. This method returns an array of length 0 if this is a primitive type, an array class, or void.

Returns:
an array of member methods

getMethod

public abstract Method getMethod(String methName,
                                 Type[] paramTypes,
                                 Type caller)
Get an object representing a method in this class with the specified parameter types

Parameters:
methName - the name of the method to look up
paramTypes - the types of the parameters
caller - the type in which the method call is contained (used to check visibility)
Returns:
a Method object representing the best-matching method with those parameter types, or null if there is none

getConstructor

public abstract Constructor getConstructor(Type[] params,
                                           Type caller)
Get an object representing the constructor for this class with the specified parameter types

Parameters:
params - the types of the parameters
caller - the type creating an instance of this object with new (used to check visibility)
Returns:
a Constructor object representing the constructor with those parameter types, or null if there is none

getInner

public abstract Type getInner(String name)
Get an inner class with a specified name. This name must begin with a dollar sign ($) for this method to work properly.

Parameters:
name - the name of the inner class to lookup
Returns:
a type corresponding to the requested inner class, if it exists, or null otherwise

getArrayType

public abstract Type getArrayType()
Get the type that corresponds to an array of this type

Returns:
an array type for this type

varType

public abstract Type varType(String varName)
Determine the type of a (static or instance) variable

Parameters:
varName - the name of the variable to look up
Returns:
the type of the variable

getMeths

public abstract Method[] getMeths(String name,
                                  Type[] params,
                                  Type caller)
Retrieve matching methods

Parameters:
name - the name of the method to match
params - the types of the parameters to the method
caller - the type of the caller
Returns:
an array of matching methods. If there are no matching methods, an array of length 0 will be returned.

dump

public abstract void dump()
Dump information about this type to standard error


superClassOf

public final boolean superClassOf(Type type)
Determine whether this type is a superclass of another type

Parameters:
type - the potential subclass of this type
Returns:
true if this is a superclass of type

superInterfaceOf

public final boolean superInterfaceOf(Type type)
Determine whether this type is a superinterface of another type

Parameters:
type - the potential subinterface of this type
Returns:
true if this is a superinterface of type

implementsInterface

public final boolean implementsInterface(Type type)
Determine whether this type implements an interface

Parameters:
type - the potential interface for this type
Returns:
true if this type implements type