Package org.intermine.metadata
Class TypeUtil
- java.lang.Object
-
- org.intermine.metadata.TypeUtil
-
public final class TypeUtil extends java.lang.ObjectProvides utility methods for working with Java types and reflection- Author:
- Mark Woodbridge, Richard Smith, Matthew Wakeling
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTypeUtil.FieldInfoInner class to hold info on a field.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidaddCollectionElement(java.lang.Object o, java.lang.String fieldName, java.lang.Object element)Adds an element to a public or protected collection of an Object given the field name.static <T> TcreateNew(java.lang.String typeName)Encapsulate the logic of requesting an object of a class namednamewhere that class has a default no-arguments constructor.static java.util.List<java.lang.Object>flatten(java.lang.Object obj)Make all nested objects top-level in returned collectionstatic java.lang.StringgenerateClassName(java.lang.String packageName, java.lang.String className)Generate the full class name, eg.static java.lang.reflect.MethodgetAdder(java.lang.Class<?> c, java.lang.String fieldName)Returns the Method object that is the adder for the field namestatic java.lang.Class<?>getClass(java.lang.String className)Instantiate a class by unqualified name The name should be "Date" or that of a primitive container class such as "Integer"static java.lang.Class<?>getClass(java.lang.String className, Model model)Instantiate a class by unqualified name The name should be "InterMineObject" or the name of class in the model providedstatic java.lang.Class<? extends FastPathObject>getElementType(java.lang.Class<?> c, java.lang.String fieldName)Returns the element type of a collection given the field name.static TypeUtil.FieldInfogetFieldInfo(java.lang.Class<?> c, java.lang.String fieldname)Returns a FieldInfo object for the given class and field name.static java.util.Map<java.lang.String,TypeUtil.FieldInfo>getFieldInfos(java.lang.Class<?> c)Returns the Map from field name to TypeUtil.FieldInfo objects for all the fields in a given class.static java.lang.ObjectgetFieldProxy(java.lang.Object o, java.lang.String fieldName)Returns the value of a public or protected Field of an Object given by the field name without dereferencing any ProxyReference objects.static java.lang.Class<?>getFieldType(java.lang.Class<?> c, java.lang.String fieldName)Returns the type of a field given the field name.static java.lang.ObjectgetFieldValue(java.lang.Object o, java.lang.String fieldName)Returns the value of a public or protected Field of an Object given the field namestatic java.lang.reflect.MethodgetGetter(java.lang.Class<?> c, java.lang.String fieldName)Returns the Method object that is the getter for the field name.static java.lang.reflect.Method[]getGetters(java.lang.Class<?> c)Gets the getter methods for the bean properties of a classstatic java.lang.reflect.MethodgetProxyGetter(java.lang.Class<?> c, java.lang.String fieldName)Returns the Method object that is the proxyGetter for the field namestatic java.lang.reflect.MethodgetProxySetter(java.lang.Class<?> c, java.lang.String fieldName)Returns the Method object that is the proxySetter for the field namestatic java.lang.reflect.MethodgetSetter(java.lang.Class<?> c, java.lang.String fieldName)Returns the Method object that is the setter for the field namestatic java.lang.Class<?>getTypeByName(java.lang.String name)Returns the Class for a given name (promoting primitives to their container class) and returningnullif the name does not refer to any known class.static java.lang.Class<?>instantiate(java.lang.String type)Deprecated.This method is named incorrectly - usegetTypeByNameinstead.static booleanisInstanceOf(FastPathObject object, java.lang.String className)Return true if and only if the object is an instance of the class given by the className.static java.lang.StringjavaiseClassName(java.lang.String s)Filter a URI fragment to remove illegal charactersstatic java.lang.StringjavaisePackageName(java.lang.String s)Filter a string to remove illegal characters and join the rest in lower case.static java.lang.StringpackageName(java.lang.String className)Returns the package name from a fully qualified class namestatic java.lang.ObjectstringToObject(java.lang.Class<?> clazz, java.lang.String value)Returns an object for a given String.static java.lang.StringunqualifiedName(java.lang.String className)Returns the unqualified class name from a fully qualified class name
-
-
-
Method Detail
-
packageName
public static java.lang.String packageName(java.lang.String className)
Returns the package name from a fully qualified class name- Parameters:
className- the fully qualified class name- Returns:
- the package name
-
unqualifiedName
public static java.lang.String unqualifiedName(java.lang.String className)
Returns the unqualified class name from a fully qualified class name- Parameters:
className- the fully qualified class name- Returns:
- the unqualified name
-
getFieldValue
public static java.lang.Object getFieldValue(java.lang.Object o, java.lang.String fieldName) throws java.lang.IllegalAccessExceptionReturns the value of a public or protected Field of an Object given the field name- Parameters:
o- the ObjectfieldName- the name of the relevant Field- Returns:
- the value of the Field
- Throws:
java.lang.IllegalAccessException- if the field is inaccessible
-
getFieldProxy
public static java.lang.Object getFieldProxy(java.lang.Object o, java.lang.String fieldName) throws java.lang.IllegalAccessExceptionReturns the value of a public or protected Field of an Object given by the field name without dereferencing any ProxyReference objects.- Parameters:
o- the ObjectfieldName- the name of the relevant Field- Returns:
- the value of the field, without dereferencing ProxyReferences
- Throws:
java.lang.IllegalAccessException- if the field is inaccessible
-
addCollectionElement
public static void addCollectionElement(java.lang.Object o, java.lang.String fieldName, java.lang.Object element)Adds an element to a public or protected collection of an Object given the field name.- Parameters:
o- the ObjectfieldName- the name of the relevant collectionelement- the element to add to the collection
-
getGetter
public static java.lang.reflect.Method getGetter(java.lang.Class<?> c, java.lang.String fieldName)Returns the Method object that is the getter for the field name.- Parameters:
c- the ClassfieldName- the name of the relevant field- Returns:
- the Getter, or null if the field is not found
-
getSetter
public static java.lang.reflect.Method getSetter(java.lang.Class<?> c, java.lang.String fieldName)Returns the Method object that is the setter for the field name- Parameters:
c- the ClassfieldName- the name of the relevant field- Returns:
- the setter, or null if the field is not found
-
getProxySetter
public static java.lang.reflect.Method getProxySetter(java.lang.Class<?> c, java.lang.String fieldName)Returns the Method object that is the proxySetter for the field name- Parameters:
c- the ClassfieldName- the name of the relevant field- Returns:
- the proxySetter, or null if it is not present or the field is not found
-
getProxyGetter
public static java.lang.reflect.Method getProxyGetter(java.lang.Class<?> c, java.lang.String fieldName)Returns the Method object that is the proxyGetter for the field name- Parameters:
c- the ClassfieldName- the name of the relevant field- Returns:
- the proxyGetter, or null if it is not present or the field is not found
-
getAdder
public static java.lang.reflect.Method getAdder(java.lang.Class<?> c, java.lang.String fieldName)Returns the Method object that is the adder for the field name- Parameters:
c- the ClassfieldName- the name of the relevant collection- Returns:
- the adder, or null if it is not present or if the field is not found
-
getFieldType
public static java.lang.Class<?> getFieldType(java.lang.Class<?> c, java.lang.String fieldName)Returns the type of a field given the field name.- Parameters:
c- the ClassfieldName- the name of the relevant field- Returns:
- the class of the field, or null if the field is not found
-
getElementType
public static java.lang.Class<? extends FastPathObject> getElementType(java.lang.Class<?> c, java.lang.String fieldName)
Returns the element type of a collection given the field name.- Parameters:
c- the ClassfieldName- the name of the relevant collection- Returns:
- the class of the field, or null if the field is not found
- Throws:
java.lang.IllegalArgumentException- if the field is not a collection
-
getFieldInfos
public static java.util.Map<java.lang.String,TypeUtil.FieldInfo> getFieldInfos(java.lang.Class<?> c)
Returns the Map from field name to TypeUtil.FieldInfo objects for all the fields in a given class.- Parameters:
c- the Class- Returns:
- a Map from field name to FieldInfo object
-
getFieldInfo
public static TypeUtil.FieldInfo getFieldInfo(java.lang.Class<?> c, java.lang.String fieldname)
Returns a FieldInfo object for the given class and field name.- Parameters:
c- the Classfieldname- the fieldname- Returns:
- a FieldInfo object, or null if the fieldname is not found
-
getGetters
public static java.lang.reflect.Method[] getGetters(java.lang.Class<?> c) throws java.beans.IntrospectionExceptionGets the getter methods for the bean properties of a class- Parameters:
c- the Class- Returns:
- an array of the getter methods
- Throws:
java.beans.IntrospectionException- if an error occurs
-
flatten
public static java.util.List<java.lang.Object> flatten(java.lang.Object obj) throws java.lang.ExceptionMake all nested objects top-level in returned collection- Parameters:
obj- a top-level object or collection of such objects- Returns:
- a set of objects
- Throws:
java.lang.Exception- if a problem occurred during flattening
-
createNew
public static <T> T createNew(java.lang.String typeName)
Encapsulate the logic of requesting an object of a class namednamewhere that class has a default no-arguments constructor. If any of the contracts are violated you will get an IllegalArgumentException.- Type Parameters:
T- The type of the object we are instantiating, for type inference purposes.- Parameters:
typeName- The name of the class.- Returns:
- An instance of that class.
-
getTypeByName
public static java.lang.Class<?> getTypeByName(java.lang.String name)
Returns the Class for a given name (promoting primitives to their container class) and returningnullif the name does not refer to any known class.- Parameters:
name- a classname- Returns:
- the corresponding Class
-
instantiate
@Deprecated public static java.lang.Class<?> instantiate(java.lang.String type)
Deprecated.This method is named incorrectly - usegetTypeByNameinstead.Returns the Class for a given name (promoting primitives to their container class)- Parameters:
type- a class-name- Returns:
- the corresponding Class
-
javaiseClassName
public static java.lang.String javaiseClassName(java.lang.String s)
Filter a URI fragment to remove illegal characters- Parameters:
s- the relevant string- Returns:
- the filtered string
-
generateClassName
public static java.lang.String generateClassName(java.lang.String packageName, java.lang.String className)Generate the full class name, eg. org.intermine.bio.SequenceFeature from a SO term and a package name.- Parameters:
packageName- namespace, eg. org.intermine.bioclassName- so term name, eg. sequence_feature- Returns:
- full name of class, eg. org.intermine.bio.SequenceFeature
-
isInstanceOf
public static boolean isInstanceOf(FastPathObject object, java.lang.String className) throws java.lang.ClassNotFoundException
Return true if and only if the object is an instance of the class given by the className.- Parameters:
object- the object to testclassName- the super class name to test for- Returns:
- true if object is an instance of className
- Throws:
java.lang.ClassNotFoundException- if the class given by className cannot be located
-
getClass
public static java.lang.Class<?> getClass(java.lang.String className)
Instantiate a class by unqualified name The name should be "Date" or that of a primitive container class such as "Integer"- Parameters:
className- the name of the class- Returns:
- the relevant Class
-
getClass
public static java.lang.Class<?> getClass(java.lang.String className, Model model) throws java.lang.ClassNotFoundExceptionInstantiate a class by unqualified name The name should be "InterMineObject" or the name of class in the model provided- Parameters:
className- the name of the classmodel- the Model used to resolve class names- Returns:
- the relevant Class
- Throws:
java.lang.ClassNotFoundException- if the class name is not in the model
-
javaisePackageName
public static java.lang.String javaisePackageName(java.lang.String s)
Filter a string to remove illegal characters and join the rest in lower case.- Parameters:
s- e.g. modMine_TEST-2.r- Returns:
- a string with no special character such as space, "_" or others, e.g. modminetest2r
-
stringToObject
public static java.lang.Object stringToObject(java.lang.Class<?> clazz, java.lang.String value)Returns an object for a given String. This used to be in TypeUtil.- Parameters:
clazz- the class to convert tovalue- the value to convert- Returns:
- the corresponding Class
-
-