Example usage for org.objectweb.asm ClassReader getMaxStringLength

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

Introduction

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

Prototype

public int getMaxStringLength() 

Source Link

Document

Returns a conservative estimate of the maximum length of the strings contained in the class's constant pool table.

Usage

From source file:net.orfjackal.retrolambda.test.TestUtil.java

License:Open Source License

public static void visitConstantPool(ClassReader reader, ConstantPoolVisitor visitor) {
    char[] buf = new char[reader.getMaxStringLength()];
    for (int item = 0; item < reader.getItemCount(); item++) {
        try {/*  w  w  w .  j a va 2s.c o  m*/
            Object constant = reader.readConst(item, buf);
            visitor.visit(item, constant);
        } catch (Exception e) {
            // XXX: constant pool entry which is a Methodref, InvokeDynamic or similar non-plain constant
            // FIXME: readConst throws ArrayIndexOutOfBoundsException nearly all the time; how to use it???
            //e.printStackTrace();
        }
    }
}

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();
            }/* www. j a v a  2 s.  c  o 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 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.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();
            }/* w  w  w .  jav  a2  s  . c  om*/
            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  ww  w.j  ava 2  s. 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.
 *//*from   ww w. j  av  a2 s .  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.
 */// w ww .  ja va  2s . c o  m
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);
    }
}