Java Class Loader getClassInputStream(String className, ClassLoader cl)

Here you can find the source of getClassInputStream(String className, ClassLoader cl)

Description

get Class Input Stream

License

Open Source License

Declaration

public static InputStream getClassInputStream(String className, ClassLoader cl) 

Method Source Code

//package com.java2s;
/*// www.ja  v  a 2 s.c  o  m
 * Copyright (c) 2015, Parallel Universe Software Co. All rights reserved.
 * 
 * This program and the accompanying materials are licensed under
 * GNU General Public License, version 2, with the Classpath Exception
 * 
 * http://openjdk.java.net/legal/gplv2+ce.html
 */

import java.io.InputStream;

public class Main {
    private static final String CLASS_FILE_NAME_EXTENSION = ".class";

    public static InputStream getClassInputStream(String className, ClassLoader cl) {
        return cl.getResourceAsStream(classToResource(className));
    }

    public static InputStream getClassInputStream(Class<?> clazz) {
        final InputStream is = getClassInputStream(clazz.getName(), clazz.getClassLoader());
        if (is == null)
            throw new UnsupportedOperationException("Class file " + clazz.getName()
                    + " could not be loaded by the class's classloader " + clazz.getClassLoader());
        return is;
    }

    public static String classToResource(String className) {
        if (className == null)
            return null;
        return className.replace('.', '/') + CLASS_FILE_NAME_EXTENSION;
    }

    public static String classToResource(Class<?> clazz) {
        if (clazz == null)
            return null;
        return classToResource(clazz.getName());
    }
}

Related

  1. createClassLoader(final String[] cpEntries)
  2. createClassLoader(String fileName)
  3. createFallbackClassLoader(Collection files)
  4. dumpClassLoader(ClassLoader cl, PrintWriter out)
  5. getClass(Object classLoaderMakedObject, File file, String targetClass)
  6. getClassLoader()
  7. getClassLoader(File[] directories)
  8. getClassLoader(String path)
  9. getClassLoaderForClass(final Class clazz)