use of java.net.URLClassLoader in project pinpoint by naver.
the class TestBootstrapClass method test.
@Test
public void test() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, CannotCompileException {
URLClassLoader classLoader = new URLClassLoader(new URL[] {});
LoaderClassPath loaderClassPath = new LoaderClassPath(classLoader);
ClassPool cp = new ClassPool();
cp.appendClassPath(loaderClassPath);
CtClass ctClass = cp.makeClass(TEST_CLASS_NAME);
byte[] bytes = ctClass.toBytecode();
logger.debug(classLoader.getClass().getName());
Class<?> aClass = BytecodeUtils.defineClass(classLoader, TEST_CLASS_NAME, bytes);
logger.debug("{}", aClass.getName());
}
use of java.net.URLClassLoader in project pinpoint by naver.
the class HierarchyMultipleClassPoolTest method testTestClass.
@Test
public void testTestClass() throws Exception {
NamedClassPool pool = new NamedClassPool("test");
pool.childFirstLookup = true;
HierarchyMultipleClassPool multipleClassPool = new HierarchyMultipleClassPool(pool);
ClassLoader classLoader = new URLClassLoader(new URL[0], ClassLoader.getSystemClassLoader());
multipleClassPool.getClassPool(classLoader);
logger.debug("size {}", multipleClassPool.size());
for (NamedClassPool classPool1 : multipleClassPool.values()) {
logger.debug("classPool:{} name:{}", classPool1, classPool1.getName());
}
Assert.assertEquals(2, multipleClassPool.size());
}
use of java.net.URLClassLoader in project generator by mybatis.
the class ClassloaderUtility method getCustomClassloader.
public static ClassLoader getCustomClassloader(List<String> entries) {
List<URL> urls = new ArrayList<URL>();
File file;
if (entries != null) {
for (String classPathEntry : entries) {
file = new File(classPathEntry);
if (!file.exists()) {
throw new RuntimeException(getString("RuntimeError.9", //$NON-NLS-1$
classPathEntry));
}
try {
urls.add(file.toURI().toURL());
} catch (MalformedURLException e) {
// this shouldn't happen, but just in case...
throw new RuntimeException(getString("RuntimeError.9", //$NON-NLS-1$
classPathEntry));
}
}
}
ClassLoader parent = Thread.currentThread().getContextClassLoader();
URLClassLoader ucl = new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
return ucl;
}
use of java.net.URLClassLoader in project generator by mybatis.
the class RunGeneratorThread method setClassLoader.
private void setClassLoader() {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IJavaProject javaProject = getJavaProject();
try {
if (javaProject != null) {
List<URL> entries = new ArrayList<URL>();
IPath path = javaProject.getOutputLocation();
IResource iResource = root.findMember(path);
path = iResource.getLocation();
path = path.addTrailingSeparator();
entries.add(path.toFile().toURI().toURL());
IClasspathEntry[] cpEntries = javaProject.getRawClasspath();
for (IClasspathEntry cpEntry : cpEntries) {
switch(cpEntry.getEntryKind()) {
case IClasspathEntry.CPE_SOURCE:
path = cpEntry.getOutputLocation();
if (path != null) {
iResource = root.findMember(path);
path = iResource.getLocation();
path = path.addTrailingSeparator();
entries.add(path.toFile().toURI().toURL());
}
break;
case IClasspathEntry.CPE_LIBRARY:
iResource = root.findMember(cpEntry.getPath());
if (iResource == null) {
// resource is not in workspace, must be an external JAR
path = cpEntry.getPath();
} else {
path = iResource.getLocation();
}
entries.add(path.toFile().toURI().toURL());
break;
}
}
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
URL[] entryArray = new URL[entries.size()];
entries.toArray(entryArray);
ClassLoader newCl = new URLClassLoader(entryArray, oldCl);
Thread.currentThread().setContextClassLoader(newCl);
oldClassLoader = oldCl;
}
} catch (Exception e) {
// ignore - something too complex is wrong
;
}
}
use of java.net.URLClassLoader in project druid by druid-io.
the class Initialization method getClassLoaderForExtension.
/**
* @param extension The File instance of the extension we want to load
*
* @return a URLClassLoader that loads all the jars on which the extension is dependent
*
* @throws MalformedURLException
*/
public static URLClassLoader getClassLoaderForExtension(File extension) throws MalformedURLException {
URLClassLoader loader = loadersMap.get(extension);
if (loader == null) {
final Collection<File> jars = FileUtils.listFiles(extension, new String[] { "jar" }, false);
final URL[] urls = new URL[jars.size()];
int i = 0;
for (File jar : jars) {
final URL url = jar.toURI().toURL();
log.info("added URL[%s]", url);
urls[i++] = url;
}
loadersMap.putIfAbsent(extension, new URLClassLoader(urls, Initialization.class.getClassLoader()));
loader = loadersMap.get(extension);
}
return loader;
}
Aggregations