Example usage for org.objectweb.asm.commons Method Method

List of usage examples for org.objectweb.asm.commons Method Method

Introduction

In this page you can find the example usage for org.objectweb.asm.commons Method Method.

Prototype

public Method(final String name, final Type returnType, final Type[] argumentTypes) 

Source Link

Document

Constructs a new Method .

Usage

From source file:io.datakernel.codegen.ExpressionCallStaticSelf.java

License:Apache License

@Override
public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();
    Type ownerType = owner.type(ctx);

    List<Type> argumentTypes = new ArrayList<>();
    for (Expression argument : arguments) {
        argument.load(ctx);/*from   w w  w  .  ja v a2 s. co m*/
        argumentTypes.add(argument.type(ctx));
    }

    Type returnType = type(ctx);
    g.invokeStatic(ownerType, new Method(methodName, returnType, argumentTypes.toArray(new Type[] {})));
    return returnType;
}

From source file:io.datakernel.codegen.ExpressionComparator.java

License:Apache License

@Override
public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();

    Label labelNe = new Label();

    for (int i = 0; i < left.size(); i++) {
        Type leftFieldType = left.get(i).load(ctx);
        Type rightFieldType = right.get(i).load(ctx);

        Preconditions.check(leftFieldType.equals(rightFieldType));
        if (isPrimitiveType(leftFieldType)) {
            g.invokeStatic(wrap(leftFieldType),
                    new Method("compare", INT_TYPE, new Type[] { leftFieldType, leftFieldType }));
            g.dup();/*  ww w. j a  va  2s  .  c  o  m*/
            g.ifZCmp(NE, labelNe);
            g.pop();
        } else {
            g.invokeVirtual(leftFieldType,
                    new Method("compareTo", INT_TYPE, new Type[] { Type.getType(Object.class) }));
            g.dup();
            g.ifZCmp(NE, labelNe);
            g.pop();
        }
    }

    g.push(0);

    g.mark(labelNe);

    return INT_TYPE;
}

From source file:io.datakernel.codegen.ExpressionComparatorNullable.java

License:Apache License

@Override
public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();

    Label labelReturn = new Label();

    for (int i = 0; i < left.size(); i++) {
        Type leftFieldType = left.get(i).load(ctx); // [left]
        Type rightFieldType = right.get(i).load(ctx); // [left, right]

        Preconditions.check(leftFieldType.equals(rightFieldType));
        if (isPrimitiveType(leftFieldType)) {
            g.invokeStatic(wrap(leftFieldType),
                    new Method("compare", INT_TYPE, new Type[] { leftFieldType, leftFieldType }));
            g.dup();//  w  w  w . ja va2s  .c om
            g.ifZCmp(NE, labelReturn);
            g.pop();
        } else {
            VarLocal varRight = newLocal(ctx, rightFieldType);
            varRight.store(ctx);

            VarLocal varLeft = newLocal(ctx, leftFieldType);
            varLeft.store(ctx);

            Label continueLabel = new Label();
            Label nonNulls = new Label();
            Label leftNonNull = new Label();

            varLeft.load(ctx);
            g.ifNonNull(leftNonNull);

            varRight.load(ctx);
            g.ifNull(continueLabel);
            g.push(-1);
            g.returnValue();

            g.mark(leftNonNull);

            varRight.load(ctx);
            g.ifNonNull(nonNulls);
            g.push(1);
            g.returnValue();

            g.mark(nonNulls);

            varLeft.load(ctx);
            varRight.load(ctx);

            g.invokeVirtual(leftFieldType,
                    new Method("compareTo", INT_TYPE, new Type[] { Type.getType(Object.class) }));
            g.dup();
            g.ifZCmp(NE, labelReturn);
            g.pop();

            g.mark(continueLabel);
        }
    }

    g.push(0);

    g.mark(labelReturn);

    return INT_TYPE;
}

From source file:io.datakernel.codegen.ExpressionToString.java

License:Apache License

