Javaのクラスローダの階層を表示する

はじめに

以下のようなクラスローダの階層を出力するクラスを作成した。 クラスパスも表示しているが、動的に変わったり、クラスローダの実装によっては取得不可能だったりするので、参考程度。 Java9には未対応。 JigsawやBuiltinClassLoaderを理解して作りなおす必要がある。

bootstrap class loader {id=0}
    /opt/java/jdk1.8.0_131/jre/lib/endorsed
    /opt/java/jdk1.8.0_131/jre/lib/resources.jar
    /opt/java/jdk1.8.0_131/jre/lib/rt.jar
    /opt/java/jdk1.8.0_131/jre/lib/sunrsasign.jar
    /opt/java/jdk1.8.0_131/jre/lib/jsse.jar
    /opt/java/jdk1.8.0_131/jre/lib/jce.jar
    /opt/java/jdk1.8.0_131/jre/lib/charsets.jar
    /opt/java/jdk1.8.0_131/jre/lib/jfr.jar
    /opt/java/jdk1.8.0_131/jre/classes
sun.misc.Launcher$ExtClassLoader {id=405662939}
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/sunpkcs11.jar
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/nashorn.jar
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/jaccess.jar
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/sunec.jar
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/zipfs.jar
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/dnsns.jar
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/localedata.jar
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/sunjce_provider.jar
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/jfxrt.jar
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/cldrdata.jar
sun.misc.Launcher$AppClassLoader {context-class-loader=true, id=1252169911, system-class-loader=true}
    file:/path/to/classes

ソースコード

package redj.classloader;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class ClassLoaders {

    private static final Handler[] HANDLERS = {
        new BootstrapClassLoaderHandler(),
        new URLClassLoaderHandler(),
        new UnknownHandler()
    };

    public static String getDump(ClassLoader classLoader) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        dump(classLoader, out);

        return out.toString();
    }

    public static void dump(ClassLoader classLoader) {
        dump(classLoader, System.out);
    }

    public static void dump(ClassLoader classLoader, OutputStream out) {
        PrintWriter writer = new PrintWriter(out, true);
        try {
            dump(classLoader, writer);
        } finally {
            writer.flush();
        }
    }

    public static void dump(ClassLoader classLoader, PrintWriter writer) {
        for (Handler handler : HANDLERS) {
            if (handler.accept(classLoader)) {
                handler.handle(classLoader, writer);
                break;
            }
        }
    }

    public interface Handler {

        public boolean accept(ClassLoader classLoader);

        public default void handle(ClassLoader classLoader, PrintWriter writer) {
            // delegate to parent class loader
            delegateToParent(classLoader, writer);

            // print class loader name and attributes
            SortedMap attributes = getAttributes(classLoader);
            writer.println(getClassLoaderName(classLoader) + (!attributes.isEmpty() ? " " + attributes : ""));

            // print classpath
            for (String path : getClasspath(classLoader)) {
                writer.println("    " + path);
            }
        }

        public default void delegateToParent(ClassLoader classLoader, PrintWriter writer) {
            dump(classLoader.getParent(), writer);
        }

        public default String getClassLoaderName(ClassLoader classLoader) {
            return classLoader.getClass().getName();
        }

        public default SortedMap getAttributes(ClassLoader classLoader) {
            SortedMap<String, String> result = new TreeMap<>();

            result.put("id", "" + System.identityHashCode(classLoader));

            if (classLoader == ClassLoader.getSystemClassLoader()) {
                result.put("system-class-loader", "true");
            }

            if (classLoader == Thread.currentThread().getContextClassLoader()) {
                result.put("context-class-loader", "true");
            }

            return result;
        }

        public List<String> getClasspath(ClassLoader classLoader);
    }

    private static class UnknownHandler implements Handler {

        @Override
        public boolean accept(ClassLoader classLoader) {
            return true;
        }

        @Override
        public List<String> getClasspath(ClassLoader classLoader) {
            return Collections.EMPTY_LIST;
        }
    }

    private static class BootstrapClassLoaderHandler implements Handler {

        @Override
        public boolean accept(ClassLoader classLoader) {
            return classLoader == null;
        }

        @Override
        public void delegateToParent(ClassLoader classLoader, PrintWriter writer) {
        }

        @Override
        public String getClassLoaderName(ClassLoader classLoader) {
            return "bootstrap class loader";
        }

        @Override
        public List<String> getClasspath(ClassLoader classLoader) {
            List<String> result = new ArrayList<>();

            result.addAll(splitClassPath(System.getProperty("java.endorsed.dirs")));
            result.addAll(splitClassPath(System.getProperty("sun.boot.class.path")));

            return result;
        }

        private List<String> splitClassPath(String classpath) {
            List<String> result = Collections.EMPTY_LIST;;

            if (classpath != null) {
                result = Arrays.asList(classpath.split(System.getProperty("path.separator")));
            }

            return result;
        }
    }

    private static class URLClassLoaderHandler implements Handler {

        @Override
        public boolean accept(ClassLoader classLoader) {
            return classLoader instanceof URLClassLoader;
        }

        @Override
        public List<String> getClasspath(ClassLoader classLoader) {
            return Arrays
                    .stream(((URLClassLoader) classLoader).getURLs())
                    .map((url) -> url.toExternalForm())
                    .collect(Collectors.toList());
        }
    }
}

