Example usage for org.objectweb.asm.commons GeneratorAdapter catchException

List of usage examples for org.objectweb.asm.commons GeneratorAdapter catchException

Introduction

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

Prototype

public void catchException(final Label start, final Label end, final Type exception) 

Source Link

Document

Marks the start of an exception handler.

Usage

From source file:org.evosuite.testcase.ConstructorStatement.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from w w  w  .  j a va2  s  . c  o  m*/
public void getBytecode(GeneratorAdapter mg, Map<Integer, Integer> locals, Throwable exception) {
    logger.debug("Invoking constructor");
    Label start = mg.newLabel();
    Label end = mg.newLabel();

    // if(exception != null)
    mg.mark(start);

    mg.newInstance(Type.getType(retval.getVariableClass()));
    mg.dup();
    int num = 0;
    for (VariableReference parameter : parameters) {
        parameter.loadBytecode(mg, locals);
        if (constructor.getConstructor().getParameterTypes()[num].isPrimitive()) {
            if (parameter.getGenericClass().isWrapperType()) {
                mg.unbox(Type.getType(parameter.getGenericClass().getUnboxedType()));
            } else if (!parameter.getGenericClass().isPrimitive()) {
                Class<?> parameterClass = new GenericClass(constructor.getParameterTypes()[num]).getBoxedType();
                Type parameterType = Type.getType(parameterClass);
                mg.checkCast(parameterType);
                mg.unbox(Type.getType(constructor.getConstructor().getParameterTypes()[num]));
            }

            if (!constructor.getParameterTypes()[num].equals(parameter.getVariableClass())) {
                logger.debug("Types don't match - casting {} to {}", parameter.getVariableClass().getName(),
                        constructor.getConstructor().getParameterTypes()[num].getName());
                mg.cast(Type.getType(parameter.getVariableClass()),
                        Type.getType(constructor.getConstructor().getParameterTypes()[num]));
            }
        } else if (parameter.getVariableClass().isPrimitive()) {
            mg.box(Type.getType(parameter.getVariableClass()));
        }
        num++;
    }
    mg.invokeConstructor(Type.getType(retval.getVariableClass()),
            Method.getMethod(constructor.getConstructor()));
    logger.debug("Storing result");
    retval.storeBytecode(mg, locals);

    // if(exception != null) {
    mg.mark(end);
    Label l = mg.newLabel();
    mg.goTo(l);
    // mg.catchException(start, end,
    // Type.getType(getExceptionClass(exception)));
    mg.catchException(start, end, Type.getType(Throwable.class));
    mg.pop(); // Pop exception from stack
    if (!retval.isVoid()) {
        Class<?> clazz = retval.getVariableClass();
        if (clazz.equals(boolean.class))
            mg.push(false);
        else if (clazz.equals(char.class))
            mg.push(0);
        else if (clazz.equals(int.class))
            mg.push(0);
        else if (clazz.equals(short.class))
            mg.push(0);
        else if (clazz.equals(long.class))
            mg.push(0L);
        else if (clazz.equals(float.class))
            mg.push(0.0F);
        else if (clazz.equals(double.class))
            mg.push(0.0);
        else if (clazz.equals(byte.class))
            mg.push(0);
        else if (clazz.equals(String.class))
            mg.push("");
        else
            mg.visitInsn(Opcodes.ACONST_NULL);

        retval.storeBytecode(mg, locals);
    }
    mg.mark(l);
    // }

}

