aboutsummaryrefslogtreecommitdiff
path: root/third_party/jacoco-make-probe-inserter-subclassable.patch
blob: 03cfe5e6ea570c0fa1bb1bbe51eb754aeab57ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// SPDX-License-Identifier: EPL-2.0 and Apache-2.0
// These patches apply to JaCoCo (https://github.com/jacoco/jacoco) and are hereby made available under the terms of the
// Eclipse Public License 2.0 available at:
//   http://www.eclipse.org/legal/epl-2.0
diff --git org.jacoco.core/src/org/jacoco/core/internal/instr/ClassInstrumenter.java org.jacoco.core/src/org/jacoco/core/internal/instr/ClassInstrumenter.java
index 476c9e34..bc192dc6 100644
--- org.jacoco.core/src/org/jacoco/core/internal/instr/ClassInstrumenter.java
+++ org.jacoco.core/src/org/jacoco/core/internal/instr/ClassInstrumenter.java
@@ -24,6 +24,7 @@ import org.objectweb.asm.MethodVisitor;
 public class ClassInstrumenter extends ClassProbesVisitor {
 
 	private final IProbeArrayStrategy probeArrayStrategy;
+	private final IProbeInserterFactory probeInserterFactory;
 
 	private String className;
 
@@ -40,6 +41,22 @@ public class ClassInstrumenter extends ClassProbesVisitor {
 			final ClassVisitor cv) {
 		super(cv);
 		this.probeArrayStrategy = probeArrayStrategy;
+		this.probeInserterFactory = new IProbeInserterFactory() {
+			@Override
+			public ProbeInserter makeProbeInserter(int access, String name,
+					String desc, MethodVisitor mv,
+					IProbeArrayStrategy arrayStrategy) {
+				return new ProbeInserter(access, name, desc, mv, arrayStrategy);
+			}
+		};
+	}
+
+	public ClassInstrumenter(final IProbeArrayStrategy probeArrayStrategy,
+			final IProbeInserterFactory probeInserterFactory,
+			final ClassVisitor cv) {
+		super(cv);
+		this.probeArrayStrategy = probeArrayStrategy;
+		this.probeInserterFactory = probeInserterFactory;
 	}
 
 	@Override
@@ -71,8 +88,9 @@ public class ClassInstrumenter extends ClassProbesVisitor {
 			return null;
 		}
 		final MethodVisitor frameEliminator = new DuplicateFrameEliminator(mv);
-		final ProbeInserter probeVariableInserter = new ProbeInserter(access,
-				name, desc, frameEliminator, probeArrayStrategy);
+		final ProbeInserter probeVariableInserter =
+				probeInserterFactory.makeProbeInserter(access, name, desc,
+						frameEliminator, probeArrayStrategy);
 		return new MethodInstrumenter(probeVariableInserter,
 				probeVariableInserter);
 	}
diff --git org.jacoco.core/src/org/jacoco/core/internal/instr/IProbeInserterFactory.java org.jacoco.core/src/org/jacoco/core/internal/instr/IProbeInserterFactory.java
new file mode 100644
index 00000000..19c2a7e2
--- /dev/null
+++ org.jacoco.core/src/org/jacoco/core/internal/instr/IProbeInserterFactory.java
@@ -0,0 +1,8 @@
+package org.jacoco.core.internal.instr;
+
+import org.objectweb.asm.MethodVisitor;
+
+public interface IProbeInserterFactory {
+    ProbeInserter makeProbeInserter(int access, String name, String desc,
+            MethodVisitor mv, IProbeArrayStrategy arrayStrategy);
+}
diff --git org.jacoco.core/src/org/jacoco/core/internal/instr/ProbeInserter.java org.jacoco.core/src/org/jacoco/core/internal/instr/ProbeInserter.java
index 0f5b99ff..80965dfe 100644
--- org.jacoco.core/src/org/jacoco/core/internal/instr/ProbeInserter.java
+++ org.jacoco.core/src/org/jacoco/core/internal/instr/ProbeInserter.java
@@ -25,7 +25,7 @@ import org.objectweb.asm.TypePath;
  * addition the probe array has to be retrieved at the beginning of the method
  * and stored in a local variable.
  */
-class ProbeInserter extends MethodVisitor implements IProbeInserter {
+public class ProbeInserter extends MethodVisitor implements IProbeInserter {
 
 	private final IProbeArrayStrategy arrayStrategy;
 
@@ -36,7 +36,7 @@ class ProbeInserter extends MethodVisitor implements IProbeInserter {
 	private final boolean clinit;
 
 	/** Position of the inserted variable. */
-	private final int variable;
+	protected final int variable;
 
 	/** Label for the new beginning of the method */
 	private final Label beginLabel;
@@ -56,7 +56,7 @@ class ProbeInserter extends MethodVisitor implements IProbeInserter {
 	 *            callback to create the code that retrieves the reference to
 	 *            the probe array
 	 */
-	ProbeInserter(final int access, final String name, final String desc,
+	public ProbeInserter(final int access, final String name, final String desc,
 			final MethodVisitor mv, final IProbeArrayStrategy arrayStrategy) {
 		super(InstrSupport.ASM_API_VERSION, mv);
 		this.clinit = InstrSupport.CLINIT_NAME.equals(name);
@@ -91,6 +91,10 @@ class ProbeInserter extends MethodVisitor implements IProbeInserter {
 		mv.visitInsn(Opcodes.BASTORE);
 	}
 
+	protected Object getLocalVariableType() {
+		return InstrSupport.DATAFIELD_DESC;
+	}
+
 	@Override
 	public void visitCode() {
 		mv.visitLabel(beginLabel);
@@ -118,6 +122,10 @@ class ProbeInserter extends MethodVisitor implements IProbeInserter {
 	public AnnotationVisitor visitLocalVariableAnnotation(final int typeRef,
 			final TypePath typePath, final Label[] start, final Label[] end,
 			final int[] index, final String descriptor, final boolean visible) {
+		if (getLocalVariableType() == null) {
+			return mv.visitLocalVariableAnnotation(typeRef, typePath, start, end, index, descriptor, visible);
+		}
+
 		final int[] newIndex = new int[index.length];
 		for (int i = 0; i < newIndex.length; i++) {
 			newIndex[i] = map(index[i]);
@@ -137,6 +145,9 @@ class ProbeInserter extends MethodVisitor implements IProbeInserter {
 	}
 
 	private int map(final int var) {
+		if (getLocalVariableType() == null) {
+			return var;
+		}
 		if (var < variable) {
 			return var;
 		} else {
@@ -153,13 +164,18 @@ class ProbeInserter extends MethodVisitor implements IProbeInserter {
 					"ClassReader.accept() should be called with EXPAND_FRAMES flag");
 		}
 
+        if (getLocalVariableType() == null) {
+			mv.visitFrame(type, nLocal, local, nStack, stack);
+			return;
+		}
+
 		final Object[] newLocal = new Object[Math.max(nLocal, variable) + 1];
 		int idx = 0; // Arrays index for existing locals
 		int newIdx = 0; // Array index for new locals
 		int pos = 0; // Current variable position
 		while (idx < nLocal || pos <= variable) {
 			if (pos == variable) {
-				newLocal[newIdx++] = InstrSupport.DATAFIELD_DESC;
+				newLocal[newIdx++] = getLocalVariableType();
 				pos++;
 			} else {
 				if (idx < nLocal) {