java.net.URLClassLoader.getParent()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(15.6k)|赞(0)|评价(0)|浏览(157)

本文整理了Java中java.net.URLClassLoader.getParent()方法的一些代码示例,展示了URLClassLoader.getParent()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLClassLoader.getParent()方法的具体详情如下:
包路径:java.net.URLClassLoader
类名称:URLClassLoader
方法名:getParent

URLClassLoader.getParent介绍

暂无

代码示例

代码示例来源:origin: org.glassfish.main.web/war-util

  1. @Override
  2. public URLClassLoader run() {
  3. return new URLClassLoader(urlArray, webAppCl.getParent());
  4. }
  5. });

代码示例来源:origin: zhong-j-yu/bayou

  1. ClassLoaderFactory(URLClassLoader launcherClassLoader)
  2. {
  3. this.parentCL = launcherClassLoader.getParent(); // could be null.
  4. this.allUrls.addAll(Arrays.asList(launcherClassLoader.getURLs()));
  5. }

代码示例来源:origin: GeeQuery/ef-orm

  1. /**
  2. * 得到虚拟机扩展的ClassLoader
  3. */
  4. public static URLClassLoader getJvmExtClassLoader() {
  5. return (URLClassLoader) getAppClassLoader().getParent();
  6. }

代码示例来源:origin: org.ow2.frascati/frascati-assembly-factory

  1. /**
  2. * Debug output for given ClassLoader - print loaded URLs
  3. *
  4. * @param classLoader ClassLoader used
  5. */
  6. protected final void debug(ClassLoader classLoader, PrintStream printStream)
  7. {
  8. ClassLoader cl = (classLoader != null) ? classLoader : this.mainClassLoader;
  9. PrintStream out = (printStream != null) ? printStream : System.err;
  10. out.println("----- Debugging ClassLoader ----------");
  11. out.println("List of URL loaded for ClassLoader : " + cl);
  12. out.println("--------------------------------------");
  13. while (cl instanceof URLClassLoader) {
  14. URLClassLoader urlcl = (URLClassLoader) cl;
  15. for (URL url : urlcl.getURLs()) {
  16. out.println("> " + url);
  17. }
  18. cl = urlcl.getParent();
  19. }
  20. out.println("--------------------------------------");
  21. }

代码示例来源:origin: apache/twill

  1. /**
  2. * Creates a {@link ClassLoader} to be used by this container that load classes from the given classpath.
  3. */
  4. private static ClassLoader createContainerClassLoader(URL[] classpath) {
  5. String containerClassLoaderName = System.getProperty(Constants.TWILL_CONTAINER_CLASSLOADER);
  6. URLClassLoader classLoader = new URLClassLoader(classpath);
  7. if (containerClassLoaderName == null) {
  8. return classLoader;
  9. }
  10. try {
  11. @SuppressWarnings("unchecked")
  12. Class<? extends ClassLoader> cls = (Class<? extends ClassLoader>) classLoader.loadClass(containerClassLoaderName);
  13. // Instantiate with constructor (URL[] classpath, ClassLoader parentClassLoader)
  14. return cls.getConstructor(URL[].class, ClassLoader.class).newInstance(classpath, classLoader.getParent());
  15. } catch (ClassNotFoundException e) {
  16. throw new RuntimeException("Failed to load container class loader class " + containerClassLoaderName, e);
  17. } catch (NoSuchMethodException e) {
  18. throw new RuntimeException("Container class loader must have a public constructor with " +
  19. "parameters (URL[] classpath, ClassLoader parent)", e);
  20. } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
  21. throw new RuntimeException("Failed to create container class loader of class " + containerClassLoaderName, e);
  22. }
  23. }