From source file:org.evosuite.testcase.FieldStatement.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from  w ww.  j ava2 s.co m*/
public void getBytecode(GeneratorAdapter mg, Map<Integer, Integer> locals, Throwable exception) {

    Label start = mg.newLabel();
    Label end = mg.newLabel();

    // if(exception != null)
    mg.mark(start);

    if (!isStatic()) {
        source.loadBytecode(mg, locals);
    }
    if (isStatic())
        mg.getStatic(Type.getType(field.getField().getDeclaringClass()), field.getName(),
                Type.getType(field.getField().getType()));
    else {
        if (!source.getVariableClass().isInterface()) {
            mg.getField(Type.getType(source.getVariableClass()), field.getName(),
                    Type.getType(field.getField().getType()));
        } else {
            mg.getField(Type.getType(field.getField().getDeclaringClass()), field.getName(),
                    Type.getType(field.getField().getType()));
        }
    }

    if (!retval.getVariableClass().equals(field.getField().getType())) {
        if (!retval.getVariableClass().isPrimitive()) {
            if (field.getField().getType().isPrimitive()) {
                mg.box(Type.getType(field.getField().getType()));
            }
            mg.checkCast(Type.getType(retval.getVariableClass()));
        } else {
            mg.cast(Type.getType(field.getField().getType()), Type.getType(retval.getVariableClass()));
        }
    }
    retval.storeBytecode(mg, locals);

    // if(exception != null) {
    mg.mark(end);
    Label l = mg.newLabel();
    mg.goTo(l);
    // mg.catchException(start, end, Type.getType(exception.getClass()));
    mg.catchException(start, end, Type.getType(Throwable.class));
    mg.pop(); // Pop exception from stack
    if (!retval.isVoid()) {
        Class<?> clazz = retval.getVariableClass();
        if (clazz.equals(boolean.class))
            mg.push(false);
        else if (clazz.equals(char.class))
            mg.push(0);
        else if (clazz.equals(int.class))
            mg.push(0);
        else if (clazz.equals(short.class))
            mg.push(0);
        else if (clazz.equals(long.class))
            mg.push(0L);
        else if (clazz.equals(float.class))
            mg.push(0.0F);
        else if (clazz.equals(double.class))
            mg.push(0.0);
        else if (clazz.equals(byte.class))
            mg.push(0);
        else if (clazz.equals(String.class))
            mg.push("");
        else
            mg.visitInsn(Opcodes.ACONST_NULL);

        retval.storeBytecode(mg, locals);
    }
    mg.mark(l);
    // }
}

