aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/instr/ClassFileVersionsTest.java
blob: 41138c448e8cc30369c5391ac09dad99be25ed62 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*******************************************************************************
 * Copyright (c) 2009, 2022 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.core.instr;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_SUPER;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.F_NEW;
import static org.objectweb.asm.Opcodes.IFEQ;
import static org.objectweb.asm.Opcodes.ILOAD;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.POP;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.V10;
import static org.objectweb.asm.Opcodes.V11;
import static org.objectweb.asm.Opcodes.V12;
import static org.objectweb.asm.Opcodes.V13;
import static org.objectweb.asm.Opcodes.V14;
import static org.objectweb.asm.Opcodes.V15;
import static org.objectweb.asm.Opcodes.V16;
import static org.objectweb.asm.Opcodes.V1_1;
import static org.objectweb.asm.Opcodes.V1_2;
import static org.objectweb.asm.Opcodes.V1_3;
import static org.objectweb.asm.Opcodes.V1_4;
import static org.objectweb.asm.Opcodes.V1_5;
import static org.objectweb.asm.Opcodes.V1_6;
import static org.objectweb.asm.Opcodes.V1_7;
import static org.objectweb.asm.Opcodes.V1_8;
import static org.objectweb.asm.Opcodes.V9;

import java.io.IOException;

import org.jacoco.core.internal.instr.CondyProbeArrayStrategy;
import org.jacoco.core.internal.instr.InstrSupport;
import org.jacoco.core.runtime.IRuntime;
import org.jacoco.core.runtime.SystemPropertiesRuntime;
import org.junit.Test;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
 * Test class inserted stackmap frames for different class file versions.
 */
public class ClassFileVersionsTest {

	@Test
	public void test_1_1() throws IOException {
		testVersion(V1_1, false);
	}

	@Test
	public void test_1_2() throws IOException {
		testVersion(V1_2, false);
	}

	@Test
	public void test_1_3() throws IOException {
		testVersion(V1_3, false);
	}

	@Test
	public void test_1_4() throws IOException {
		testVersion(V1_4, false);
	}

	@Test
	public void test_1_5() throws IOException {
		testVersion(V1_5, false);
	}

	@Test
	public void test_1_6() throws IOException {
		testVersion(V1_6, true);
	}

	@Test
	public void test_1_7() throws IOException {
		testVersion(V1_7, true);
	}

	@Test
	public void test_1_8() throws IOException {
		testVersion(V1_8, true);
	}

	@Test
	public void test_9() throws IOException {
		testVersion(V9, true);
	}

	@Test
	public void test_10() throws IOException {
		testVersion(V10, true);
	}

	@Test
	public void test_11() throws IOException {
		testVersion(V11, true);
	}

	@Test
	public void test_12() throws IOException {
		testVersion(V12, true);
	}

	@Test
	public void test_13() throws IOException {
		testVersion(V13, true);
	}

	@Test
	public void test_14() throws IOException {
		testVersion(V14, true);
	}

	@Test
	public void test_15() throws IOException {
		testVersion(V15, true);
	}

	@Test
	public void test_16() throws IOException {
		testVersion(V16, true);
	}

	private void testVersion(int version, boolean frames) throws IOException {
		final byte[] original = createClass(version, frames);

		IRuntime runtime = new SystemPropertiesRuntime();
		Instrumenter instrumenter = new Instrumenter(runtime);
		byte[] instrumented = instrumenter.instrument(original, "TestTarget");

		assertFrames(instrumented, frames);
	}

	private void assertFrames(byte[] source, final boolean expected) {
		InstrSupport.classReaderFor(source)
				.accept(new ClassVisitor(InstrSupport.ASM_API_VERSION) {

					@Override
					public MethodVisitor visitMethod(int access, String name,
							final String desc, String signature,
							String[] exceptions) {
						return new MethodVisitor(InstrSupport.ASM_API_VERSION) {
							boolean frames = false;

							@Override
							public void visitFrame(int type, int nLocal,
									Object[] local, int nStack,
									Object[] stack) {
								frames = true;
							}

							@Override
							public void visitEnd() {
								if (CondyProbeArrayStrategy.B_DESC
										.equals(desc)) {
									assertFalse(
											"CondyProbeArrayStrategy does not need frames",
											frames);
								} else {
									assertEquals(Boolean.valueOf(expected),
											Boolean.valueOf(frames));
								}
							}
						};
					}
				}, 0);
	}

	/**
	 * Creates a class that requires a frame before the return statement. Also
	 * for this class instrumentation should insert another frame.
	 *
	 * <code><pre>
	 * public class Sample {
	 *   public Sample(boolean b){
	 *     if(b){
	 *       toString();
	 *     }
	 *     return;
	 *   }
	 * }
	 * </pre></code>
	 */
	private byte[] createClass(int version, boolean frames) {

		ClassWriter cw = new ClassWriter(0);
		MethodVisitor mv;

		cw.visit(version, ACC_PUBLIC + ACC_SUPER, "org/jacoco/test/Sample",
				null, "java/lang/Object", null);

		mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Z)V", null, null);
		mv.visitCode();
		mv.visitVarInsn(ALOAD, 0);
		mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V",
				false);
		mv.visitVarInsn(ILOAD, 1);
		Label l1 = new Label();
		mv.visitJumpInsn(IFEQ, l1);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString",
				"()Ljava/lang/String;", false);
		mv.visitInsn(POP);
		mv.visitLabel(l1);
		if (frames) {
			mv.visitFrame(F_NEW, 2,
					new Object[] { "org/jacoco/test/Sample", Opcodes.INTEGER },
					0, new Object[] {});
		}
		mv.visitInsn(RETURN);
		mv.visitMaxs(1, 2);
		mv.visitEnd();

		cw.visitEnd();

		return cw.toByteArray();
	}

}