📄 utilities.java
字号:
/*
* Created on Aug 29, 2004
*/
package org.flexdock.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author Christopher Butler
*/
public class Utilities {
private static Log log = LogFactory.getLog(Utilities.class);
/**
* A constant representing the Java version. This constant is {@code true}
* if the version is 1.4.
*/
public static final boolean JAVA_1_4 = isJavaVersion("1.4");
/**
* A constant representing the Java version. This constant is {@code true}
* if the version is 1.5.
*/
public static final boolean JAVA_1_5 = isJavaVersion("1.5");
private Utilities() {
// does nothing
}
/**
* Returns an {@code int} value for the specified {@code String}. This
* method calls {@code Integer.parseInt(String s)} and returns the resulting
* {@code int} value. If any {@code Exception} is thrown, this method
* returns a value of {@code 0}.
*
* @param data
* a {@code String} containing the {@code int} representation to
* be parsed
* @return the integer value represented by the argument in decimal
* @see #getInt(String, int)
* @see Integer#parseInt(java.lang.String)
*/
public static int getInt(String data) {
return getInt(data, 0);
}
/**
* Returns an {@code int} value for the specified {@code String}. This
* method calls {@code Integer.parseInt(String s)} and returns the resulting
* {@code int} value. If any {@code Exception} is thrown, this method
* returns the value supplied by the {@code defaultValue} parameter.
*
* @param data
* a {@code String} containing the {@code int} representation to
* be parsed
* @param defaultValue
* the value to return if an {@code Exception} is encountered.
* @return the integer value represented by the argument in decimal
* @see Integer#parseInt(java.lang.String)
*/
public static int getInt(String data, int defaultValue) {
if (data == null)
return defaultValue;
try {
return Integer.parseInt(data);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Returns a {@code float} value for the specified {@code String}. This
* method calls {@code Float.parseFloat(String s)} and returns the resulting
* {@code float} value. If any {@code Exception} is thrown by
* {@code parseFloat}, this method returns the value supplied by the
* {@code defaultValue} parameter.
*
* @param data
* a {@code String} containing the {@code float} representation
* to be parsed
* @param defaultValue
* the value to return if an {@code Exception} is encountered on
* the underlying parse mechanism.
* @return the floating-point value represented by the argument in decimal
* @see Float#parseFloat(java.lang.String)
*/
public static float getFloat(String data, float defaultValue) {
if (data == null)
return defaultValue;
try {
return Float.parseFloat(data);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Returns {@code true} if the specified {@code String} is {@code null} or
* contains only whitespace. Otherwise, returns {@code false}. The
* whitespace check is performed by calling {@code trim()} and checking to
* see if the trimmed string {@code length()} is zero.
*
* @param data
* the {@code String} to check for non-whitespace content
* @return {@code true} if the specified {@code String} is {@code null} or
* contains only whitespace, {@code false} otherwise.
*/
public static boolean isEmpty(String data) {
return data == null ? true : data.trim().length() == 0;
}
/**
* Returns an instance of the specified class name. If {@code className} is
* {@code null}, then this method returns a {@code null} reference.
* <p>
* This method will try two different means of obtaining an instance of
* {@code className}. First, it will attempt to resolve the {@code Class}
* of {@code className} via {@code Class.forName(String className)}. It
* will then use reflection to search for a method on the class named
* {@code "getInstance()"}. If the method is found, then it is invoked and
* the object instance is returned.
* <p>
* If there are any problems encountered while attempting to invoke
* {@code getInstance()} on the specified class, the {@code Throwable} is
* caught and this method dispatches to
* {@code createInstance(String className, boolean failSilent)} with an
* argument of {@code false} for {@code failSilent}.
* {@code createInstance(String className, boolean failSilent)} will attempt
* to invoke {@code newInstance()} on the {@code Class} for the specified
* class name. If any {@code Throwable} is encountered during this process,
* the value of {@code false} for {@code failSilent} will cause the stack
* trace to be printed to the {@code System.err} and a {@code null}
* reference will be returned.
*
* @param className
* the fully qualified name of the desired class.
* @return an instance of the specified class
* @see #getInstance(String, boolean)
* @see #createInstance(String, boolean)
* @see Class#forName(java.lang.String)
* @see Class#getMethod(java.lang.String, java.lang.Class[])
* @see Method#invoke(java.lang.Object, java.lang.Object[])
* @see Class#newInstance()
*/
public static Object getInstance(String className) {
return getInstance(className, false);
}
/**
* Returns an instance of the specified class name. If {@code className} is
* {@code null}, then this method returns a {@code null} reference.
* <p>
* This method will try two different means of obtaining an instance of
* {@code className}. First, it will attempt to resolve the {@code Class}
* of {@code className} via {@code Class.forName(String className)}. It
* will then use reflection to search for a method on the class named
* {@code "getInstance()"}. If the method is found, then it is invoked and
* the object instance is returned.
* <p>
* If there are any problems encountered while attempting to invoke
* {@code getInstance()} on the specified class, the {@code Throwable} is
* caught and this method dispatches to
* {@code createInstance(String className, boolean failSilent)}, passing
* the specified value for {@code failSilent}.
* {@code createInstance(String className, boolean failSilent)} will attempt
* to invoke {@code newInstance()} on the {@code Class} for the specified
* class name. If any {@code Throwable} is encountered during this process,
* the value of {@code failSilent} is checked to determine whether the stack
* stack trace should be printed to the {@code System.err}. A {@code null}
* reference will be returned if any problems are encountered.
*
* @param className
* the fully qualified name of the desired class.
* @param failSilent
* {@code true} if the stack trace should <b>not</b> be printed
* to the {@code System.err} when a {@code Throwable} is caught,
* {@code false} otherwise.
* @return an instance of the specified class
* @see #createInstance(String, boolean)
* @see Class#forName(java.lang.String)
* @see Class#getMethod(java.lang.String, java.lang.Class[])
* @see Method#invoke(java.lang.Object, java.lang.Object[])
* @see Class#newInstance()
*/
public static Object getInstance(String className, boolean failSilent) {
if (className == null)
return null;
try {
Class c = Class.forName(className);
Method m = c.getMethod("getInstance", new Class[0]);
return m.invoke(null, new Object[0]);
} catch (Throwable e) {
return createInstance(className, failSilent);
}
}
/**
* Creates and returns an instance of the specified class name using
* {@code Class.newInstance()}. If {@code className} is {@code null}, then
* this method returns a {@code null} reference. This dispatches to
* {@code createInstance(String className, Class superType, boolean failSilent)}
* with an argument of {@code null} for {@code superType} and {@code false}
* for {@code failSilent}.
* <p>
* This method will attempt to resolve the {@code Class} of
* {@code className} via {@code Class.forName(String className)}. No class
* assignability checkes are performed because this method uses a
* {@code null} {@code superType}.
* <p>
* Once the desired class has been resolved, a new instance of it is created
* and returned by invoking its {@code newInstance()} method. If there are
* any problems encountered during this process, the value of {@code false}
* for {@code failSilent} will ensure the stack stack trace is be printed to
* the {@code System.err}. A {@code null} reference will be returned if any
* problems are encountered.
*
* @param className
* the fully qualified name of the desired class.
* @return an instance of the specified class
* @see #createInstance(String, Class, boolean)
* @see Class#forName(java.lang.String)
* @see Class#newInstance()
*/
public static Object createInstance(String className) {
return createInstance(className, null);
}
/**
* Creates and returns an instance of the specified class name using
* {@code Class.newInstance()}. If {@code className} is {@code null}, then
* this method returns a {@code null} reference. The {@code failSilent}
* parameter will determine whether error stack traces should be reported to
* the {@code System.err} before this method returns {@code null}. This
* method dispatches to
* {@code createInstance(String className, Class superType, boolean failSilent)}
* with an argument of {@code null} for {@code superType}.
* <p>
* This method will attempt to resolve the {@code Class} of
* {@code className} via {@code Class.forName(String className)}. No class
* assignability checkes are performed because this method uses a
* {@code null} {@code superType}.
* <p>
* Once the desired class has been resolved, a new instance of it is created
* and returned by invoking its {@code newInstance()} method. If there are
* any problems encountered during this process, the value of
* {@code failSilent} is checked to determine whether the stack stack trace
* should be printed to the {@code System.err}. A {@code null} reference
* will be returned if any problems are encountered.
*
* @param className
* the fully qualified name of the desired class.
* @param failSilent
* {@code true} if the stack trace should <b>not</b> be printed
* to the {@code System.err} when a {@code Throwable} is caught,
* {@code false} otherwise.
* @return an instance of the specified class
* @see #createInstance(String, Class, boolean)
* @see Class#forName(java.lang.String)
* @see Class#newInstance()
*/
public static Object createInstance(String className, boolean failSilent) {
return createInstance(className, null, failSilent);
}
/**
* Creates and returns an instance of the specified class name using
* {@code Class.newInstance()}. If {@code className} is {@code null}, then
* this method returns a {@code null} reference. If {@code superType} is
* non-{@code null}, then this method will enforce polymorphic identity
* via {@code Class.isAssignableFrom(Class cls)}. This method dispatches to
* {@code createInstance(String className, Class superType, boolean failSilent)}
* with an argument of {@code false} for {@code failSilent}.
* <p>
* This method will attempt to resolve the {@code Class} of
* {@code className} via {@code Class.forName(String className)}. If
* {@code superType} is non-{@code null}, then class identity is checked
* by calling {@code superType.isAssignableFrom(c)} to ensure the resolved
* class is an valid equivalent, descendent, or implementation of the
* specified {@code className}. If this check fails, then a
* {@code ClassCastException} is thrown and caught internally and this
* method returns {@code null}. If {@code superType} is {@code null}, then
* no assignability checks are performed on the resolved class.
* <p>
* Once the desired class has been resolved, a new instance of it is created
* and returned by invoking its {@code newInstance()} method. If there are
* any problems encountered during this process, the value of {@code false}
* for {@code failSilent} will ensure the stack stack trace is be printed to
* the {@code System.err}. A {@code null} reference will be returned if any
* problems are encountered.
*
* @param className
* the fully qualified name of the desired class.
* @param superType
* optional paramter used as a means of enforcing the inheritance
* hierarchy
* @return an instance of the specified class
* @see #createInstance(String, Class, boolean)
* @see Class#forName(java.lang.String)
* @see Class#isAssignableFrom(java.lang.Class)
* @see Class#newInstance()
*/
public static Object createInstance(String className, Class superType) {
return createInstance(className, superType, false);
}
/**
* Creates and returns an instance of the specified class name using
* {@code Class.newInstance()}. If {@code className} is {@code null}, then
* this method returns a {@code null} reference. If {@code superType} is
* non-{@code null}, then this method will enforce polymorphic identity
* via {@code Class.isAssignableFrom(Class cls)}. The {@code failSilent}
* parameter will determine whether error stack traces should be reported to
* the {@code System.err} before this method returns {@code null}.
* <p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -