📄 localutil.java
字号:
if (paramType == Character.class || paramType == Character.TYPE)
{
if (value.length() == 1)
{
return new Character(value.charAt(0));
}
else
{
throw new IllegalArgumentException("Can't more than one character in string - can't convert to char: '" + value + "'"); //$NON-NLS-1$ //$NON-NLS-2$
}
}
String trimValue = value.trim();
if (paramType == Boolean.class || paramType == Boolean.TYPE)
{
return Boolean.valueOf(trimValue);
}
if (paramType == Integer.class || paramType == Integer.TYPE)
{
if (trimValue.length() == 0)
{
return new Integer(0);
}
return Integer.valueOf(trimValue);
}
if (paramType == Short.class || paramType == Short.TYPE)
{
if (trimValue.length() == 0)
{
return new Short((short) 0);
}
return Short.valueOf(trimValue);
}
if (paramType == Byte.class || paramType == Byte.TYPE)
{
if (trimValue.length() == 0)
{
return new Byte((byte) 0);
}
return Byte.valueOf(trimValue);
}
if (paramType == Long.class || paramType == Long.TYPE)
{
if (trimValue.length() == 0)
{
return new Long(0);
}
return Long.valueOf(trimValue);
}
if (paramType == Float.class || paramType == Float.TYPE)
{
if (trimValue.length() == 0)
{
return new Float(0);
}
return Float.valueOf(trimValue);
}
if (paramType == Double.class || paramType == Double.TYPE)
{
if (trimValue.length() == 0)
{
return new Double(0);
}
return Double.valueOf(trimValue);
}
throw new IllegalArgumentException("Unsupported conversion type: " + paramType.getName()); //$NON-NLS-1$
}
/**
* The javascript outbound marshaller prefixes the toString value with a
* colon and the original type information. This undoes that.
* @param data The string to be split up
* @return A string array containing the split data
*/
public static String[] splitInbound(String data)
{
String[] reply = new String[2];
int colon = data.indexOf(ConversionConstants.INBOUND_TYPE_SEPARATOR);
if (colon == -1)
{
log.error("Missing : in conversion data (" + data + ')'); //$NON-NLS-1$
reply[LocalUtil.INBOUND_INDEX_TYPE] = ConversionConstants.TYPE_STRING;
reply[LocalUtil.INBOUND_INDEX_VALUE] = data;
}
else
{
reply[LocalUtil.INBOUND_INDEX_TYPE] = data.substring(0, colon);
reply[LocalUtil.INBOUND_INDEX_VALUE] = data.substring(colon + 1);
}
return reply;
}
/**
* Get the short class name (i.e. without the package part)
* @param clazz the class to get the short name of
* @return the class name of the class without the package name
*/
public static String getShortClassName(Class clazz)
{
String className = clazz.getName();
char[] chars = className.toCharArray();
int lastDot = 0;
for (int i = 0; i < chars.length; i++)
{
if (chars[i] == '.')
{
lastDot = i + 1;
}
else if (chars[i] == '$')
{
chars[i] = '.';
}
}
return new String(chars, lastDot, chars.length - lastDot);
}
/**
* 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.
* @param name The name to test for JSON compatibility
* @return true if the name is simple
*/
public static boolean isSimpleName(String name)
{
if (name.length() == 0)
{
return false;
}
if (JavascriptUtil.isReservedWord(name))
{
return false;
}
boolean isSimple = Character.isLetter(name.charAt(0));
for (int i = 1; isSimple && i < name.length(); i++)
{
if (!Character.isLetterOrDigit(name.charAt(i)))
{
isSimple = false;
}
}
return isSimple;
}
/**
* 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.
* @param name The name for debugging purposes
* @param className The class to create
* @param impl The implementation class - what should className do?
* @return The class if it is safe or null otherwise.
*/
public static Class classForName(String name, String className, Class impl)
{
Class clazz;
try
{
clazz = Class.forName(className);
}
catch (ClassNotFoundException ex)
{
log.info("Skipping '" + name + "' due to ClassNotFoundException on " + className + ". Cause: " + ex.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return null;
}
catch (NoClassDefFoundError ex)
{
log.info("Skipping '" + name + "' due to NoClassDefFoundError on " + className + ". Cause: " + ex.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return null;
}
// Check it is of the right type
if (!impl.isAssignableFrom(clazz))
{
throw new IllegalArgumentException(Messages.getString("DefaultCreatorManager.CreatorNotAssignable", clazz.getName(), impl.getName())); //$NON-NLS-1$
}
// Check we can create it
try
{
clazz.newInstance();
}
catch (InstantiationException ex)
{
throw new IllegalArgumentException(Messages.getString("DefaultCreatorManager.CreatorNotInstantiatable", className, ex.toString())); //$NON-NLS-1$
}
catch (IllegalAccessException ex)
{
throw new IllegalArgumentException(Messages.getString("DefaultCreatorManager.CreatorNotAccessable", className, ex.toString())); //$NON-NLS-1$
}
catch (Exception ex)
{
// For some reason we can't catch this?
if (ex instanceof ClassNotFoundException)
{
log.info("Skipping '" + name + "' due to ClassNotFoundException on " + className + ". Cause: " + ex.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return null;
}
else
{
log.warn("Failed to load '" + name + "' (" + className + ")", ex); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return null;
}
}
return clazz;
}
/**
* Calling methods using reflection is useful for graceful fallback - this
* is a helper method to make this easy
* @param object The object to use as 'this'
* @param method The method to call, can be null in which case null is returned
* @param params The parameters to pass to the reflection call
* @return The results of calling method.invoke() or null
* @throws IllegalStateException If anything goes wrong
*/
public static Object invoke(Object object, Method method, Object[] params) throws IllegalStateException
{
Object reply = null;
if (method != null)
{
try
{
reply = method.invoke(object, params);
}
catch (InvocationTargetException ex)
{
throw new IllegalStateException("InvocationTargetException calling " + method.getName() + ": " + ex.getTargetException().toString()); //$NON-NLS-1$ //$NON-NLS-2$
}
catch (Exception ex)
{
throw new IllegalStateException("Reflection error calling " + method.getName() + ": " + ex.toString()); //$NON-NLS-1$ //$NON-NLS-2$
}
}
return reply;
}
/**
* 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.
* @param name The name for debugging purposes
* @param className The class to create
* @param impl The implementation class - what should className do?
* @return The new instance if it is safe or null otherwise.
*/
public static Object classNewInstance(String name, String className, Class impl)
{
Class clazz;
try
{
clazz = Class.forName(className);
}
catch (ClassNotFoundException ex)
{
log.info("Skipping '" + name + "' due to ClassNotFoundException on " + className + ". Cause: " + ex.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return null;
}
catch (NoClassDefFoundError ex)
{
log.info("Skipping '" + name + "' due to NoClassDefFoundError on " + className + ". Cause: " + ex.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return null;
}
// Check it is of the right type
if (!impl.isAssignableFrom(clazz))
{
throw new IllegalArgumentException(Messages.getString("DefaultCreatorManager.CreatorNotAssignable", clazz.getName(), impl.getName())); //$NON-NLS-1$
}
// Check we can create it
try
{
return clazz.newInstance();
}
catch (InstantiationException ex)
{
throw new IllegalArgumentException(Messages.getString("DefaultCreatorManager.CreatorNotInstantiatable", className, ex.toString())); //$NON-NLS-1$
}
catch (IllegalAccessException ex)
{
throw new IllegalArgumentException(Messages.getString("DefaultCreatorManager.CreatorNotAccessable", className, ex.toString())); //$NON-NLS-1$
}
catch (Exception ex)
{
log.warn("Failed to load creator '" + name + "', classname=" + className + ": ", ex); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return null;
}
}
/**
* InputStream closer that can cope if the input stream is null.
* If anything goes wrong, the errors are logged and ignored.
* @param in The resource to close
*/
public static void close(InputStream in)
{
if (in == null)
{
return;
}
try
{
in.close();
}
catch (IOException ex)
{
log.warn(ex.getMessage(), ex);
}
}
/**
* InputStream closer that can cope if the input stream is null.
* If anything goes wrong, the errors are logged and ignored.
* @param in The resource to close
*/
public static void close(RandomAccessFile in)
{
if (in == null)
{
return;
}
try
{
in.close();
}
catch (IOException ex)
{
log.warn(ex.getMessage(), ex);
}
}
/**
* The log stream
*/
private static final Logger log = Logger.getLogger(LocalUtil.class);
/**
* Have we given a warning about URLDecoder.decode() in jdk 1.3
*/
private static boolean warn13 = false;
/**
* Have we tested for the correct URLDecoder.decode()
*/
private static boolean testedDecoder = false;
/**
* Are we using the jdk 1.4 version of URLDecoder.decode()
*/
private static Method decode14 = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -