org.directwebremoting.util
Class LocalUtil

java.lang.Object
  extended by org.directwebremoting.util.LocalUtil

public final class LocalUtil
extends java.lang.Object

Various utilities, mostly to make up for JDK 1.4 functionallity that is not in JDK 1.3

Author:
Joe Walker [joe at getahead dot ltd dot uk]

Field Summary
static int INBOUND_INDEX_TYPE
          splitInbound() returns the type info in this parameter
static int INBOUND_INDEX_VALUE
          splitInbound() returns the value info in this parameter
 
Method Summary
static void addNoCacheHeaders(HttpServletResponse resp)
          Add headers to prevent browers and proxies from caching this reply.
static java.lang.Class classForName(java.lang.String className)
          Utility to essentially do Class forName and allow configurable Classloaders.
static java.lang.Class classForName(java.lang.String name, java.lang.String className, java.lang.Class impl)
          Utility to essentially do Class forName with the assumption that the environment expects failures for missing jar files and can carry on if this process fails.
static java.lang.Object classNewInstance(java.lang.String name, java.lang.String className, java.lang.Class impl)
          Utility to essentially do Class forName and newInstance with the assumption that the environment expects failures for missing jar files and can carry on if this process fails.
static void close(java.io.InputStream in)
          InputStream closer that can cope if the input stream is null.
static void close(java.io.RandomAccessFile in)
          InputStream closer that can cope if the input stream is null.
static void close(java.io.Reader in)
          InputStream closer that can cope if the input stream is null.
static java.lang.String decode(java.lang.String value)
          URL decode a value.
static java.lang.reflect.Field[] getAllFields(java.lang.Class clazz)
          Return a list of all fields (whatever access status, and on whatever superclass they were defined) that can be found on this class.
static java.util.List getAllSuperclasses(java.lang.Class clazz)
          Return a List of superclasses for the given class.
static java.lang.Class getNonPrimitiveType(java.lang.Class type)
           
