📄 localutil.java
字号:
* @throws ClassNotFoundException If <code>className</code> is not valid */ public static Class classForName(String className) throws ClassNotFoundException { // Class.forName(className); return Thread.currentThread().getContextClassLoader().loadClass(className); } /** * 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()); } catch (Exception ex) { throw new IllegalStateException("Reflection error calling " + method.getName() + ": " + ex.toString()); } } return reply; } /** * 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 = classForName(className); } catch (ClassNotFoundException ex) { // We expect this sometimes, hence debug log.debug("Skipping '" + name + "' due to ClassNotFoundException on " + className + ". Cause: " + ex.getMessage()); return null; } catch (NoClassDefFoundError ex) { // We expect this sometimes, hence debug log.debug("Skipping '" + name + "' due to NoClassDefFoundError on " + className + ". Cause: " + ex.getMessage()); return null; } catch (TransformerFactoryConfigurationError ex) { // We expect this sometimes, hence debug log.debug("Skipping '" + name + "' due to TransformerFactoryConfigurationError on " + className + ". Cause: " + ex.getMessage()); log.debug("Maybe you need to add xalan.jar to your webserver?"); return null; } // Check it is of the right type if (!impl.isAssignableFrom(clazz)) { log.error("Class '" + clazz.getName() + "' does not implement '" + impl.getName() + "'."); return null; } // Check we can create it try { clazz.newInstance(); } catch (InstantiationException ex) { log.error("InstantiationException for '" + name + "' failed:", ex); return null; } catch (IllegalAccessException ex) { log.error("IllegalAccessException for '" + name + "' failed:", ex); return null; } catch (NoClassDefFoundError ex) { // We expect this sometimes, hence debug log.debug("Skipping '" + name + "' due to NoClassDefFoundError on " + className + ". Cause: " + ex.getMessage()); return null; } catch (TransformerFactoryConfigurationError ex) { // We expect this sometimes, hence debug log.debug("Skipping '" + name + "' due to TransformerFactoryConfigurationError on " + className + ". Cause: " + ex.getMessage()); log.debug("Maybe you need to add xalan.jar to your webserver?"); return null; } catch (Exception ex) { // For some reason we can't catch this? if (ex instanceof ClassNotFoundException) { // We expect this sometimes, hence debug log.debug("Skipping '" + name + "' due to ClassNotFoundException on " + className + ". Cause: " + ex.getMessage()); return null; } else { log.error("Failed to load '" + name + "' (" + className + ")", ex); return null; } } return clazz; } /** * 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 = LocalUtil.classForName(className); } catch (ClassNotFoundException ex) { // We expect this sometimes, hence debug log.debug("Skipping '" + name + "' due to ClassNotFoundException on " + className + ". Cause: " + ex.getMessage()); return null; } catch (NoClassDefFoundError ex) { // We expect this sometimes, hence debug log.debug("Skipping '" + name + "' due to NoClassDefFoundError on " + className + ". Cause: " + ex.getMessage()); return null; } catch (TransformerFactoryConfigurationError ex) { // We expect this sometimes, hence debug log.debug("Skipping '" + name + "' due to TransformerFactoryConfigurationError on " + className + ". Cause: " + ex.getMessage()); return null; } // Check it is of the right type if (!impl.isAssignableFrom(clazz)) { log.error("Class '" + clazz.getName() + "' does not implement '" + impl.getName() + "'."); return null; } // Check we can create it try { return clazz.newInstance(); } catch (InstantiationException ex) { log.error("InstantiationException for '" + name + "' failed:", ex); return null; } catch (IllegalAccessException ex) { log.error("IllegalAccessException for '" + name + "' failed:", ex); return null; } catch (TransformerFactoryConfigurationError ex) { log.error("TransformerFactoryConfigurationError for '" + name + "' failed:", ex); return null; } catch (Exception ex) { log.error("Failed to load creator '" + name + "', classname=" + className + ": ", ex); 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(Reader 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); } } /** * Return a List of superclasses for the given class. * @param clazz the class to look up * @return the List of superclasses in order going up from this one */ public static List getAllSuperclasses(Class clazz) { List classes = new ArrayList(); Class superclass = clazz.getSuperclass(); while (superclass != null) { classes.add(superclass); superclass = superclass.getSuperclass(); } return classes; } /** * Return a list of all fields (whatever access status, and on whatever * superclass they were defined) that can be found on this class. * <p>This is like a union of {@link Class#getDeclaredFields()} which * ignores and superclasses, and {@link Class#getFields()} which ignored * non-pubic fields * @param clazz The class to introspect * @return The complete list of fields */ public static Field[] getAllFields(Class clazz) { List classes = getAllSuperclasses(clazz); classes.add(clazz); return getAllFields(classes); } /** * As {@link #getAllFields(Class)} but acts on a list of {@link Class}s and * uses only {@link Class#getDeclaredFields()}. * @param classes The list of classes to reflect on * @return The complete list of fields */ private static Field[] getAllFields(List classes) { Set fields = new HashSet(); for (Iterator it = classes.iterator(); it.hasNext();) { Class clazz = (Class) it.next(); fields.addAll(Arrays.asList(clazz.getDeclaredFields())); } return (Field[]) fields.toArray(new Field[fields.size()]); } /** * 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 + -