Search in sources :

Example 6 with Method

use of org.objectweb.asm.commons.Method in project aries by apache.

the class AbstractWovenProxyAdapter method writeFinalWovenProxyMethods.

/**
   * Write the methods we need for wovenProxies on the highest supertype
   */
private final void writeFinalWovenProxyMethods() {
    // add private fields for the Callable<Object> dispatcher
    // and InvocationListener. These aren't static because we can have
    // multiple instances of the same proxy class. These should not be
    // serialized, or used in JPA or any other thing we can think of,
    // so we annotate them as necessary
    generateField(DISPATCHER_FIELD, Type.getDescriptor(Callable.class));
    generateField(LISTENER_FIELD, Type.getDescriptor(InvocationListener.class));
    // a general methodAdapter field that we will use to with GeneratorAdapters
    // to create the methods required to implement WovenProxy
    GeneratorAdapter methodAdapter;
    // add a method for unwrapping the dispatcher
    methodAdapter = getMethodGenerator(PUBLIC_GENERATED_METHOD_ACCESS, new Method("org_apache_aries_proxy_weaving_WovenProxy_unwrap", DISPATCHER_TYPE, NO_ARGS));
    // /////////////////////////////////////////////////////
    // Implement the method
    // load this to get the field
    methodAdapter.loadThis();
    // get the dispatcher field and return
    methodAdapter.getField(typeBeingWoven, DISPATCHER_FIELD, DISPATCHER_TYPE);
    methodAdapter.returnValue();
    methodAdapter.endMethod();
    // /////////////////////////////////////////////////////
    // add a method for checking if the dispatcher is set
    methodAdapter = getMethodGenerator(PUBLIC_GENERATED_METHOD_ACCESS, new Method("org_apache_aries_proxy_weaving_WovenProxy_isProxyInstance", Type.BOOLEAN_TYPE, NO_ARGS));
    // /////////////////////////////////////////////////////
    // Implement the method
    // load this to get the field
    methodAdapter.loadThis();
    // make a label for return true
    Label returnTrueLabel = methodAdapter.newLabel();
    // get the dispatcher field for the stack
    methodAdapter.getField(typeBeingWoven, DISPATCHER_FIELD, DISPATCHER_TYPE);
    // check if the dispatcher was non-null and goto return true if it was
    methodAdapter.ifNonNull(returnTrueLabel);
    methodAdapter.loadThis();
    // get the listener field for the stack
    methodAdapter.getField(typeBeingWoven, LISTENER_FIELD, LISTENER_TYPE);
    // check if the listener field was non-null and goto return true if it was
    methodAdapter.ifNonNull(returnTrueLabel);
    // return false if we haven't jumped anywhere
    methodAdapter.push(false);
    methodAdapter.returnValue();
    // mark the returnTrueLable
    methodAdapter.mark(returnTrueLabel);
    methodAdapter.push(true);
    methodAdapter.returnValue();
    // end the method
    methodAdapter.endMethod();
// ///////////////////////////////////////////////////////
}
Also used : InvocationListener(org.apache.aries.proxy.InvocationListener) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) Callable(java.util.concurrent.Callable)

Example 7 with Method

use of org.objectweb.asm.commons.Method in project aries by apache.

the class AbstractWovenProxyMethodAdapter method unwrapEqualsArgument.

/**
   * This method unwraps woven proxy instances for use in the right-hand side
   * of equals methods
   */
protected final void unwrapEqualsArgument() {
    //Create and initialize a local for our work
    int unwrapLocal = newLocal(OBJECT_TYPE);
    visitInsn(ACONST_NULL);
    storeLocal(unwrapLocal);
    Label startUnwrap = newLabel();
    mark(startUnwrap);
    //Load arg and check if it is a WovenProxy instances
    loadArg(0);
    instanceOf(WOVEN_PROXY_IFACE_TYPE);
    Label unwrapFinished = newLabel();
    //Jump if zero (false)
    visitJumpInsn(Opcodes.IFEQ, unwrapFinished);
    //Arg is a wovenProxy, if it is the same as last time then we're done
    loadLocal(unwrapLocal);
    loadArg(0);
    ifCmp(OBJECT_TYPE, EQ, unwrapFinished);
    //Not the same, store current arg in unwrapLocal for next loop
    loadArg(0);
    storeLocal(unwrapLocal);
    //So arg is a WovenProxy, but not the same as last time, cast it and store 
    //the result of unwrap.call in the arg
    loadArg(0);
    checkCast(WOVEN_PROXY_IFACE_TYPE);
    //Now unwrap
    invokeInterface(WOVEN_PROXY_IFACE_TYPE, new Method("org_apache_aries_proxy_weaving_WovenProxy_unwrap", DISPATCHER_TYPE, NO_ARGS));
    //Now we may have a Callable to invoke
    int callable = newLocal(DISPATCHER_TYPE);
    storeLocal(callable);
    loadLocal(callable);
    ifNull(unwrapFinished);
    loadLocal(callable);
    invokeInterface(DISPATCHER_TYPE, new Method("call", OBJECT_TYPE, NO_ARGS));
    //Store the result and we're done (for this iteration)
    storeArg(0);
    goTo(startUnwrap);
    mark(unwrapFinished);
}
Also used : Label(org.objectweb.asm.Label) Method(org.objectweb.asm.commons.Method)

Example 8 with Method