@Override
public Type load(Context ctx) {
    final GeneratorAdapter g = ctx.getGeneratorAdapter();

    g.newInstance(getType(StringBuilder.class));
    g.dup();//from w ww .  java2  s.com
    g.invokeConstructor(getType(StringBuilder.class), getMethod("void <init> ()"));

    boolean first = true;

    for (Object key : arguments.keySet()) {
        String str = first ? begin : separator;
        first = false;
        if (key instanceof String) {
            str += key;
        }
        if (!str.isEmpty()) {
            g.dup();
            g.push(str);
            g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
            g.pop();
        }

        g.dup();
        final Expression expression = arguments.get(key);
        final Type type = expression.load(ctx);
        if (isPrimitiveType(type)) {
            g.invokeStatic(wrap(type), new Method("toString", getType(String.class), new Type[] { type }));
        } else {
            final Label nullLabel = new Label();
            final Label afterToString = new Label();
            g.dup();
            g.ifNull(nullLabel);
            g.invokeVirtual(type, getMethod("String toString()"));
            g.goTo(afterToString);
            g.mark(nullLabel);
            g.pop();
            g.push(("null"));
            g.mark(afterToString);
        }
        g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
        g.pop();
    }

    if (!end.isEmpty()) {
        g.dup();
        g.push(end);
        g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
        g.pop();
    }

    g.invokeVirtual(getType(StringBuilder.class), getMethod("String toString()"));
    return type(ctx);
}

From source file:io.datakernel.codegen.PredicateDefCmp.java

License:Apache License

@Override
public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();
    Label labelTrue = new Label();
    Label labelExit = new Label();

    Type leftFieldType = left.type(ctx);
    Preconditions.check(leftFieldType.equals(right.type(ctx)));
    left.load(ctx);//from  w  ww. j av a 2 s.  c  o m
    right.load(ctx);

    if (isPrimitiveType(leftFieldType)) {
        g.ifCmp(leftFieldType, operation.opCode, labelTrue);
    } else {
        if (operation == EQ || operation == NE) {
            g.invokeVirtual(leftFieldType,
                    new Method("equals", BOOLEAN_TYPE, new Type[] { Type.getType(Object.class) }));
            g.push(operation == EQ);
            g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.EQ, labelTrue);
        } else {
            g.invokeVirtual(leftFieldType,
                    new Method("compareTo", INT_TYPE, new Type[] { Type.getType(Object.class) }));
            if (operation == LT) {
                g.ifZCmp(GeneratorAdapter.LT, labelTrue);
            } else if (operation == GT) {
                g.ifZCmp(GeneratorAdapter.GT, labelTrue);
            } else if (operation == LE) {
                g.ifZCmp(GeneratorAdapter.LE, labelTrue);
            } else if (operation == GE) {
                g.ifZCmp(GeneratorAdapter.GE, labelTrue);
            }
        }
    }

    g.push(false);
    g.goTo(labelExit);

    g.mark(labelTrue);
    g.push(true);

    g.mark(labelExit);

    return BOOLEAN_TYPE;
}

From source file:lucee.runtime.type.util.ComponentUtil.java

License:Open Source License

private static int createMethod(BytecodeContext statConstr, BytecodeContext constr,
        java.util.List<LitString> keys, ClassWriter cw, String className, Object member, int max,
        boolean writeLog, boolean suppressWSbeforeArg, boolean output) throws PageException {

    boolean hasOptionalArgs = false;

    if (member instanceof UDF) {
        UDF udf = (UDF) member;//from  www .  java2 s  .  c o  m
        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);//Type.getType(Caster.cfTypeToClass(args[y].getTypeAsString()));
            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, statConstr, constr, getPage(statConstr, constr), keys,
                cw, className, adapter, method, writeLog, suppressWSbeforeArg, output);
        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(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;
}

From source file:lucee.transformer.bytecode.BodyBase.java

License:Open Source License

public static void writeOut(final BytecodeContext bc, List<Statement> statements) throws BytecodeException {
    GeneratorAdapter adapter = bc.getAdapter();
    boolean isOutsideMethod;
    GeneratorAdapter a = null;//from  w  w  w . ja va2s  . c o  m
    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.getStaticConstructor(), 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();
    }
}

From source file:lucee.transformer.bytecode.BodyBase.java

License:Open Source License

private static void addToSubMethod(BytecodeContext bc, Statement... statements) throws BytecodeException {
    if (statements == null || statements.length == 0)
        return;/* w w w .  j ava2 s.  c  o  m*/

    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.getStaticConstructor(), 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();
}

From source file:lucee.transformer.bytecode.cast.CastBoolean.java

License:Open Source License