From source file:org.evosuite.testcase.MethodStatement.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from  w  ww . j a v  a 2s .co m*/
public void getBytecode(GeneratorAdapter mg, Map<Integer, Integer> locals, Throwable exception) {
    Label start = mg.newLabel();
    Label end = mg.newLabel();

    // if(exception != null)
    mg.mark(start);

    if (!isStatic()) {
        callee.loadBytecode(mg, locals);
        if (!method.getMethod().getDeclaringClass().equals(callee.getVariableClass())) {
            logger.debug("Types don't match - casting!");
            mg.cast(Type.getType(callee.getVariableClass()),
                    Type.getType(method.getMethod().getDeclaringClass()));
        }
    }
    int num = 0;
    for (VariableReference parameter : parameters) {
        parameter.loadBytecode(mg, locals);
        if (method.getMethod().getParameterTypes()[num].isPrimitive()) {
            if (parameter.getGenericClass().isWrapperType()) {
                mg.unbox(Type.getType(parameter.getGenericClass().getUnboxedType()));
            } else if (!parameter.getGenericClass().isPrimitive()) {
                Class<?> parameterClass = new GenericClass(method.getParameterTypes()[num]).getBoxedType();
                Type parameterType = Type.getType(parameterClass);
                mg.checkCast(parameterType);
                mg.unbox(Type.getType(method.getMethod().getParameterTypes()[num]));
            }

            if (!method.getParameterTypes()[num].equals(parameter.getVariableClass())) {
                logger.debug("Types don't match - casting!");
                mg.cast(Type.getType(parameter.getVariableClass()),
                        Type.getType(method.getMethod().getParameterTypes()[num]));
            }
        } else if (parameter.getVariableClass().isPrimitive()) {
            mg.box(Type.getType(parameter.getVariableClass()));
        }
        num++;
    }
    logger.debug("Invoking method");
    // if(exception != null) {
    //
    // mg.visitTryCatchBlock(start, end, handler,
    // exception.getClass().getName().replace('.', '/'));
    // }
    if (isStatic())
        mg.invokeStatic(Type.getType(method.getMethod().getDeclaringClass()),
                org.objectweb.asm.commons.Method.getMethod(method.getMethod()));
    else {
        if (!callee.getVariableClass().isInterface()) {
            mg.invokeVirtual(Type.getType(callee.getVariableClass()),
                    org.objectweb.asm.commons.Method.getMethod(method.getMethod()));
        } else {
            mg.invokeInterface(Type.getType(callee.getVariableClass()),
                    org.objectweb.asm.commons.Method.getMethod(method.getMethod()));
        }
    }

    if (!retval.isVoid()) {
        if (!retval.getVariableClass().equals(method.getReturnType())) {
            if (!retval.getVariableClass().isPrimitive()) {
                mg.checkCast(Type.getType(retval.getVariableClass()));
            } else {
                mg.cast(Type.getType(method.getMethod().getReturnType()),
                        Type.getType(retval.getVariableClass()));
            }
        }
        retval.storeBytecode(mg, locals);
    }

    // if(exception != null) {
    mg.mark(end);
    Label l = mg.newLabel();
    mg.goTo(l);
    // mg.catchException(start, end,
    // Type.getType(getExceptionClass(exception)));
    mg.catchException(start, end, Type.getType(Throwable.class));
    mg.pop(); // Pop exception from stack
    if (!retval.isVoid()) {
        Class<?> clazz = retval.getVariableClass();
        if (clazz.equals(boolean.class))
            mg.push(false);
        else if (clazz.equals(char.class))
            mg.push(0);
        else if (clazz.equals(int.class))
            mg.push(0);
        else if (clazz.equals(short.class))
            mg.push(0);
        else if (clazz.equals(long.class))
            mg.push(0L);
        else if (clazz.equals(float.class))
            mg.push(0.0F);
        else if (clazz.equals(double.class))
            mg.push(0.0);
        else if (clazz.equals(byte.class))
            mg.push(0);
        else if (clazz.equals(String.class))
            mg.push("");
        else
            mg.visitInsn(Opcodes.ACONST_NULL);

        retval.storeBytecode(mg, locals);
    }
    mg.mark(l);
    // }
}

From source file:org.formulacompiler.compiler.internal.bytecode.ExpressionCompiler.java

License:Open Source License

private void compileIsException(final ExpressionNodeForFunction _node, final boolean _testForErrors,
        final Type[] _handledTypesReturningTrue, final Type[] _handledTypesReturningFalse)
        throws CompilerException {
    /*/*from  www .ja  v  a 2 s . c  o m*/
     * Move the handler into its own method because exception handlers clobber the stack.
     */
    final Iterable<LetEntry<Compilable>> closure = closureOf(_node);
    compileHelpedExpr(new HelperCompiler(sectionInContext(), _node, closure) {

        @Override
        protected void compileBody() throws CompilerException {
            final GeneratorAdapter mv = this.mv();
            final ExpressionCompiler ec = expressionCompiler();
            final Label handled = mv.newLabel();

            final Label beginHandling = mv.mark();
            ec.compile(_node.argument(0));
            ec.compileExceptionalValueTest(_testForErrors);
            mv.goTo(handled);
            final Label endHandling = mv.mark();

            for (final Type a_handledTypesReturningTrue : _handledTypesReturningTrue) {
                mv.catchException(beginHandling, endHandling, a_handledTypesReturningTrue);
                mv.visitVarInsn(Opcodes.ASTORE, method().newLocal(1));
                ec.compileConst(Boolean.TRUE);
                mv.goTo(handled);
            }

            for (final Type a_handledTypesReturningFalse : _handledTypesReturningFalse) {
                mv.catchException(beginHandling, endHandling, a_handledTypesReturningFalse);
                mv.visitVarInsn(Opcodes.ASTORE, method().newLocal(1));
                ec.compileConst(Boolean.FALSE);
                mv.goTo(handled);
            }

            mv.mark(handled);

            mv.returnValue();
        }

    }, closure);
}