Java2OWL
Class Utilities

Object
  extended by Utilities

public class Utilities
extends Object

This is a collection of useful static methods.


Constructor Summary
Utilities()
           
 
Method Summary
static String adjustPairs(String text, String separator)
          adjusts lines consisting of pairs separated by the given separator.
static List<Object> asList(Object... objects)
          turns the input parameters into an ArrayList.
static void bcelify(Class jcl, String filename)
          BCELifies the class and sends it to the file or System.out.
static Object[] classesFromDirectory(File directory)
          loads all compiled Java-classes in the given directory.
static Class classFromFile(File file)
          loads a compiled Java-class from the given file.
static String concatenate(String s, int n)
          returns a string generated by concatenating the given string s n-times.
static Constructor findConstructor(Class jcl, Class argument)
          searches a constructor with a given argument class
static StackTraceElement findStackFrame(String prefix)
          finds the stackframe just above the last one whose class starts with the given prefix.
static boolean getAndResetObjectFlag(Object object, String name, int flag)
          sets the flag in the object field to 0, but returns the previous value.
static boolean getAndSetObjectFlag(Object object, String name, int flag)
          sets the flag in the object field to 1, but returns the previous value.
static Class getArrayClass(GenericArrayType type)
          returns for a generic array type the corresponding Java class
static BasicType getBasicType(Class type)
          returns for a primitive Java-type the BCEL BasicType.
static String getEnvirentmentVariables()
          returns a string describing all environment variables.
static String getName(Object object)
          returns the name of the object.
static boolean getObjectFlag(Object object, String name, int flag)
          returns true if the flag in the object field is 1.
static String getSystemProperties()
          returns a string describing all system properties.
static String getWrapperType(BasicType type)
          returns for a primitive type the corresponding wrapper type name.
static Object invoke0(Object object, String methodname)
          applies a method (with empty argument list) to an object.
static Class isArray(Type t)
          checks whether the given type is an array type.
static Class isClass(Type t)
          checks whether the given type is an ordinary class, no array and no parameterized type.
static Class isCollection(Type t)
          checks whether the type is an admissible collection class.
static ParameterizedType isParameterizedType(Type t)
          checks whether the given type is a parameterized type.
static void main(String[] args)
          The main method prints the environment variables and the system properties.
static boolean map(Object object, Function function)
          applies the function to the object or the components of an array or a collection.
static Object[] parseParameters(String[] parameters, Set<String> postfixes, Set<String> keys)
          This method can be used to parse argument strings of main methods.
static void printStack()
          prints the current stack trace (for testing purposes).
static void resetObjectFlag(Object object, String name, int flag)
          sets the flag in the object field to 0.
static Type[] returnsMap(Method method)
          checks whether the methods returns a map
static void setObjectField(Object object, String name, Object value)
          sets the value into an object field.
static void setObjectFlag(Object object, String name, int flag)
          sets the flag in the object field to 1.
static void setStaticField(Class jcl, String name, boolean value)
          sets the Boolean value of a static field.
static void setStaticField(Class jcl, String name, Object value)
          sets the value of a static field.
static String stackTraceString(Throwable throwable)
          returns the result of printStackTrace() as a string.
static void transferCommonFields(Object[] fromObjects, int i, Object toObject, Class toClass)
          This method transfers the contents of the common non-static fields from fromObjects[0],...
static void transferFields(Object fromObject, Object toObject, Class toClass)
          transfers the values of fromObject's non-static fields to the corresponding fields of the toObject.
static Object transferSet(Set items, Class targetClass)
          transfers a set into a collection of the given targetClass
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utilities

public Utilities()
Method Detail

returnsMap

public static Type[] returnsMap(Method method)
checks whether the methods returns a map

Parameters:
method - any method
Returns:
[type1,type2] if the method returns a map, otherwise null

Example: HashMap getMap() yields [String,Integer]


concatenate

public static String concatenate(String s,
                                 int n)
returns a string generated by concatenating the given string s n-times.


adjustPairs

public static String adjustPairs(String text,
                                 String separator)
adjusts lines consisting of pairs separated by the given separator.

The resulting string should be formatted as a proper table.

Parameters:
text - a string consisting of lines like, for example, abc -> cde.
separator - a string separating pairs in the line.
Returns:
the string with the first element of each pair filled with blanks.

parseParameters

public static Object[] parseParameters(String[] parameters,
                                       Set<String> postfixes,
                                       Set<String> keys)
This method can be used to parse argument strings of main methods.

