Example usage for org.objectweb.asm Opcodes ASTORE

List of usage examples for org.objectweb.asm Opcodes ASTORE

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ASTORE.

Prototype

int ASTORE

To view the source code for org.objectweb.asm Opcodes ASTORE.

Click Source Link

Usage

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   www  . j  a  v  a  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();
    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.apache.drill.exec.compile.bytecode.InstructionModifier.java

License:Apache License

@Override
public void visitVarInsn(int opcode, int var) {
    ReplacingBasicValue v;// w ww  . java  2 s .c  o m
    if (opcode == Opcodes.ASTORE && (v = popCurrent(true)) != null) {
        if (!v.isFunctionReturn) {
            ValueHolderSub from = oldToNew.get(v.getIndex());

            ReplacingBasicValue current = local(var);
            // if local var is set, then transfer to it to the existing holders in the local position.
            if (current != null) {
                if (oldToNew.get(current.getIndex()).iden() == from.iden()) {
                    int targetFirst = oldToNew.get(current.index).first();
                    from.transfer(this, targetFirst);
                    return;
                }
            }

            // if local var is not set, then check map to see if existing holders are mapped to local var.
            if (oldLocalToFirst.containsKey(var)) {
                ValueHolderSub sub = oldToNew.get(oldLocalToFirst.lget());
                if (sub.iden() == from.iden()) {
                    // if they are, then transfer to that.
                    from.transfer(this, oldToNew.get(oldLocalToFirst.lget()).first());
                    return;
                }
            }

            // map from variables to global space for future use.
            oldLocalToFirst.put(var, v.getIndex());

        } else {
            // this is storage of a function return, we need to map the fields to the holder spots.
            int first;
            if (oldLocalToFirst.containsKey(var)) {
                first = oldToNew.get(oldLocalToFirst.lget()).first();
                v.iden.transferToLocal(adder, first);
            } else {
                first = v.iden.createLocalAndTrasfer(adder);
            }
            ValueHolderSub from = v.iden.getHolderSubWithDefinedLocals(first);
            oldToNew.put(v.getIndex(), from);
            v.disableFunctionReturn();
        }

    } else if (opcode == Opcodes.ALOAD && (v = getReturn()) != null) {
        // noop.
    } else {
        super.visitVarInsn(opcode, var);
    }

}

From source file:org.apache.drill.exec.compile.bytecode.ValueHolderIden.java

License:Apache License

private static void initType(int index, Type t, DirectSorter v) {
    switch (t.getSort()) {
    case Type.BOOLEAN:
    case Type.BYTE:
    case Type.CHAR:
    case Type.SHORT:
    case Type.INT:
        v.visitInsn(Opcodes.ICONST_0);/*  w w  w .  jav  a2  s.  com*/
        v.directVarInsn(Opcodes.ISTORE, index);
        break;
    case Type.LONG:
        v.visitInsn(Opcodes.LCONST_0);
        v.directVarInsn(Opcodes.LSTORE, index);
        break;
    case Type.FLOAT:
        v.visitInsn(Opcodes.FCONST_0);
        v.directVarInsn(Opcodes.FSTORE, index);
        break;
    case Type.DOUBLE:
        v.visitInsn(Opcodes.DCONST_0);
        v.directVarInsn(Opcodes.DSTORE, index);
        break;
    case Type.OBJECT:
        v.visitInsn(Opcodes.ACONST_NULL);
        v.directVarInsn(Opcodes.ASTORE, index);
        break;
    default:
        throw new UnsupportedOperationException();
    }
}

From source file:org.apache.sling.metrics.impl.TimerAdapter.java

License:Apache License