/**
 * @see lucee.transformer.bytecode.expression.Expression#writeOut(org.objectweb.asm.commons.GeneratorAdapter, int)
 *//*from w w w  .j ava  2 s. co m*/
public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException {
    GeneratorAdapter adapter = bc.getAdapter();
    if (expr instanceof ExprDouble) {
        expr.writeOut(bc, MODE_VALUE);
        if (mode == MODE_VALUE)
            adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_BOOLEAN_VALUE_FROM_DOUBLE);
        else
            adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_BOOLEAN_FROM_DOUBLE);
    } else if (expr instanceof ExprString) {
        expr.writeOut(bc, MODE_REF);
        if (mode == MODE_VALUE)
            adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_BOOLEAN_VALUE_FROM_STRING);
        else
            adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_BOOLEAN_FROM_STRING);
    } else {
        Type rtn = expr.writeOut(bc, mode);

        if (mode == MODE_VALUE) {
            if (!Types.isPrimitiveType(rtn)) {
                adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_BOOLEAN_VALUE);
            } else if (Types.BOOLEAN_VALUE.equals(rtn)) {
            } else if (Types.DOUBLE_VALUE.equals(rtn)) {
                adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_BOOLEAN_VALUE_FROM_DOUBLE);
            } else {
                adapter.invokeStatic(Types.CASTER,
                        new Method("toRef", Types.toRefType(rtn), new Type[] { rtn }));
                adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_BOOLEAN_VALUE);
            }
            //return Types.BOOLEAN_VALUE;
        } else {
            if (Types.BOOLEAN.equals(rtn)) {
            } else
                adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_BOOLEAN);
        }
    }

    if (mode == MODE_VALUE)
        return Types.BOOLEAN_VALUE;
    return Types.BOOLEAN;
}

From source file:lucee.transformer.bytecode.cast.CastDouble.java

License:Open Source License

/**
 * @see lucee.transformer.bytecode.expression.Expression#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int)
 *///ww  w.  j a v  a2  s  .co  m
public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException {

    GeneratorAdapter adapter = bc.getAdapter();
    if (expr instanceof ExprBoolean) {
        expr.writeOut(bc, MODE_VALUE);
        if (mode == MODE_VALUE)
            adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_VALUE_FROM_BOOLEAN);
        else
            adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_FROM_BOOLEAN);
    } else if (expr instanceof ExprDouble) {
        expr.writeOut(bc, mode);
        //if(mode==MODE_VALUE)adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_DOUBLE_VALUE_FROM_DOUBLE);
        //if(mode==MODE_REF) adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_DOUBLE_FROM_DOUBLE);
    } else if (expr instanceof ExprString) {
        expr.writeOut(bc, MODE_REF);
        if (mode == MODE_VALUE)
            adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_VALUE_FROM_STRING);
        else
            adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_FROM_STRING);
    } else {
        Type rtn = expr.writeOut(bc, mode);
        if (mode == MODE_VALUE) {
            if (!Types.isPrimitiveType(rtn)) {
                adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_VALUE);
            } else if (Types.DOUBLE_VALUE.equals(rtn)) {
            } else if (Types.BOOLEAN_VALUE.equals(rtn)) {
                adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_VALUE_FROM_BOOLEAN);
            } else {
                adapter.invokeStatic(Types.CASTER,
                        new Method("toRef", Types.toRefType(rtn), new Type[] { rtn }));
                adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_VALUE);
            }
            return Types.DOUBLE_VALUE;
        } else if (Types.isPrimitiveType(rtn)) {
            if (Types.DOUBLE_VALUE.equals(rtn)) {
                adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_FROM_DOUBLE);
            } else if (Types.BOOLEAN_VALUE.equals(rtn)) {
                adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_FROM_BOOLEAN);
            } else {
                adapter.invokeStatic(Types.CASTER,
                        new Method("toRef", Types.toRefType(rtn), new Type[] { rtn }));
                adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE);
            }
            return Types.DOUBLE;
        }
        //else {
        if (!Types.DOUBLE.equals(rtn))
            adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE);
        return Types.DOUBLE;
        //}
    }

    if (mode == MODE_VALUE)
        return Types.DOUBLE_VALUE;
    return Types.DOUBLE;
}