use of org.objectweb.asm.commons.Method in project aries by apache.

the class TCCLSetterVisitor method visitEnd.

@Override
public void visitEnd() {
    if (!woven) {
        // if this class wasn't woven, then don't add the synthesized method either.
        super.visitEnd();
        return;
    }
    // Add generated static method
    Set<String> methodNames = new HashSet<String>();
    for (WeavingData wd : weavingData) {
        /* Equivalent to:
             * private static void $$FCCL$$<className>$<methodName>(Class<?> cls) {
             *   Util.fixContextClassLoader("java.util.ServiceLoader", "load", cls, WovenClass.class.getClassLoader());
             * }
             */
        String methodName = getGeneratedMethodName(wd);
        if (methodNames.contains(methodName))
            continue;
        methodNames.add(methodName);
        Method method = new Method(methodName, Type.VOID_TYPE, new Type[] { CLASS_TYPE });
        GeneratorAdapter mv = new GeneratorAdapter(cv.visitMethod(ACC_PRIVATE + ACC_STATIC, methodName, method.getDescriptor(), null, null), ACC_PRIVATE + ACC_STATIC, methodName, method.getDescriptor());
        //Load the strings, method parameter and target
        mv.visitLdcInsn(wd.getClassName());
        mv.visitLdcInsn(wd.getMethodName());
        mv.loadArg(0);
        mv.visitLdcInsn(targetClass);
        //Change the class on the stack into a classloader
        mv.invokeVirtual(CLASS_TYPE, new Method("getClassLoader", CLASSLOADER_TYPE, new Type[0]));
        //Call our util method
        mv.invokeStatic(UTIL_CLASS, new Method("fixContextClassloader", Type.VOID_TYPE, new Type[] { String_TYPE, String_TYPE, CLASS_TYPE, CLASSLOADER_TYPE }));
        mv.returnValue();
        mv.endMethod();
    }
    super.visitEnd();
}
Also used : Type(org.objectweb.asm.Type) WeavingData(org.apache.aries.spifly.WeavingData) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) HashSet(java.util.HashSet)

Example 9 with Method

use of org.objectweb.asm.commons.Method in project deltaspike by apache.

the class AsmProxyClassGenerator method defineDefaultConstructor.

private static void defineDefaultConstructor(ClassWriter cw, Type proxyType, Type superType) {
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, new Method("<init>", Type.VOID_TYPE, new Type[] {}), null, null, cw);
    mg.visitCode();
    // invoke super constructor
    mg.loadThis();
    mg.invokeConstructor(superType, Method.getMethod("void <init> ()"));
    mg.returnValue();
    mg.endMethod();
    mg.visitEnd();
}
Also used : Type(org.objectweb.asm.Type) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Example 10 with Method

use of org.objectweb.asm.commons.Method in project deltaspike by apache.

the class AsmProxyClassGenerator method defineDeltaSpikeProxyMethods.

private static void defineDeltaSpikeProxyMethods(ClassWriter cw, Type proxyType, Type delegateInvocationHandlerType) {
    try {
        // implement #setDelegateInvocationHandler
        Method asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("setDelegateInvocationHandler", InvocationHandler.class));
        GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
        mg.visitCode();
        mg.loadThis();
        mg.loadArg(0);
        mg.checkCast(delegateInvocationHandlerType);
        mg.putField(proxyType, FIELDNAME_DELEGATE_INVOCATION_HANDLER, delegateInvocationHandlerType);
        mg.returnValue();
        mg.visitMaxs(2, 1);
        mg.visitEnd();
        // implement #getDelegateInvocationHandler
        asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("getDelegateInvocationHandler"));
        mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
        mg.visitCode();
        mg.loadThis();
        mg.getField(proxyType, FIELDNAME_DELEGATE_INVOCATION_HANDLER, delegateInvocationHandlerType);
        mg.returnValue();
        mg.visitMaxs(2, 1);
        mg.visitEnd();
    } catch (NoSuchMethodException e) {
        throw new IllegalStateException("Unable to implement " + DeltaSpikeProxy.class.getName(), e);
    }
}
Also used : DeltaSpikeProxy(org.apache.deltaspike.proxy.spi.DeltaSpikeProxy) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) InterceptManualInvocationHandler(org.apache.deltaspike.proxy.impl.invocation.InterceptManualInvocationHandler) DelegateManualInvocationHandler(org.apache.deltaspike.proxy.impl.invocation.DelegateManualInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler)

Aggregations

Method (org.objectweb.asm.commons.Method)32 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)24 Type (org.objectweb.asm.Type)16 Label (org.objectweb.asm.Label)8 ClassWriter (org.objectweb.asm.ClassWriter)7 MethodVisitor (org.objectweb.asm.MethodVisitor)7 IOException (java.io.IOException)6 ClassReader (org.objectweb.asm.ClassReader)5 Schema (co.cask.cdap.api.data.schema.Schema)4 Set (java.util.Set)3 ClassVisitor (org.objectweb.asm.ClassVisitor)3 TypeToken (com.google.common.reflect.TypeToken)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 SchemaHash (co.cask.cdap.api.data.schema.SchemaHash)1 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)1 Encoder (co.cask.cdap.common.io.Encoder)1 Sets (com.google.common.collect.Sets)1 JClassType (com.google.gwt.core.ext.typeinfo.JClassType)1 InterceptorType (com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorType)1 File (java.io.File)1