Package freemarker.template.utility
Class ClassUtil
java.lang.Object
freemarker.template.utility.ClassUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic Class
boxingClassToPrimitiveClass(Class boxingClass)
The exact reverse ofprimitiveClassToBoxingClass(java.lang.Class)
.static Class
Similar toClass.forName(java.lang.String)
, but attempts to load through the thread context class loader.static Class<?>
getArrayClass(Class<?> elementType, int dimensions)
Returns the array type that corresponds to the element type and the given number of array dimensions.static String
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 InputStream
getReasourceAsStream(Class<?> baseClass, String resource, boolean optional)
Very similar toClass.getResourceAsStream(String)
, but throwsIOException
instead of returningnull
ifoptional
isfalse
, and attempts to work around "IllegalStateException: zip file closed" and similarsun.net.www.protocol.jar.JarURLConnection
-related glitches.static InputStream
getReasourceAsStream(ClassLoader classLoader, String resource, boolean optional)
Same asgetReasourceAsStream(Class, String, boolean)
, but uses aClassLoader
directly instead of aClass
.static String
getShortClassName(Class pClass)
Same asgetShortClassName(pClass, false)
.static String
getShortClassName(Class pClass, boolean shortenFreeMarkerClasses)
Returns a class name without "java.lang." and "java.util." prefix, also shows array types in a format likeint[]
; useful for printing class names in error messages.static String
static String
getShortClassNameOfObject(Object obj, boolean shortenFreeMarkerClasses)
getShortClassName(Class, boolean)
called withobject.getClass()
, but returns the fictional class nameNull
for anull
value.static boolean
isNumerical(Class type)
Tells if a type is numerical; works both for primitive types and classes.static Properties
loadProperties(Class<?> baseClass, String resource)
Loads a class loader resource into aProperties
; tries to work around "zip file closed" and relatedsun.net.www.protocol.jar.JarURLConnection
glitches.static Class
primitiveClassToBoxingClass(Class primitiveClass)
static Class<?>
resolveIfPrimitiveTypeName(String typeName)
Returns theClass
for a primitive type name, ornull
if it's not the name of a primitive type.
-
Method Details
-
forName
Similar toClass.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:
ClassNotFoundException
-
resolveIfPrimitiveTypeName
Returns theClass
for a primitive type name, ornull
if it's not the name of a primitive type.- Since:
- 2.3.30
-
getArrayClass
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
Same asgetShortClassName(pClass, false)
.- Since:
- 2.3.20
-
getShortClassName
Returns a class name without "java.lang." and "java.util." prefix, also shows array types in a format likeint[]
; useful for printing class names in error messages.- Parameters:
pClass
- can benull
, in which case the method returnsnull
.shortenFreeMarkerClasses
- iftrue
, 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 tof.e.b.NumberModel
.- Since:
- 2.3.20
-
getShortClassNameOfObject
- Since:
- 2.3.20
-
getShortClassNameOfObject
getShortClassName(Class, boolean)
called withobject.getClass()
, but returns the fictional class nameNull
for anull
value.- Since:
- 2.3.20
-
getFTLTypeDescription
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
- Parameters:
primitiveClass
- AClass
likeint.type
,boolean.type
, etc. If it's not a primitive class, or it'snull
, 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
-
boxingClassToPrimitiveClass
The exact reverse ofprimitiveClassToBoxingClass(java.lang.Class)
.- Since:
- 2.3.21
-
isNumerical
Tells if a type is numerical; works both for primitive types and classes.- Parameters:
type
- can't benull
- Since:
- 2.3.21
-
getReasourceAsStream
public static InputStream getReasourceAsStream(Class<?> baseClass, String resource, boolean optional) throws IOExceptionVery similar toClass.getResourceAsStream(String)
, but throwsIOException
instead of returningnull
ifoptional
isfalse
, and attempts to work around "IllegalStateException: zip file closed" and similarsun.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 theInputStream
is read (loadProperties(Class, String)
works that around as well).- Returns:
- If
optional
isfalse
, it's nevernull
, otherwisenull
indicates that the resource doesn't exist. - Throws:
IOException
- If the resource wasn't found, or otherIOException
occurs.- Since:
- 2.3.27
-
getReasourceAsStream
public static InputStream getReasourceAsStream(ClassLoader classLoader, String resource, boolean optional) throws IOExceptionSame asgetReasourceAsStream(Class, String, boolean)
, but uses aClassLoader
directly instead of aClass
.- Throws:
IOException
- Since:
- 2.3.27
-
loadProperties
Loads a class loader resource into aProperties
; tries to work around "zip file closed" and relatedsun.net.www.protocol.jar.JarURLConnection
glitches.- Throws:
IOException
- Since:
- 2.3.27
-