代码示例来源:origin: cdapio/cdap

  1. /**
  2. * Creates a {@link ClassLoader} to be used by this container that load classes from the given classpath.
  3. */
  4. private static ClassLoader createContainerClassLoader(URL[] classpath) {
  5. String containerClassLoaderName = System.getProperty(Constants.TWILL_CONTAINER_CLASSLOADER);
  6. URLClassLoader classLoader = new URLClassLoader(classpath);
  7. if (containerClassLoaderName == null) {
  8. return classLoader;
  9. }
  10. try {
  11. @SuppressWarnings("unchecked")
  12. Class<? extends ClassLoader> cls = (Class<? extends ClassLoader>) classLoader.loadClass(containerClassLoaderName);
  13. // Instantiate with constructor (URL[] classpath, ClassLoader parentClassLoader)
  14. return cls.getConstructor(URL[].class, ClassLoader.class).newInstance(classpath, classLoader.getParent());
  15. } catch (ClassNotFoundException e) {
  16. throw new RuntimeException("Failed to load container class loader class " + containerClassLoaderName, e);
  17. } catch (NoSuchMethodException e) {
  18. throw new RuntimeException("Container class loader must have a public constructor with " +
  19. "parameters (URL[] classpath, ClassLoader parent)", e);
  20. } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
  21. throw new RuntimeException("Failed to create container class loader of class " + containerClassLoaderName, e);
  22. }
  23. }

代码示例来源:origin: org.apache.twill/twill-core

  1. /**
  2. * Creates a {@link ClassLoader} to be used by this container that load classes from the given classpath.
  3. */
  4. private static ClassLoader createContainerClassLoader(URL[] classpath) {
  5. String containerClassLoaderName = System.getProperty(Constants.TWILL_CONTAINER_CLASSLOADER);
  6. URLClassLoader classLoader = new URLClassLoader(classpath);
  7. if (containerClassLoaderName == null) {
  8. return classLoader;
  9. }
  10. try {
  11. @SuppressWarnings("unchecked")
  12. Class<? extends ClassLoader> cls = (Class<? extends ClassLoader>) classLoader.loadClass(containerClassLoaderName);
  13. // Instantiate with constructor (URL[] classpath, ClassLoader parentClassLoader)
  14. return cls.getConstructor(URL[].class, ClassLoader.class).newInstance(classpath, classLoader.getParent());
  15. } catch (ClassNotFoundException e) {
  16. throw new RuntimeException("Failed to load container class loader class " + containerClassLoaderName, e);
  17. } catch (NoSuchMethodException e) {
  18. throw new RuntimeException("Container class loader must have a public constructor with " +
  19. "parameters (URL[] classpath, ClassLoader parent)", e);
  20. } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
  21. throw new RuntimeException("Failed to create container class loader of class " + containerClassLoaderName, e);
  22. }
  23. }

代码示例来源:origin: co.cask.cdap/cdap-app-fabric

  1. /**
  2. * Creates a {@link ClassLoader} to be used by this container that load classes from the given classpath.
  3. */
  4. private static ClassLoader createContainerClassLoader(URL[] classpath) {
  5. String containerClassLoaderName = System.getProperty(Constants.TWILL_CONTAINER_CLASSLOADER);
  6. URLClassLoader classLoader = new URLClassLoader(classpath);
  7. if (containerClassLoaderName == null) {
  8. return classLoader;
  9. }
  10. try {
  11. @SuppressWarnings("unchecked")
  12. Class<? extends ClassLoader> cls = (Class<? extends ClassLoader>) classLoader.loadClass(containerClassLoaderName);
  13. // Instantiate with constructor (URL[] classpath, ClassLoader parentClassLoader)
  14. return cls.getConstructor(URL[].class, ClassLoader.class).newInstance(classpath, classLoader.getParent());
  15. } catch (ClassNotFoundException e) {
  16. throw new RuntimeException("Failed to load container class loader class " + containerClassLoaderName, e);
  17. } catch (NoSuchMethodException e) {
  18. throw new RuntimeException("Container class loader must have a public constructor with " +
  19. "parameters (URL[] classpath, ClassLoader parent)", e);
  20. } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
  21. throw new RuntimeException("Failed to create container class loader of class " + containerClassLoaderName, e);
  22. }
  23. }

代码示例来源:origin: gridkit/nanocloud

  1. public static Collection<URL> listCurrentClasspath(URLClassLoader classLoader) {
  2. Set<URL> result = new LinkedHashSet<URL>();
  3. while(true) {
  4. for(URL url: classLoader.getURLs()) {
  5. addEntriesFromManifest(result, url);
  6. }
  7. ClassLoader cls = classLoader.getParent();
  8. if (cls instanceof URLClassLoader) {
  9. if (cls.getClass().getName().endsWith("$ExtClassLoader")) {
  10. break;
  11. }
  12. classLoader = (URLClassLoader) cls;
  13. }
  14. else {
  15. break;
  16. }
  17. }
  18. return new ArrayList<URL>(result);
  19. }

代码示例来源:origin: org.dspace.dependencies.solr/dspace-solr-core

  1. private static URLClassLoader replaceClassLoader(final URLClassLoader oldLoader,
  2. final File base,
  3. final FileFilter filter) {
  4. if (null != base && base.canRead() && base.isDirectory()) {
  5. File[] files = base.listFiles(filter);
  6. if (null == files || 0 == files.length) return oldLoader;
  7. URL[] oldElements = oldLoader.getURLs();
  8. URL[] elements = new URL[oldElements.length + files.length];
  9. System.arraycopy(oldElements, 0, elements, 0, oldElements.length);
  10. for (int j = 0; j < files.length; j++) {
  11. try {
  12. URL element = files[j].toURI().normalize().toURL();
  13. log.info("Adding '" + element.toString() + "' to classloader");
  14. elements[oldElements.length + j] = element;
  15. } catch (MalformedURLException e) {
  16. SolrException.log(log, "Can't add element to classloader: " + files[j], e);
  17. }
  18. }
  19. return URLClassLoader.newInstance(elements, oldLoader.getParent());
  20. }
  21. // are we still here?
  22. return oldLoader;
  23. }

代码示例来源:origin: org.apache.geronimo.modules/geronimo-jaxws-builder

  1. public static void getClassLoaderClasspath(ClassLoader loader, LinkedHashSet<URL> classpath) {
  2. if (loader == null || loader == ClassLoader.getSystemClassLoader()) {
  3. return;
  4. // } else if (loader instanceof MultiParentClassLoader) {
  5. // MultiParentClassLoader cl = (MultiParentClassLoader)loader;
  6. // for (ClassLoader parent : cl.getParents()) {
  7. // getClassLoaderClasspath(parent, classpath);
  8. // }
  9. // for (URL u : cl.getURLs()) {
  10. // classpath.add(u);
  11. // }
  12. } else if (loader instanceof URLClassLoader) {
  13. URLClassLoader cl = (URLClassLoader)loader;
  14. getClassLoaderClasspath(cl.getParent(), classpath);
  15. for (URL u : cl.getURLs()) {
  16. classpath.add(u);
  17. }
  18. } else {
  19. getClassLoaderClasspath(loader.getParent(), classpath);
  20. }
  21. }

代码示例来源:origin: com.googlecode.fighting-layout-bugs/fighting-layout-bugs

  1. /**
  2. * Constructs the class path of the {@link Thread#getContextClassLoader() current class loader}.
  3. */
  4. public ClassPath() {
  5. List<File> files = new ArrayList<File>();
  6. URLClassLoader classLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
  7. do {
  8. URL[] urls = classLoader.getURLs();
  9. if (urls.length == 1 && "sun.misc.Launcher$AppClassLoader".equals(classLoader.getClass().getName())) {
  10. File jarFile = toFile(urls[0]);
  11. files.add(jarFile);
  12. files.addAll(readClassPathFromManifestOf(jarFile));
  13. } else {
  14. for (URL url : urls) {
  15. File file = toFile(url);
  16. files.add(file);
  17. if (SUREFIREBOOTER_JAR_FILE_NAME_PATTERN.matcher(file.getName()).matches()) {
  18. files.addAll(readClassPathFromManifestOf(file));
  19. }
  20. }
  21. }
  22. ClassLoader parentClassLoader = classLoader.getParent();
  23. // noinspection ObjectEquality
  24. classLoader = (parentClassLoader instanceof URLClassLoader && parentClassLoader != classLoader ? (URLClassLoader) parentClassLoader : null);
  25. } while (classLoader != null);
  26. _files = Collections.unmodifiableList(files);
  27. }

