From e4a474ce30af55463d114b5c18c9b59eadbef00b Mon Sep 17 00:00:00 2001 From: "Marc R. Hoffmann" Date: Sun, 4 Jan 2015 18:51:18 +0100 Subject: Encapsulate try/catch probe logic in MethodProbesAdapter. --- .../core/internal/flow/LabelFlowAnalyzer.java | 21 +-------------- .../core/internal/flow/MethodProbesAdapter.java | 30 +++++++++++++--------- 2 files changed, 19 insertions(+), 32 deletions(-) (limited to 'org.jacoco.core') 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()); } /** @@ -60,19 +54,6 @@ public final class LabelFlowAnalyzer extends MethodVisitor { super(JaCoCo.ASM_API_VERSION); } - @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); 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 tryCatchProbeLabels = new HashMap(); + private final Map 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(); } /** @@ -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); } -- cgit v1.2.3