Search in sources :

Example 46 with URLClassLoader

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());
}
Also used : CtClass(javassist.CtClass) URLClassLoader(java.net.URLClassLoader) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath) Test(org.junit.Test)

Example 47 with URLClassLoader

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());
}
Also used : URLClassLoader(java.net.URLClassLoader) NamedClassPool(com.navercorp.pinpoint.profiler.instrument.classpool.NamedClassPool) HierarchyMultipleClassPool(com.navercorp.pinpoint.profiler.instrument.classpool.HierarchyMultipleClassPool) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Example 48 with URLClassLoader

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;
}
Also used : MalformedURLException(java.net.MalformedURLException) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) File(java.io.File) URL(java.net.URL)

Example 49 with URLClassLoader

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
        ;
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) URL(java.net.URL) CoreException(org.eclipse.core.runtime.CoreException) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) SQLException(java.sql.SQLException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException) XMLParserException(org.mybatis.generator.exception.XMLParserException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) IResource(org.eclipse.core.resources.IResource)

Example 50 with URLClassLoader

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;
}
Also used : URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL)

Aggregations

URLClassLoader (java.net.URLClassLoader)1351 URL (java.net.URL)872 File (java.io.File)514 Test (org.junit.Test)317 IOException (java.io.IOException)256 ArrayList (java.util.ArrayList)202 MalformedURLException (java.net.MalformedURLException)186 Method (java.lang.reflect.Method)177 InvocationTargetException (java.lang.reflect.InvocationTargetException)68 JarFile (java.util.jar.JarFile)54 InputStream (java.io.InputStream)50 HashSet (java.util.HashSet)49 HashMap (java.util.HashMap)44 URISyntaxException (java.net.URISyntaxException)41 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)35 Path (java.nio.file.Path)33 QuickTest (com.hazelcast.test.annotation.QuickTest)32 Test (org.junit.jupiter.api.Test)28 URI (java.net.URI)27 List (java.util.List)27