代码示例来源:origin: org.robolectric/robolectric-sandbox

  1. public SandboxClassLoader(URLClassLoader systemClassLoader, InstrumentationConfiguration config, URL... urls) {
  2. super(systemClassLoader.getURLs(), systemClassLoader.getParent());
  3. this.systemClassLoader = systemClassLoader;
  4. this.config = config;
  5. this.urls = new URLClassLoader(urls, null);
  6. classesToRemap = convertToSlashes(config.classNameTranslations());
  7. methodsToIntercept = convertToSlashes(config.methodsToIntercept());
  8. for (URL url : urls) {
  9. Logger.debug("Loading classes from: %s", url);
  10. }
  11. }

代码示例来源:origin: 8tory/SimpleWeibo

  1. /**
  2. * Make a different ClassLoader that loads the same URLs as this one, and use it to compile
  3. * an {@code @RetroWeibo} class. If Velocity loads its managers using the context class loader,
  4. * and that loader is still the original one that loaded this test, then it will find the
  5. * original copy of the Velocity classes rather than the one from the new loader, and fail.
  6. *
  7. * <p>This test assumes that the test class was loaded by a URLClassLoader and that that loader's
  8. * URLs also include the Velocity classes.
  9. */
  10. public void testClassLoaderHack() throws Exception {
  11. URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
  12. URLClassLoader newLoader = new URLClassLoader(myLoader.getURLs(), myLoader.getParent());
  13. String velocityClassName = Velocity.class.getName();
  14. Class<?> myVelocity = myLoader.loadClass(velocityClassName);
  15. Class<?> newVelocity = newLoader.loadClass(velocityClassName);
  16. assertThat(myVelocity).isNotEqualTo(newVelocity);
  17. Runnable test = (Runnable) newLoader.loadClass(RunInClassLoader.class.getName()).newInstance();
  18. assertThat(test.getClass()).isNotEqualTo(RunInClassLoader.class);
  19. test.run();
  20. }

代码示例来源:origin: javapathfinder/jpf-core

  1. @Test
  2. public void testConstructorEmptyURLs () {
  3. if (verifyNoPropertyViolation()) {
  4. URLClassLoader cl = new URLClassLoader(new URL[0]);
  5. assertNotNull(cl.getParent());
  6. assertEquals(cl.getParent(), ClassLoader.getSystemClassLoader());
  7. }
  8. }

代码示例来源:origin: yongjhih/AutoJson

  1. /**
  2. * Make a different ClassLoader that loads the same URLs as this one, and use it to compile
  3. * an {@code @AutoJson} class. If Velocity loads its managers using the context class loader,
  4. * and that loader is still the original one that loaded this test, then it will find the
  5. * original copy of the Velocity classes rather than the one from the new loader, and fail.
  6. *
  7. * <p>This test assumes that the test class was loaded by a URLClassLoader and that that loader's
  8. * URLs also include the Velocity classes.
  9. */
  10. public void testClassLoaderHack() throws Exception {
  11. URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
  12. URLClassLoader newLoader = new URLClassLoader(myLoader.getURLs(), myLoader.getParent());
  13. String velocityClassName = Velocity.class.getName();
  14. Class<?> myVelocity = myLoader.loadClass(velocityClassName);
  15. Class<?> newVelocity = newLoader.loadClass(velocityClassName);
  16. assertThat(myVelocity).isNotEqualTo(newVelocity);
  17. Runnable test = (Runnable) newLoader.loadClass(RunInClassLoader.class.getName()).newInstance();
  18. assertThat(test.getClass()).isNotEqualTo(RunInClassLoader.class);
  19. test.run();
  20. }

