Java Class Loader dumpClassLoader(ClassLoader cl, PrintWriter out)

Here you can find the source of dumpClassLoader(ClassLoader cl, PrintWriter out)

Description

dump Class Loader

License

Open Source License

Declaration

public static void dumpClassLoader(ClassLoader cl, PrintWriter out) 

Method Source Code


//package com.java2s;
/*// ww w . ja v  a2 s  .c o  m
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.PrintWriter;

import java.lang.reflect.Method;
import java.net.URL;

public class Main {
    public static void dumpClassLoader(ClassLoader cl, PrintWriter out) {
        int level = 0;
        while (cl != null) {
            String msg = "Servlet ClassLoader[" + level + "]: " + cl.getClass().getName() + ':' + cl.hashCode();
            out.println(msg);
            URL[] urls = getClassLoaderURLs(cl);
            msg = "  URLs:";
            out.println(msg);
            for (int u = 0; u < urls.length; u++) {
                msg = "  [" + u + "] = " + urls[u];
                out.println(msg);
            }
            cl = cl.getParent();
            level++;
        }
    }

    /** Use reflection to access a URL[] getURLs method so that non-URLClassLoader
     *class loaders that support this method can provide info.
     */
    private static URL[] getClassLoaderURLs(ClassLoader cl) {
        URL[] urls = {};
        try {
            Class returnType = urls.getClass();
            Class[] parameterTypes = {};
            Method getURLs = cl.getClass().getMethod("getURLs", parameterTypes);
            if (returnType.isAssignableFrom(getURLs.getReturnType())) {
                Object[] args = {};
                urls = (URL[]) getURLs.invoke(cl, args);
            }
        } catch (Exception ignore) {
        }
        return urls;
    }
}

Related

  1. createClassloader(File libDir, ClassLoader cl)
  2. createClassLoader(File root)
  3. createClassLoader(final String[] cpEntries)
  4. createClassLoader(String fileName)
  5. createFallbackClassLoader(Collection files)
  6. getClass(Object classLoaderMakedObject, File file, String targetClass)
  7. getClassInputStream(String className, ClassLoader cl)
  8. getClassLoader()
  9. getClassLoader(File[] directories)