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

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

Introduction

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

Prototype

public Label mark() 

Source Link

Document

Marks the current code position with a new label.

Usage

From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Generates method body for encoding Collection value. The logic is like this:
 *
 * <pre>/*w w w  .j  a va2  s.  c o m*/
 * {@code
 *
 * encoder.writeInt(collection.size());
 * for (T element : collection) {
 *   encodeElement(element, encoder, elementSchema, seenRefs);
 * }
 * if (collection.size() > 0) {
 *   encoder.writeInt(0);
 * }
 * }
 * </pre>
 * @param mg
 * @param componentType
 * @param componentSchema
 * @param value
 * @param encoder
 */
private void encodeCollection(GeneratorAdapter mg, TypeToken<?> componentType, Schema componentSchema,
        int value, int encoder, int schemaLocal, int seenRefs) {
    // Encode and store the collection length locally
    mg.loadArg(value);
    mg.invokeInterface(Type.getType(Collection.class), getMethod(int.class, "size"));
    int length = mg.newLocal(Type.INT_TYPE);
    mg.storeLocal(length);

    mg.loadArg(encoder);
    mg.loadLocal(length);
    mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, "writeInt", int.class));
    mg.pop();

    // Store the component schema
    mg.loadArg(schemaLocal);
    mg.invokeVirtual(Type.getType(Schema.class), getMethod(Schema.class, "getComponentSchema"));
    int componentSchemaLocal = mg.newLocal(Type.getType(Schema.class));
    mg.storeLocal(componentSchemaLocal);

    // Store the iterator
    int iterator = mg.newLocal(Type.getType(Iterator.class));
    mg.loadArg(value);
    mg.invokeInterface(Type.getType(Collection.class), getMethod(Iterator.class, "iterator"));
    mg.storeLocal(iterator);

    // For loop with iterator. Encode each component
    Label beginFor = mg.mark();
    Label endFor = mg.newLabel();
    mg.loadLocal(iterator);
    mg.invokeInterface(Type.getType(Iterator.class), getMethod(boolean.class, "hasNext"));
    mg.ifZCmp(GeneratorAdapter.EQ, endFor);

    // Call the encode method for encoding the element.
    mg.loadThis();

    mg.loadLocal(iterator);
    mg.invokeInterface(Type.getType(Iterator.class), getMethod(Object.class, "next"));
    doCast(mg, componentType, componentSchema);
    mg.loadArg(encoder);
    mg.loadLocal(componentSchemaLocal);
    mg.loadArg(seenRefs);
    mg.invokeVirtual(classType, getEncodeMethod(componentType, componentSchema));
    mg.goTo(beginFor);

    mg.mark(endFor);

    // if length > 0, write out 0 at the end of array.
    Label zeroLength = mg.newLabel();
    mg.loadLocal(length);
    mg.ifZCmp(GeneratorAdapter.LE, zeroLength);
    encodeInt(mg, 0, encoder);
    mg.mark(zeroLength);
}

From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Generates method body for encoding array value. The logic is similar to the one in
 * {@link #encodeCollection}, with collection size replaced with array length.
 *
 * @param mg/*w  w  w.j a v a  2 s.  com*/
 * @param componentType
 * @param componentSchema
 * @param value
 * @param encoder
 * @param schemaLocal
 * @param seenRefs
 */
private void encodeArray(GeneratorAdapter mg, TypeToken<?> componentType, Schema componentSchema, int value,
        int encoder, int schemaLocal, int seenRefs) {
    // Encode and store the array length locally
    mg.loadArg(value);
    mg.arrayLength();
    int length = mg.newLocal(Type.INT_TYPE);
    mg.storeLocal(length);

    mg.loadArg(encoder);
    mg.loadLocal(length);
    mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, "writeInt", int.class));
    mg.pop();

    // Store the component schema
    mg.loadArg(schemaLocal);
    mg.invokeVirtual(Type.getType(Schema.class), getMethod(Schema.class, "getComponentSchema"));
    int componentSchemaLocal = mg.newLocal(Type.getType(Schema.class));
    mg.storeLocal(componentSchemaLocal);

    // for (int idx = 0; idx < array.length; idx++)
    mg.push(0);
    int idx = mg.newLocal(Type.INT_TYPE);
    mg.storeLocal(idx);
    Label beginFor = mg.mark();
    Label endFor = mg.newLabel();
    mg.loadLocal(idx);
    mg.loadLocal(length);
    mg.ifICmp(GeneratorAdapter.GE, endFor);

    // Call encode method to encode array[idx]
    mg.loadThis();
    mg.loadArg(value);
    mg.loadLocal(idx);
    TypeToken<?> callTypeToken = getCallTypeToken(componentType, componentSchema);
    mg.arrayLoad(Type.getType(callTypeToken.getRawType()));
    mg.loadArg(encoder);
    mg.loadLocal(componentSchemaLocal);
    mg.loadArg(seenRefs);
    mg.invokeVirtual(classType, getEncodeMethod(componentType, componentSchema));

    mg.iinc(idx, 1);
    mg.goTo(beginFor);
    mg.mark(endFor);

    // if length > 0, write out 0 at the end of array.
    Label zeroLength = mg.newLabel();
    mg.loadLocal(length);
    mg.ifZCmp(GeneratorAdapter.LE, zeroLength);
    encodeInt(mg, 0, encoder);
    mg.mark(zeroLength);
}

From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Generates method body for encoding map value. The logic is like this:
 *
 * <pre>//  w ww.  j a  v  a 2s  . c  o m
 * {@code
 *
 * encoder.writeInt(map.size();
 *
 * for (Map.Entry<Key, Value> entry : map.entrySet()) {
 *   encodeKey(entry.getKey(), encoder, keySchema, seenRefs);
 *   encodeValue(entry.getValue(), encoder, valueSchema, seenRefs);
 * }
 *
 * if (map.size() > 0) {
 *   encoder.writeInt(0);
 * }
 * }
 * </pre>
 *
 * @param mg
 * @param keyType
 * @param valueType
 * @param keySchema
 * @param valueSchema
 * @param value
 * @param encoder
 * @param schemaLocal
 * @param seenRefs
 */
private void encodeMap(GeneratorAdapter mg, TypeToken<?> keyType, TypeToken<?> valueType, Schema keySchema,
        Schema valueSchema, int value, int encoder, int schemaLocal, int seenRefs) {
    // Encode and store the map length locally
    mg.loadArg(value);
    mg.invokeInterface(Type.getType(Map.class), getMethod(int.class, "size"));
    int length = mg.newLocal(Type.INT_TYPE);
    mg.storeLocal(length);

    mg.loadArg(encoder);
    mg.loadLocal(length);
    mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, "writeInt", int.class));
    mg.pop();

    // Stores the key and value schema
    mg.loadArg(schemaLocal);
    mg.invokeVirtual(Type.getType(Schema.class), getMethod(Map.Entry.class, "getMapSchema"));
    mg.dup();

    int keySchemaLocal = mg.newLocal(Type.getType(Schema.class));
    mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getKey"));
    mg.checkCast(Type.getType(Schema.class));
    mg.storeLocal(keySchemaLocal);

    int valueSchemaLocal = mg.newLocal(Type.getType(Schema.class));
    mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getValue"));
    mg.checkCast(Type.getType(Schema.class));
    mg.storeLocal(valueSchemaLocal);

    // Store the entry set iterator
    int iterator = mg.newLocal(Type.getType(Iterator.class));
    mg.loadArg(value);
    mg.invokeInterface(Type.getType(Map.class), getMethod(Set.class, "entrySet"));
    mg.invokeInterface(Type.getType(Set.class), getMethod(Iterator.class, "iterator"));
    mg.storeLocal(iterator);

    // For loop the entry set iterator, encode each key-value pairs
    Label beginFor = mg.mark();
    Label endFor = mg.newLabel();
    mg.loadLocal(iterator);
    mg.invokeInterface(Type.getType(Iterator.class), getMethod(boolean.class, "hasNext"));
    mg.ifZCmp(GeneratorAdapter.EQ, endFor);

    int entry = mg.newLocal(Type.getType(Map.Entry.class));
    mg.loadLocal(iterator);
    mg.invokeInterface(Type.getType(Iterator.class), getMethod(Object.class, "next"));
    mg.checkCast(Type.getType(Map.Entry.class));
    mg.storeLocal(entry);

    // encode key
    mg.loadThis();
    mg.loadLocal(entry);
    mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getKey"));
    doCast(mg, keyType, keySchema);
    mg.loadArg(encoder);
    mg.loadLocal(keySchemaLocal);
    mg.loadArg(seenRefs);
    mg.invokeVirtual(classType, getEncodeMethod(keyType, keySchema));

    // encode value
    mg.loadThis();
    mg.loadLocal(entry);
    mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getValue"));
    doCast(mg, valueType, valueSchema);
    mg.loadArg(encoder);
    mg.loadLocal(valueSchemaLocal);
    mg.loadArg(seenRefs);
    mg.invokeVirtual(classType, getEncodeMethod(valueType, valueSchema));

    mg.goTo(beginFor);
    mg.mark(endFor);

    // if length > 0, write out 0 at the end of map
    Label zeroLength = mg.newLabel();
    mg.loadLocal(length);
    mg.ifZCmp(GeneratorAdapter.LE, zeroLength);
    encodeInt(mg, 0, encoder);
    mg.mark(zeroLength);
}

From source file:com.mulberry.athena.asm.ASMTest.java

License:Open Source License

@Test
public void generateClassUseClassVisitor() throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = new TraceClassVisitor(cw, new PrintWriter(System.out));
    cv.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "Hello", null, "java/lang/Object", null);
    {/* ww  w .j  a  v a  2 s . c  o m*/
        GeneratorAdapter clinitgen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, clinit, null, null, cv);
        clinitgen.visitCode();
        clinitgen.visitLineNumber(1, clinitgen.mark());
        clinitgen.returnValue();
        clinitgen.endMethod();
    }

    {
        GeneratorAdapter ctorgen = new GeneratorAdapter(ACC_PUBLIC, voidctor, null, null, cv);
        Label start = ctorgen.newLabel();
        Label end = ctorgen.newLabel();
        ctorgen.visitCode();
        ctorgen.visitLineNumber(2, ctorgen.mark());
        ctorgen.visitLabel(start);
        ctorgen.loadThis();
        ctorgen.invokeConstructor(Type.getObjectType("java/lang/Object"), voidctor);
        ctorgen.visitLabel(end);
        ctorgen.returnValue();
        ctorgen.endMethod();
    }
    {
        GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC, Method.getMethod("int getRequiredArity()"),
                null, null, cv);
        gen.visitCode();
        gen.push(3);
        gen.returnValue();
        gen.endMethod();
    }

    {
        Method method = Method.getMethod("void main(java/lang/String[])", true);
        GeneratorAdapter maingen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, method, null, null, cv);
        Label start = maingen.newLabel();
        maingen.visitCode();
        maingen.visitLineNumber(3, maingen.mark());
        maingen.visitLabel(start);
        maingen.getStatic(Type.getType("java/lang/System"), "out", Type.getType(PrintStream.class));
        maingen.push("IT WORKS!");
        maingen.invokeVirtual(Type.getType("java/io/PrintStream"),
                Method.getMethod("void println(java/lang/String)", true));
        Label end = maingen.newLabel();
        maingen.visitLabel(end);
        maingen.returnValue();
        maingen.endMethod();
    }

    cv.visitEnd();

    Class<?> clazz = new DynamicClassLoader().defineClass("Hello", cw.toByteArray());
    java.lang.reflect.Method method = clazz.getMethod("main", String[].class);
    final String[] arguments = new String[] { "hello", "world" };
    method.invoke(clazz, (Object) arguments);
}

From source file:org.apache.commons.weaver.privilizer.ActionGenerator.java

License:Apache License

/**
 * Add fields and generate constructor.// ww w  . j a  v a2s .co m
 */
private void init() {
    for (final Field field : fields) {
        visitField(field.access, field.name, field.type.getDescriptor(), null, null).visitEnd();
    }
    final Method init = new Method("<init>", Type.VOID_TYPE, helper.getArgumentTypes());

    final GeneratorAdapter mgen = new GeneratorAdapter(0, init, null, Privilizer.EMPTY_TYPE_ARRAY, this);

    mgen.visitCode();
    final Label begin = mgen.mark();

    // invoke super constructor
    mgen.loadThis();
    mgen.invokeConstructor(Type.getType(Object.class), Method.getMethod("void <init> ()"));
    // assign remaining fields

    int arg = 0;
    for (final Field field : fields) {
        mgen.loadThis();
        mgen.loadArg(arg++);
        mgen.putField(action, field.name, field.type);
    }
    mgen.returnValue();
    final Label end = mgen.mark();

    // declare local vars
    mgen.visitLocalVariable("this", action.getDescriptor(), null, begin, end, 0);
    arg = 1;
    for (final Field field : fields) {
        mgen.visitLocalVariable("arg" + arg, field.type.getDescriptor(), null, begin, end, arg++);
    }
    mgen.endMethod();
}

From source file:org.apache.deltaspike.partialbean.impl.proxy.AsmProxyClassGenerator.java

License:Apache License