Apache TomcatにデプロイしたWebアプリのクラスローダの表示例

ここでは、Apache Tomcat 8.5.15に対して2つのWebアプリtest-webapp-1.warとtest-webapp-2.warをデプロイし、それぞれのアプリからのクラスローダのダンプを出力する。 Webアプリにはそれぞれ以下のようなServletを準備しておき、そのServletにアクセスすることでダンプを出力する。

package redj.classloader;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/dump/*")
public class DumpServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        ClassLoaders.dump(getClass().getClassLoader(), response.getWriter());
    }
}

test-webapp-1.warにアクセスした結果、出力されたダンプは以下。

$ curl http://localhost:8080/test-webapp-1/dump
bootstrap class loader {id=0}
    /opt/java/jdk1.8.0_131/jre/lib/endorsed
    /opt/java/jdk1.8.0_131/jre/lib/resources.jar
    ...
sun.misc.Launcher$ExtClassLoader {id=159259014}
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/sunpkcs11.jar
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/nashorn.jar
    ...
sun.misc.Launcher$AppClassLoader {id=1442407170, system-class-loader=true}
    file:/opt/apache-tomcat-8.5.15/bin/bootstrap.jar
    file:/opt/apache-tomcat-8.5.15/bin/tomcat-juli.jar
java.net.URLClassLoader {id=142257191}
    file:/opt/apache-tomcat-8.5.15/lib/
    file:/opt/apache-tomcat-8.5.15/lib/tomcat-jdbc.jar
    ...
org.apache.catalina.loader.ParallelWebappClassLoader {context-class-loader=true, id=819380823}
    file:/opt/apache-tomcat-8.5.15/webapps/test-webapp-1/WEB-INF/classes/

test-webapp-2.warにアクセスした結果、出力されたダンプは以下。

$ curl http://localhost:8080/test-webapp-2/dump
bootstrap class loader {id=0}
    /opt/java/jdk1.8.0_131/jre/lib/endorsed
    /opt/java/jdk1.8.0_131/jre/lib/resources.jar
    ...
sun.misc.Launcher$ExtClassLoader {id=159259014}
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/sunpkcs11.jar
    file:/opt/java/jdk1.8.0_131/jre/lib/ext/nashorn.jar
    ...
sun.misc.Launcher$AppClassLoader {id=1442407170, system-class-loader=true}
    file:/opt/apache-tomcat-8.5.15/bin/bootstrap.jar
    file:/opt/apache-tomcat-8.5.15/bin/tomcat-juli.jar
java.net.URLClassLoader {id=142257191}
    file:/opt/apache-tomcat-8.5.15/lib/
    file:/opt/apache-tomcat-8.5.15/lib/tomcat-jdbc.jar
    ...
org.apache.catalina.loader.ParallelWebappClassLoader {context-class-loader=true, id=1256777078}
    file:/opt/apache-tomcat-8.5.15/webapps/test-webapp-2/WEB-INF/classes/

クラスローダのidより、以下のようなツリー構造であることが分かる。

bootstrap class loader {id=0}
`-- ExtClassLoader {id=159259014}
    `-- AppClassLoader {id=1442407170}
        `-- URLClassLoader {id=142257191}
            |-- ParallelWebappClassLoader {id=819380823}  // test-webapp-1
            `-- ParallelWebappClassLoader {id=1256777078} // test-webapp-2

またParallelWebappClassLoaderはそれぞれcontext-class-loader=trueとマークされており、すなわちコンテキストクラスローダは以下のように設定されていることが分かる。

Webアプリ コンテキストクラスローダ
test-webapp-1 ParallelWebappClassLoader {id=819380823}
test-webapp-2 ParallelWebappClassLoader {id=1256777078}