|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
ObjectUtilities
public class Utilities
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 |
---|
public Utilities()
Method Detail |
---|
public static Type[] returnsMap(Method method)
method
- any method
Example: HashMap
public static String concatenate(String s, int n)
public static String adjustPairs(String text, String separator)
The resulting string should be formatted as a proper table.
text
- a string consisting of lines like, for example, abc -> cde.separator
- a string separating pairs in the line.
public static Object[] parseParameters(String[] parameters, Set<String> postfixes, Set<String> keys)
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.
HashMap<String,ArrayList<String>> postFixParameters
the recognised parameters matching the postfixes.HashMap<String,String> keyValues
the recognised values of the key-value pairs.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.
public static String getEnvirentmentVariables()
public static String getSystemProperties()
public static Class classFromFile(File file) throws ClassNotFoundException
If the class has already been loaded, the loaded class is returned.
file
- the compiled Java-file.
ClassNotFoundException.
ClassNotFoundException
public static Object[] classesFromDirectory(File directory)
directory
- the directory with the compiled Java-files.
public static boolean map(Object object, Function function)
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.
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
public static StackTraceElement findStackFrame(String prefix)
Classes starting with java, sun., org., are ignored.
prefix
- The prefix of a fully qualified class name.
public static String stackTraceString(Throwable throwable)
public static Object invoke0(Object object, String methodname) throws Exception
This works for cases where object.<methodname>()
does not work
because it is unknown how to cast the object.
object
- a Java-objectmethodname
- the name of a method without arguments
Exception
- if the invocation failed.public static String getName(Object object)
First it tries a getName() method, then the toString() method.
public static void setStaticField(Class jcl, String name, boolean value) throws Exception
jcl
- a Java classname
- the name of a static fieldvalue
- the boolean value to be set.
Exception
- if the operation failed.public static void setStaticField(Class jcl, String name, Object value) throws Exception
jcl
- a Java classname
- the name of a static fieldvalue
- the boolean value to be set.
Exception
- if the operation failed.public static void setObjectField(Object object, String name, Object value) throws Exception
object
- a Java object.name
- the name of an instance fieldvalue
- the value to be set.
Exception
- if the operation failed.public static void setObjectFlag(Object object, String name, int flag) throws Exception
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,...
Exception
- if the operation failed.public static boolean getObjectFlag(Object object, String name, int flag) throws Exception
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,...
Exception
- if the operation failed.public static void resetObjectFlag(Object object, String name, int flag) throws Exception
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,...
Exception
- if the operation failed.public static boolean getAndResetObjectFlag(Object object, String name, int flag) throws Exception
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,...
Exception
- if the operation failed.public static boolean getAndSetObjectFlag(Object object, String name, int flag) throws Exception
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,...
Exception
- if the operation failed.public static List<Object> asList(Object... objects)
Notice that this method is not generic because objects of mixed types are to be put into the list.
public static void printStack()
public static String getWrapperType(BasicType type)
public static BasicType getBasicType(Class type)
public static void bcelify(Class jcl, String filename) throws ClassNotFoundException, FileNotFoundException
jcl
- a Java classfilename
- a filename or null
ClassNotFoundException
FileNotFoundException
public static void transferFields(Object fromObject, Object toObject, Class toClass)
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!
public static void transferCommonFields(Object[] fromObjects, int i, Object toObject, Class toClass)
fromObjects
- an array of objects.i
- an index into fromObjectstoObject
- 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.
public static Constructor findConstructor(Class jcl, Class argument)
jcl
- the class containting the constructor.argument
- the class of the first argument of the constructor.
public static Object transferSet(Set items, Class targetClass)
items
- a settargetClass
- a collection class
public static Class isArray(Type t)
t
- any type
public static Class isClass(Type t)
t
- any type
public static ParameterizedType isParameterizedType(Type t)
t
- any type
public static Class isCollection(Type t)
t
-
Examples:
HashSet
Set
public static Class getArrayClass(GenericArrayType type)
public static void main(String[] args)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |