Search in sources :

Example 96 with Type

use of org.objectweb.asm.Type in project buck by facebook.

the class FirstOrderHelper method addTypeAndSupers.

private void addTypeAndSupers(Type type) {
    addType(type);
    FirstOrderTypeInfo info = knownTypes.get(type);
    if (info != null) {
        addTypeAndSupers(info.superType);
        for (Type interfaceType : info.interfaceTypes) {
            addTypeAndSupers(interfaceType);
        }
    }
}
Also used : Type(org.objectweb.asm.Type)

Example 97 with Type

use of org.objectweb.asm.Type in project gradle by gradle.

the class MethodMember method toString.

@Override
public String toString() {
    StringBuilder methodDesc = new StringBuilder();
    methodDesc.append(Modifier.toString(getAccess())).append(" ");
    methodDesc.append(Type.getReturnType(getTypeDesc()).getClassName()).append(" ");
    methodDesc.append(getName());
    methodDesc.append("(");
    Type[] argumentTypes = Type.getArgumentTypes(getTypeDesc());
    for (int i = 0, argumentTypesLength = argumentTypes.length; i < argumentTypesLength; i++) {
        Type type = argumentTypes[i];
        methodDesc.append(type.getClassName());
        if (i < argumentTypesLength - 1) {
            methodDesc.append(", ");
        }
    }
    methodDesc.append(")");
    return methodDesc.toString();
}
Also used : Type(org.objectweb.asm.Type)

Example 98 with Type

use of org.objectweb.asm.Type in project gradle by gradle.

the class ClasspathInferer method find.

/**
     * 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.
     */
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);
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) InputStream(java.io.InputStream) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) GradleException(org.gradle.api.GradleException) Type(org.objectweb.asm.Type) GradleException(org.gradle.api.GradleException) ClassReader(org.objectweb.asm.ClassReader) Java9ClassReader(org.gradle.util.internal.Java9ClassReader) Java9ClassReader(org.gradle.util.internal.Java9ClassReader) File(java.io.File)

Example 99 with Type

use of org.objectweb.asm.Type in project groovy-core by groovy.

the class AsmClassGenerator method visitAnnotationDefaultExpression.

void visitAnnotationDefaultExpression(AnnotationVisitor av, ClassNode type, Expression exp) {
    if (exp instanceof ClosureExpression) {
        ClassNode closureClass = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) exp, ACC_PUBLIC);
        Type t = Type.getType(BytecodeHelper.getTypeDescription(closureClass));
        av.visit(null, t);
    } else if (type.isArray()) {
        ListExpression list = (ListExpression) exp;
        AnnotationVisitor avl = av.visitArray(null);
        ClassNode componentType = type.getComponentType();
        for (Expression lExp : list.getExpressions()) {
            visitAnnotationDefaultExpression(avl, componentType, lExp);
        }
    } else if (ClassHelper.isPrimitiveType(type) || type.equals(ClassHelper.STRING_TYPE)) {
        ConstantExpression constExp = (ConstantExpression) exp;
        av.visit(null, constExp.getValue());
    } else if (ClassHelper.CLASS_Type.equals(type)) {
        ClassNode clazz = exp.getType();
        Type t = Type.getType(BytecodeHelper.getTypeDescription(clazz));
        av.visit(null, t);
    } else if (type.isDerivedFrom(ClassHelper.Enum_Type)) {
        PropertyExpression pExp = (PropertyExpression) exp;
        ClassExpression cExp = (ClassExpression) pExp.getObjectExpression();
        String desc = BytecodeHelper.getTypeDescription(cExp.getType());
        String name = pExp.getPropertyAsString();
        av.visitEnum(null, desc, name);
    } else if (type.implementsInterface(ClassHelper.Annotation_TYPE)) {
        AnnotationConstantExpression avExp = (AnnotationConstantExpression) exp;
        AnnotationNode value = (AnnotationNode) avExp.getValue();
        AnnotationVisitor avc = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(avExp.getType()));
        visitAnnotationAttributes(value, avc);
    } else {
        throw new GroovyBugError("unexpected annotation type " + type.getName());
    }
    av.visitEnd();
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) Type(org.objectweb.asm.Type) GenericsType(org.codehaus.groovy.ast.GenericsType) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor)

Example 100 with Type

use of org.objectweb.asm.Type in project gradle by gradle.

the class ManagedCollectionProxyClassGenerator method generateConstructors.

private <T> void generateConstructors(ClassWriter visitor, Class<? extends T> implClass, Type superclassType) {
    for (Constructor<?> constructor : implClass.getConstructors()) {
        Type[] paramTypes = new Type[constructor.getParameterTypes().length];
        for (int i = 0; i < paramTypes.length; i++) {
            paramTypes[i] = Type.getType(constructor.getParameterTypes()[i]);
        }
        String methodDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, paramTypes);
        MethodVisitor constructorVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME, methodDescriptor, CONCRETE_SIGNATURE, NO_EXCEPTIONS);
        constructorVisitor.visitCode();
        putThisOnStack(constructorVisitor);
        for (int i = 0; i < paramTypes.length; i++) {
            constructorVisitor.visitVarInsn(paramTypes[i].getOpcode(Opcodes.ILOAD), i + 1);
        }
        constructorVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), CONSTRUCTOR_NAME, methodDescriptor, false);
        finishVisitingMethod(constructorVisitor);
    }
}
Also used : Type(org.objectweb.asm.Type) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

Type (org.objectweb.asm.Type)151 MethodVisitor (org.objectweb.asm.MethodVisitor)33 Label (org.objectweb.asm.Label)21 Method (org.objectweb.asm.commons.Method)16 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)14 ClassWriter (org.objectweb.asm.ClassWriter)13 ArrayList (java.util.ArrayList)11 ClassReader (org.objectweb.asm.ClassReader)10 Method (java.lang.reflect.Method)9 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)9 ClassVisitor (org.objectweb.asm.ClassVisitor)9 PropertyAccessorType (org.gradle.internal.reflect.PropertyAccessorType)7 ModelType (org.gradle.model.internal.type.ModelType)7 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)6 MethodType (java.lang.invoke.MethodType)6 List (java.util.List)6 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)5 InterceptorType (com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorType)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4