List of usage examples for org.objectweb.asm Opcodes ATHROW
int ATHROW
To view the source code for org.objectweb.asm Opcodes ATHROW.
Click Source Link
From source file:org.jacoco.core.test.validation.java5.StructuredLockingTest.java
License:Open Source License
private void assertStructuredLocking(String owner, MethodNode mn) throws Exception { Analyzer<BasicValue> analyzer = new Analyzer<BasicValue>(new BasicInterpreter()) { @Override//from w ww .j a v a2 s . c o m protected Frame<BasicValue> newFrame(int nLocals, int nStack) { return new LockFrame(nLocals, nStack); } @Override protected Frame<BasicValue> newFrame(Frame<? extends BasicValue> src) { return new LockFrame(src); } }; Frame<BasicValue>[] frames = analyzer.analyze(owner, mn); // Make sure no locks are left when method exits: for (int i = 0; i < frames.length; i++) { AbstractInsnNode insn = mn.instructions.get(i); switch (insn.getOpcode()) { case Opcodes.IRETURN: case Opcodes.LRETURN: case Opcodes.FRETURN: case Opcodes.DRETURN: case Opcodes.ARETURN: case Opcodes.RETURN: ((LockFrame) frames[i]).assertNoLock("Exit with lock"); break; case Opcodes.ATHROW: List<TryCatchBlockNode> handlers = analyzer.getHandlers(i); if (handlers == null || handlers.isEmpty()) { ((LockFrame) frames[i]).assertNoLock("Exit with lock"); } break; } } // Only instructions protected by a catch-all handler can hold locks: for (int i = 0; i < frames.length; i++) { AbstractInsnNode insn = mn.instructions.get(i); if (insn.getOpcode() > 0) { boolean catchAll = false; List<TryCatchBlockNode> handlers = analyzer.getHandlers(i); if (handlers != null) { for (TryCatchBlockNode node : handlers) { catchAll |= node.type == null; } } if (!catchAll) { ((LockFrame) frames[i]).assertNoLock("No handlers for insn with lock"); } } } }
From source file:org.jacoco.playground.filter.JavacFinallyFilter.java
License:Open Source License
private boolean isBlockEnd(AbstractInsnNode node, int var) { if (node.getOpcode() != Opcodes.ALOAD) { return false; }//ww w. j a v a 2s. co m if (((VarInsnNode) node).var != var) { return false; } final AbstractInsnNode next = getNext(node); return next != null && next.getOpcode() == Opcodes.ATHROW; }
From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java
License:Open Source License
/** * Generates the instruction to throw an exception. */ public void throwException() { visitInsn(Opcodes.ATHROW); }
From source file:org.jboss.byteman.agent.adapter.RuleTriggerMethodAdapter.java
License:Open Source License
@Override public void visitInsn(int opcode) { super.visitInsn(opcode); // look for interesting instructions which need inserting into the CFG switch (opcode) { case Opcodes.IRETURN: case Opcodes.LRETURN: case Opcodes.FRETURN: case Opcodes.DRETURN: case Opcodes.ARETURN: case Opcodes.RETURN: case Opcodes.ATHROW: { // add this instruction to the current block and then start a new current block cfg.add(opcode);/*from w w w. j a v a 2 s . co m*/ Label newStart = super.newLabel(); // must call split before visiting the label cfg.split(newStart); visitLabel(newStart); } break; case Opcodes.MONITORENTER: case Opcodes.MONITOREXIT: { // just add this instruction to the current block cfg.add(opcode); } break; default: { cfg.add(opcode); } } }
From source file:org.jboss.byteman.agent.adapter.RuleTriggerMethodAdapter.java
License:Open Source License
@Override public void visitMaxs(int maxStack, int maxLocals) { Type returnType = Type.getReturnType(descriptor); // check whether there are outstanding monitor opens at the start of the trigger // block and, if so, insert a handler which unlocks the monitor and then rethrows // the exception. Iterator<TriggerDetails> iterator = cfg.triggerDetails(); boolean noneLeft = true; while (iterator.hasNext()) { TriggerDetails details = iterator.next(); // see if this trigger has any open monitor enters which will need closing Iterator<CodeLocation> openEnters = cfg.getOpenMonitors(details); if (openEnters.hasNext()) { // add a handler here which unlocks each object and rethrows the // saved exception then protect it with a try catch block and update // the trigger details so that it is the target of this block // generate a rethrow handler for each exception type Label newStart = newLabel(); Label newEnd = newLabel(); // if we get here the return and throw handlers labels should be null if (details.getEarlyReturnHandler() != null || details.getThrowHandler() != null) { System.out.println("unexpected : trigger region with open monitorenters has subtype handler!"); }/*from w ww. jav a 2 s. c om*/ // generate rethrow code and mark the handler as a try catch block for all // three exception types newStart = newLabel(); newEnd = newLabel(); Label newEarlyReturn = newLabel(); Label newThrow = newLabel(); visitLabel(details.getExecuteHandler()); Label newExecute = newLabel(); visitLabel(newStart); while (openEnters.hasNext()) { CodeLocation enterLocation = openEnters.next(); int varIdx = cfg.getSavedMonitorIdx(enterLocation); // call super method to avoid indexing these instructions visitVarInsn(Opcodes.ALOAD, varIdx); visitInsn(Opcodes.MONITOREXIT); } visitInsn(Opcodes.ATHROW); // add try catch blocks for the 3 exception types visitTryCatchBlock(newStart, newEnd, newEarlyReturn, CFG.EARLY_RETURN_EXCEPTION_TYPE_NAME); visitTryCatchBlock(newStart, newEnd, newThrow, CFG.THROW_EXCEPTION_TYPE_NAME); visitTryCatchBlock(newStart, newEnd, newExecute, CFG.EXECUTE_EXCEPTION_TYPE_NAME); // visit the end of the handler blocks so they gets processed visitLabel(newEnd); // update the details so it tracks these new ecetpion regions details.setThrowHandler(newThrow); details.setEarlyReturnHandler(newEarlyReturn); details.setExecuteHandler(newExecute); } } // ok, so now we have to add the handler code for trigger block try catch handlers // we only need to add the handler code once but we need to make sure it is the target of // all the try catch blocks by visiting their handler label before we insert the code iterator = cfg.triggerDetails(); while (iterator.hasNext()) { TriggerDetails details = iterator.next(); visitLabel(details.getEarlyReturnHandler()); } if (Transformer.isVerbose()) { getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class)); visitLdcInsn("caught ReturnException"); invokeVirtual(Type.getType(PrintStream.class), Method.getMethod("void println(String)")); } // add exception handling code subclass first if (returnType == Type.VOID_TYPE) { // drop exception and just return pop(); visitInsn(Opcodes.RETURN); } else { // fetch value from exception, unbox if needed and return value Method getReturnValueMethod = Method.getMethod("Object getReturnValue()"); invokeVirtual(CFG.EARLY_RETURN_EXCEPTION_TYPE, getReturnValueMethod); unbox(returnType); returnValue(); } iterator = cfg.triggerDetails(); while (iterator.hasNext()) { TriggerDetails details = iterator.next(); visitLabel(details.getThrowHandler()); } if (Transformer.isVerbose()) { getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class)); visitLdcInsn("caught ThrowException"); invokeVirtual(Type.getType(PrintStream.class), Method.getMethod("void println(String)")); } // fetch value from exception, unbox if needed and return value Method getThrowableMethod = Method.getMethod("Throwable getThrowable()"); invokeVirtual(CFG.THROW_EXCEPTION_TYPE, getThrowableMethod); throwException(); // execute exception comes last because it is the super of the othher two classes iterator = cfg.triggerDetails(); while (iterator.hasNext()) { TriggerDetails details = iterator.next(); visitLabel(details.getExecuteHandler()); } if (Transformer.isVerbose()) { getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class)); visitLdcInsn("caught ExecuteException"); invokeVirtual(Type.getType(PrintStream.class), Method.getMethod("void println(String)")); } // rethrow an execute exception throwException(); super.visitMaxs(maxStack, maxLocals); // hmm, don't think we need this cfg.visitMaxs(); }
From source file:org.jboss.byteman.rule.expression.ReturnExpression.java
License:Open Source License
public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException { // make sure we are at the right source line compileContext.notifySourceLine(line); Type valueType = (returnValue == null ? Type.VOID : returnValue.getType()); int currentStack = compileContext.getStackCount(); int expected = 1; // ok, we need to create the EarlyReturnException instance and then // initialise it using the appropriate return value or null if no // return value is needed. strictly we should maybe delay the // new until after computing the return expression so we avoid a new // if the expression throws an error. but that means we end up doing // stack manipulations so lets do it the easy way. // create am EarlyReturnException -- adds 1 to stack String exceptionClassName = Type.internalName(EarlyReturnException.class); mv.visitTypeInsn(Opcodes.NEW, exceptionClassName); compileContext.addStackCount(1);/*from ww w .jav a 2 s . c om*/ // copy the exception so we can initialise it -- adds 1 to stack mv.visitInsn(Opcodes.DUP); compileContext.addStackCount(1); // stack a string constant to initialise the exception with -- adds 1 to stack mv.visitLdcInsn("return from " + rule.getName()); compileContext.addStackCount(1); // stack any required return value or null -- adds 1 to stack but may use 2 slots if (returnValue != null) { returnValue.compile(mv, compileContext); // we may need to convert from the value type to the return type if (valueType != type) { compileTypeConversion(valueType, type, mv, compileContext); } if (type.isPrimitive()) { // we need an object not a primitive compileBox(Type.boxType(type), mv, compileContext); } } else { // just push null mv.visitInsn(Opcodes.ACONST_NULL); compileContext.addStackCount(1); } // construct the exception -- pops 3 mv.visitMethodInsn(Opcodes.INVOKESPECIAL, exceptionClassName, "<init>", "(Ljava/lang/String;Ljava/lang/Object;)V"); compileContext.addStackCount(-3); // check current stack and increment max stack if necessary if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("ReturnExpression.compile : invalid stack height " + compileContext.getStackCount() + " expecting " + (currentStack + expected)); } // now insert the throw instruction and decrement the stack height accordingly mv.visitInsn(Opcodes.ATHROW); compileContext.addStackCount(-1); }
From source file:org.jboss.byteman.rule.expression.ThrowExpression.java
License:Open Source License
public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException { // make sure we are at the right source line compileContext.notifySourceLine(line); int currentStack = compileContext.getStackCount(); int expected = 1; int extraParams = 0; // ok, we need to create the thrown exception instance and then // initialise it. // create the thrown exception instance -- adds 1 to stack String exceptionClassName = type.getInternalName(); mv.visitTypeInsn(Opcodes.NEW, exceptionClassName); compileContext.addStackCount(1);//w w w .j av a 2s . c o m // copy the exception so we can init it mv.visitInsn(Opcodes.DUP); compileContext.addStackCount(1); int argCount = arguments.size(); // stack each of the arguments to the constructor for (int i = 0; i < argCount; i++) { Type argType = argumentTypes.get(i); Type paramType = paramTypes.get(i); int paramCount = (paramType.getNBytes() > 4 ? 2 : 1); // track extra storage used after type conversion extraParams += (paramCount); arguments.get(i).compile(mv, compileContext); compileTypeConversion(argType, paramType, mv, compileContext); } // construct the exception mv.visitMethodInsn(Opcodes.INVOKESPECIAL, exceptionClassName, "<init>", getDescriptor()); // modify the stack height to account for the removed exception and params compileContext.addStackCount(-(extraParams + 1)); if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("ThrowExpression.compile : invalid stack height " + compileContext.getStackCount() + " expecting " + (currentStack + expected)); } // now create a ThrowException to wrap the user exception // create the thrown exception instance -- adds 1 to stack [UE] --> [UE, THE] exceptionClassName = "org/jboss/byteman/rule/exception/ThrowException"; mv.visitTypeInsn(Opcodes.NEW, exceptionClassName); compileContext.addStackCount(1); // copy the ThrowException so we can init it [UE, THE] --> [THE, UE, THE] mv.visitInsn(Opcodes.DUP_X1); compileContext.addStackCount(1); // reverse the order of the top two words [THE, UE, THE] --> [THE, THE, UE] mv.visitInsn(Opcodes.SWAP); // construct the exception [THE, THE, UE] --> [UE] mv.visitMethodInsn(Opcodes.INVOKESPECIAL, exceptionClassName, "<init>", "(Ljava/lang/Throwable;)V"); // we should now have just the ThrowException on the stack compileContext.addStackCount(-2); if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("ThrowExpression.compile : invalid stack height " + compileContext.getStackCount() + " expecting " + (currentStack + expected)); } // now throw the exception and decrement the stack height mv.visitInsn(Opcodes.ATHROW); compileContext.addStackCount(-1); }
From source file:org.jruby.javasupport.proxy.JavaProxyClassFactory.java
License:LGPL
private static void generateProxyMethod(Type selfType, Type superType, ClassVisitor cw, GeneratorAdapter clazzInit, MethodData md) { if (!md.generateProxyMethod()) { return;/*from ww w .j av a 2 s. com*/ } org.objectweb.asm.commons.Method m = md.getMethod(); Type[] ex = toType(md.getExceptions()); String field_name = "__mth$" + md.getName() + md.scrambledSignature(); // create static private method field FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, field_name, PROXY_METHOD_TYPE.getDescriptor(), null, null); fv.visitEnd(); clazzInit.dup(); clazzInit.push(m.getName()); clazzInit.push(m.getDescriptor()); clazzInit.push(md.isImplemented()); clazzInit.invokeStatic(PROXY_HELPER_TYPE, PROXY_HELPER_GET_METHOD); clazzInit.putStatic(selfType, field_name, PROXY_METHOD_TYPE); org.objectweb.asm.commons.Method sm = new org.objectweb.asm.commons.Method("__super$" + m.getName(), m.getReturnType(), m.getArgumentTypes()); // // construct the proxy method // GeneratorAdapter ga = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, ex, cw); ga.loadThis(); ga.getField(selfType, INVOCATION_HANDLER_FIELD_NAME, INVOCATION_HANDLER_TYPE); // if the method is extending something, then we have // to test if the handler is initialized... if (md.isImplemented()) { ga.dup(); Label ok = ga.newLabel(); ga.ifNonNull(ok); ga.loadThis(); ga.loadArgs(); ga.invokeConstructor(superType, m); ga.returnValue(); ga.mark(ok); } ga.loadThis(); ga.getStatic(selfType, field_name, PROXY_METHOD_TYPE); if (m.getArgumentTypes().length == 0) { // load static empty array ga.getStatic(JAVA_PROXY_TYPE, "NO_ARGS", Type.getType(Object[].class)); } else { // box arguments ga.loadArgArray(); } Label before = ga.mark(); ga.invokeInterface(INVOCATION_HANDLER_TYPE, INVOCATION_HANDLER_INVOKE_METHOD); Label after = ga.mark(); ga.unbox(m.getReturnType()); ga.returnValue(); // this is a simple rethrow handler Label rethrow = ga.mark(); ga.visitInsn(Opcodes.ATHROW); for (int i = 0; i < ex.length; i++) { ga.visitTryCatchBlock(before, after, rethrow, ex[i].getInternalName()); } ga.visitTryCatchBlock(before, after, rethrow, "java/lang/Error"); ga.visitTryCatchBlock(before, after, rethrow, "java/lang/RuntimeException"); Type thr = Type.getType(Throwable.class); Label handler = ga.mark(); Type udt = Type.getType(UndeclaredThrowableException.class); int loc = ga.newLocal(thr); ga.storeLocal(loc, thr); ga.newInstance(udt); ga.dup(); ga.loadLocal(loc, thr); ga.invokeConstructor(udt, org.objectweb.asm.commons.Method.getMethod("void <init>(java.lang.Throwable)")); ga.throwException(); ga.visitTryCatchBlock(before, after, handler, "java/lang/Throwable"); ga.endMethod(); // // construct the super-proxy method // if (md.isImplemented()) { GeneratorAdapter ga2 = new GeneratorAdapter(Opcodes.ACC_PUBLIC, sm, null, ex, cw); ga2.loadThis(); ga2.loadArgs(); ga2.invokeConstructor(superType, m); ga2.returnValue(); ga2.endMethod(); } }
From source file:org.krzogr.trackthread.instrument.RunMethodVisitor.java
License:Open Source License
@Override public void visitMaxs(final int maxStack, final int maxLocals) { Label endFinally = new Label(); mv.visitTryCatchBlock(startFinally, endFinally, endFinally, null); mv.visitLabel(endFinally);/*from w w w .j av a2 s .c o m*/ onFinally(Opcodes.ATHROW); mv.visitInsn(Opcodes.ATHROW); mv.visitMaxs(maxStack, maxLocals); }
From source file:org.krzogr.trackthread.instrument.RunMethodVisitor.java
License:Open Source License
@Override protected void onMethodExit(final int opcode) { if (opcode != Opcodes.ATHROW) { onFinally(opcode); } }