Example usage for java.lang ClassLoader toString

List of usage examples for java.lang ClassLoader toString

Introduction

In this page you can find the example usage for java.lang ClassLoader toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.java

private static void cleanUpGlobalClassValue(@Nonnull ClassLoader loader) throws Exception {
    Class<?> classInfoC = Class.forName("org.codehaus.groovy.reflection.ClassInfo");
    // TODO switch to MethodHandle for speed
    Field globalClassValueF;//www  .  jav a  2s. c  o  m
    try {
        globalClassValueF = classInfoC.getDeclaredField("globalClassValue");
    } catch (NoSuchFieldException x) {
        return; // Groovy 1, fine
    }
    globalClassValueF.setAccessible(true);
    Object globalClassValue = globalClassValueF.get(null);
    Class<?> groovyClassValuePreJava7C = Class
            .forName("org.codehaus.groovy.reflection.GroovyClassValuePreJava7");
    if (!groovyClassValuePreJava7C.isInstance(globalClassValue)) {
        return; // using GroovyClassValueJava7 due to -Dgroovy.use.classvalue or on IBM J9, fine
    }
    Field mapF = groovyClassValuePreJava7C.getDeclaredField("map");
    mapF.setAccessible(true);
    Object map = mapF.get(globalClassValue);
    Class<?> groovyClassValuePreJava7Map = Class
            .forName("org.codehaus.groovy.reflection.GroovyClassValuePreJava7$GroovyClassValuePreJava7Map");
    Collection entries = (Collection) groovyClassValuePreJava7Map.getMethod("values").invoke(map);
    Method removeM = groovyClassValuePreJava7Map.getMethod("remove", Object.class);
    Class<?> entryC = Class.forName("org.codehaus.groovy.util.AbstractConcurrentMapBase$Entry");
    Method getValueM = entryC.getMethod("getValue");
    List<Class<?>> toRemove = new ArrayList<>(); // not sure if it is safe against ConcurrentModificationException or not
    try {
        Field classRefF = classInfoC.getDeclaredField("classRef"); // 2.4.8+
        classRefF.setAccessible(true);
        for (Object entry : entries) {
            Object value = getValueM.invoke(entry);
            toRemove.add(((WeakReference<Class<?>>) classRefF.get(value)).get());
        }
    } catch (NoSuchFieldException x) {
        Field klazzF = classInfoC.getDeclaredField("klazz"); // 2.4.7-
        klazzF.setAccessible(true);
        for (Object entry : entries) {
            Object value = getValueM.invoke(entry);
            toRemove.add((Class) klazzF.get(value));
        }
    }
    Iterator<Class<?>> it = toRemove.iterator();
    while (it.hasNext()) {
        Class<?> klazz = it.next();
        ClassLoader encounteredLoader = klazz.getClassLoader();
        if (encounteredLoader != loader) {
            it.remove();
            LOGGER.log(Level.FINEST, "ignoring {0} with loader {1}",
                    new Object[] { klazz, /* do not hold from LogRecord */String.valueOf(encounteredLoader) });
        }
    }
    LOGGER.log(Level.FINE, "cleaning up {0} associated with {1}",
            new Object[] { toRemove.toString(), loader.toString() });
    for (Class<?> klazz : toRemove) {
        removeM.invoke(map, klazz);
    }
}

From source file:org.ofbiz.base.start.Start.java

private void initClasspath() throws StartupException {
    Classpath classPath = new Classpath(System.getProperty("java.class.path"));
    try {/*from   ww w. j av a2s. com*/
        this.config.initClasspath(classPath);
    } catch (IOException e) {
        throw (StartupException) new StartupException("Couldn't initialized classpath").initCause(e);
    }
    // Set the classpath/classloader
    System.setProperty("java.class.path", classPath.toString());
    ClassLoader classloader = classPath.getClassLoader();
    Thread.currentThread().setContextClassLoader(classloader);
    if (System.getProperty("DEBUG") != null) {
        System.out.println("Startup Classloader: " + classloader.toString());
        System.out.println("Startup Classpath: " + classPath.toString());
    }
}

From source file:org.sonar.classloader.ClassloaderBuilderTest.java

@Test
public void minimal_system_classloader() throws Exception {
    // create a classloader based on system classloader
    // -> access only to JRE
    Map<String, ClassLoader> classloaders = sut.newClassloader("example").build();

    assertThat(classloaders).hasSize(1);
    ClassLoader classloader = classloaders.get("example");
    assertThat(classloader.toString()).isEqualTo("ClassRealm{example}");
    assertThat(canLoadClass(classloader, HashMap.class.getName())).isTrue();
    assertThat(canLoadClass(classloader, Test.class.getName())).isFalse();
    assertThat(canLoadClass(classloader, "A")).isFalse();
    assertThat(canLoadResource(classloader, "a.txt")).isFalse();
}

From source file:org.sonar.core.platform.PluginLoader.java

public void unload(Collection<Plugin> plugins) {
    for (Plugin plugin : plugins) {
        ClassLoader classLoader = plugin.getClass().getClassLoader();
        if (classLoader instanceof Closeable && classLoader != classloaderFactory.baseClassLoader()) {
            try {
                ((Closeable) classLoader).close();
            } catch (Exception e) {
                Loggers.get(getClass()).error("Fail to close classloader " + classLoader.toString(), e);
            }//from  ww w.j a  v a  2  s . c o  m
        }
    }
}