Parameters:
parameters - this is typically the argument list of a main method.
postfixes - a list of postfixes to be matched by the parameters.
keys - a list of keys for key,value pairs.
Returns:
An array with three elements:
1. HashMap<String,ArrayList<String>> postFixParameters the recognised parameters matching the postfixes.
2. HashMap<String,String> keyValues the recognised values of the key-value pairs.
3. ArrayList<String> rest all the unrecognised parameters.

Example for a postfix parameter ".class" to be matched with all java-class files For the key-value pairs, it is assumed that they occur in a sequence "key value" in the parameters.


getEnvirentmentVariables

public static String getEnvirentmentVariables()
returns a string describing all environment variables.


getSystemProperties

public static String getSystemProperties()
returns a string describing all system properties.


classFromFile

public static Class classFromFile(File file)
                           throws ClassNotFoundException
loads a compiled Java-class from the given file.

If the class has already been loaded, the loaded class is returned.

Parameters:
file - the compiled Java-file.
Returns:
the class or null if a read-error occurred or it is an interface.
Throws:
ClassNotFoundException.
ClassNotFoundException

classesFromDirectory

public static Object[] classesFromDirectory(File directory)
loads all compiled Java-classes in the given directory.

Parameters:
directory - the directory with the compiled Java-files.
Returns:
[classes,error-string] Interfaces are not included.

map

public static boolean map(Object object,
                          Function function)
applies the function to the object or the components of an array or a collection.

If the object is an array or a collection, then the function is applied to all of its elements.
The application is stopped as soon as the apply-function returns false.
The apply-function is tried in the following sequence:
1. if object is an array,
2. if object is a collection,
3. otherwise it is just applied to the object itself.

Parameters:
object - an array, a collection or just an object.
function - a function to be applied at the elements of the array or collection or the object itself
Returns:
true if all applications succeeded.

findStackFrame

public static StackTraceElement findStackFrame(String prefix)
finds the stackframe just above the last one whose class starts with the given prefix.

Classes starting with java, sun., org., are ignored.

Parameters:
prefix - The prefix of a fully qualified class name.
Returns:
the stackframe above the first occurrence of this class or null if none could be found.

stackTraceString

public static String stackTraceString(Throwable throwable)
returns the result of printStackTrace() as a string.


invoke0

public static Object invoke0(Object object,
                             String methodname)
                      throws Exception
applies a method (with empty argument list) to an object.

This works for cases where object.<methodname>() does not work because it is unknown how to cast the object.

Parameters:
object - a Java-object
methodname - the name of a method without arguments
Returns:
the result of invoking the method to the object.
Throws:
Exception - if the invocation failed.

getName

public static String getName(Object object)
returns the name of the object.

First it tries a getName() method, then the toString() method.


setStaticField

public static void setStaticField(Class jcl,
                                  String name,
                                  boolean value)
                           throws Exception
sets the Boolean value of a static field.

Parameters:
jcl - a Java class
name - the name of a static field
value - the boolean value to be set.
Throws:
Exception - if the operation failed.

setStaticField

public static void setStaticField(Class jcl,
                                  String name,
                                  Object value)
                           throws Exception
sets the value of a static field.

Parameters:
jcl - a Java class
name - the name of a static field
value - the boolean value to be set.
Throws:
Exception - if the operation failed.

setObjectField

public static void setObjectField(Object object,
                                  String name,
                                  Object value)
                           throws Exception
sets the value into an object field.

Parameters:
object - a Java object.
name - the name of an instance field
value - the value to be set.
Throws:
Exception - if the operation failed.

setObjectFlag

public static void setObjectFlag(Object object,
                                 String name,
                                 int flag)
                          throws Exception
sets the flag in the object field to 1.

Parameters:
object - an object with an int field named 'name'
name - the name of this field.
flag - a position in a word, e.g. 1,2,4,8,16,...
Throws:
Exception - if the operation failed.

getObjectFlag

public static boolean getObjectFlag(Object object,
                                    String name,
                                    int flag)
                             throws Exception
returns true if the flag in the object field is 1.

Parameters:
object - an object with an int field named 'name'
name - the name of this field.
flag - a position in a word, e.g. 1,2,4,8,16,...
Throws:
Exception - if the operation failed.

resetObjectFlag

public static void resetObjectFlag(Object object,
                                   String name,
                                   int flag)
                            throws Exception
sets the flag in the object field to 0.

