Search in sources :

Example 66 with Method

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

the class AsmDeltaSpikeProxyClassGenerator method defineDeltaSpikeProxyMethods.

private static void defineDeltaSpikeProxyMethods(ClassWriter cw, Type proxyType) {
    try {
        // implement #setInvocationHandler
        Method asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("setInvocationHandler", DeltaSpikeProxyInvocationHandler.class));
        GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
        mg.visitCode();
        mg.loadThis();
        mg.loadArg(0);
        mg.checkCast(TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER);
        mg.putField(proxyType, FIELDNAME_INVOCATION_HANDLER, TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER);
        mg.returnValue();
        mg.visitMaxs(2, 1);
        mg.visitEnd();
        // implement #getInvocationHandler
        asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("getInvocationHandler"));
        mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
        mg.visitCode();
        mg.loadThis();
        mg.getField(proxyType, FIELDNAME_INVOCATION_HANDLER, TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER);
        mg.returnValue();
        mg.visitMaxs(2, 1);
        mg.visitEnd();
        // implement #setDelegateInvocationHandler
        asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("setDelegateInvocationHandler", InvocationHandler.class));
        mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
        mg.visitCode();
        mg.loadThis();
        mg.loadArg(0);
        mg.checkCast(TYPE_INVOCATION_HANDLER);
        mg.putField(proxyType, FIELDNAME_DELEGATE_INVOCATION_HANDLER, TYPE_INVOCATION_HANDLER);
        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, TYPE_INVOCATION_HANDLER);
        mg.returnValue();
        mg.visitMaxs(2, 1);
        mg.visitEnd();
        // implement #setDelegateMethods
        asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("setDelegateMethods", java.lang.reflect.Method[].class));
        mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
        mg.visitCode();
        mg.loadThis();
        mg.loadArg(0);
        mg.checkCast(TYPE_METHOD_ARRAY);
        mg.putField(proxyType, FIELDNAME_DELEGATE_METHODS, TYPE_METHOD_ARRAY);
        mg.returnValue();
        mg.visitMaxs(2, 1);
        mg.visitEnd();
        // implement #getDelegateMethods
        asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("getDelegateMethods"));
        mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
        mg.visitCode();
        mg.loadThis();
        mg.getField(proxyType, FIELDNAME_DELEGATE_METHODS, TYPE_METHOD_ARRAY);
        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) DeltaSpikeProxyInvocationHandler(org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Example 67 with Method

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

the class AsmDeltaSpikeProxyClassGenerator method defineMethod.

private static void defineMethod(ClassWriter cw, java.lang.reflect.Method method, Type proxyType) {
    Type methodType = Type.getType(method);
    ArrayList<Type> exceptionsToCatch = new ArrayList<Type>();
    for (Class<?> exception : method.getExceptionTypes()) {
        if (!RuntimeException.class.isAssignableFrom(exception)) {
            exceptionsToCatch.add(Type.getType(exception));
        }
    }
    // push the method definition
    int modifiers = (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED) & method.getModifiers();
    Method asmMethod = Method.getMethod(method);
    GeneratorAdapter mg = new GeneratorAdapter(modifiers, asmMethod, null, getTypes(method.getExceptionTypes()), cw);
    // copy annotations
    for (Annotation annotation : method.getDeclaredAnnotations()) {
        mg.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true).visitEnd();
    }
    mg.visitCode();
    Label tryBlockStart = mg.mark();
    mg.loadThis();
    mg.getField(proxyType, FIELDNAME_INVOCATION_HANDLER, TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER);
    mg.loadThis();
    loadCurrentMethod(mg, method, methodType);
    loadArguments(mg, method, methodType);
    mg.invokeVirtual(TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER, Method.getMethod("Object invoke(Object, java.lang.reflect.Method, Object[])"));
    // cast the result
    mg.unbox(methodType.getReturnType());
    // build try catch
    Label tryBlockEnd = mg.mark();
    // push return
    mg.returnValue();
    // catch runtime exceptions and rethrow it
    Label rethrow = mg.mark();
    mg.visitVarInsn(Opcodes.ASTORE, 1);
    mg.visitVarInsn(Opcodes.ALOAD, 1);
    mg.throwException();
    mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, Type.getInternalName(RuntimeException.class));
    // catch checked exceptions and rethrow it
    boolean throwableCatched = false;
    if (!exceptionsToCatch.isEmpty()) {
        rethrow = mg.mark();
        mg.visitVarInsn(Opcodes.ASTORE, 1);
        mg.visitVarInsn(Opcodes.ALOAD, 1);
        mg.throwException();
        // catch declared exceptions and rethrow it...
        for (Type exceptionType : exceptionsToCatch) {
            if (exceptionType.getClassName().equals(Throwable.class.getName())) {
                throwableCatched = true;
            }
            mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, exceptionType.getInternalName());
        }
    }
    // if throwable isn't alreached cachted, catch it and wrap it with an UndeclaredThrowableException and throw it
    if (!throwableCatched) {
        Type uteType = Type.getType(UndeclaredThrowableException.class);
        Label wrapAndRethrow = mg.mark();
        mg.visitVarInsn(Opcodes.ASTORE, 1);
        mg.newInstance(uteType);
        mg.dup();
        mg.visitVarInsn(Opcodes.ALOAD, 1);
        mg.invokeConstructor(uteType, Method.getMethod("void <init>(java.lang.Throwable)"));
        mg.throwException();
        mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, wrapAndRethrow, Type.getInternalName(Throwable.class));
    }
    // finish the method
    mg.endMethod();
    mg.visitMaxs(12, 12);
    mg.visitEnd();
}
Also used : Type(org.objectweb.asm.Type) ArrayList(java.util.ArrayList) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) Annotation(java.lang.annotation.Annotation)

