Class 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 Detail

      • 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.
      • getField

        public static Field getField​(Class c,
                                     String field)
        Return an instance of 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.
      • 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.
      • 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)