private static void defineMethod(ClassWriter cw, java.lang.reflect.Method method, Type proxyType,
        Type invocationHandlerType, Type superType, boolean callInvocationHandler) {
    Type methodType = Type.getType(method);
    Type[] exceptionTypes = getTypes(method.getExceptionTypes());

    // 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, exceptionTypes, cw);

    // copy annotations
    for (Annotation annotation : method.getDeclaredAnnotations()) {
        mg.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true).visitEnd();
    }//from  w  w  w. j  a  va 2  s  .  c om

    mg.visitCode();

    Label tryBlockStart = mg.mark();

    mg.loadThis();
    loadCurrentMethod(mg, method, methodType);
    loadArguments(mg, method, methodType);

    // invoke our ProxyInvocationHandler
    mg.invokeStatic(Type.getType(ManualInvocationHandler.class),
            Method.getMethod("Object staticInvoke(Object, java.lang.reflect.Method, Object[])"));

    // cast the result
    mg.unbox(methodType.getReturnType());

    Label tryBlockEnd = mg.mark();

    // push return
    mg.returnValue();

    boolean throwableCatched = false;

    // catch ProceedOriginalRuntimeException
    Label proceedOriginal = mg.mark();
    if (callInvocationHandler) {
        // call stored InvocationHandler
        mg.loadThis();
        mg.getField(proxyType, FIELDNAME_HANDLER, invocationHandlerType);
        mg.loadThis();
        loadCurrentMethod(mg, method, methodType);
        loadArguments(mg, method, methodType);
        mg.invokeVirtual(invocationHandlerType,
                Method.getMethod("Object invoke(Object, java.lang.reflect.Method, Object[])"));
        mg.unbox(methodType.getReturnType());
        mg.returnValue();
    } else {
        // call super method
        mg.loadThis();
        mg.loadArgs();
        mg.visitMethodInsn(Opcodes.INVOKESPECIAL, superType.getInternalName(), method.getName(),
                Type.getMethodDescriptor(method), false);
        mg.returnValue();
    }
    mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, proceedOriginal,
            Type.getInternalName(ProceedOriginalMethodException.class));

    // catch declared exceptions
    if (exceptionTypes.length > 0) {
        Label rethrow = mg.mark();
        mg.visitVarInsn(Opcodes.ASTORE, 1);
        mg.visitVarInsn(Opcodes.ALOAD, 1);
        mg.throwException();

        // catch declared exceptions and rethrow it...
        for (Type exceptionType : exceptionTypes) {
            if (exceptionType.getClassName().equals(Throwable.class.getName())) {
                throwableCatched = true;
            }
            mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, exceptionType.getInternalName());
        }
    }

    if (!throwableCatched) {
        // catch Throwable and wrap it with a UndeclaredThrowableException
        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(10, 10);
    mg.visitEnd();
}

From source file:org.apache.deltaspike.proxy.impl.AsmDeltaSpikeProxyClassGenerator.java

License:Apache License

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));
        }//w  ww . ja  va 2 s .  c o  m
    }

    // 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();
}

From source file:org.apache.deltaspike.proxy.impl.AsmProxyClassGenerator.java

License:Apache License

private static void defineMethod(ClassWriter cw, java.lang.reflect.Method method,
        Class manualInvocationHandlerClass) {
    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));
        }//from w  w w .  ja  va  2s.com
    }

    // 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();
    loadCurrentMethod(mg, method, methodType);
    loadArguments(mg, method, methodType);

    // invoke our ProxyInvocationHandler
    mg.invokeStatic(Type.getType(manualInvocationHandlerClass),
            Method.getMethod("Object staticInvoke(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.size() > 0) {
        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(10, 10);
    mg.visitEnd();
}

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  w ww . ja va  2s.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);
}

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

License:Open Source License

private void compileScanArray(ForEachElementCompilation _forElement) throws CompilerException {
    final GeneratorAdapter mv = mv();
    final int loc = localsOffset();
    incLocalsOffset(4);/* w  w  w.jav  a 2 s.  c o m*/

    // store array
    mv.visitVarInsn(Opcodes.ASTORE, loc);

    // store array length
    mv.visitVarInsn(Opcodes.ALOAD, loc);
    mv.arrayLength();
    mv.visitVarInsn(Opcodes.ISTORE, 1 + loc);

    // loop index
    mv.push(0);
    mv.visitVarInsn(Opcodes.ISTORE, 2 + loc);

    // loop start
    final Label l0 = mv.mark();

    // check loop condition
    mv.visitVarInsn(Opcodes.ILOAD, 2 + loc);
    mv.visitVarInsn(Opcodes.ILOAD, 1 + loc);
    final Label l1 = new Label();
    mv.ifICmp(GeneratorAdapter.GE, l1);

    // loop body
    mv.visitVarInsn(Opcodes.ALOAD, loc);
    mv.visitVarInsn(Opcodes.ILOAD, 2 + loc);
    mv.visitInsn(Opcodes.AALOAD);
    mv.visitVarInsn(Opcodes.ASTORE, 3 + loc);
    _forElement.compile(3 + loc, 2 + loc);

    // inc loop index
    mv.visitIincInsn(2 + loc, 1);

    mv.goTo(l0);
    mv.visitLabel(l1);
}