Package org.intermine.util
Class DynamicUtil
- java.lang.Object
-
- org.intermine.util.DynamicUtil
-
public final class DynamicUtil extends java.lang.Object
Utilities to create DynamicBeans- Author:
- Andrew Varley
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Class<? extends FastPathObject>
composeClass(java.lang.Class<?>... classes)
Return the Class for the array of Class objects.static java.lang.Class<? extends FastPathObject>
composeClass(java.util.Set<java.lang.Class<?>> classes)
Return the Class for a set of Class objects.static java.lang.Class<? extends FastPathObject>
composeDescriptiveClass(java.lang.Class<?>... classes)
Return the Class for the array of Class objects.protected static java.util.Set<java.lang.Class<?>>
convertToClasses(java.util.Set<java.lang.String> names)
Convert a set of interface names to a set of Class objectsstatic <C extends FastPathObject>
CcreateObject(java.lang.Class<C> clazz)
Create a new object given a class (not an interface).static FastPathObject
createObject(java.util.Set<? extends java.lang.Class<?>> classes)
Create a DynamicBean from a Set of Class objectsstatic java.lang.String
getFriendlyDesc(java.lang.Object o)
Creates a friendly description of an object - that is, the class and the ID (if it has one).static java.lang.Class<? extends FastPathObject>
getSimpleClass(java.lang.Class<? extends FastPathObject> clazz)
Returns the result of decomposeClass if that is a single class, or throws an exception if there are more than one.static java.lang.Class<? extends FastPathObject>
getSimpleClass(FastPathObject obj)
For the given object returns the result of decomposeClass if that is a single class, or throws an exception if there are more than one class.static java.lang.String
getSimpleClassName(java.lang.Class<?> clazz)
Returns the simple class name for the given class or throws an exception if there are more than one.static java.lang.String
getSimpleClassName(FastPathObject obj)
Returns the simple class name for the given object or throws an exception if there are more than one.static FastPathObject
instantiateObject(java.lang.String className, java.lang.String implementations)
Create an outline business object from a class name and a list of interface namesstatic boolean
isAssignableFrom(java.lang.Class<?> sup, java.lang.Class<?> sub)
Returns true if sup is a superclass of sub (or the same), taking into account dynamic classes.static boolean
isInstance(java.lang.Object obj, java.lang.Class<?> clazz)
Returns true if obj is an instance of clazz, taking into account dynamic classes.static void
setFieldValue(java.lang.Object o, java.lang.String fieldName, java.lang.Object fieldValue)
Sets the value of a public or protected Field of an Object given the field name.static <C extends FastPathObject>
CsimpleCreateObject(java.lang.Class<C> clazz)
Create a new object given a class, which may be an interface.
-
-
-
Method Detail
-
createObject
public static FastPathObject createObject(java.util.Set<? extends java.lang.Class<?>> classes)
Create a DynamicBean from a Set of Class objects- Parameters:
classes
- the classes and interfaces to extend/implement- Returns:
- the DynamicBean
- Throws:
java.lang.IllegalArgumentException
- if there is more than one Class, or if fields are not compatible.
-
simpleCreateObject
public static <C extends FastPathObject> C simpleCreateObject(java.lang.Class<C> clazz)
Create a new object given a class, which may be an interface. This method is equivalent to calling createObject(Collections.singleton(clazz)), except that it is genericised.- Type Parameters:
C
- The type of the object that is expected- Parameters:
clazz
- the class of the object to instantiate- Returns:
- the object
- Throws:
java.lang.IllegalArgumentException
- if an error occurs
-
createObject
public static <C extends FastPathObject> C createObject(java.lang.Class<C> clazz)
Create a new object given a class (not an interface). To create an object from interfaces use createObject(Set classes) or simpleCreateObject(Class).- Type Parameters:
C
- The type of the object that is expected- Parameters:
clazz
- the class of the object to instantiate- Returns:
- the object
- Throws:
java.lang.IllegalArgumentException
- if an error occurs
-
composeDescriptiveClass
public static java.lang.Class<? extends FastPathObject> composeDescriptiveClass(java.lang.Class<?>... classes)
Return the Class for the array of Class objects. This method returns a Class which is descriptive of the component classes, but not necessarily valid for instantiating. This means that passing a single interface into this method will return the interface rather than a class composed from it.- Parameters:
classes
- the classes and interfaces to extend/implement- Returns:
- the Class
- Throws:
java.lang.IllegalArgumentException
- if there is more than one Class, or if the fields are not compatible.
-
composeClass
public static java.lang.Class<? extends FastPathObject> composeClass(java.lang.Class<?>... classes)
Return the Class for the array of Class objects.- Parameters:
classes
- the classes and interfaces to extend/implement- Returns:
- the Class
- Throws:
java.lang.IllegalArgumentException
- is there is more than one Class, or if the fields are not compatible.
-
composeClass
public static java.lang.Class<? extends FastPathObject> composeClass(java.util.Set<java.lang.Class<?>> classes)
Return the Class for a set of Class objects. NOTE: Creating an instance of this class is not trivial: after calling Class.newInstance(), cast the Object to net.sf.cglib.proxy.Factory, and call interceptor(new org.intermine.util.DynamicBean()) on it.- Parameters:
classes
- the classes and interfaces to extend/implement- Returns:
- the Class
- Throws:
java.lang.IllegalArgumentException
- if there is more than one Class, or if the fields are not compatible.
-
convertToClasses
protected static java.util.Set<java.lang.Class<?>> convertToClasses(java.util.Set<java.lang.String> names) throws java.lang.ClassNotFoundException
Convert a set of interface names to a set of Class objects- Parameters:
names
- the set of interface names- Returns:
- set of Class objects
- Throws:
java.lang.ClassNotFoundException
- if class cannot be found
-
instantiateObject
public static FastPathObject instantiateObject(java.lang.String className, java.lang.String implementations) throws java.lang.ClassNotFoundException
Create an outline business object from a class name and a list of interface names- Parameters:
className
- the class nameimplementations
- a space separated list of interface names- Returns:
- the materialised business object
- Throws:
java.lang.ClassNotFoundException
- if className can't be found
-
getFriendlyDesc
public static java.lang.String getFriendlyDesc(java.lang.Object o)
Creates a friendly description of an object - that is, the class and the ID (if it has one).- Parameters:
o
- the object to be described- Returns:
- a String description
-
getSimpleClassName
public static java.lang.String getSimpleClassName(java.lang.Class<?> clazz)
Returns the simple class name for the given class or throws an exception if there are more than one.- Parameters:
clazz
- the class- Returns:
- the simple class name
-
getSimpleClassName
public static java.lang.String getSimpleClassName(FastPathObject obj)
Returns the simple class name for the given object or throws an exception if there are more than one.- Parameters:
obj
- an object from the model- Returns:
- the simple class name
-
isAssignableFrom
public static boolean isAssignableFrom(java.lang.Class<?> sup, java.lang.Class<?> sub)
Returns true if sup is a superclass of sub (or the same), taking into account dynamic classes.- Parameters:
sup
- the supposed superclasssub
- the supposed subclass- Returns:
- a boolean
-
isInstance
public static boolean isInstance(java.lang.Object obj, java.lang.Class<?> clazz)
Returns true if obj is an instance of clazz, taking into account dynamic classes.- Parameters:
obj
- the Objectclazz
- the Class- Returns:
- a boolean
-
getSimpleClass
public static java.lang.Class<? extends FastPathObject> getSimpleClass(java.lang.Class<? extends FastPathObject> clazz)
Returns the result of decomposeClass if that is a single class, or throws an exception if there are more than one.- Parameters:
clazz
- the class- Returns:
- the corresponding non-dynamic class
-
getSimpleClass
public static java.lang.Class<? extends FastPathObject> getSimpleClass(FastPathObject obj)
For the given object returns the result of decomposeClass if that is a single class, or throws an exception if there are more than one class.- Parameters:
obj
- an object from the model- Returns:
- the corresponding non-dynamic class
-
setFieldValue
public static void setFieldValue(java.lang.Object o, java.lang.String fieldName, java.lang.Object fieldValue)
Sets the value of a public or protected Field of an Object given the field name. This used to be in TypeUtil.- Parameters:
o
- the ObjectfieldName
- the name of the relevant FieldfieldValue
- the value of the Field
-
-