edu.neu.ccs.jpf
Class JPFHelper

java.lang.Object
  extended byedu.neu.ccs.jpf.JPFHelper

public class JPFHelper
extends Object

Class JPFHelper collects utility methods that in earlier releases of the Java Power Framework were located in various other classes of the jpf package.

To support applets, earlier methods that directly used the JPT console object that is defined in the interface ConsoleAware have been refactored to remove such references and to instead return String data that may be used as desired by the caller.

In 2.7.0, this class used getDeclaredMethods from class Class. It turns out that calling this method in an applet in a browser causes a security exception. Hence, this revision (2.7.0a) uses getMethods from class Class instead. The name of the method getExtraMethods has been changed to getPublicMethods to emphasize that we now extract only public methods.

Also, in 2.7.0a, we added a method getClassChain that gets the chain of super classes starting from a given class.

Since:
2.7.0
Version:
2.7.0a

Field Summary
private static Object[][] primitivePairs
          The array primitivePairs pairs a primitive type, the Java wrapper for a primitive type, or the Stringable wrapper for a primitive type with its corresponding Stringable wrapper.
private static QuickHashtable primitiveTable
          The hash table primitiveTable pairs a primitive type, the Java wrapper for a primitive type, or the Stringable wrapper for a primitive type with its corresponding Stringable wrapper.
 
Constructor Summary
private JPFHelper()
          Prevent instantiation.
 
Method Summary
static void acceptJPFMethods(Method[] methods)
          Retains those methods in the given methods array that are JPF methods and sets the other array slots to null.
static String className(Class c)
          Returns the unqualified name of the given Class.
static String classQualifier(Class c)
          Returns the qualifier of the given Class.
static String getAdaptedExceptionTrace(Throwable exception, Method method)
          Returns an adapted exception trace string for an exception that is thrown in the execution of a JPF method with the given method name.
static Class[] getClassChain(Class c, Class d)
          Returns the chain of classes starting with the given class c first and proceeding back to but not including class d.
static String getMethodToolTip(Method method)
          Returns a tool tip appropriate for the given method.
static String getParameterNames(Method method)
          Returns a String representing the parameters of the method.
static Class getPrimitiveStringable(Class c)
          If the given class is similar to a primitive type, then returns the associated Stringable class.
static Method[] getPublicMethods(Class c, Class d)
          Returns the public methods in Class c that do not come from the base Class d.
static boolean isAbstract(Method method)
          Returns true if the method is abstract.
static boolean isAcceptableTypeForParameter(Class c)
          Returns true if the type is acceptable for a method parameter in the JPF.
static boolean isAcceptableTypeForReturn(Class c)
          Returns true if the type is acceptable for a method return in the JPF.
static boolean isDuplicate(Method method1, Method method2)
          Returns true if the two methods have the same signature.
static boolean isDuplicateParameterList(Method method1, Method method2)
          Returns true if the two methods have the same parameter list types in the same order.
static boolean isGUIMethod(Method method)
          Returns true if the method should be converted to a button with a method GUI in the JPF.
static boolean isJPFMethod(Method method)
          Returns true if the method should define a button in the JPF.
static boolean isPublic(Method method)
          Returns true if the method is public.
static boolean isSimilarToPrimitive(Class c)
          Returns true if the given class corresponds to a primitive type, the Java wrapper for a primitive type, or the Stringable wrapper for a primitive type.
static boolean isSimpleMethod(Method method)
          Returns true if the method should be converted to a simple button in the JPF.
static boolean isStatic(Method method)
          Returns true if the method is static.
static Method[] joinMethodArrays(Method[] listA, Method[] listB)
          Joins two Method arrays into one.
static String nameQualifier(String name)
          Returns the substring of the given string that corresponds to the qualifier of a qualified name.
static void removeDuplicateVirtualMethods(Method[] methods)
          Removes duplicate virtual methods defined in several classes.
static String simpleName(String name)
          Returns the substring of the given string that corresponds to a simple name, that is, one with all qualifiers removed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

primitivePairs

private static Object[][] primitivePairs

The array primitivePairs pairs a primitive type, the Java wrapper for a primitive type, or the Stringable wrapper for a primitive type with its corresponding Stringable wrapper.