Parameters:
object - an object with an int field named 'name'
name - the name of this field.
flag - a position in a word, e.g. 1,2,4,8,16,...
Throws:
Exception - if the operation failed.

getAndResetObjectFlag

public static boolean getAndResetObjectFlag(Object object,
                                            String name,
                                            int flag)
                                     throws Exception
sets the flag in the object field to 0, but returns the previous value.

Parameters:
object - an object with an int field named 'name'
name - the name of this field.
flag - a position in a word, e.g. 1,2,4,8,16,...
Returns:
true if the flag was 1.
Throws:
Exception - if the operation failed.

getAndSetObjectFlag

public static boolean getAndSetObjectFlag(Object object,
                                          String name,
                                          int flag)
                                   throws Exception
sets the flag in the object field to 1, but returns the previous value.

Parameters:
object - an object with an int field named 'name'
name - the name of this field.
flag - a position in a word, e.g. 1,2,4,8,16,...
Returns:
true if the flag was 1.
Throws:
Exception - if the operation failed.

asList

public static List<Object> asList(Object... objects)
turns the input parameters into an ArrayList.

Notice that this method is not generic because objects of mixed types are to be put into the list.


printStack

public static void printStack()
prints the current stack trace (for testing purposes).


getWrapperType

public static String getWrapperType(BasicType type)
returns for a primitive type the corresponding wrapper type name.


getBasicType

public static BasicType getBasicType(Class type)
returns for a primitive Java-type the BCEL BasicType.


bcelify

public static void bcelify(Class jcl,
                           String filename)
                    throws ClassNotFoundException,
                           FileNotFoundException
BCELifies the class and sends it to the file or System.out.

Parameters:
jcl - a Java class
filename - a filename or null
Throws:
ClassNotFoundException
FileNotFoundException

transferFields

public static void transferFields(Object fromObject,
                                  Object toObject,
                                  Class toClass)
transfers the values of fromObject's non-static fields to the corresponding fields of the toObject.

Parameters:
fromObject - the sending Java object.
toObject - the receiving Java object.
toClass - the to-object's class.

All member-fields, public, private, protected, inherited are transferred. Only fields protected by a special security manager cannot be transferred. For a particular field F in the to-object, the method goes top-down in the inheritance hierarchy to find a field with the same name and type as F. This field is transferred. No deep copy is made!


transferCommonFields

public static void transferCommonFields(Object[] fromObjects,
                                        int i,
                                        Object toObject,
                                        Class toClass)
This method transfers the contents of the common non-static fields from fromObjects[0],...,fromObjects[i-1] to toObject.

Parameters:
fromObjects - an array of objects.
i - an index into fromObjects
toObject - an object.
toClass - the toObject's class.

For each field F in toObject, it searches fromObjects[0],...,fromObjects[i-1] to find a field with the same name and type. The contents of the first one it finds is transferred to F.

If the field is protected by a security manager, it is not transferred.

Fields in toObjects which have already a non-null value are kept as they are.


findConstructor

public static Constructor findConstructor(Class jcl,
                                          Class argument)
searches a constructor with a given argument class

Parameters:
jcl - the class containting the constructor.
argument - the class of the first argument of the constructor.
Returns:
the constructor or null if none could be found.

transferSet

public static Object transferSet(Set items,
                                 Class targetClass)
transfers a set into a collection of the given targetClass

Parameters:
items - a set
targetClass - a collection class
Returns:
a collection containing the items, or null if an error occurred.

isArray

public static Class isArray(Type t)
checks whether the given type is an array type.

Parameters:
t - any type
Returns:
the corresponding array class or null if it is not an array.

isClass

public static Class isClass(Type t)
checks whether the given type is an ordinary class, no array and no parameterized type.

Parameters:
t - any type
Returns:
the corresponding class or null if it is not a normal class.

isParameterizedType

public static ParameterizedType isParameterizedType(Type t)
checks whether the given type is a parameterized type.

Parameters:
t - any type
Returns:
the corresponding parameterized type or null if it is not a parameterized.

isCollection

public static Class isCollection(Type t)
checks whether the type is an admissible collection class.

Parameters:
t -
Returns:
the corresponding class or null if it is not admissible.

Examples:
HashSet is admissible
Set is not admissible because Set cannot be instantiated. HashSet is not admissible because the components must be ordinary classes


getArrayClass

public static Class getArrayClass(GenericArrayType type)
returns for a generic array type the corresponding Java class


main

public static void main(String[] args)
The main method prints the environment variables and the system properties.