Search in sources :

Example 1 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 2 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)

Example 3 with Method

use of org.objectweb.asm.commons.Method in project Lucee by lucee.

the class ComponentUtil method createMethod.

private static int createMethod(ConstrBytecodeContext constr, java.util.List<LitString> keys, ClassWriter cw, String className, Object member, int max, boolean writeLog, boolean suppressWSbeforeArg, boolean output, boolean returnValue) throws PageException {
    boolean hasOptionalArgs = false;
    if (member instanceof UDF) {
        UDF udf = (UDF) member;
        FunctionArgument[] args = udf.getFunctionArguments();
        Type[] types = new Type[max < 0 ? args.length : max];
        for (int y = 0; y < types.length; y++) {
            types[y] = toType(args[y].getTypeAsString(), true);
            if (!args[y].isRequired())
                hasOptionalArgs = true;
        }
        Type rtnType = toType(udf.getReturnTypeAsString(), true);
        Method method = new Method(udf.getFunctionName(), rtnType, types);
        GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, method, null, null, cw);
        BytecodeContext bc = new BytecodeContext(null, constr, getPage(constr), keys, cw, className, adapter, method, writeLog, suppressWSbeforeArg, output, returnValue);
        Label start = adapter.newLabel();
        adapter.visitLabel(start);
        // ComponentController.invoke(name, args);
        // name
        adapter.push(udf.getFunctionName());
        // args
        ArrayVisitor av = new ArrayVisitor();
        av.visitBegin(adapter, Types.OBJECT, types.length);
        for (int y = 0; y < types.length; y++) {
            av.visitBeginItem(adapter, y);
            adapter.loadArg(y);
            av.visitEndItem(bc.getAdapter());
        }
        av.visitEnd();
        adapter.invokeStatic(Types.COMPONENT_CONTROLLER, INVOKE);
        adapter.checkCast(rtnType);
        // ASMConstants.NULL(adapter);
        adapter.returnValue();
        Label end = adapter.newLabel();
        adapter.visitLabel(end);
        for (int y = 0; y < types.length; y++) {
            adapter.visitLocalVariable(args[y].getName().getString(), types[y].getDescriptor(), null, start, end, y + 1);
        }
        adapter.endMethod();
        if (hasOptionalArgs) {
            if (max == -1)
                max = args.length - 1;
            else
                max--;
            return max;
        }
    }
    return -1;
}
Also used : Type(org.objectweb.asm.Type) UDF(lucee.runtime.type.UDF) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) ArrayVisitor(lucee.transformer.bytecode.visitor.ArrayVisitor) FunctionArgument(lucee.runtime.type.FunctionArgument) BytecodeContext(lucee.transformer.bytecode.BytecodeContext) ConstrBytecodeContext(lucee.transformer.bytecode.ConstrBytecodeContext)

Example 4 with Method

use of org.objectweb.asm.commons.Method in project Lucee by lucee.

the class BodyBase method writeOut.

public static void writeOut(final BytecodeContext bc, List<Statement> statements) throws TransformerException {
    GeneratorAdapter adapter = bc.getAdapter();
    boolean isOutsideMethod;
    GeneratorAdapter a = null;
    Method m;
    BytecodeContext _bc = bc;
    Iterator<Statement> it = statements.iterator();
    boolean split = bc.getPage().getSplitIfNecessary();
    // int lastLine=-1;
    while (it.hasNext()) {
        isOutsideMethod = bc.getMethod().getReturnType().equals(Types.VOID);
        Statement s = it.next();
        if (split && _bc.incCount() > MAX_STATEMENTS && bc.doSubFunctions() && (isOutsideMethod || !s.hasFlowController()) && s.getStart() != null) {
            if (a != null) {
                a.returnValue();
                a.endMethod();
            }
            // ExpressionUtil.visitLine(bc, s.getLine());
            String method = ASMUtil.createOverfowMethod(bc.getMethod().getName(), bc.getPage().getMethodCount());
            ExpressionUtil.visitLine(bc, s.getStart());
            // ExpressionUtil.lastLine(bc);
            m = new Method(method, Types.VOID, new Type[] { Types.PAGE_CONTEXT });
            a = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, m, null, new Type[] { Types.THROWABLE }, bc.getClassWriter());
            _bc = new BytecodeContext(bc.getConstructor(), bc.getKeys(), bc, a, m);
            if (bc.getRoot() != null)
                _bc.setRoot(bc.getRoot());
            else
                _bc.setRoot(bc);
            adapter.visitVarInsn(Opcodes.ALOAD, 0);
            adapter.visitVarInsn(Opcodes.ALOAD, 1);
            adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, bc.getClassName(), method, "(Llucee/runtime/PageContext;)V");
        }
        if (_bc != bc && s.hasFlowController()) {
            if (a != null) {
                a.returnValue();
                a.endMethod();
            }
            _bc = bc;
            a = null;
        }
        ExpressionUtil.writeOut(s, _bc);
    }
    if (a != null) {
        a.returnValue();
        a.endMethod();
    }
}
Also used : Type(org.objectweb.asm.Type) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) LitString(lucee.transformer.expression.literal.LitString)

Example 5 with Method

use of org.objectweb.asm.commons.Method in project Lucee by lucee.

the class BodyBase method addToSubMethod.

private static void addToSubMethod(BytecodeContext bc, Statement... statements) throws TransformerException {
    if (statements == null || statements.length == 0)
        return;
    GeneratorAdapter adapter = bc.getAdapter();
    String method = ASMUtil.createOverfowMethod(bc.getMethod().getName(), bc.getPage().getMethodCount());
    for (int i = 0; i < statements.length; i++) {
        if (statements[i].getStart() != null) {
            ExpressionUtil.visitLine(bc, statements[i].getStart());
            break;
        }
    }
    // ExpressionUtil.lastLine(bc);
    Method m = new Method(method, Types.VOID, new Type[] { Types.PAGE_CONTEXT });
    GeneratorAdapter a = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, m, null, new Type[] { Types.THROWABLE }, bc.getClassWriter());
    BytecodeContext _bc = new BytecodeContext(bc.getConstructor(), bc.getKeys(), bc, a, m);
    if (bc.getRoot() != null)
        _bc.setRoot(bc.getRoot());
    else
        _bc.setRoot(bc);
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitVarInsn(Opcodes.ALOAD, 1);
    adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, bc.getClassName(), method, "(Llucee/runtime/PageContext;)V");
    for (int i = 0; i < statements.length; i++) {
        ExpressionUtil.writeOut(statements[i], _bc);
    }
    a.returnValue();
    a.endMethod();
}
Also used : GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) LitString(lucee.transformer.expression.literal.LitString) Method(org.objectweb.asm.commons.Method)

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