aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc R. Hoffmann <hoffmann@mountainminds.com>2015-01-04 18:51:18 +0100
committerMarc R. Hoffmann <hoffmann@mountainminds.com>2015-01-04 18:51:18 +0100
commite4a474ce30af55463d114b5c18c9b59eadbef00b (patch)
treec50f0ae481c77aebab03cb30d5bad0a9934b8991
parent7946741470a5c7efc95af2079ad72d334a511b83 (diff)
downloadjacoco-e4a474ce30af55463d114b5c18c9b59eadbef00b.tar.gz
Encapsulate try/catch probe logic in MethodProbesAdapter.
-rw-r--r--org.jacoco.core.test/src/org/jacoco/core/internal/flow/LabelFlowAnalyzerTest.java11
-rw-r--r--org.jacoco.core.test/src/org/jacoco/core/internal/flow/MethodProbesAdapterTest.java14
-rw-r--r--org.jacoco.core/src/org/jacoco/core/internal/flow/LabelFlowAnalyzer.java21
-rw-r--r--org.jacoco.core/src/org/jacoco/core/internal/flow/MethodProbesAdapter.java30
4 files changed, 28 insertions, 48 deletions
diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/flow/LabelFlowAnalyzerTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/flow/LabelFlowAnalyzerTest.java
index 5d3ec36e..6748ba54 100644
--- a/org.jacoco.core.test/src/org/jacoco/core/internal/flow/LabelFlowAnalyzerTest.java
+++ b/org.jacoco.core.test/src/org/jacoco/core/internal/flow/LabelFlowAnalyzerTest.java
@@ -107,15 +107,6 @@ public class LabelFlowAnalyzerTest {
@Test
public void testFlowScenario10() {
- analyzer.visitTryCatchBlock(new Label(), new Label(), label,
- "java/lang/Exception");
- analyzer.visitJumpInsn(GOTO, label);
- assertTrue(LabelInfo.isMultiTarget(label));
- assertFalse(LabelInfo.isSuccessor(label));
- }
-
- @Test
- public void testFlowScenario11() {
// Even if the same label is referenced multiple times but from the same
// source instruction this is only counted as one target.
analyzer.visitLookupSwitchInsn(label, new int[] { 0, 1 }, new Label[] {
@@ -125,7 +116,7 @@ public class LabelFlowAnalyzerTest {
}
@Test
- public void testFlowScenario12() {
+ public void testFlowScenario11() {
// Even if the same label is referenced multiple times but from the same
// source instruction this is only counted as one target.
analyzer.visitTableSwitchInsn(0, 1, label, new Label[] { label, label });
diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/flow/MethodProbesAdapterTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/flow/MethodProbesAdapterTest.java
index 5590323f..f4925460 100644
--- a/org.jacoco.core.test/src/org/jacoco/core/internal/flow/MethodProbesAdapterTest.java
+++ b/org.jacoco.core.test/src/org/jacoco/core/internal/flow/MethodProbesAdapterTest.java
@@ -321,7 +321,8 @@ public class MethodProbesAdapterTest implements IProbeIdGenerator {
adapter.visitTryCatchBlock(start, end, handler, "java/lang/Exception");
adapter.visitLabel(start);
- expectedVisitor.visitTryCatchBlock(start, end, handler, "java/lang/Exception");
+ expectedVisitor.visitTryCatchBlock(start, end, handler,
+ "java/lang/Exception");
expectedVisitor.visitLabel(start);
}
@@ -329,7 +330,6 @@ public class MethodProbesAdapterTest implements IProbeIdGenerator {
public void testVisitTryCatchBlockWithProbe() {
Label target = new Label();
LabelInfo.setSuccessor(target);
- LabelInfo.setTarget(target);
Label end = new Label();
Label handler = new Label();
Label start = new Label();
@@ -337,7 +337,8 @@ public class MethodProbesAdapterTest implements IProbeIdGenerator {
adapter.visitTryCatchBlock(target, end, handler, "java/lang/Exception");
adapter.visitLabel(target);
- expectedVisitor.visitTryCatchBlock(start, end, handler, "java/lang/Exception");
+ expectedVisitor.visitTryCatchBlock(start, end, handler,
+ "java/lang/Exception");
expectedVisitor.visitLabel(start);
expectedVisitor.visitProbe(1000);
expectedVisitor.visitLabel(target);
@@ -347,7 +348,6 @@ public class MethodProbesAdapterTest implements IProbeIdGenerator {
public void testVisitMultipleTryCatchBlocksWithProbe() {
Label target = new Label();
LabelInfo.setSuccessor(target);
- LabelInfo.setTarget(target);
Label end = new Label();
Label handler1 = new Label();
Label handler2 = new Label();
@@ -357,8 +357,10 @@ public class MethodProbesAdapterTest implements IProbeIdGenerator {
adapter.visitTryCatchBlock(target, end, handler2, "java/io/IOException");
adapter.visitLabel(target);
- expectedVisitor.visitTryCatchBlock(start, end, handler1, "java/lang/Exception");
- expectedVisitor.visitTryCatchBlock(start, end, handler2, "java/io/IOException");
+ expectedVisitor.visitTryCatchBlock(start, end, handler1,
+ "java/lang/Exception");
+ expectedVisitor.visitTryCatchBlock(start, end, handler2,
+ "java/io/IOException");
expectedVisitor.visitLabel(start);
expectedVisitor.visitProbe(1000);
expectedVisitor.visitLabel(target);
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);
}