Example usage for org.objectweb.asm ClassReader readByte

List of usage examples for org.objectweb.asm ClassReader readByte

Introduction

In this page you can find the example usage for org.objectweb.asm ClassReader readByte.

Prototype

public int readByte(final int offset) 

Source Link

Document

Reads a byte value in this ClassReader .

Usage

From source file:com.j2biz.pencil.LogAttribute.java

License:Open Source License

protected Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
    return new LogAttribute(cr.readByte(off));
}

From source file:org.gradle.api.internal.tasks.compile.incremental.analyzer.ClassDependenciesAnalyzer.java

License:Apache License

private List<String> getClassDependencies(ClassRelevancyFilter filter, ClassReader reader) {
    List<String> out = new LinkedList<String>();
    char[] charBuffer = new char[reader.getMaxStringLength()];
    for (int i = 1; i < reader.getItemCount(); i++) {
        int itemOffset = reader.getItem(i);
        if (itemOffset > 0 && reader.readByte(itemOffset - 1) == 7) {
            // A CONSTANT_Class entry, read the class descriptor
            String classDescriptor = reader.readUTF8(itemOffset, charBuffer);
            Type type = Type.getObjectType(classDescriptor);
            while (type.getSort() == Type.ARRAY) {
                type = type.getElementType();
            }/*from  w  w  w.ja v  a  2  s  .  co  m*/
            if (type.getSort() != Type.OBJECT) {
                // A primitive type
                continue;
            }
            String name = type.getClassName();
            if (filter.isRelevant(name)) {
                out.add(name);
            }
        }
    }
    return out;
}

From source file:org.gradle.api.internal.tasks.compile.incremental.analyzer.DefaultClassDependenciesAnalyzer.java

License:Apache License

private Set<String> getClassDependencies(ClassRelevancyFilter filter, ClassReader reader) {
    Set<String> out = new HashSet<String>();
    char[] charBuffer = new char[reader.getMaxStringLength()];
    for (int i = 1; i < reader.getItemCount(); i++) {
        int itemOffset = reader.getItem(i);
        if (itemOffset > 0 && reader.readByte(itemOffset - 1) == 7) {
            // A CONSTANT_Class entry, read the class descriptor
            String classDescriptor = reader.readUTF8(itemOffset, charBuffer);
            Type type = Type.getObjectType(classDescriptor);
            while (type.getSort() == Type.ARRAY) {
                type = type.getElementType();
            }/*from   w ww . j  a  va  2s.  co m*/
            if (type.getSort() != Type.OBJECT) {
                // A primitive type
                continue;
            }
            String name = type.getClassName();
            if (filter.isRelevant(name)) {
                out.add(name);
            }
        }
    }
    return out;
}

From source file:org.gradle.api.internal.tasks.compile.incremental.asm.ClassDependenciesVisitor.java

License:Apache License

private void collectClassDependencies(ClassReader reader) {
    char[] charBuffer = new char[reader.getMaxStringLength()];
    for (int i = 1; i < reader.getItemCount(); i++) {
        int itemOffset = reader.getItem(i);
        if (itemOffset > 0 && reader.readByte(itemOffset - 1) == 7) {
            // A CONSTANT_Class entry, read the class descriptor
            String classDescriptor = reader.readUTF8(itemOffset, charBuffer);
            Type type = Type.getObjectType(classDescriptor);
            while (type.getSort() == Type.ARRAY) {
                type = type.getElementType();
            }/*from   ww w.j  a v a2 s . com*/
            if (type.getSort() != Type.OBJECT) {
                // A primitive type
                continue;
            }
            String name = type.getClassName();
            maybeAddDependentType(name);
        }
    }
}

From source file:org.gradle.tooling.internal.consumer.connection.ActionClasspathFactory.java

License:Apache License

/**
 * Locates the classpath required by the given target class. Traverses the dependency graph of classes used by the specified class and collects the result in the given collection.
 *//*from   w  w  w  . j  a va  2s.co  m*/
