下面列出了org.objectweb.asm.Opcodes# ATHROW 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void insert() {
InsnList trInsns = transformerNode.instructions;
AbstractInsnNode node = trInsns.getLast();
while (true) {
if (node == null)
break;
if (node instanceof LabelNode) {
node = node.getPrevious();
continue;
} else if (node.getOpcode() == Opcodes.RETURN) {
trInsns.remove(node);
} else if (node.getOpcode() == Opcodes.ATHROW && node.getPrevious().getOpcode() == Opcodes.ACONST_NULL) {
AbstractInsnNode prev = node.getPrevious();
trInsns.remove(node);
trInsns.remove(prev);
}
break;
}
resultNode.instructions.insert(trInsns);
}
private void instrumentToTrackUnhandledExceptions() {
// Based on: http://modularity.info/conference/2007/program/industry/I5-UsingASMFramework.pdf
logger.log(" - instrumentToTrackUnhandledExceptions");
Label endFinally = new Label();
super.visitTryCatchBlock(startFinally, endFinally, endFinally, "java/lang/Throwable");
super.visitLabel(endFinally);
// Record exception
super.visitInsn(Opcodes.DUP);
super.visitLdcInsn(lineNumber);
super.visitLdcInsn(methodName);
super.visitLdcInsn(Type.getType("L" + className + ";"));
super.visitMethodInsn(Opcodes.INVOKESTATIC, instrumentationActions.trackerClass, "trackUnhandledException", "(Ljava/lang/Throwable;ILjava/lang/String;Ljava/lang/Class;)V", false);
// Rethrow
super.visitInsn(Opcodes.ATHROW);
}
public void visitEnd() {
if (!methodDone && methodStartLabel != null && constructor) {
methodDone = true;
Label methodEndLabel = super.mark();
super.catchException(methodStartLabel, methodEndLabel, Type
.getType(RuntimeException.class));
super.visitMethodInsn(Opcodes.INVOKESTATIC, name,
LOG_INTERNAL_ALLOC_DEC, VOID_SIGNATURE);
super.visitInsn(Opcodes.ATHROW);
if (exceptions != null) {
for (String ex : exceptions) {
super.catchException(methodStartLabel, methodEndLabel,
Type.getObjectType(ex));
super.visitMethodInsn(Opcodes.INVOKESTATIC, name,
LOG_INTERNAL_ALLOC_DEC, VOID_SIGNATURE);
super.visitInsn(Opcodes.ATHROW);
}
}
}
super.visitEnd();
}
@Override
public void visitInsn(int opcode) {
if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW) {
if (autoMatch) {
matches.add(new StackTraceElement(className, methodName, fileName, lineNumber));
}
}
super.visitInsn(opcode);
}
@Override
public void visitInsn(int opcode) {
if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW) {
if (autoMatch) {
emitAutoMatchEvent();
}
}
super.visitInsn(opcode);
}
@Override
public void visitInsn(final int opcode) {
super.visitInsn(opcode);
execute(opcode, 0, null);
if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW) {
this.locals = null;
this.stack = null;
}
}
@Override
public void visitEnd() {
if (!hasCode) {
super.visitTypeInsn(Opcodes.NEW, EXCEPTION_INTERNAL_NAME);
super.visitInsn(Opcodes.DUP);
super.visitMethodInsn(
Opcodes.INVOKESPECIAL, EXCEPTION_INTERNAL_NAME, "<init>", "()V", /*itf*/ false);
super.visitInsn(Opcodes.ATHROW);
super.visitMaxs(0, 0); // triggers computation of the actual max's
}
super.visitEnd();
}
@Override
public void visitInsn(int opcode) {
this.currentBuildingBlock.add(opcode);
// Note that this could be an athrow, in which case we should handle this as a label.
// (this, like the jump case, shouldn't normally matter since there shouldn't be unreachable code after it).
if (Opcodes.ATHROW == opcode) {
handleLabel();
}
}
@Override
public void visitInsn(final int opcode) {
super.visitInsn(opcode);
execute(opcode, 0, null);
if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW) {
this.locals = null;
this.stack = null;
}
}
@Test
public void test_throwException() throws Exception {
List<BasicBlock> initBlocks = METHOD_BLOCKS.get("throwException()I");
int[][] expectedInitBlocks = new int[][]{
{Opcodes.NEW, Opcodes.DUP, Opcodes.INVOKESPECIAL, Opcodes.ATHROW},
};
boolean didMatch = compareBlocks(expectedInitBlocks, initBlocks);
Assert.assertTrue(didMatch);
BytecodeFeeScheduler s = new BytecodeFeeScheduler();
s.initialize();
}
@Override
public void visitInsn(int opcode) {
if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN)
|| opcode == Opcodes.ATHROW) {
//方法在返回之前,打印"end"
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("end");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
}
mv.visitInsn(opcode);
}
@Override
public void visitInsn(int opcode) {
switch (opcode) {
case Opcodes.MONITORENTER:
mBuilder.setUsesConcurrency();
break;
case Opcodes.ARETURN:
case Opcodes.IRETURN:
case Opcodes.LRETURN:
case Opcodes.DRETURN:
case Opcodes.FRETURN:
if (identityState == IdentityMethodState.LOADED_PARAMETER) {
mBuilder.setIsIdentity();
}
sawReturn = true;
break;
case Opcodes.RETURN:
sawReturn = true;
break;
case Opcodes.ATHROW:
if (stubState == StubState.INITIALIZE_RUNTIME) {
sawStubThrow = true;
} else if (justSawInitializationOfUnsupportedOperationException) {
sawUnsupportedThrow = true;
} else {
sawNormalThrow = true;
}
break;
default:
break;
}
resetState();
}
@Override
public void visitInsn(final int opcode) {
super.visitInsn(opcode);
execute(opcode, 0, null);
if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW) {
this.locals = null;
this.stack = null;
}
}
@Override
public void visitInsn(int opcode) {
if (opcode == Opcodes.ATHROW) {
this.opcodesStack.add(opcode);
finishTracking();
}
super.visitInsn(opcode);
}
public boolean isComplexInstruction() {
return opcode == Opcodes.ATHROW;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
// see issue #249135
if (patchXnio && "org.xnio.nio.WorkerThread".equals(name)) { // NOI18N
try {
LOGGER.log(Level.INFO, "Patching the issue #249135");
String path = name.replace('.', '/').concat(".class"); // NOI18N
try (InputStream is = super.getResourceAsStream(path)) {
ClassReader cr = new ClassReader(is);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES) {
@Override
protected String getCommonSuperClass(String string, String string1) {
if ("org/xnio/nio/NioHandle".equals(string) // NOI18N
|| "org/xnio/nio/NioHandle".equals(string1)) { // NOI18N
return "java/lang/Object"; // NOI18N
}
return super.getCommonSuperClass(string, string1);
}
};
ClassNode node = new ClassNode(Opcodes.ASM7);
cr.accept(node, 0);
for (MethodNode m : (Collection<MethodNode>) node.methods) {
if ("execute".equals(m.name) // NOI18N
&& "(Ljava/lang/Runnable;)V".equals(m.desc)) { // NOI18N
InsnList list = m.instructions;
for (ListIterator it = list.iterator(); it.hasNext(); ) {
AbstractInsnNode n = (AbstractInsnNode) it.next();
if (n instanceof MethodInsnNode) {
MethodInsnNode mn = (MethodInsnNode) n;
if ("org/xnio/nio/Log".equals(mn.owner) // NOI18N
&& "threadExiting".equals(mn.name) // NOI18N
&& "()Ljava/util/concurrent/RejectedExecutionException;".equals(mn.desc)) { // NOI18N
if (it.hasNext()) {
AbstractInsnNode possibleThrow = (AbstractInsnNode) it.next();
if (possibleThrow.getOpcode() == Opcodes.ATHROW) {
it.set(new InsnNode(Opcodes.POP));
break;
}
}
}
}
}
break;
}
}
node.accept(cw);
byte[] newBytecode = cw.toByteArray();
return super.defineClass(name, newBytecode, 0, newBytecode.length);
}
} catch (Exception ex) {
// just fallback to original behavior
LOGGER.log(Level.INFO, null, ex);
}
}
return super.findClass(name);
}
protected void onMethodExit(int opcode) {
if (done || opcode == Opcodes.ATHROW || !isSynchronized()) return;
has_monitor_operation = true;
super.visitMethodInsn(Opcodes.INVOKESTATIC, className, LOG_INTERNAL_EXIT_METHOD, LOG_CLASS_SIGNATURE);
}
public void onMethodExit(int opcode) {
if (opcode != Opcodes.ATHROW)
addDec();
}
/**
* {@inheritDoc}
*
* @see org.objectweb.asm.MethodVisitor#visitInsn(int)
*/
@Override
public void visitInsn ( int opcode ) {
switch ( opcode ) {
case Opcodes.ARETURN:
Object ret = this.stack.pop();
Type sigType = Type.getReturnType(this.ref.getSignature());
Type retType = null;
Set<Type> altTypes = null;
if ( ret != null ) {
if ( ret instanceof SimpleType ) {
retType = ( (SimpleType) ret ).getType();
altTypes = ( (SimpleType) ret ).getAlternativeTypes();
}
else if ( ret instanceof MultiAlternatives ) {
retType = ( (MultiAlternatives) ret ).getCommonType();
}
}
if ( retType != null ) {
this.returnTypes.add(retType);
if ( altTypes != null ) {
this.returnTypes.addAll(altTypes);
}
}
else {
this.returnTypes.add(sigType);
}
this.stack.clear();
break;
case Opcodes.IRETURN:
case Opcodes.LRETURN:
case Opcodes.FRETURN:
case Opcodes.DRETURN:
case Opcodes.RETURN:
if ( this.log.isTraceEnabled() ) {
this.log.trace("Found return " + this.stack.pop()); //$NON-NLS-1$
}
this.stack.clear();
break;
case Opcodes.ATHROW:
Object thrw = this.stack.pop();
this.log.trace("Found throw " + thrw); //$NON-NLS-1$
this.stack.clear();
break;
default:
JVMImpl.handleJVMInsn(opcode, this.stack);
}
super.visitInsn(opcode);
}
public ThrowFrame(Frame throwable) {
super(Opcodes.ATHROW);
this.throwable = throwable;
this.throwable.children.add(this);
}