public final class MBeanUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
MBeanUtils.ClassImportHolder
Implementation allowing to import the classes of a package, but not of its sub-packages.
|
static interface |
MBeanUtils.ImportHolder
Describes a class which will be able to register one or more imports and to resolve a Class by it's simple name, using these imports.
|
static class |
MBeanUtils.ImportHolderCombiner
Implementation to combine several ImportHolder, in a predictible order.
|
static class |
MBeanUtils.ImportValidationException
Exception dedicated to ImportHolder validation.
|
static class |
MBeanUtils.PackageImportHolder
Implementation allowing to import the classes of a package, but not of its sub-packages.
|
Modifier and Type | Method and Description |
---|---|
static <C> C |
advancedNewInstance(Class<C> clazz)
Dispatches between
newCollectionInstance(Class) , newMapInstance(Class) and newInstance(Class)
depending on provided parameter. |
static boolean |
arraysEq(Object o1,
Object o2)
Calls
arraysEq(Object, Object, boolean) with valForNull false
(i.e. if one is null, result is false). |
static boolean |
arraysEq(Object o1,
Object o2,
boolean valForNull)
Compares the 2 provided arrays, returning :
true if both are null
valForNull if one is null
result of
Arrays.equals(Object[], Object[]) otherwise
|
static Object |
call(Object inst,
String mName)
Shortcut for a no-arg call.
|
static Object |
call(Object inst,
String mName,
Class<?>[] argTypes,
Object... args)
Performs a call by reflection on the provided instance to the method name provided.
|
static boolean |
clearContent(Object value)
If accurate, clears the object content.
|
static <A> A |
coalesce(A data,
A defaultIfDataNull) |
static <A extends Comparable<? super A>> |
comp(A o1,
A o2)
Compares 2 Comparable handling null values.
|
static <A extends Comparable<? super A>> |
comp(A o1,
A o2,
boolean nullFirst)
Compares 2 Comparable handling null values.
|
static int |
comp(boolean i1,
boolean i2)
Compares 2 boolean.
|
static int |
comp(double d1,
double d2)
Compares 2 double.
|
static int |
comp(float f1,
float f2)
Compares 2 float.
|
static int |
comp(int i1,
int i2)
Compares 2 int.
|
static int |
comp(long l1,
long l2)
Compares 2 long.
|
static int |
computeArrayHashCode(Object o)
Computes the hashcode of an array.
|
static int |
computeHashCode(Object o)
Computes the hashcode of an object.
|
static int |
computeHashCodeNoOrder(Collection<?> c)
Computes the hashcode of a Collection, ignoring elements order.
|
static <C extends Collection<T>,T> |
convertToCollection(Class<C> collectionClass,
Class<T> paramClass,
Object value)
Converts provided value to a new
Collection :
If Iterable provided : duplicates into a new collection of type collectionClass if provided,
otherwise of the same class as value if value is a Collection, otherwise an instance of and implementation of Collection. |
static boolean |
eq(boolean o1,
boolean o2)
Tests if these two values are equals.
|
static boolean |
eq(byte o1,
byte o2)
Tests if these two values are equals.
|
static boolean |
eq(char o1,
char o2)
Tests if these two values are equals.
|
static boolean |
eq(double o1,
double o2)
Tests if these two values are equals.
|
static boolean |
eq(double o1,
double o2,
double delta)
Tests if these two values are equals, authorizing a difference of max delta.
|
static boolean |
eq(float o1,
float o2)
Tests if these two values are equals.
|
static boolean |
eq(float o1,
float o2,
float delta)
Tests if these two values are equals, authorizing a difference of max delta..
|
static boolean |
eq(int o1,
int o2)
Tests if these two values are equals.
|
static boolean |
eq(long o1,
long o2)
Tests if these two values are equals.
|
static boolean |
eq(Object o1,
Object o2)
Calls
eq(Object, Object, boolean) with valForNull false
(i.e. if one is null, result is false). |
static boolean |
eq(Object o1,
Object o2,
boolean valForNull)
Compares the 2 provided objects, returning :
true if both are null
valForNull if one is null
result of
arraysEq(Object, Object, boolean) if arrays are compared
result of equals() otherwise
|
static boolean |
eq(short o1,
short o2)
Tests if these two values are equals.
|
static Class<?> |
forName(String clazzName,
boolean wrapPrimitive)
Performs the Class.forName operation, handling correctly primitive names
and resolving unqualified names according to registered imported packages.
|
static Class<?> |
forName(String clazzName,
boolean wrapPrimitive,
boolean initialize) |
static Class<?> |
forName(String clazzName,
boolean wrapPrimitive,
boolean initialize,
ClassLoader loader) |
static Class<?> |
forName(String clazzName,
boolean wrapPrimitive,
boolean initialize,
ClassLoader loader,
MBeanUtils.ImportHolder holder) |
static Class<?> |
forName(String clazzName,
boolean wrapPrimitive,
boolean initialize,
MBeanUtils.ImportHolder holder) |
static Class<?> |
forName(String clazzName,
boolean wrapPrimitive,
MBeanUtils.ImportHolder holder)
Performs the Class.forName operation, handling correctly primitive names
and resolving unqualified names according to provided holder (if not null) and then to registered imported packages.
|
static String |
getCanonicalClassName(String className)
Converts a given name of class into canonical format.
|
static <C extends Collection> |
getCollectionConcrete(Class<C> base)
Gets a concrete class to be used when instantiating a collection from an interface or abstract class.
|
static Class<?> |
getComponentType(Class<?> array)
Returns the final component type of a multidimensional array.
|
static Integer |
getContentSize(Object value)
Returns the size of an Objects content, if accurate.
|
static Object |
getDefaultPrimitiveValue(Class<?> c)
For a wrapper or a primitive, retrieves the corresponding default value.
|
static int |
getDimensionsCount(Class<?> array)
Counts the number of dimension of the Array represented by the provided Class.
|
static int |
getDimensionsCount(Object array)
Counts the number of dimension of the provided Array.
|
static Class<?> |
getGenericCollectionType(Field f)
Returns the generic type if the field type is a collection, or null if it cannot be resolved
or if the property type is not a collection.
|
static Class<?> |
getGenericCollectionType(Method readMethod)
Returns the generic type if the method return type is a collection, or null if it cannot be resolved
or if the property type is not a collection.
|
static Class<?> |
getGenericMapKeyType(Field f)
Returns the key type if the field type is a map, or null if it cannot be resolved
or if the property type is not a map.
|
static Class<?> |
getGenericMapKeyType(Method readMethod)
Returns the key type if the method return type is a map, or null if it cannot be resolved
or if the property type is not a map.
|
static Class<?> |
getGenericMapValueType(Field f)
Returns the value type if the field type is a map, or null if it cannot be resolved
or if the property type is not a map.
|
static Class<?> |
getGenericMapValueType(Method readMethod)
Returns the value type if the method return type is a map, or null if it cannot be resolved
or if the property type is not a map.
|
static Class<?>[] |
getGenericTypes(Field f)
Returns the parameterized types of the field, or null if it cannot be resolved.
|
static Class<?>[] |
getGenericTypes(Method readMethod)
Returns the parameterized types of the method, or null if it cannot be resolved.
|
static Set<String> |
getImportedPackage() |
static String |
getJlsClassName(String className)
Converts a class name to a JLS style class name.
|
static <C extends Map> |
getMapConcrete(Class<C> base)
Gets a concrete class to be used when instantiating a Map from an interface or abstract class.
|
static Integer |
getNumberOfElements(Object value)
Returns the size of an Objects content, if accurate.
|
static Class<?> |
getPrimitive(Class<?> c)
For a wrapper, retrieves the corresponding primitive class or null.
|
static Class<?> |
getPrimitiveClass(String name)
For a primitive, retrieves the corresponding Wrapper class or null.
|
static Class<?> |
getWrapper(Class<?> c)
For a primitive, retrieves the corresponding Wrapper class.
|
static boolean |
hasDefaultConstructor(Class<?> c) |
static boolean |
isCollectionClass(Class<?> c,
boolean withArrays)
Tests if this Class represents a Collection.
|
static boolean |
isCollectionInstance(Object o,
boolean withArrays)
Tests if this object is an instance of a Collection.
|
static boolean |
isCollectionsEquivalent(Collection<?> srcCol,
Collection<?> refCol)
Tests if 2 collections are equivalent in terms of content.
|
static Boolean |
isContentEmpty(Object value)
Returns the size of an Objects content, if accurate.
|
static boolean |
isListClass(Class<?> c,
boolean withArrays)
Tests if this Class represents a List.
|
static boolean |
isListInstance(Object o,
boolean withArrays)
Tests if this object is an instance of a List.
|
static boolean |
isMapClass(Class<?> c)
Tests if this Class represents a Map.
|
static boolean |
isMapInstance(Object o)
Tests if this object is an instance of a Map.
|
static <A extends Comparable<? super A>> |
max(A o1,
A o2)
Compares the 2 provided objects, returning the "max".
|
static <A extends Comparable<? super A>> |
min(A o1,
A o2)
Compares the 2 provided objects, returning the "min".
|
static Object |
newArray(Class<?> type,
int length,
int dimensions)
Instantiates new Array of this type, of the specified length, with the specified number of dimensions.
|
static <C extends Collection<?>> |
newCollectionInstance(Class<C> clazz)
Instanciates a new Collection of this Class.
|
static <C> C |
newInstance(Class<C> clazz)
Simply tries to instantiate provided class.
|
static <T,C extends T> |
newInstance(String clazzName,
Class<T> targetClazz)
Returns a new instance of the provided Class, which must be instanciable with default constructor.
|
static <C extends Map<?,?>> |
newMapInstance(Class<C> clazz)
Instantiates a new Map of this Class.
|
static <I extends Iterable> |
registerCollectionConcrete(Class<I> base,
Class<? extends I> impl)
Registers a concrete class to be used when instantiating a collection from an interface class.
|
static boolean |
registerCollectionConcrete(String base,
String impl)
Safe version of
registerCollectionConcrete(Class, Class) , preserving from lacking class in older JDK. |
static void |
registerImportedPackage(String p) |
static <I extends Map> |
registerMapConcrete(Class<I> base,
Class<? extends I> impl)
Registers a concrete class to be used when instantiating a map from an interface class.
|
static boolean |
registerMapConcrete(String base,
String impl)
Safe version of
registerMapConcrete(Class, Class) , preserving from lacking class in older JDK. |
static boolean |
setAccessible(AccessibleObject o) |
static Object |
staticCall(Class<?> clazz,
String mName)
Shortcut for a no-arg static call.
|
static Object |
staticCall(Class<?> clazz,
String mName,
Class<?>[] argTypes,
Object... args)
Performs a static call by reflection on the provided class to the method name provided.
|
public static <A> A coalesce(A data, A defaultIfDataNull)
public static int comp(boolean i1, boolean i2)
public static int comp(int i1, int i2)
public static int comp(long l1, long l2)
public static int comp(float f1, float f2)
public static int comp(double d1, double d2)
public static <A extends Comparable<? super A>> int comp(A o1, A o2)
public static <A extends Comparable<? super A>> int comp(A o1, A o2, boolean nullFirst)
public static boolean eq(int o1, int o2)
public static boolean eq(long o1, long o2)
public static boolean eq(boolean o1, boolean o2)
public static boolean eq(char o1, char o2)
public static boolean eq(byte o1, byte o2)
public static boolean eq(short o1, short o2)
public static boolean eq(double o1, double o2)
public static boolean eq(double o1, double o2, double delta)
public static boolean eq(float o1, float o2, float delta)
public static boolean eq(float o1, float o2)
public static boolean eq(Object o1, Object o2)
eq(Object, Object, boolean)
with valForNull false
(i.e. if one is null, result is false).public static boolean eq(Object o1, Object o2, boolean valForNull)
arraysEq(Object, Object, boolean)
if arrays are comparedpublic static boolean arraysEq(Object o1, Object o2)
arraysEq(Object, Object, boolean)
with valForNull false
(i.e. if one is null, result is false).public static boolean arraysEq(Object o1, Object o2, boolean valForNull)
Arrays.equals(Object[], Object[])
otherwisepublic static Class<?> getPrimitiveClass(String name)
public static Class<?> getWrapper(Class<?> c)
public static Class<?> getPrimitive(Class<?> c)
public static Object getDefaultPrimitiveValue(Class<?> c)
public static boolean registerMapConcrete(String base, String impl)
registerMapConcrete(Class, Class)
, preserving from lacking class in older JDK.public static <I extends Map> Class<? extends I> registerMapConcrete(Class<I> base, Class<? extends I> impl)
IllegalArgumentException
- if some problem occurs during registration process.
As this method should not be part of the compilation / execution process, no dedicated Exception is raised.public static <C extends Map> Class<? extends C> getMapConcrete(Class<C> base)
public static <C extends Map<?,?>> C newMapInstance(Class<C> clazz) throws UtilsException
getMapConcrete(Class)
to obtain one.UtilsException
public static boolean registerCollectionConcrete(String base, String impl)
registerCollectionConcrete(Class, Class)
, preserving from lacking class in older JDK.public static <I extends Iterable> Class<? extends I> registerCollectionConcrete(Class<I> base, Class<? extends I> impl)
IllegalArgumentException
- if some problem occurs during registration process.
As this method should not be part of the compilation / execution process, no dedicated Exception is raised.public static <C extends Collection> Class<? extends C> getCollectionConcrete(Class<C> base)
public static <C extends Collection<?>> C newCollectionInstance(Class<C> clazz) throws UtilsException
getCollectionConcrete(Class)
to obtain one.UtilsException
public static <C extends Collection<T>,T> C convertToCollection(Class<C> collectionClass, Class<T> paramClass, Object value) throws UtilsException
Collection
:
Iterable
provided : duplicates into a new collection of type collectionClass if provided,
otherwise of the same class as value if value is a Collection, otherwise an instance of and implementation of Collection.collectionClass
- paramClass
- value
- UtilsException
public static boolean hasDefaultConstructor(Class<?> c)
public static boolean isCollectionsEquivalent(Collection<?> srcCol, Collection<?> refCol)
public static boolean isMapInstance(Object o)
public static boolean isMapClass(Class<?> c)
public static boolean isCollectionInstance(Object o, boolean withArrays)
public static boolean isCollectionClass(Class<?> c, boolean withArrays)
public static boolean isListInstance(Object o, boolean withArrays)
public static boolean isListClass(Class<?> c, boolean withArrays)
public static void registerImportedPackage(String p)
public static String getJlsClassName(String className)
Converts a class name to a JLS style class name.
If name of class is not a name of array class it returns
unchanged name.
Example:
getJlsClassName("int[]") = "[I"
getJlsClassName("java.lang.String[]") = "[Ljava.lang.String;"
getJlsClassName("java.lang.String") = "java.lang.String"
className
- the class namepublic static String getCanonicalClassName(String className)
Converts a given name of class into canonical format.
If name of class is not a name of array class it returns
unchanged name.
Example:
getCanonicalName("[I") = "int[]"
getCanonicalName("[Ljava.lang.String;") = "java.lang.String[]"
getCanonicalName("java.lang.String") = "java.lang.String"
className
- the name of classpublic static Class<?> forName(String clazzName, boolean wrapPrimitive) throws UtilsException
UtilsException
public static Class<?> forName(String clazzName, boolean wrapPrimitive, MBeanUtils.ImportHolder holder) throws UtilsException
UtilsException
public static Class<?> forName(String clazzName, boolean wrapPrimitive, boolean initialize) throws UtilsException
UtilsException
forName(String, boolean)
public static Class<?> forName(String clazzName, boolean wrapPrimitive, boolean initialize, MBeanUtils.ImportHolder holder) throws UtilsException
UtilsException
forName(String, boolean, ImportHolder)
public static Class<?> forName(String clazzName, boolean wrapPrimitive, boolean initialize, ClassLoader loader) throws UtilsException
UtilsException
forName(String, boolean)
public static Class<?> forName(String clazzName, boolean wrapPrimitive, boolean initialize, ClassLoader loader, MBeanUtils.ImportHolder holder) throws UtilsException
UtilsException
forName(String, boolean, ImportHolder)
public static <C> C advancedNewInstance(Class<C> clazz) throws UtilsException
newCollectionInstance(Class)
, newMapInstance(Class)
and newInstance(Class)
depending on provided parameter.UtilsException
public static <T,C extends T> C newInstance(String clazzName, Class<T> targetClazz) throws UtilsException
UtilsException
public static <C> C newInstance(Class<C> clazz) throws UtilsException
UtilsException
public static boolean setAccessible(AccessibleObject o)
public static Object call(Object inst, String mName) throws UtilsException
UtilsException
public static Object call(Object inst, String mName, Class<?>[] argTypes, Object... args) throws UtilsException
UtilsException
public static Object staticCall(Class<?> clazz, String mName) throws UtilsException
UtilsException
public static Object staticCall(Class<?> clazz, String mName, Class<?>[] argTypes, Object... args) throws UtilsException
UtilsException
public static Class<?>[] getGenericTypes(Method readMethod)
public static Class<?> getGenericCollectionType(Method readMethod)
public static Class<?> getGenericMapValueType(Method readMethod)
public static Class<?> getGenericMapKeyType(Method readMethod)
public static Class<?>[] getGenericTypes(Field f)
public static Class<?> getGenericCollectionType(Field f)
public static Class<?> getGenericMapValueType(Field f)
public static Class<?> getGenericMapKeyType(Field f)
public static <A extends Comparable<? super A>> A min(A o1, A o2)
public static <A extends Comparable<? super A>> A max(A o1, A o2)
public static int computeHashCode(Object o)
computeArrayHashCode(Object)
. If null, returns 0.public static int computeHashCodeNoOrder(Collection<?> c)
public static int computeArrayHashCode(Object o)
IllegalArgumentException
. If null, returns 0.public static int getDimensionsCount(Object array)
public static int getDimensionsCount(Class<?> array)
public static Class<?> getComponentType(Class<?> array)
public static Object newArray(Class<?> type, int length, int dimensions)
Array.newInstance(Class, int...)
public static Integer getContentSize(Object value)
value
- public static Integer getNumberOfElements(Object value)
value
- public static Boolean isContentEmpty(Object value)
value
- Value to checkpublic static boolean clearContent(Object value) throws UnsupportedOperationException
value
- Value to clearUnsupportedOperationException
- is clear is not supported by implementation (for collections or maps for instance)