Class ClassUtils

java.lang.Object
org.openl.util.ClassUtils

public final class ClassUtils extends Object
A util to manipulate with Java classes.
Author:
Yury Molchan
  • Method Details

    • getCurrentClassLoader

      public static ClassLoader getCurrentClassLoader(Class<?> clazz)
    • primitiveToWrapper

      public static Class<?> primitiveToWrapper(Class<?> cls)

      Converts the specified primitive Class object to its corresponding wrapper Class object.

      Parameters:
      cls - the class to convert, may be null
      Returns:
      the wrapper class for cls or cls if cls is not a primitive. null if null input.
    • wrapperToPrimitive

      public static Class<?> wrapperToPrimitive(Class<?> cls)

      Converts the specified wrapper class to its corresponding primitive class.

      This method is the counter part of primitiveToWrapper(). If the passed in class is a wrapper class for a primitive type, this primitive type will be returned (e.g. Integer.TYPE for Integer.class). For other classes, or if the parameter is null, the return value is null.

      Parameters:
      cls - the class to convert, may be null
      Returns:
      the corresponding primitive type if cls is a wrapper class, null otherwise
    • getShortClassName

      public static String getShortClassName(Class<?> cls)

      Gets the class name minus the package name from a Class.

      Consider using the Java 5 API Class.getSimpleName() instead. The one known difference is that this code will return "Map.Entry" while the java.lang.Class variant will simply return "Entry".

      Parameters:
      cls - the class to get the short name for.
      Returns:
      the class name without the package name or an empty string
    • getPackageName

      public static String getPackageName(Class<?> cls)

      Gets the package name of a Class.

      Parameters:
      cls - the class to get the package name for, may be null.
      Returns:
      the package name or an empty string
    • isAssignable

      public static boolean isAssignable(Class<?> cls, Class<?> toClass)

      Checks if one Class can be assigned to a variable of another Class.

      Unlike the Class.isAssignableFrom(java.lang.Class) method, this method takes into account widenings of primitive classes and null s.

      Primitive widenings allow an int to be assigned to a long, float or double. This method returns the correct result for these cases.

      Null may be assigned to any reference type. This method will return true if null is passed in and the toClass is non-primitive.

      Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion widening primitive or widening reference conversion. See The Java Language Specification , sections 5.1.1, 5.1.2 and 5.1.4 for details.

      Parameters:
      cls - the Class to check, may be null
      toClass - the Class to try to assign into
      Returns:
      true if assignment possible
    • commonType

      public static Class<?> commonType(Class<?> a, Class<?> b)
    • capitalize

      public static String capitalize(String name)
    • decapitalize

      public static String decapitalize(String name)
    • getter

      public static String getter(String name)
    • setter

      public static String setter(String name)
    • toFieldName

      public static String toFieldName(String name)
    • isValidPackageName

      public static boolean isValidPackageName(String packageName)
    • set

      public static void set(Object target, String fieldName, Object value) throws Exception
      Throws:
      Exception
    • get

      public static Object get(Object target, String fieldName) throws Exception
      Throws:
      Exception
    • getType

      public static Class<?> getType(Object target, String fieldName)