Specifically, the types that are paired are:

Primitive Java wrapper Stringable
byte Byte XByte
short Short XShort
int Integer XInt
long Long XLong
float Float XFloat
double Double XDouble
char Character XChar
boolean Boolean XBoolean

A Stringable type is paired with itself.

This array is used to construct the corresponding hash table that makes the same mapping.


primitiveTable

private static QuickHashtable primitiveTable

The hash table primitiveTable pairs a primitive type, the Java wrapper for a primitive type, or the Stringable wrapper for a primitive type with its corresponding Stringable wrapper.

Specifically, the types that are paired are:

Primitive Java wrapper Stringable
byte Byte XByte
short Short XShort
int Integer XInt
long Long XLong
float Float XFloat
double Double XDouble
char Character XChar
boolean Boolean XBoolean

A Stringable type is paired with itself.

Constructor Detail

JPFHelper

private JPFHelper()
Prevent instantiation.

Method Detail

isPublic

public static boolean isPublic(Method method)

Returns true if the method is public.

Parameters:
method - the method to test

isStatic

public static boolean isStatic(Method method)

Returns true if the method is static.

Parameters:
method - the method to test

isAbstract

public static boolean isAbstract(Method method)

Returns true if the method is abstract.

Parameters:
method - the method to test

isJPFMethod

public static boolean isJPFMethod(Method method)

Returns true if the method should define a button in the JPF.

Critera:

Equivalently, either isSimpleMethod or isGUIMethod must return true.

Parameters:
method - the method to test
See Also:
isAcceptableTypeForParameter(Class), isAcceptableTypeForReturn(Class), isSimpleMethod(Method), isGUIMethod(Method)

isSimpleMethod

public static boolean isSimpleMethod(Method method)

Returns true if the method should be converted to a simple button in the JPF.

Critera:

Parameters:
method - the method to test
See Also:
isJPFMethod(Method)

isGUIMethod

public static boolean isGUIMethod(Method method)

Returns true if the method should be converted to a button with a method GUI in the JPF.

Critera:

Parameters:
method - the method to test
See Also:
isAcceptableTypeForParameter(Class), isAcceptableTypeForReturn(Class), isJPFMethod(Method)

isSimilarToPrimitive

public static boolean isSimilarToPrimitive(Class c)

Returns true if the given class corresponds to a primitive type, the Java wrapper for a primitive type, or the Stringable wrapper for a primitive type.

Specifically, this method returns true for the Class object of one of the following types:

Primitive Java wrapper Stringable
byte Byte XByte
short Short XShort
int Integer XInt
long Long XLong
float Float XFloat
double Double XDouble
char Character XChar
boolean Boolean XBoolean

Remark: The Java class Class has a method isPrimitive() that returns true for the 8 primitive class types and also for void.class. This method does not follow that convention but rather returns false for void.class.

Parameters:
c - the class of the type to test
See Also:
getPrimitiveStringable(Class)

getPrimitiveStringable

public static Class getPrimitiveStringable(Class c)
If the given class is similar to a primitive type, then returns the associated Stringable class.

Parameters:
c - the class of the type to test
See Also:
isSimilarToPrimitive(Class)

isAcceptableTypeForParameter

public static boolean isAcceptableTypeForParameter(Class c)

Returns true if the type is acceptable for a method parameter in the JPF.

Critera:

Parameters:
c - the class to test
See Also:
isAcceptableTypeForReturn(Class)

isAcceptableTypeForReturn

public static boolean isAcceptableTypeForReturn(Class c)

Returns true if the type is acceptable for a method return in the JPF.

Critera:

Parameters:
c - the class to test
See Also:
isAcceptableTypeForParameter(Class)

getClassChain

public static Class[] getClassChain(Class c,
                                    Class d)

Returns the chain of classes starting with the given class c first and proceeding back to but not including class d.

Returns an empty array if c is null or c equals d.

Parameters:
c - the start class
d - the stop class

getPublicMethods

public static Method[] getPublicMethods(Class c,
                                        Class d)

Returns the public methods in Class c that do not come from the base Class d.

Calls the Java method getMethods() in class Class. This Java method returns all public methods from the Class c used in its call. The purpose of this method is to prune the methods arising from Class d or its super classes.