代码示例来源:origin: yongjhih/RetroFacebook

  1. /**
  2. * Make a different ClassLoader that loads the same URLs as this one, and use it to compile
  3. * an {@code @RetroFacebook} class. If Velocity loads its managers using the context class loader,
  4. * and that loader is still the original one that loaded this test, then it will find the
  5. * original copy of the Velocity classes rather than the one from the new loader, and fail.
  6. *
  7. * <p>This test assumes that the test class was loaded by a URLClassLoader and that that loader's
  8. * URLs also include the Velocity classes.
  9. */
  10. public void testClassLoaderHack() throws Exception {
  11. URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
  12. URLClassLoader newLoader = new URLClassLoader(myLoader.getURLs(), myLoader.getParent());
  13. String velocityClassName = Velocity.class.getName();
  14. Class<?> myVelocity = myLoader.loadClass(velocityClassName);
  15. Class<?> newVelocity = newLoader.loadClass(velocityClassName);
  16. assertThat(myVelocity).isNotEqualTo(newVelocity);
  17. Runnable test = (Runnable) newLoader.loadClass(RunInClassLoader.class.getName()).newInstance();
  18. assertThat(test.getClass()).isNotEqualTo(RunInClassLoader.class);
  19. test.run();
  20. }

代码示例来源:origin: yongjhih/RetroFacebook

  1. /**
  2. * Make a different ClassLoader that loads the same URLs as this one, and use it to compile
  3. * an {@code @RetroFacebook} class. If Velocity loads its managers using the context class loader,
  4. * and that loader is still the original one that loaded this test, then it will find the
  5. * original copy of the Velocity classes rather than the one from the new loader, and fail.
  6. *
  7. * <p>This test assumes that the test class was loaded by a URLClassLoader and that that loader's
  8. * URLs also include the Velocity classes.
  9. */
  10. public void testClassLoaderHack() throws Exception {
  11. URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
  12. URLClassLoader newLoader = new URLClassLoader(myLoader.getURLs(), myLoader.getParent());
  13. String velocityClassName = Velocity.class.getName();
  14. Class<?> myVelocity = myLoader.loadClass(velocityClassName);
  15. Class<?> newVelocity = newLoader.loadClass(velocityClassName);
  16. assertThat(myVelocity).isNotEqualTo(newVelocity);
  17. Runnable test = (Runnable) newLoader.loadClass(RunInClassLoader.class.getName()).newInstance();
  18. assertThat(test.getClass()).isNotEqualTo(RunInClassLoader.class);
  19. test.run();
  20. }

代码示例来源:origin: javapathfinder/jpf-core

  1. @Test
  2. public void testConstructorParent() {
  3. if (verifyNoPropertyViolation()) {
  4. URL[] urls = new URL[0];
  5. ClassLoader parent = new TestClassLoader(urls);
  6. URLClassLoader cl = new URLClassLoader(urls, parent);
  7. assertNotNull(parent.getParent());
  8. assertEquals(parent.getParent(), ClassLoader.getSystemClassLoader());
  9. assertNotNull(cl.getParent());
  10. assertEquals(cl.getParent(), parent);
  11. }
  12. }

代码示例来源:origin: javapathfinder/jpf-core

  1. @Test
  2. public void testNewInstance2() throws MalformedURLException, ClassNotFoundException {
  3. movePkgOut();
  4. if (verifyNoPropertyViolation()) {
  5. URL[] urls = new URL[1];
  6. urls[0] = new URL(dirUrl);
  7. URLClassLoader parent = URLClassLoader.newInstance(urls);
  8. URLClassLoader cl = URLClassLoader.newInstance(urls, parent);
  9. assertSame(parent, cl.getParent());
  10. Class<?> c = cl.loadClass(pkg + ".Class1");
  11. assertNotNull(c);
  12. assertSame(c.getClassLoader(), parent);
  13. String resName = pkg + "/Interface1.class";
  14. URL resource = cl.getResource(resName);
  15. assertNotNull(resource);
  16. resource = cl.getParent().getResource(resName);
  17. assertNotNull(resource);
  18. }
  19. movePkgBack();
  20. }

相关文章