Search in sources :

Example 26 with Type

use of org.objectweb.asm.Type in project spring-loaded by spring-projects.

the class MethodProvider method create.

public static MethodProvider create(TypeRegistry registry, TypeDescriptor typeDescriptor) {
    if (typeDescriptor.isReloadable()) {
        ReloadableType rtype = registry.getReloadableType(typeDescriptor.getName(), false);
        if (rtype == null) {
            TypeRegistry tr = registry;
            while (rtype == null) {
                ClassLoader pcl = tr.getClassLoader().getParent();
                if (pcl == null) {
                    break;
                } else {
                    tr = TypeRegistry.getTypeRegistryFor(pcl);
                    if (tr == null) {
                        break;
                    }
                    rtype = tr.getReloadableType(typeDescriptor.getName(), false);
                }
            }
        }
        if (rtype != null) {
            return new ReloadableTypeMethodProvider(rtype);
        }
    //			ReloadableType rtype = registry.getReloadableType(typeDescriptor.getName(), true);
    //			// TODO rtype can be null if this type hasn't been loaded yet for the first time, is that true?
    //			// e.g. CGLIB generated proxy for a service type in grails
    //			if (rtype != null) {
    //				return new ReloadableTypeMethodProvider(rtype);
    //			}
    }
    try {
        try {
            Type objectType = Type.getObjectType(typeDescriptor.getName());
            // TODO doing things this way would mean we aren't 'guessing' the delegation strategy, we
            // are instead allowing it to do its thing then looking for the right registry.
            // Above we are guessing regular parent delegation.
            Class<?> class1 = Utils.toClass(objectType, registry.getClassLoader());
            if (typeDescriptor.isReloadable()) {
                ClassLoader cl = class1.getClassLoader();
                TypeRegistry tr = TypeRegistry.getTypeRegistryFor(cl);
                ReloadableType rtype = tr.getReloadableType(typeDescriptor.getName(), true);
                if (rtype != null) {
                    return new ReloadableTypeMethodProvider(rtype);
                }
            }
            return create(class1);
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException("We have a type descriptor for '" + typeDescriptor.getName() + " but no corresponding Java class", e);
        }
    } catch (RuntimeException re) {
        re.printStackTrace();
        throw re;
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Type(org.objectweb.asm.Type) ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry)

Example 27 with Type

use of org.objectweb.asm.Type in project es6draft by anba.

the class SimpleTypeTextifier method visitInvokeDynamicInsn.

@Override
public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) {
    buf.setLength(0);
    buf.append(tab2).append("INVOKEDYNAMIC").append(' ').append(name);
    appendDescriptor(METHOD_DESCRIPTOR, desc);
    if (bsmArgs.length != 0) {
        buf.append(" [");
        for (int i = 0; i < bsmArgs.length; ++i) {
            Object arg = bsmArgs[i];
            if (arg instanceof String) {
                appendString(buf, (String) arg);
            } else if (arg instanceof Type) {
                Type type = (Type) arg;
                if (type.getSort() == Type.METHOD) {
                    appendDescriptor(METHOD_DESCRIPTOR, type.getDescriptor());
                } else {
                    appendDescriptor(INTERNAL_NAME, type.getDescriptor());
                }
            } else if (arg instanceof Handle) {
                Handle handle = (Handle) arg;
                appendDescriptor(HANDLE_DESCRIPTOR, getMethodDescriptor(handle.getDesc()));
            } else {
                buf.append(arg);
            }
            if (i + 1 < bsmArgs.length) {
                buf.append(", ");
            }
        }
        buf.append("]");
    }
    buf.append("\n");
    text.add(buf.toString());
}
Also used : Type(org.objectweb.asm.Type) Handle(org.objectweb.asm.Handle)

Example 28 with Type

use of org.objectweb.asm.Type in project groovy by apache.

the class ProxyGeneratorAdapter method visitMethod.

