Class MetaUtils
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
static enum
static class
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
buildHints
(JsonReader reader, JsonObject jObj, Map<Class<?>, List<MetaUtils.ParameterHint>> hints, Set<String> fieldsAlreadyInHints) static Class<?>
classForName
(String name, ClassLoader classLoader) Given the passed in String class name, return the named JVM class.static Map<Class<?>,
Collection<Accessor>> static double
fillArgsWithHints
(Parameter[] parameters, Object[] arguments, Map<Class<?>, List<MetaUtils.ParameterHint>> hints) Return an Object[] of instance values that can be passed into a given Constructor.static Object
findAndConstructWithAppropriateConstructor
(Class<?> c, Map<Class<?>, List<MetaUtils.ParameterHint>> paramHints) getDeepDeclaredFields
(Class<?> c) static int
getDistance
(Class<?> a, Class<?> b) static 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 outputstatic String
getLogMessage
(String methodName, Object[] args, int argCharLen) static <K,
V> V static <K,
V> V getValueWithDefaultForMissing
(Map map, K key, V defaultValue) static <K,
V> V getValueWithDefaultForNull
(Map map, K key, V defaultValue) static boolean
isLogicalPrimitive
(Class<?> c) static boolean
isPrimitive
(Class<?> c) static <T> List<T>
listOf
(T... items) For JDK1.8 support.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 Object
newInstance
(Class<?> c) C language malloc() for Javastatic <T> T
safelyIgnoreException
(MetaUtils.Callable<T> callable, T defaultValue) 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 boolean
trySetAccessible
(AccessibleObject object)
-
Method Details
-
setUseUnsafe
public static void setUseUnsafe(boolean state) Globally turn on (or off) the 'unsafe' option of Class construction. The unsafe option is used when all constructors have been tried and the Java class could not be instantiated.- Parameters:
state
- boolean true = on, false = off.
-
listOf
For JDK1.8 support. Remove this and change to MetaUtils.listOf() for JDK11+ -
setOf
For JDK1.8 support. Remove this and change to Set.of() for JDK11+ -
mapOf
For JDK1.8 support. Remove this and change to Map.of() for JDK11+ -
mapOf
-
mapOf
-
mapOf
-
getField
Return an instance of the Java Field class corresponding to the passed in field name.- Parameters:
c
- class containing the field / field namefield
- String name of a field on the class.- Returns:
- Field instance if the field with the corresponding name is found, null otherwise.
-
getDeepDeclaredFields
- Parameters:
c
- Class instance- Returns:
- ClassMeta which contains fields of class. The results are cached internally for performance when called again with same Class.
-
getDistance
- Parameters:
a
- Class source classb
- Class target class- Returns:
- inheritance distance between two classes, or Integer.MAX_VALUE if they are not related. Each step upward in the inheritance from one class to the next (calling class.getSuperclass()) is counted as 1. This can be a lot of computational effort, therefore the results of this determination should be cached.
-
isPrimitive
- Parameters:
c
- Class to test- Returns:
- boolean true if the passed in class is a Java primitive, false otherwise. The Wrapper classes Integer, Long, Boolean, etc. are considered primitives by this method.
-
isLogicalPrimitive
- Parameters:
c
- Class to test- Returns:
- boolean true if the passed in class is a 'logical' primitive. A logical primitive is defined as all Java primitives, the primitive wrapper classes, String, Number, and Class. The reason these are considered 'logical' primitives is that they are immutable and therefore can be written without references in JSON content (making the JSON more readable - less @id / @ref), without breaking the semantics (shape) of the object graph being written.
-
getClassIfEnum
-
classForName
Given the passed in String class name, return the named JVM class.- Parameters:
name
- String name of a JVM class.classLoader
- ClassLoader to use when searching for JVM classes.- Returns:
- Class instance of the named JVM class or null if not found.
-
newInstance
C language malloc() for Java
Create a new instance of the passed in Class. This method will make a valiant effort to instantiate the passed in Class, including calling all of its constructors until successful. The order they are tried are public with the fewest arguments first to private with the most arguments. If, after exhausting all constructors, then it will attempt using the 'unsafe allocate' from Sun. This step is optional - by default it will use this if on a Sun (Oracle) JVM unless MetaUtil.setUseUnsafe(false) is called.
This method will handle common interfaces, such as Collection, Map, etc. which commonly show up in parameterized types. Any other interface passed to this method will cause a JsonIoException to be thrown.
To improve performance, when called a 2nd time for the same Class, the constructor that was successfully used to construct the instance will be retrieved from an internal cache.
- Parameters:
c
- Class to instantiate- Returns:
- an instance of the instantiated class. This instance is intended to have its fields 'stuffed' by direct assignment, not called via setter methods.
- Throws:
JsonIoException
- if it cannot instantiate the passed in class.
-
buildHints
public static void buildHints(JsonReader reader, JsonObject jObj, Map<Class<?>, List<MetaUtils.ParameterHint>> hints, Set<String> fieldsAlreadyInHints) -
findAndConstructWithAppropriateConstructor
-
fillArgsWithHints
public static double fillArgsWithHints(Parameter[] parameters, Object[] arguments, Map<Class<?>, List<MetaUtils.ParameterHint>> hints) Return an Object[] of instance values that can be passed into a given Constructor. This method will return an array of nulls if useNull is true, otherwise it will return sensible values for primitive classes, and null for non-known primitive / primitive wrappers. This class is used when attempting to call constructors on Java objects to get them instantiated, since there is no 'malloc' in Java. -
getLogMessage
Format a nice looking method signature for logging output -
getLogMessage
-
convertStringFieldNamesToAccessors
public static Map<Class<?>,Collection<Accessor>> convertStringFieldNamesToAccessors(Map<Class<?>, Collection<String>> map) -
getValue
-
getValueWithDefaultForNull
-
getValueWithDefaultForMissing
-
setFieldValue
-
trySetAccessible
-
safelyIgnoreException
-