Java Class Loader readBytecodeForClass(ClassLoader loader, String className, boolean mustExist)

Here you can find the source of readBytecodeForClass(ClassLoader loader, String className, boolean mustExist)

Description

read Bytecode For Class

License

Apache License

Declaration

public static byte[] readBytecodeForClass(ClassLoader loader,
            String className, boolean mustExist) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.*;

import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    public static byte[] readBytecodeForClass(ClassLoader loader,
            String className, boolean mustExist) {
        String path = toClassPath(className);
        InputStream stream = null;

        try {/*from  ww  w .  j a  v a 2s.  c om*/
            stream = getStreamForPath(loader, path);

            if (stream == null) {
                if (mustExist)
                    throw new RuntimeException(
                            String.format(
                                    "Unable to locate class file for '%s' in class loader %s.",
                                    className, loader));

                return null;
            }

            return readBytestream(stream);
        } catch (IOException ex) {
            throw new RuntimeException(String.format(
                    "Failure reading bytecode for class %s: %s", className,
                    toMessage(ex)), ex);
        } finally {
            close(stream);
        }
    }

    public static String toClassPath(String className) {
        return toInternalName(className) + ".class";
    }

    static InputStream getStreamForPath(ClassLoader loader, String path)
            throws IOException {
        URL url = loader.getResource(path);

        if (url == null) {
            return null;
        }

        // This *should* handle Tomcat better, where the Tomcat class loader appears to be caching
        // the contents of files; this bypasses Tomcat to re-read the files from the disk directly.

        if (url.getProtocol().equals("file")) {
            try {
                return new FileInputStream(new File(url.toURI()));
            } catch (URISyntaxException e) {
                return null;
            }
        }

        return url.openStream();
    }

    static byte[] readBytestream(InputStream stream) throws IOException {
        byte[] buffer = new byte[5000];

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        while (true) {
            int length = stream.read(buffer);

            if (length < 0)
                break;

            bos.write(buffer, 0, length);
        }

        bos.close();

        return bos.toByteArray();
    }

    public static String toMessage(Throwable t) {
        String message = t.getMessage();

        return isBlank(message) ? t.getClass().getName() : message;
    }

    public static void close(Closeable closeable) {
        try {
            if (closeable != null)
                closeable.close();
        } catch (IOException ex) {
            // Ignore it.
        }
    }

    public static String toInternalName(String className) {
        assert isNonBlank(className);

        return className.replace('.', '/');
    }

    public static boolean isBlank(String input) {
        return input == null || input.length() == 0
                || input.trim().length() == 0;
    }

    public static boolean isNonBlank(String input) {
        return !isBlank(input);
    }
}

Related

  1. loadStyleSheet(Class type)
  2. loadTextFile(Class relToThisClass, String relFileName)
  3. pathFromLoaders(final Class clazz)
  4. print(ClassLoader loader)
  5. readAll(ClassLoader cl, String path)
  6. resolveClientClassLoader(Map env)
  7. resolveServerClassLoader(Map env, MBeanServer mbs)
  8. scanPackage(ClassLoader classLoader, Package pkg)
  9. toInputStream(String name, ClassLoader cl)