static java.lang.String getShortClassName(java.lang.Class clazz)
          Get the short class name (i.e.
static java.lang.Object invoke(java.lang.Object object, java.lang.reflect.Method method, java.lang.Object[] params)
          Calling methods using reflection is useful for graceful fallback - this is a helper method to make this easy
static boolean isEquivalent(java.lang.Class c1, java.lang.Class c2)
          True if c1 is java.lang.Boolean and c2 is boolean, etc.
static boolean isJavaIdentifier(java.lang.String test)
          Determines if the specified string is permissible as a Java identifier.
static boolean isLetterOrDigitOrUnderline(java.lang.String test)
          Determines if the specified string contains only Unicode letters or digits as defined by Character.isLetterOrDigit(char)
static boolean isServletClass(java.lang.Class paramType)
          Is this class one that we auto fill, so the user can ignore?
static boolean isSimpleName(java.lang.String name)
          Is this object property one that we can use in a JSON style or do we need to get fancy.
static boolean isTypeSimplyConvertable(java.lang.Class paramType)
          Can the type be used in a call to simpleConvert(String, Class)?
static java.lang.String replace(java.lang.String text, java.lang.String repl, java.lang.String with)
          Replacement for String#replaceAll(String, String) in JDK 1.4+
static void setParams(java.lang.Object object, java.util.Map params, java.util.List ignore)
          Set use reflection to set the setters on the object called by the keys in the params map with the corresponding values
static void setProperty(java.lang.Object object, java.lang.String key, java.lang.Object value)
          Set a property on an object using reflection
static java.lang.Object simpleConvert(java.lang.String value, java.lang.Class paramType)
          A very simple conversion function for all the IoC style setup and reflection that we are doing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INBOUND_INDEX_TYPE

public static final int INBOUND_INDEX_TYPE
splitInbound() returns the type info in this parameter

See Also:
Constant Field Values

INBOUND_INDEX_VALUE

public static final int INBOUND_INDEX_VALUE
splitInbound() returns the value info in this parameter

See Also:
Constant Field Values
Method Detail

replace

public static java.lang.String replace(java.lang.String text,
                                       java.lang.String repl,
                                       java.lang.String with)
Replacement for String#replaceAll(String, String) in JDK 1.4+

Parameters:
text - source text
repl - the stuff to get rid of
with - the stuff to replace it with
Returns:
replaced text or null if any args are null

isJavaIdentifier

public static boolean isJavaIdentifier(java.lang.String test)
Determines if the specified string is permissible as a Java identifier. Returns true if the string is non-null, non-zero length with a Java identifier start as the first character and Java identifier parts in all remaining characters.

Parameters:
test - the string to be tested.
Returns:
true if the string is a Java identifier, false otherwise.
See Also:
Character.isJavaIdentifierPart(char), Character.isJavaIdentifierStart(char)

isLetterOrDigitOrUnderline

public static boolean isLetterOrDigitOrUnderline(java.lang.String test)
Determines if the specified string contains only Unicode letters or digits as defined by Character.isLetterOrDigit(char)

Parameters:
test - The string to test
Returns:
true if the string is non-null, non-empty and contains only characters that are unicode letters or digits
See Also:
Character.isLetterOrDigit(char)

isEquivalent

public static boolean isEquivalent(java.lang.Class c1,
                                   java.lang.Class c2)
True if c1 is java.lang.Boolean and c2 is boolean, etc.

Parameters:
c1 - the first class to test
c2 - the second class to test
Returns:
true if the classes are equivalent

getNonPrimitiveType

public static java.lang.Class getNonPrimitiveType(java.lang.Class type)
Parameters:
type - The class to de-primitivize
Returns:
The non-privitive version of the class

addNoCacheHeaders

public static void addNoCacheHeaders(HttpServletResponse resp)
Add headers to prevent browers and proxies from caching this reply.

Parameters:
resp - The response to add headers to

isServletClass

public static boolean isServletClass(java.lang.Class paramType)
Is this class one that we auto fill, so the user can ignore?

Parameters:
paramType - The type to test
Returns:
true if the type is a Servlet type

decode

public static java.lang.String decode(java.lang.String value)
URL decode a value. This method gets around the lack of a decode(String, String) method in JDK 1.3.

Parameters:
value - The string to decode
Returns:
The decoded string

setParams

public static void setParams(java.lang.Object object,
                             java.util.Map params,
                             java.util.List ignore)
Set use reflection to set the setters on the object called by the keys in the params map with the corresponding values

Parameters:
object - The object to setup
params - The settings to use
ignore - List of keys to not warn about if they are not properties Note only the warning is skipped, we still try the setter

setProperty

public static void setProperty(java.lang.Object object,
                               java.lang.String key,
                               java.lang.Object value)
                        throws java.lang.NoSuchMethodException,
                               java.lang.SecurityException,
                               java.lang.IllegalAccessException,
                               java.lang.IllegalArgumentException,
                               java.lang.reflect.InvocationTargetException
Set a property on an object using reflection

Parameters:
object - The object to call the setter on
key - The name of the property to set.
value - The new value to use for the property
Throws:
java.lang.NoSuchMethodException - Passed on from reflection code
java.lang.SecurityException - Passed on from reflection code
java.lang.IllegalAccessException - Passed on from reflection code
java.lang.IllegalArgumentException - Passed on from reflection code
java.lang.reflect.InvocationTargetException - Passed on from reflection code

isTypeSimplyConvertable

public static boolean isTypeSimplyConvertable(java.lang.Class paramType)
Can the type be used in a call to simpleConvert(String, Class)?

Parameters:
paramType - The type to test
Returns:
true if the type is acceptable to simpleConvert()

simpleConvert

public static java.lang.Object simpleConvert(java.lang.String value,
                                             java.lang.Class paramType)
A very simple conversion function for all the IoC style setup and reflection that we are doing.

Parameters:
value - The value to convert
paramType - The type to convert to. Currently ony primitive types and String are supported.
Returns:
The converted object.

getShortClassName

public static java.lang.String getShortClassName(java.lang.Class clazz)
Get the short class name (i.e. without the package part)

Parameters:
clazz - the class to get the short name of
Returns:
the class name of the class without the package name

isSimpleName

public static boolean isSimpleName(java.lang.String name)
Is this object property one that we can use in a JSON style or do we need to get fancy. i.e does it contain only letters and numbers with an initial letter.

Parameters:
name - The name to test for JSON compatibility
Returns:
true if the name is simple

classForName

public static java.lang.Class classForName(java.lang.String className)
                                    throws java.lang.ClassNotFoundException
Utility to essentially do Class forName and allow configurable Classloaders.

The initial implementation makes use of the context classloader for the current thread.

Parameters:
className - The class to create
Returns:
The class if it is safe or null otherwise.
Throws:
java.lang.ClassNotFoundException - If className is not valid

invoke

public static java.lang.Object invoke(java.lang.Object object,
                                      java.lang.reflect.Method method,
                                      java.lang.Object[] params)
                               throws java.lang.IllegalStateException
Calling methods using reflection is useful for graceful fallback - this is a helper method to make this easy

Parameters:
object - The object to use as 'this'
method - The method to call, can be null in which case null is returned
params - The parameters to pass to the reflection call
Returns:
The results of calling method.invoke() or null
Throws:
java.lang.IllegalStateException - If anything goes wrong

classForName

public static java.lang.Class classForName(java.lang.String name,
                                           java.lang.String className,
                                           java.lang.Class impl)
Utility to essentially do Class forName with the assumption that the environment expects failures for missing jar files and can carry on if this process fails.

Parameters:
name - The name for debugging purposes
className - The class to create
impl - The implementation class - what should className do?
Returns:
The class if it is safe or null otherwise.

classNewInstance

public static java.lang.Object classNewInstance(java.lang.String name,
                                                java.lang.String className,
                                                java.lang.Class impl)
Utility to essentially do Class forName and newInstance with the assumption that the environment expects failures for missing jar files and can carry on if this process fails.

Parameters:
name - The name for debugging purposes
className - The class to create
impl - The implementation class - what should className do?
Returns:
The new instance if it is safe or null otherwise.

close

public static void close(java.io.InputStream in)
InputStream closer that can cope if the input stream is null. If anything goes wrong, the errors are logged and ignored.

Parameters:
in - The resource to close

close

public static void close(java.io.Reader in)
InputStream closer that can cope if the input stream is null. If anything goes wrong, the errors are logged and ignored.

Parameters:
in - The resource to close

close

public static void close(java.io.RandomAccessFile in)
InputStream closer that can cope if the input stream is null. If anything goes wrong, the errors are logged and ignored.

Parameters:
in - The resource to close

getAllSuperclasses

public static java.util.List getAllSuperclasses(java.lang.Class clazz)
Return a List of superclasses for the given class.

Parameters:
clazz - the class to look up
Returns:
the List of superclasses in order going up from this one

getAllFields

public static java.lang.reflect.Field[] getAllFields(java.lang.Class clazz)
Return a list of all fields (whatever access status, and on whatever superclass they were defined) that can be found on this class.

This is like a union of Class.getDeclaredFields() which ignores and superclasses, and Class.getFields() which ignored non-pubic fields

Parameters:
clazz - The class to introspect
Returns:
The complete list of fields