diff options
author | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2015-01-04 18:51:18 +0100 |
---|---|---|
committer | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2015-01-04 18:51:18 +0100 |
commit | e4a474ce30af55463d114b5c18c9b59eadbef00b (patch) | |
tree | c50f0ae481c77aebab03cb30d5bad0a9934b8991 /org.jacoco.core | |
parent | 7946741470a5c7efc95af2079ad72d334a511b83 (diff) | |
download | jacoco-e4a474ce30af55463d114b5c18c9b59eadbef00b.tar.gz |
Encapsulate try/catch probe logic in MethodProbesAdapter.
Diffstat (limited to 'org.jacoco.core')
-rw-r--r-- | org.jacoco.core/src/org/jacoco/core/internal/flow/LabelFlowAnalyzer.java | 21 | ||||
-rw-r--r-- | org.jacoco.core/src/org/jacoco/core/internal/flow/MethodProbesAdapter.java | 30 |
2 files changed, 19 insertions, 32 deletions
diff --git a/org.jacoco.core/src/org/jacoco/core/internal/flow/LabelFlowAnalyzer.java b/org.jacoco.core/src/org/jacoco/core/internal/flow/LabelFlowAnalyzer.java index dd32a5f9..2ea154ea 100644 --- a/org.jacoco.core/src/org/jacoco/core/internal/flow/LabelFlowAnalyzer.java +++ b/org.jacoco.core/src/org/jacoco/core/internal/flow/LabelFlowAnalyzer.java @@ -32,13 +32,7 @@ public final class LabelFlowAnalyzer extends MethodVisitor { * Method to mark labels */ public static void markLabels(final MethodNode method) { - // We do not use the accept() method as ASM resets labels after every - // call to accept() - final MethodVisitor lfa = new LabelFlowAnalyzer(); - for (int i = method.tryCatchBlocks.size(); --i >= 0;) { - method.tryCatchBlocks.get(i).accept(lfa); - } - method.instructions.accept(lfa); + method.instructions.accept(new LabelFlowAnalyzer()); } /** @@ -61,19 +55,6 @@ public final class LabelFlowAnalyzer extends MethodVisitor { } @Override - public void visitTryCatchBlock(final Label start, final Label end, - final Label handler, final String type) { - // Enforce probe at the beginning of the block. Assuming the start of - // the block already is successor of some other code, adding a target - // makes the start a multitarget. However, if the start of the block - // also is the start of the method, no probe will be added. - LabelInfo.setTarget(start); - - // Mark exception handler as possible target of the block - LabelInfo.setTarget(handler); - } - - @Override public void visitJumpInsn(final int opcode, final Label label) { LabelInfo.setTarget(label); if (opcode == Opcodes.JSR) { diff --git a/org.jacoco.core/src/org/jacoco/core/internal/flow/MethodProbesAdapter.java b/org.jacoco.core/src/org/jacoco/core/internal/flow/MethodProbesAdapter.java index 2fe7e315..ce59d2f5 100644 --- a/org.jacoco.core/src/org/jacoco/core/internal/flow/MethodProbesAdapter.java +++ b/org.jacoco.core/src/org/jacoco/core/internal/flow/MethodProbesAdapter.java @@ -11,15 +11,15 @@ *******************************************************************************/ package org.jacoco.core.internal.flow; +import java.util.HashMap; +import java.util.Map; + import org.jacoco.core.JaCoCo; import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; import org.objectweb.asm.commons.AnalyzerAdapter; -import java.util.HashMap; -import java.util.Map; - /** * Adapter that creates additional visitor events for probes to be inserted into * a method. @@ -32,7 +32,7 @@ public final class MethodProbesAdapter extends MethodVisitor { private AnalyzerAdapter analyzer; - private Map<Label, Label> tryCatchProbeLabels = new HashMap<Label, Label>(); + private final Map<Label, Label> tryCatchProbeLabels; /** * Create a new adapter instance. @@ -47,6 +47,7 @@ public final class MethodProbesAdapter extends MethodVisitor { super(JaCoCo.ASM_API_VERSION, probesVisitor); this.probesVisitor = probesVisitor; this.idGenerator = idGenerator; + this.tryCatchProbeLabels = new HashMap<Label, Label>(); } /** @@ -63,12 +64,14 @@ public final class MethodProbesAdapter extends MethodVisitor { @Override public void visitTryCatchBlock(Label start, final Label end, final Label handler, final String type) { - // If a probe will be inserted before the start label, we'll need to use a - // different label for the try-catch block. + // If it is not the direct start of a method we insert an extra probe at + // the beginning of every try/catch block. To ensure that the probe + // itsel is still within the try/catch block, we introduce a new start + // label. if (tryCatchProbeLabels.containsKey(start)) { start = tryCatchProbeLabels.get(start); - } else if (LabelInfo.isMultiTarget(start) && LabelInfo.isSuccessor(start)) { - Label probeLabel = new Label(); + } else if (LabelInfo.isSuccessor(start)) { + final Label probeLabel = new Label(); LabelInfo.setSuccessor(probeLabel); tryCatchProbeLabels.put(start, probeLabel); start = probeLabel; @@ -78,11 +81,14 @@ public final class MethodProbesAdapter extends MethodVisitor { @Override public void visitLabel(final Label label) { - if (LabelInfo.isMultiTarget(label) && LabelInfo.isSuccessor(label)) { - if (tryCatchProbeLabels.containsKey(label)) { - probesVisitor.visitLabel(tryCatchProbeLabels.get(label)); - } + final Label tryCatchStartLabel = tryCatchProbeLabels.get(label); + if (tryCatchStartLabel != null) { + probesVisitor.visitLabel(tryCatchStartLabel); probesVisitor.visitProbe(idGenerator.nextId()); + } else { + if (LabelInfo.isMultiTarget(label) && LabelInfo.isSuccessor(label)) { + probesVisitor.visitProbe(idGenerator.nextId()); + } } probesVisitor.visitLabel(label); } |