@Override
protected void onMethodEnter() {
    mv.visitLdcInsn(timerName);//www  .  j av  a 2s .c om
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, METRICS_UTIL_CL, "getTimer", METRICS_UTIL_GETMETER_DESC, false);
    // store the meter.
    contextid = newLocal(Type.getType(Context.class));
    mv.visitVarInsn(Opcodes.ASTORE, contextid);
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private int addMethodParametersVariable(InsnList il) {
    il.add(TreeInstructions.getPushInstruction(this.methodArguments.length));
    il.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));
    int methodParametersIndex = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, methodParametersIndex));
    this.mn.maxLocals++;
    for (int i = 0; i < this.methodArguments.length; i++) {
        il.add(new VarInsnNode(Opcodes.ALOAD, methodParametersIndex));
        il.add(TreeInstructions.getPushInstruction(i));
        il.add(TreeInstructions.getLoadInst(methodArguments[i], getArgumentPosition(i)));
        MethodInsnNode mNode = TreeInstructions.getWrapperContructionInst(methodArguments[i]);
        if (mNode != null) {
            il.add(mNode);//from  ww  w.  jav  a  2 s  .  co  m
        }
        il.add(new InsnNode(Opcodes.AASTORE));
    }
    return methodParametersIndex;
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addGetMethodInvocation(InsnList il) {
    il.add(TreeInstructions.getPushInstruction(this.methodArguments.length));
    il.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Class"));
    int parameterClassesIndex = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, parameterClassesIndex));
    this.mn.maxLocals++;
    for (int i = 0; i < this.methodArguments.length; i++) {
        il.add(new VarInsnNode(Opcodes.ALOAD, parameterClassesIndex));
        il.add(TreeInstructions.getPushInstruction(i));
        il.add(TreeInstructions.getClassReferenceInstruction(methodArguments[i], cn.version & 0xFFFF));
        il.add(new InsnNode(Opcodes.AASTORE));
    }//from  w  ww.  ja v  a  2  s.c o  m
    il.add(TreeInstructions.getClassConstantReference(this.classType, cn.version & 0xFFFF));
    il.add(new LdcInsnNode(this.mn.name));
    il.add(new VarInsnNode(Opcodes.ALOAD, parameterClassesIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "org/brutusin/instrumentation/utils/Helper", "getSource",
            "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/Object;", false));
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addStoreMethod(InsnList il) {
    this.methodVarIndex = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, this.methodVarIndex));
    this.mn.maxLocals++;
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addTraceStart() {
    InsnList il = new InsnList();
    int methodParametersIndex = addMethodParametersVariable(il);
    addGetMethodInvocation(il);/*from  www  .  j ava  2  s.  c o m*/
    addStoreMethod(il);
    addGetCallback(il);

    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(new VarInsnNode(Opcodes.ALOAD, methodParametersIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback", "onStart",
            "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/String;", false));

    this.executionIdIndex = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, this.executionIdIndex));
    this.mn.maxLocals++;
    this.startNode = new LabelNode();
    this.mn.instructions.insert(startNode);
    this.mn.instructions.insert(il);
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addCatchBlock(LabelNode startNode, LabelNode endNode) {

    InsnList il = new InsnList();
    LabelNode handlerNode = new LabelNode();
    il.add(handlerNode);//from  ww w  .ja  v  a  2s.c om

    int exceptionVariablePosition = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, exceptionVariablePosition));
    this.methodOffset++; // Actualizamos el offset
    addGetCallback(il);
    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));
    il.add(new VarInsnNode(Opcodes.ALOAD, this.executionIdIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback",
            "onThrowableUncatched", "(Ljava/lang/Object;Ljava/lang/Throwable;Ljava/lang/String;)V", false));

    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));
    il.add(new InsnNode(Opcodes.ATHROW));

    TryCatchBlockNode blockNode = new TryCatchBlockNode(startNode, endNode, handlerNode, null);

    this.mn.tryCatchBlocks.add(blockNode);
    this.mn.instructions.add(il);
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private InsnList getThrowTraceInstructions() {
    InsnList il = new InsnList();

    int exceptionVariablePosition = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, exceptionVariablePosition));

    this.methodOffset++; // Actualizamos el offset
    addGetCallback(il);//from   w w w  .  j a  va2  s .  c  om
    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));
    il.add(new VarInsnNode(Opcodes.ALOAD, this.executionIdIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback",
            "onThrowableThrown", "(Ljava/lang/Object;Ljava/lang/Throwable;Ljava/lang/String;)V", false));

    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));

    return il;
}