public class MetaUtils extends Object
Modifier and Type | Method and Description |
---|---|
static Class<?> |
classForName(String name,
ClassLoader classLoader)
Given the passed in String class name, return the named JVM class.
|
static int |
comparePrimitiveToWrapper(Class<?> source,
Class<?> destination)
Compare a primitive to a primitive Wrapper.
|
static int |
computeInheritanceDistance(Class<?> source,
Class<?> destination)
Computes the inheritance distance between two classes/interfaces/primitive types.
|
static String |
createCacheKey(Class<?> c,
Collection<?> args) |
static Optional<Class> |
getClassIfEnum(Class<?> c) |
static Map<String,Field> |
getDeepDeclaredFields(Class<?> c) |
static Field |
getField(Class<?> c,
String field)
Return an instance of the Java Field class corresponding to the passed in field name.
|
static String |
getLogMessage(String methodName,
Object[] args)
Format a nice looking method signature for logging output
|
static String |
getLogMessage(String methodName,
Object[] args,
int argCharLen) |
static <K,V> V |
getValue(Map map,
K key) |
static <K,V> V |
getValueWithDefaultForMissing(Map map,
K key,
V defaultValue) |
static <K,V> V |
getValueWithDefaultForNull(Map map,
K key,
V defaultValue) |
static int |
indexOfBestValue(int[] array)
Returns the index of the smallest value in an array.
|
static boolean |
isLogicalPrimitive(Class<?> c) |
static boolean |
isPrimitive(Class c) |
static <T> List<T> |
listOf(T... items)
For JDK1.8 support.
|
static void |
loadMapDefinition(Map<String,String> map,
String resName) |
static byte[] |
loadResourceAsBytes(String resourceName)
Loads resource content as a byte[].
|
static String |
loadResourceAsString(String resourceName)
Loads resource content as a String.
|
static void |
loadSetDefinition(Set<String> set,
String resName) |
static <K,V> Map<K,V> |
mapOf()
For JDK1.8 support.
|
static <K,V> Map<K,V> |
mapOf(K k,
V v) |
static <K,V> Map<K,V> |
mapOf(K k1,
V v1,
K k2,
V v2) |
static <K,V> Map<K,V> |
mapOf(K k1,
V v1,
K k2,
V v2,
K k3,
V v3) |
static List<Object> |
matchArgumentsToParameters(Collection<Object> values,
Parameter[] parameterTypes,
boolean useNull)
Build a List the same size of parameterTypes, where the objects in the list are ordered
to best match the parameters.
|
static Object |
newInstance(Class<?> c,
Collection<?> argumentValues)
Create a new instance of the passed in class c.
|
static String |
removeLeadingAndTrailingQuotes(String input)
Strip leading and trailing double quotes from the passed in String.
|
static <T> T |
safelyIgnoreException(Callable<T> callable,
T defaultValue) |
static void |
safelyIgnoreException(Runnable runnable) |
static void |
setFieldValue(Field field,
Object instance,
Object value) |
static <T> Set<T> |
setOf(T... items)
For JDK1.8 support.
|
static void |
setUseUnsafe(boolean state)
Globally turn on (or off) the 'unsafe' option of Class construction.
|
static void |
trySetAccessible(AccessibleObject object) |
public static void setUseUnsafe(boolean state)
state
- boolean true = on, false = off.@SafeVarargs public static <T> List<T> listOf(T... items)
@SafeVarargs public static <T> Set<T> setOf(T... items)
public static <K,V> Map<K,V> mapOf()
public static <K,V> Map<K,V> mapOf(K k, V v)
public static <K,V> Map<K,V> mapOf(K k1, V v1, K k2, V v2)
public static <K,V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3)
public static Field getField(Class<?> c, String field)
c
- class containing the field / field namefield
- String name of a field on the class.public static Map<String,Field> getDeepDeclaredFields(Class<?> c)
c
- Class instancepublic static int comparePrimitiveToWrapper(Class<?> source, Class<?> destination)
public static int computeInheritanceDistance(Class<?> source, Class<?> destination)
source
- The source class, interface, or primitive type.destination
- The destination class, interface, or primitive type.public static Class<?> classForName(String name, ClassLoader classLoader)
name
- String name of a JVM class.classLoader
- ClassLoader to use when searching for JVM classes.public static String removeLeadingAndTrailingQuotes(String input)
public static List<Object> matchArgumentsToParameters(Collection<Object> values, Parameter[] parameterTypes, boolean useNull)
values
- A list of potential arguments. This list can be smaller than parameterTypes
or larger.parameterTypes
- A list of classes that the values will be matched against.public static int indexOfBestValue(int[] array)
array
- The array to search.public static String createCacheKey(Class<?> c, Collection<?> args)
public static Object newInstance(Class<?> c, Collection<?> argumentValues)
c
- Class to instantiate.argumentValues
- List of values to supply to a constructor on 'c'. The constructor chosen on 'c'
will be the one with a combination of the most fields that are satisfied with
non-null values from the 'argumentsValues.' The method will attempt to use values
from the list as constructor arguments for the passed in class c, ordering them
to best-fit the constructor, by matching the class type of the argument values
to the class types of the parameters on 'c' constructors. It will use all
constructors exhaustively, until it is successful. If not, then it will look at
the 'unsafe' setting and attempt to use that.JsonIoException
- if it could not instantiate the passed in class. In that case, it is best to
create a ClassFactory for this specific class, and add that to the ReadOptions as an instantiator
that is associated to the class 'c'. In the ClassFactory, the JsonObject containing the data from the
associated JsonObject { } is passed in, allowing you to instantiate and load the values in one operation.
If you do that, and no further sub-objects exist, or you load the sub-objects in your ClassFactory,
make sure to return 'true' for isObjectFinal().public static String getLogMessage(String methodName, Object[] args)
public static <K,V> V getValue(Map map, K key)
public static <K,V> V getValueWithDefaultForNull(Map map, K key, V defaultValue)
public static <K,V> V getValueWithDefaultForMissing(Map map, K key, V defaultValue)
public static void trySetAccessible(AccessibleObject object)
public static <T> T safelyIgnoreException(Callable<T> callable, T defaultValue)
public static void safelyIgnoreException(Runnable runnable)
public static boolean isPrimitive(Class c)
c
- Class to testpublic static boolean isLogicalPrimitive(Class<?> c)
c
- Class to testpublic static String loadResourceAsString(String resourceName)
resourceName
- Name of the resource file.public static byte[] loadResourceAsBytes(String resourceName)
resourceName
- Name of the resource file.Copyright © 2023. All rights reserved.