public static InputStream getResourceAsStream(String resource) { InputStream stream = getContextClassLoader().getResourceAsStream(resource); if (stream == null) { // fallback stream = _ClassUtils.class.getClassLoader().getResourceAsStream(resource); } return stream; }
public static Object newInstance(String type) throws FacesException { if (type == null) { return null; } return newInstance(simpleClassForName(type)); }
Class<?> componentTypeClass = classForName(componentType); return Array.newInstance(componentTypeClass, 0).getClass(); return classForName(type);
public static Object newInstance(String type, Class<?> expectedType) throws FacesException { return newInstance(type, expectedType == null ? null : new Class[] { expectedType }); }
/** * Same as {@link #javaTypeToClass(String)}, but throws a RuntimeException (FacesException) instead of a * ClassNotFoundException. * * @return the corresponding Class * @throws NullPointerException * if type is null * @throws FacesException * if class not found */ public static Class<?> simpleJavaTypeToClass(String type) { try { return javaTypeToClass(type); } catch (ClassNotFoundException e) { log.log(Level.SEVERE, "Class " + type + " not found", e); throw new FacesException(e); } }
/** * @param resource * Name of resource(s) to find in classpath * @param defaultObject * The default object to use to determine the class loader (if none associated with current thread.) * @return Iterator over URL Objects */ public static Collection<? extends URL> getResources(String resource, Object defaultObject) { try { Enumeration<URL> resources = getCurrentLoader(defaultObject).getResources(resource); List<URL> lst = new ArrayList<URL>(); while (resources.hasMoreElements()) { lst.add(resources.nextElement()); } return lst; } catch (IOException e) { log.log(Level.SEVERE, e.getMessage(), e); throw new FacesException(e); } }
/** * Same as {@link #classForName(String)}, but throws a RuntimeException (FacesException) instead of a * ClassNotFoundException. * * @return the corresponding Class * @throws NullPointerException * if type is null * @throws FacesException * if class not found */ public static Class<?> simpleClassForName(String type) { try { return classForName(type); } catch (ClassNotFoundException e) { log.log(Level.SEVERE, "Class " + type + " not found", e); throw new FacesException(e); } }
public static Object newInstance(String type, Class<?>[] expectedTypes) { if (type == null) { return null; } Class<?> clazzForName = simpleClassForName(type); if (expectedTypes != null) { for (int i = 0, size = expectedTypes.length; i < size; i++) { if (!expectedTypes[i].isAssignableFrom(clazzForName)) { throw new FacesException("'" + type + "' does not implement expected type '" + expectedTypes[i] + "'"); } } } return newInstance(clazzForName); }
/** * Gets the ClassLoader associated with the current thread. Returns the class loader associated with the specified * default object if no context loader is associated with the current thread. * * @param defaultObject * The default object to use to determine the class loader (if none associated with current thread.) * @return ClassLoader */ protected static ClassLoader getCurrentLoader(Object defaultObject) { ClassLoader loader = getContextClassLoader(); if (loader == null) { loader = defaultObject.getClass().getClassLoader(); } return loader; }
/** * Tries a Class.loadClass with the context class loader of the current thread first and automatically falls back to * the ClassUtils class loader (i.e. the loader of the myfaces.jar lib) if necessary. * * @param type * fully qualified name of a non-primitive non-array class * @return the corresponding Class * @throws NullPointerException * if type is null * @throws ClassNotFoundException */ public static Class<?> classForName(String type) throws ClassNotFoundException { if (type == null) { throw new NullPointerException("type"); } try { // Try WebApp ClassLoader first return Class.forName(type, false, // do not initialize for faster startup getContextClassLoader()); } catch (ClassNotFoundException ignore) { // fallback: Try ClassLoader for ClassUtils (i.e. the myfaces.jar lib) return Class.forName(type, false, // do not initialize for faster startup _ClassUtils.class.getClassLoader()); } }
_ClassUtils.getContextClassLoader());