Class ClassUtilities

java.lang.Object
com.cedarsoftware.util.ClassUtilities

public class ClassUtilities extends Object
Useful utilities for Class work. For example, call computeInheritanceDistance(source, destination) to get the inheritance distance (number of super class steps to make it from source to destination. It will return the distance as an integer. If there is no inheritance relationship between the two, then -1 is returned. The primitives and primitive wrappers return 0 distance as if they are the same class.

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

License

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.
  • Constructor Details

    • ClassUtilities

      public ClassUtilities()
  • Method Details

    • addPermanentClassAlias

      public static void addPermanentClassAlias(Class<?> clazz, String alias)
      Add alias names for classes to allow .forName() to bring the class (.class) back with the alias name. Because the alias to class name mappings are static, it is expected that these are set up during initialization and not changed later.
      Parameters:
      clazz - Class to add an alias for
      alias - String alias name
    • removePermanentClassAlias

      public static void removePermanentClassAlias(String alias)
      Remove alias name for classes to prevent .forName() from fetching the class with the alias name. Because the alias to class name mappings are static, it is expected that these are set up during initialization and not changed later.
      Parameters:
      alias - String alias name
    • computeInheritanceDistance

      public static int computeInheritanceDistance(Class<?> source, Class<?> destination)
      Computes the inheritance distance between two classes/interfaces/primitive types.
      Parameters:
      source - The source class, interface, or primitive type.
      destination - The destination class, interface, or primitive type.
      Returns:
      The number of steps from the source to the destination, or -1 if no path exists.
    • 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.
    • forName

      public static Class<?> forName(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.
    • isClassFinal

      public static boolean isClassFinal(Class<?> c)
    • areAllConstructorsPrivate

      public static boolean areAllConstructorsPrivate(Class<?> c)
    • toPrimitiveWrapperClass

      public static Class<?> toPrimitiveWrapperClass(Class<?> primitiveClass)
    • doesOneWrapTheOther

      public static boolean doesOneWrapTheOther(Class<?> x, Class<?> y)