Example 68 with Method

use of org.objectweb.asm.commons.Method in project Payara by payara.

the class ProviderImplGenerator method generateConstructor.

private void generateConstructor(ClassWriter cw, String generatedClassName, FlashlightProbeProvider provider) {
    Method m = Method.getMethod("void <init> ()");
    GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, cw);
    gen.loadThis();
    gen.invokeConstructor(Type.getType(Object.class), m);
    Type probeRegType = Type.getType(ProbeRegistry.class);
    Type probeType = Type.getType(FlashlightProbe.class);
    gen.loadThis();
    for (FlashlightProbe probe : provider.getProbes()) {
        gen.dup();
        String fieldName = "_flashlight_" + probe.getProbeName();
        gen.push(probe.getId());
        gen.invokeStatic(probeRegType, Method.getMethod("org.glassfish.flashlight.provider.FlashlightProbe getProbeById(int)"));
        gen.visitFieldInsn(Opcodes.PUTFIELD, generatedClassName, fieldName, probeType.getDescriptor());
    }
    gen.pop();
    // return the value from constructor
    gen.returnValue();
    gen.endMethod();
}
Also used : Type(org.objectweb.asm.Type) FlashlightProbe(org.glassfish.flashlight.provider.FlashlightProbe) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Example 69 with Method

use of org.objectweb.asm.commons.Method in project Payara by payara.

the class BtraceClientGenerator method generateConstructor.

private static void generateConstructor(ClassWriter cw) {
    Method m = Method.getMethod("void <init> ()");
    GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, cw);
    gen.loadThis();
    gen.invokeConstructor(Type.getType(Object.class), m);
    // return the value from constructor
    gen.returnValue();
    gen.endMethod();
}
Also used : GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Example 70 with Method

use of org.objectweb.asm.commons.Method in project robolectric by robolectric.

the class ClassInstrumentor method instrumentConstructor.