private void find(Class<?> target, Collection<Class<?>> visited, Collection<URL> dest) {
    ClassLoader targetClassLoader = target.getClassLoader();
    if (targetClassLoader == null) {
        // A system class, skip it
        return;
    }
    if (!visited.add(target)) {
        // Already seen this class, skip it
        return;
    }

    String resourceName = target.getName().replace(".", "/") + ".class";
    URL resource = targetClassLoader.getResource(resourceName);
    try {
        if (resource == null) {
            LOGGER.warn("Could not determine classpath for {}", target);
            return;
        }

        File classPathRoot = ClasspathUtil.getClasspathForResource(resource, resourceName);
        dest.add(classPathRoot.toURI().toURL());

        //TODO:ADAM - remove this
        LOGGER.info("==> LOOKING FOR {}", target);
        LOGGER.info("    * ClassLoader: {}", targetClassLoader);
        CodeSource codeSource = target.getProtectionDomain().getCodeSource();
        LOGGER.info("    * Code-source: {}", codeSource == null ? null : codeSource.getLocation());
        LOGGER.info("    * Resource: {}", resource);
        LOGGER.info("    * Classpath root: {}", classPathRoot);

        // To determine the dependencies of the class, load up the byte code and look for CONSTANT_Class entries in the constant pool

        ClassReader reader;
        InputStream inputStream = resource.openStream();
        try {
            reader = new ClassReader(inputStream);
        } finally {
            inputStream.close();
        }

        char[] charBuffer = new char[reader.getMaxStringLength()];
        for (int i = 1; i < reader.getItemCount(); i++) {
            int itemOffset = reader.getItem(i);
            if (itemOffset > 0 && reader.readByte(itemOffset - 1) == 7) {
                // A CONSTANT_Class entry, read the class descriptor
                String classDescriptor = reader.readUTF8(itemOffset, charBuffer);
                Type type = Type.getObjectType(classDescriptor);
                while (type.getSort() == Type.ARRAY) {
                    type = type.getElementType();
                }
                if (type.getSort() != Type.OBJECT) {
                    // A primitive type
                    continue;
                }
                String className = type.getClassName();
                if (className.equals(target.getName())) {
                    // A reference to this class
                    continue;
                }

                Class<?> cl;
                try {
                    cl = Class.forName(className, false, targetClassLoader);
                } catch (ClassNotFoundException e) {
                    // This is fine, just ignore it
                    LOGGER.warn("Could not determine classpath for {}", target);
                    continue;
                }
                find(cl, visited, dest);
            }
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not determine the class-path for %s.", target), e);
    }
}

From source file:org.gradle.tooling.internal.provider.ClasspathInferer.java

License:Apache License

/**
 * Locates the classpath required by the given target class. Traverses the dependency graph of classes used by the specified class and collects the result in the given collection.
 *//* w  w w .j a v  a2s.  c o  m*/
private void find(Class<?> target, Collection<Class<?>> visited, Collection<URL> dest) {
    ClassLoader targetClassLoader = target.getClassLoader();
    if (targetClassLoader == null) {
        // A system class, skip it
        return;
    }
    if (!visited.add(target)) {
        // Already seen this class, skip it
        return;
    }

    String resourceName = target.getName().replace(".", "/") + ".class";
    URL resource = targetClassLoader.getResource(resourceName);
    try {
        if (resource == null) {
            LOGGER.warn("Could not determine classpath for {}", target);
            return;
        }

        File classPathRoot = ClasspathUtil.getClasspathForResource(resource, resourceName);
        dest.add(classPathRoot.toURI().toURL());

        // To determine the dependencies of the class, load up the byte code and look for CONSTANT_Class entries in the constant pool

        ClassReader reader;
        InputStream inputStream = resource.openStream();
        try {
            reader = new ClassReader(inputStream);
        } finally {
            inputStream.close();
        }

        char[] charBuffer = new char[reader.getMaxStringLength()];
        for (int i = 1; i < reader.getItemCount(); i++) {
            int itemOffset = reader.getItem(i);
            if (itemOffset > 0 && reader.readByte(itemOffset - 1) == 7) {
                // A CONSTANT_Class entry, read the class descriptor
                String classDescriptor = reader.readUTF8(itemOffset, charBuffer);
                Type type = Type.getObjectType(classDescriptor);
                while (type.getSort() == Type.ARRAY) {
                    type = type.getElementType();
                }
                if (type.getSort() != Type.OBJECT) {
                    // A primitive type
                    continue;
                }
                String className = type.getClassName();
                if (className.equals(target.getName())) {
                    // A reference to this class
                    continue;
                }

                Class<?> cl;
                try {
                    cl = Class.forName(className, false, targetClassLoader);
                } catch (ClassNotFoundException e) {
                    // This is fine, just ignore it
                    LOGGER.warn("Could not determine classpath for {}", target);
                    continue;
                }
                find(cl, visited, dest);
            }
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not determine the class-path for %s.", target), e);
    }
}

From source file:org.gradle.tooling.internal.provider.serialization.ClasspathInferer.java

License:Apache License

/**
 * Locates the classpath required by the given target class. Traverses the dependency graph of classes used by the specified class and collects the result in the given collection.
 *///from   ww w  .j  a va 2s. c  om
private void find(Class<?> target, Collection<Class<?>> visited, Collection<URL> dest) {
    ClassLoader targetClassLoader = target.getClassLoader();
    if (targetClassLoader == null || targetClassLoader == ClassLoaderUtils.getPlatformClassLoader()) {
        // A system class, skip it
        return;
    }
    if (!visited.add(target)) {
        // Already seen this class, skip it
        return;
    }

    String resourceName = target.getName().replace('.', '/') + ".class";
    URL resource = targetClassLoader.getResource(resourceName);
    try {
        if (resource == null) {
            LOGGER.warn("Could not determine classpath for {}", target);
            return;
        }

        File classPathRoot = ClasspathUtil.getClasspathForClass(target);
        dest.add(classPathRoot.toURI().toURL());

        // To determine the dependencies of the class, load up the byte code and look for CONSTANT_Class entries in the constant pool

        ClassReader reader;
        URLConnection urlConnection = resource.openConnection();
        if (urlConnection instanceof JarURLConnection) {
            // Using the caches for these connections leaves the Jar files open. Don't use the cache, so that the Jar file is closed when the stream is closed below
            // There are other options for solving this that may be more performant. However a class is inspected this way once and the result reused, so this approach is probably fine
            urlConnection.setUseCaches(false);
        }
        InputStream inputStream = urlConnection.getInputStream();
        try {
            reader = new Java9ClassReader(ByteStreams.toByteArray(inputStream));
        } finally {
            inputStream.close();
        }

        char[] charBuffer = new char[reader.getMaxStringLength()];
        for (int i = 1; i < reader.getItemCount(); i++) {
            int itemOffset = reader.getItem(i);
            if (itemOffset > 0 && reader.readByte(itemOffset - 1) == 7) {
                // A CONSTANT_Class entry, read the class descriptor
                String classDescriptor = reader.readUTF8(itemOffset, charBuffer);
                Type type = Type.getObjectType(classDescriptor);
                while (type.getSort() == Type.ARRAY) {
                    type = type.getElementType();
                }
                if (type.getSort() != Type.OBJECT) {
                    // A primitive type
                    continue;
                }
                String className = type.getClassName();
                if (className.equals(target.getName())) {
                    // A reference to this class
                    continue;
                }

                Class<?> cl;
                try {
                    cl = Class.forName(className, false, targetClassLoader);
                } catch (ClassNotFoundException e) {
                    // This is fine, just ignore it
                    LOGGER.warn("Could not determine classpath for {}", target);
                    continue;
                }
                find(cl, visited, dest);
            }
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not determine the class-path for %s.", target), e);
    }
}