The methods will also be sorted so that the methods of Class c are first, then the super class of c, etc. This is actually the current organization of the returned methods from getMethods() but since the Java documentation does not guarantee this order, we do not assume this in our code.

Within methods declared in a given class, the ordering of the methods returned will be identical to the ordering supplied by the Java method getMethods().

This method will work if the base class is null. In that case, it will return all methods defined in c or in its super classes.

This method may return an empty Method[] but will never return null.

Parameters:
c - a possible derived class
d - a possible base class for c

joinMethodArrays

public static Method[] joinMethodArrays(Method[] listA,
                                        Method[] listB)

Joins two Method arrays into one.

Parameters:
listA - method array 1
listB - method array 2

acceptJPFMethods

public static void acceptJPFMethods(Method[] methods)

Retains those methods in the given methods array that are JPF methods and sets the other array slots to null.

In particular, the given methods array object is unchanged but some of its array slots may be set to null.

Note that the non-null methods that remain in the array are public and not abstract.

Parameters:
methods - the methods array to scan
See Also:
removeDuplicateVirtualMethods(Method[])

removeDuplicateVirtualMethods

public static void removeDuplicateVirtualMethods(Method[] methods)

Removes duplicate virtual methods defined in several classes. Also removes abstract methods since they cannot be invoked.

The algorithm assumes that methods declared in a given class occur before methods defined in a super class. This is in fact the order provided by getPublicMethods.

In particular, the given methods array object is unchanged but some of its array slots may be set to null.

Parameters:
methods - the methods array to scan
See Also:
acceptJPFMethods(Method[])

isDuplicate

public static boolean isDuplicate(Method method1,
                                  Method method2)

Returns true if the two methods have the same signature.

Parameters:
method1 - method 1 to compare
method2 - method 2 to compare
See Also:
isDuplicateParameterList(Method, Method)

isDuplicateParameterList

public static boolean isDuplicateParameterList(Method method1,
                                               Method method2)

Returns true if the two methods have the same parameter list types in the same order.

Helper method for isDuplicate.

Parameters:
method1 - method 1 to compare
method2 - method 2 to compare
See Also:
isDuplicate(Method, Method)

className

public static String className(Class c)

Returns the unqualified name of the given Class.

Returns "null" if the given Class is null.

Note: The Java method call c.getName() returns what is called the fully qualified name, that is, the name with the sequence of package names separated by periods placed in front. This method returns just the name.

Parameters:
c - the class to query

classQualifier

public static String classQualifier(Class c)

Returns the qualifier of the given Class.

Returns "null" if the given Class is null.

Note: The Java method call c.getName() returns what is called the fully qualified name, that is, the name with the sequence of package names separated by periods placed in front. This method returns just the qualifier.

Parameters:
c - the class to query

simpleName

public static String simpleName(String name)

Returns the substring of the given string that corresponds to a simple name, that is, one with all qualifiers removed.

Finds that last index of the period character and returns the substring that begins after that character.

Returns the name unchanged if it is null or has no period character.


nameQualifier

public static String nameQualifier(String name)

Returns the substring of the given string that corresponds to the qualifier of a qualified name.

Finds that last index of the period character and returns the substring that begins before that character.

Returns an empty string if the name has no period character or if the last period is in position 0.

Returns null if the name is null.


getMethodToolTip

public static String getMethodToolTip(Method method)

Returns a tool tip appropriate for the given method.

Parameters:
method - the method to express as a tool tip
See Also:
getParameterNames(Method)

getParameterNames

public static String getParameterNames(Method method)

Returns a String representing the parameters of the method.

Helper method for getMethodToolTip.

Parameters:
method - the method to express as a tool tip
See Also:
getMethodToolTip(Method)

getAdaptedExceptionTrace

public static String getAdaptedExceptionTrace(Throwable exception,
                                              Method method)

Returns an adapted exception trace string for an exception that is thrown in the execution of a JPF method with the given method name.

The adaption to the exception trace is to remove reference to the use of reflection in JPF from the tail of the exception trace. Header info is also prepended to the trace to increase clarity.

Parameters:
exception - the exception being thrown
method - the method being executed