Class MetaUtils

java.lang.Object
com.cedarsoftware.util.io.MetaUtils

public class MetaUtils extends Object
This utility class has the methods mostly related to reflection related code.
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.*
  • 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

      @SafeVarargs public static <T> List<T> listOf(T... items)
      For JDK1.8 support. Remove this and change to MetaUtils.listOf() for JDK11+
    • setOf

      @SafeVarargs public static <T> Set<T> setOf(T... items)
      For JDK1.8 support. Remove this and change to Set.of() for JDK11+
    • mapOf

      public static <K, V> Map<K,V> mapOf()
      For JDK1.8 support. Remove this and change to Map.of() for JDK11+
    • mapOf

      public static <K, V> Map<K,V> mapOf(K k, V v)
    • mapOf

      public static <K, V> Map<K,V> mapOf(K k1, V v1, K k2, V v2)
    • mapOf

      public static <K, V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3)
    • getField

      public static Field getField(Class<?> c, String field)
      Return an instance of the Java Field class corresponding to the passed in field name.
      Parameters:
      c - class containing the field / field name
      field - String name of a field on the class.
      Returns:
      Field instance if the field with the corresponding name is found, null otherwise.
    • getDeepDeclaredFields

      public static Map<String,Field> getDeepDeclaredFields(Class<?> c)
      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

      public static int getDistance(Class<?> a, Class<?> b)
      Parameters:
      a - Class source class
      b - 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

      public static boolean isPrimitive(Class<?> c)
      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

      public static boolean isLogicalPrimitive(Class<?> c)
      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

      public static Optional<Class> getClassIfEnum(Class c)
    • classForName

      public static Class<?> classForName(String name, ClassLoader classLoader)
      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

      public static Object newInstance(Class<?> c)

      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

      public static Object findAndConstructWithAppropriateConstructor(Class<?> c, Map<Class<?>,List<MetaUtils.ParameterHint>> paramHints)
    • 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

      public static String getLogMessage(String methodName, Object[] args)
      Format a nice looking method signature for logging output
    • getLogMessage

      public static String getLogMessage(String methodName, Object[] args, int argCharLen)
    • convertStringFieldNamesToAccessors

      public static Map<Class<?>,Collection<Accessor>> convertStringFieldNamesToAccessors(Map<Class<?>,Collection<String>> map)
    • getValue

      public static <K, V> V getValue(Map map, K key)
    • getValueWithDefaultForNull

      public static <K, V> V getValueWithDefaultForNull(Map map, K key, V defaultValue)
    • getValueWithDefaultForMissing

      public static <K, V> V getValueWithDefaultForMissing(Map map, K key, V defaultValue)
    • setFieldValue

      public static void setFieldValue(Field field, Object instance, Object value)
    • trySetAccessible

      public static boolean trySetAccessible(AccessibleObject object)
    • safelyIgnoreException

      public static <T> T safelyIgnoreException(MetaUtils.Callable<T> callable, T defaultValue)