private static Object tryStringConstructor( String type, final String value ) throws Exception { return ClassLoaders.current().loadClass( type ).getConstructor( String.class ).newInstance( value ); }
private static void addIfNotSo(final Set<URL> urls, final URL url) { if (UNIX && isNative(url)) { return; } urls.add(url); }
public static boolean isNative(final URL url) { final File file = toFile(url); if (file != null) { final String name = file.getName(); if (!name.endsWith(".jar") && !file.isDirectory() && name.contains(".so") && file.getAbsolutePath().startsWith("/usr/lib")) { return true; } } return false; }
public static URL[] findUrls(final ClassLoader classLoader) throws IOException { if (classLoader == null || (SYSTEM.getParent() != null && classLoader == SYSTEM.getParent())) { return NO_URL; } final Set<URL> urls = new HashSet<URL>(); if (URLClassLoader.class.isInstance(classLoader) && !DONT_USE_GET_URLS) { if (!isSurefire(classLoader)) { for (final URL[] item : new URL[][] { URLClassLoader.class.cast(classLoader).getURLs(), findUrls(classLoader.getParent()) }) { for (final URL url : item) { addIfNotSo(urls, url); } } } else { // http://jira.codehaus.org/browse/SUREFIRE-928 - we could reuse findUrlFromResources but this seems faster urls.addAll(fromClassPath()); } } // DONT_USE_GET_URLS || java -jar xxx.jar and use MANIFEST.MF Class-Path? // here perf is not an issue since we would either miss all the classpath or we have a single jar if (urls.size() <= 1) { final Set<URL> urlFromResources = findUrlFromResources(classLoader); if (!urls.isEmpty()) { final URL theUrl = urls.iterator().next(); if ("file".equals(theUrl.getProtocol())) { // theUrl can be file:xxxx but it is the same entry actually urlFromResources.remove(new URL("jar:" + theUrl.toExternalForm() + "!/")); } } urls.addAll(urlFromResources); } return urls.toArray(new URL[urls.size()]); }
public static File toFile(final URL url) { if ("jar".equals(url.getProtocol())) { try { final String spec = url.getFile(); final int separator = spec.indexOf('!'); if (separator == -1) { return null; } return toFile(new URL(spec.substring(0, separator + 1))); } catch (final MalformedURLException e) { return null; } } else if ("file".equals(url.getProtocol())) { String path = decode(url.getFile()); if (path.endsWith("!")) { path = path.substring(0, path.length() - 1); } return new File(path); } return null; }
public static <T> T getInstance(final Class<T> clazz, String className) { try { Class<?> tClass = ClassLoaders.current().loadClass( className ); return (T) tClass.newInstance(); } catch ( Exception e ) { throw new SironaException( "cannot instantiante instance of '" + className + "'",e ); } }
private static <T> T newInstance(final Class<T> clazz, final String config) throws Exception { Class<?> loadedClass; try { loadedClass = ClassLoaders.current().loadClass(config); } catch (final Throwable throwable) { // NoClassDefFoundError or Exception loadedClass = clazz; } return clazz.cast(internalProcessInstance(loadedClass.newInstance())); }
if ( Number.class.isAssignableFrom( ClassLoaders.current().loadClass( type ) ) )