/**
 * Constructors are instrumented as follows:
 *
 * <ul>
 *   <li>Code other than a call to the superclass constructor is moved to a new method named
 *       {@code __constructor__} with the same signature.
 *   <li>The constructor is modified to call {@link ClassHandler#initializing(Object)} (or {@link
 *       ClassHandler#getShadowCreator(Class)} for {@code invokedynamic} JVMs).
 *   <li>The constructor is modified to then call {@link ClassHandler#methodInvoked(String,
 *       boolean, Class)} (or {@link ClassHandler#findShadowMethodHandle(Class, String,
 *       MethodType, boolean)} for {@code invokedynamic} JVMs) with the method name {@code
 *       __constructor__} and the same parameter types.
 * </ul>
 *
 * Note that most code in the constructor will not be executed unless the {@link ClassHandler}
 * arranges for it to happen.
 *
 * <p>Given a constructor like this:
 *
 * <pre>
 * public ThisClass(String name, int size) {
 *   super(name, someStaticMethod());
 *   this.size = size;
 * }
 * </pre>
 *
 * ... generates code like this:
 *
 * <pre>
 * private $$robo$$__constructor__(String name, int size) {
 *   this.size = size;
 * }
 *
 * private __constructor__(String name, int size) {
 *   Plan plan = RobolectricInternals.methodInvoked(
 *       "pkg/ThisClass/__constructor__(Ljava/lang/String;I)V", true, ThisClass.class);
 *   if (plan != null) {
 *     try {
 *       plan.run(this, new Object[] {name, size});
 *     } catch (Throwable t) {
 *       throw RobolectricInternals.cleanStackTrace(t);
 *     }
 *   } else {
 *     $$robo$$__constructor__(name, size);
 *   }
 * }
 *
 * public ThisClass(String name, int size) {
 *   super(name, someStaticMethod());
 *   $$robo$init();
 * }
 * </pre>
 *
 * @param method the constructor to instrument
 */
private void instrumentConstructor(MutableClass mutableClass, MethodNode method) {
    makeMethodPrivate(method);
    InsnList callSuper = extractCallToSuperConstructor(mutableClass, method);
    method.name = directMethodName(mutableClass, ShadowConstants.CONSTRUCTOR_METHOD_NAME);
    mutableClass.addMethod(redirectorMethod(mutableClass, method, ShadowConstants.CONSTRUCTOR_METHOD_NAME));
    String[] exceptions = exceptionArray(method);
    MethodNode initMethodNode = new MethodNode(method.access, "<init>", method.desc, method.signature, exceptions);
    makeMethodPublic(initMethodNode);
    RobolectricGeneratorAdapter generator = new RobolectricGeneratorAdapter(initMethodNode);
    initMethodNode.instructions = callSuper;
    generator.loadThis();
    generator.invokeVirtual(mutableClass.classType, new Method(ROBO_INIT_METHOD_NAME, "()V"));
    generateClassHandlerCall(mutableClass, method, ShadowConstants.CONSTRUCTOR_METHOD_NAME, generator);
    generator.endMethod();
    mutableClass.addMethod(initMethodNode);
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode) Method(org.objectweb.asm.commons.Method) InsnList(org.objectweb.asm.tree.InsnList)

Aggregations

Method (org.objectweb.asm.commons.Method)78 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)58 Type (org.objectweb.asm.Type)38 ClassWriter (org.objectweb.asm.ClassWriter)14 Label (org.objectweb.asm.Label)14 MethodVisitor (org.objectweb.asm.MethodVisitor)9 IOException (java.io.IOException)7 ClassReader (org.objectweb.asm.ClassReader)7 TypeInfo (com.google.template.soy.jbcsrc.restricted.TypeInfo)6 MethodNode (org.objectweb.asm.tree.MethodNode)6 ExprString (lucee.transformer.expression.ExprString)5 LitString (lucee.transformer.expression.literal.LitString)5 ClassVisitor (org.objectweb.asm.ClassVisitor)5 Schema (co.cask.cdap.api.data.schema.Schema)4 ArrayList (java.util.ArrayList)4 File (java.io.File)3 Set (java.util.Set)3 Nullable (javax.annotation.Nullable)3 ExprDouble (lucee.transformer.expression.ExprDouble)3 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)2