@Override
public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) {
    Object key = Arrays.asList(name, desc);
    if (visitedMethods.contains(key))
        return null;
    if (Modifier.isPrivate(access) || Modifier.isNative(access) || ((access & ACC_SYNTHETIC) != 0)) {
        // do not generate bytecode for private methods
        return null;
    }
    int accessFlags = access;
    visitedMethods.add(key);
    if ((objectDelegateMethods.contains(name + desc) || delegatedClosures.containsKey(name) || (!"<init>".equals(name) && hasWildcard)) && !Modifier.isStatic(access) && !Modifier.isFinal(access)) {
        if (!GROOVYOBJECT_METHOD_NAMESS.contains(name)) {
            if (Modifier.isAbstract(access)) {
                // prevents the proxy from being abstract
                accessFlags -= ACC_ABSTRACT;
            }
            if (delegatedClosures.containsKey(name) || (!"<init>".equals(name) && hasWildcard)) {
                delegatedClosures.put(name, Boolean.TRUE);
                return makeDelegateToClosureCall(name, desc, signature, exceptions, accessFlags);
            }
            if (generateDelegateField && objectDelegateMethods.contains(name + desc)) {
                return makeDelegateCall(name, desc, signature, exceptions, accessFlags);
            }
            delegatedClosures.put(name, Boolean.TRUE);
            return makeDelegateToClosureCall(name, desc, signature, exceptions, accessFlags);
        }
    } else if ("getProxyTarget".equals(name) && "()Ljava/lang/Object;".equals(desc)) {
        return createGetProxyTargetMethod(access, name, desc, signature, exceptions);
    } else if ("<init>".equals(name) && (Modifier.isPublic(access) || Modifier.isProtected(access))) {
        return createConstructor(access, name, desc, signature, exceptions);
    } else if (Modifier.isAbstract(access) && !GROOVYOBJECT_METHOD_NAMESS.contains(name)) {
        if (isImplemented(superClass, name, desc)) {
            return null;
        }
        accessFlags -= ACC_ABSTRACT;
        MethodVisitor mv = super.visitMethod(accessFlags, name, desc, signature, exceptions);
        mv.visitCode();
        Type[] args = Type.getArgumentTypes(desc);
        if (emptyBody) {
            Type returnType = Type.getReturnType(desc);
            if (returnType == Type.VOID_TYPE) {
                mv.visitInsn(RETURN);
            } else {
                int loadIns = getLoadInsn(returnType);
                switch(loadIns) {
                    case ILOAD:
                        mv.visitInsn(ICONST_0);
                        break;
                    case LLOAD:
                        mv.visitInsn(LCONST_0);
                        break;
                    case FLOAD:
                        mv.visitInsn(FCONST_0);
                        break;
                    case DLOAD:
                        mv.visitInsn(DCONST_0);
                        break;
                    default:
                        mv.visitInsn(ACONST_NULL);
                }
                mv.visitInsn(getReturnInsn(returnType));
                mv.visitMaxs(2, registerLen(args) + 1);
            }
        } else {
            // for compatibility with the legacy proxy generator, we should throw an UnsupportedOperationException
            // instead of an AbtractMethodException
            mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException");
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>", "()V", false);
            mv.visitInsn(ATHROW);
            mv.visitMaxs(2, registerLen(args) + 1);
        }
        mv.visitEnd();
    }
    return null;
}
Also used : Type(org.objectweb.asm.Type) GroovyObject(groovy.lang.GroovyObject) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 29 with Type

use of org.objectweb.asm.Type in project groovy by apache.

the class ProxyGeneratorAdapter method makeDelegateToClosureCall.

