Class ClassUtil


  • public class ClassUtil
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Class boxingClassToPrimitiveClass​(java.lang.Class boxingClass)
      static java.lang.Class forName​(java.lang.String className)
      Similar to Class.forName(java.lang.String), but attempts to load through the thread context class loader.
      static java.lang.Class<?> getArrayClass​(java.lang.Class<?> elementType, int dimensions)
      Returns the array type that corresponds to the element type and the given number of array dimensions.
      static java.lang.String getFTLTypeDescription​(TemplateModel tm)
      Returns the type description of a value with FTL terms (not plain class name), as it should be used in type-related error messages and for debugging purposes.
      static java.io.InputStream getReasourceAsStream​(java.lang.Class<?> baseClass, java.lang.String resource, boolean optional)
      Very similar to Class.getResourceAsStream(String), but throws IOException instead of returning null if optional is false, and attempts to work around "IllegalStateException: zip file closed" and similar sun.net.www.protocol.jar.JarURLConnection-related glitches.
      static java.io.InputStream getReasourceAsStream​(java.lang.ClassLoader classLoader, java.lang.String resource, boolean optional)
      Same as getReasourceAsStream(Class, String, boolean), but uses a ClassLoader directly instead of a Class.
      static java.lang.String getShortClassName​(java.lang.Class pClass)
      static java.lang.String getShortClassName​(java.lang.Class pClass, boolean shortenFreeMarkerClasses)
      Returns a class name without "java.lang." and "java.util." prefix, also shows array types in a format like int[]; useful for printing class names in error messages.
      static java.lang.String getShortClassNameOfObject​(java.lang.Object obj)
      static java.lang.String getShortClassNameOfObject​(java.lang.Object obj, boolean shortenFreeMarkerClasses)
      getShortClassName(Class, boolean) called with object.getClass(), but returns the fictional class name Null for a null value.
      static boolean isNumerical​(java.lang.Class type)
      Tells if a type is numerical; works both for primitive types and classes.
      static java.util.Properties loadProperties​(java.lang.Class<?> baseClass, java.lang.String resource)
      Loads a class loader resource into a Properties; tries to work around "zip file closed" and related sun.net.www.protocol.jar.JarURLConnection glitches.
      static java.lang.Class primitiveClassToBoxingClass​(java.lang.Class primitiveClass)
      Gets the wrapper class for a primitive class, like Integer for int, also returns Void for void.
      static java.lang.Class<?> resolveIfPrimitiveTypeName​(java.lang.String typeName)
      Returns the Class for a primitive type name, or null if it's not the name of a primitive type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • forName

        public static java.lang.Class forName​(java.lang.String className)
                                       throws java.lang.ClassNotFoundException
        Similar to Class.forName(java.lang.String), but attempts to load through the thread context class loader. Only if thread context class loader is inaccessible, or it can't find the class will it attempt to fall back to the class loader that loads the FreeMarker classes.
        Throws:
        java.lang.ClassNotFoundException
      • resolveIfPrimitiveTypeName

        public static java.lang.Class<?> resolveIfPrimitiveTypeName​(java.lang.String typeName)
        Returns the Class for a primitive type name, or null if it's not the name of a primitive type.
        Since:
        2.3.30
      • getArrayClass

        public static java.lang.Class<?> getArrayClass​(java.lang.Class<?> elementType,
                                                       int dimensions)
        Returns the array type that corresponds to the element type and the given number of array dimensions. If the dimension is 0, it just returns the element type as is.
        Since:
        2.3.30
      • getShortClassName

        public static java.lang.String getShortClassName​(java.lang.Class pClass,
                                                         boolean shortenFreeMarkerClasses)
        Returns a class name without "java.lang." and "java.util." prefix, also shows array types in a format like int[]; useful for printing class names in error messages.
        Parameters:
        pClass - can be null, in which case the method returns null.
        shortenFreeMarkerClasses - if true, it will also shorten FreeMarker class names. The exact rules aren't specified and might change over time, but right now, freemarker.ext.beans.NumberModel for example becomes to f.e.b.NumberModel.
        Since:
        2.3.20
      • getShortClassNameOfObject

        public static java.lang.String getShortClassNameOfObject​(java.lang.Object obj,
                                                                 boolean shortenFreeMarkerClasses)
        getShortClassName(Class, boolean) called with object.getClass(), but returns the fictional class name Null for a null value.
        Since:
        2.3.20
      • getFTLTypeDescription

        public static java.lang.String getFTLTypeDescription​(TemplateModel tm)
        Returns the type description of a value with FTL terms (not plain class name), as it should be used in type-related error messages and for debugging purposes. The exact format is not specified and might change over time, but currently it's something like "string (wrapper: f.t.SimpleScalar)" or "sequence+hash+string (ArrayList wrapped into f.e.b.CollectionModel)".
        Since:
        2.3.20
      • primitiveClassToBoxingClass

        public static java.lang.Class primitiveClassToBoxingClass​(java.lang.Class primitiveClass)
        Gets the wrapper class for a primitive class, like Integer for int, also returns Void for void.
        Parameters:
        primitiveClass - A Class like int.type, boolean.type, etc. If it's not a primitive class, or it's null, then the parameter value is returned as is. Note that performance-wise the method assumes that it's a primitive class.
        Since:
        2.3.21
      • isNumerical

        public static boolean isNumerical​(java.lang.Class type)
        Tells if a type is numerical; works both for primitive types and classes.
        Parameters:
        type - can't be null
        Since:
        2.3.21
      • getReasourceAsStream

        public static java.io.InputStream getReasourceAsStream​(java.lang.Class<?> baseClass,
                                                               java.lang.String resource,
                                                               boolean optional)
                                                        throws java.io.IOException
        Very similar to Class.getResourceAsStream(String), but throws IOException instead of returning null if optional is false, and attempts to work around "IllegalStateException: zip file closed" and similar sun.net.www.protocol.jar.JarURLConnection-related glitches. These are caused by bugs outside of FreeMarker. Note that in cases where the JAR resource becomes broken concurrently, similar errors can still occur later when the InputStream is read (loadProperties(Class, String) works that around as well).
        Returns:
        If optional is false, it's never null, otherwise null indicates that the resource doesn't exist.
        Throws:
        java.io.IOException - If the resource wasn't found, or other IOException occurs.
        Since:
        2.3.27
      • getReasourceAsStream

        public static java.io.InputStream getReasourceAsStream​(java.lang.ClassLoader classLoader,
                                                               java.lang.String resource,
                                                               boolean optional)
                                                        throws java.io.IOException
        Same as getReasourceAsStream(Class, String, boolean), but uses a ClassLoader directly instead of a Class.
        Throws:
        java.io.IOException
        Since:
        2.3.27
      • loadProperties

        public static java.util.Properties loadProperties​(java.lang.Class<?> baseClass,
                                                          java.lang.String resource)
                                                   throws java.io.IOException
        Loads a class loader resource into a Properties; tries to work around "zip file closed" and related sun.net.www.protocol.jar.JarURLConnection glitches.
        Throws:
        java.io.IOException
        Since:
        2.3.27