public class ClassUtil extends Object
Modifier and Type | Method and Description |
---|---|
static Class |
boxingClassToPrimitiveClass(Class boxingClass)
The exact reverse of
primitiveClassToBoxingClass(java.lang.Class) . |
static Class |
forName(String className)
Similar to
Class.forName(java.lang.String) , but attempts to load
through the thread context class loader. |
static 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 InputStream |
getReasourceAsStream(Class<?> baseClass,
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 InputStream |
getReasourceAsStream(ClassLoader classLoader,
String resource,
boolean optional)
Same as
getReasourceAsStream(Class, String, boolean) , but uses a ClassLoader directly
instead of a Class . |
static String |
getShortClassName(Class pClass)
Same as
getShortClassName(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 like
int[] ; useful for printing class names in error messages. |
static String |
getShortClassNameOfObject(Object obj)
|
static String |
getShortClassNameOfObject(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(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 a
Properties ; tries to work around "zip file closed" and related
sun.net.www.protocol.jar.JarURLConnection glitches. |
static Class |
primitiveClassToBoxingClass(Class primitiveClass)
|
public static Class forName(String className) throws ClassNotFoundException
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.ClassNotFoundException
public static String getShortClassName(Class pClass)
getShortClassName(pClass, false)
.public static String getShortClassName(Class pClass, boolean shortenFreeMarkerClasses)
int[]
; useful for printing class names in error messages.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
.public static String getShortClassNameOfObject(Object obj, boolean shortenFreeMarkerClasses)
getShortClassName(Class, boolean)
called with object.getClass()
, but returns the fictional
class name Null
for a null
value.public static String getFTLTypeDescription(TemplateModel tm)
"string (wrapper: f.t.SimpleScalar)"
or
"sequence+hash+string (ArrayList wrapped into f.e.b.CollectionModel)"
.public static Class primitiveClassToBoxingClass(Class primitiveClass)
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.public static Class boxingClassToPrimitiveClass(Class boxingClass)
primitiveClassToBoxingClass(java.lang.Class)
.public static boolean isNumerical(Class type)
type
- can't be null
public static InputStream getReasourceAsStream(Class<?> baseClass, String resource, boolean optional) throws IOException
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).optional
is false
, it's never null
, otherwise null
indicates that the
resource doesn't exist.IOException
- If the resource wasn't found, or other IOException
occurs.public static InputStream getReasourceAsStream(ClassLoader classLoader, String resource, boolean optional) throws IOException
getReasourceAsStream(Class, String, boolean)
, but uses a ClassLoader
directly
instead of a Class
.IOException
public static Properties loadProperties(Class<?> baseClass, String resource) throws IOException
Properties
; tries to work around "zip file closed" and related
sun.net.www.protocol.jar.JarURLConnection
glitches.IOException