protected MethodVisitor makeDelegateToClosureCall(final String name, final String desc, final String signature, final String[] exceptions, final int accessFlags) {
    MethodVisitor mv = super.visitMethod(accessFlags, name, desc, signature, exceptions);
    //        TraceMethodVisitor tmv = new TraceMethodVisitor(mv);
    //        mv = tmv;
    mv.visitCode();
    int stackSize = 0;
    // method body should be:
    //  this.$delegate$closure$methodName.call(new Object[] { method arguments })
    Type[] args = Type.getArgumentTypes(desc);
    int arrayStore = args.length + 1;
    BytecodeHelper.pushConstant(mv, args.length);
    // stack size = 1
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    stackSize = 1;
    int idx = 1;
    for (int i = 0; i < args.length; i++) {
        Type arg = args[i];
        // stack size = 2
        mv.visitInsn(DUP);
        // array index, stack size = 3
        BytecodeHelper.pushConstant(mv, i);
        stackSize = 3;
        // primitive types must be boxed
        if (isPrimitive(arg)) {
            mv.visitIntInsn(getLoadInsn(arg), idx);
            String wrappedType = getWrappedClassDescriptor(arg);
            mv.visitMethodInsn(INVOKESTATIC, wrappedType, "valueOf", "(" + arg.getDescriptor() + ")L" + wrappedType + ";", false);
        } else {
            // load argument i
            mv.visitVarInsn(ALOAD, idx);
        }
        idx += registerLen(arg);
        stackSize = Math.max(4, 3 + registerLen(arg));
        // store value into array
        mv.visitInsn(AASTORE);
    }
    // store array
    mv.visitVarInsn(ASTORE, arrayStore);
    int arrayIndex = arrayStore;
    // load this
    mv.visitVarInsn(ALOAD, 0);
    // load closure map
    mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;");
    // load method name
    mv.visitLdcInsn(name);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    arrayStore++;
    mv.visitVarInsn(ASTORE, arrayStore);
    // if null, test if wildcard exists
    Label notNull = new Label();
    mv.visitIntInsn(ALOAD, arrayStore);
    mv.visitJumpInsn(IFNONNULL, notNull);
    // load this
    mv.visitVarInsn(ALOAD, 0);
    // load closure map
    mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;");
    // load wildcard
    mv.visitLdcInsn("*");
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    mv.visitVarInsn(ASTORE, arrayStore);
    mv.visitLabel(notNull);
    mv.visitVarInsn(ALOAD, arrayStore);
    mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(this.getClass()), "ensureClosure", "(Ljava/lang/Object;)Lgroovy/lang/Closure;", false);
    // load argument array
    mv.visitVarInsn(ALOAD, arrayIndex);
    stackSize++;
    // call closure
    mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Closure", "call", "([Ljava/lang/Object;)Ljava/lang/Object;", false);
    unwrapResult(mv, desc);
    mv.visitMaxs(stackSize, arrayStore + 1);
    mv.visitEnd();
    //        System.out.println("tmv.getText() = " + tmv.getText());
    return null;
}
Also used : Type(org.objectweb.asm.Type) Label(org.objectweb.asm.Label) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 30 with Type

use of org.objectweb.asm.Type in project groovy by apache.

the class ProxyGeneratorAdapter method makeDelegateCall.

/**
     * Generate a call to the delegate object.
     */
protected MethodVisitor makeDelegateCall(final String name, final String desc, final String signature, final String[] exceptions, final int accessFlags) {
    MethodVisitor mv = super.visitMethod(accessFlags, name, desc, signature, exceptions);
    // load this
    mv.visitVarInsn(ALOAD, 0);
    // load delegate
    mv.visitFieldInsn(GETFIELD, proxyName, DELEGATE_OBJECT_FIELD, BytecodeHelper.getTypeDescription(delegateClass));
    // using InvokerHelper to allow potential intercepted calls
    int size;
    // method name
    mv.visitLdcInsn(name);
    Type[] args = Type.getArgumentTypes(desc);
    BytecodeHelper.pushConstant(mv, args.length);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    size = 6;
    int idx = 1;
    for (int i = 0; i < args.length; i++) {
        Type arg = args[i];
        mv.visitInsn(DUP);
        BytecodeHelper.pushConstant(mv, i);
        // primitive types must be boxed
        if (isPrimitive(arg)) {
            mv.visitIntInsn(getLoadInsn(arg), idx);
            String wrappedType = getWrappedClassDescriptor(arg);
            mv.visitMethodInsn(INVOKESTATIC, wrappedType, "valueOf", "(" + arg.getDescriptor() + ")L" + wrappedType + ";", false);
        } else {
            // load argument i
            mv.visitVarInsn(ALOAD, idx);
        }
        size = Math.max(size, 5 + registerLen(arg));
        idx += registerLen(arg);
        // store value into array
        mv.visitInsn(AASTORE);
    }
    mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/InvokerHelper", "invokeMethod", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;", false);
    unwrapResult(mv, desc);
    mv.visitMaxs(size, registerLen(args) + 1);
    return mv;
}
Also used : Type(org.objectweb.asm.Type) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

Type (org.objectweb.asm.Type)185 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)39 MethodVisitor (org.objectweb.asm.MethodVisitor)34 Method (org.objectweb.asm.commons.Method)28 Label (org.objectweb.asm.Label)27 ClassWriter (org.objectweb.asm.ClassWriter)16 Method (java.lang.reflect.Method)13 ArrayList (java.util.ArrayList)12 ClassReader (org.objectweb.asm.ClassReader)10 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)9 ClassVisitor (org.objectweb.asm.ClassVisitor)9 ExprString (lucee.transformer.expression.ExprString)8 ModelType (org.gradle.model.internal.type.ModelType)7 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)6 IOException (java.io.IOException)6 MethodType (java.lang.invoke.MethodType)6 HashMap (java.util.HashMap)6 List (java.util.List)6 LitString (lucee.transformer.expression.literal.LitString)6 PropertyAccessorType (org.gradle.internal.reflect.PropertyAccessorType)6