aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/TryWithResourcesJavac11Filter.java
blob: 7a20f74f379f2efdecda14fca7b330de45847704 (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
/*******************************************************************************
 * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Evgeny Mandrikov - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.core.internal.analysis.filter;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.TryCatchBlockNode;

/**
 * Filters code which is generated for try-with-resources statement by javac
 * starting from version 11.
 */
public final class TryWithResourcesJavac11Filter implements IFilter {

	public void filter(final MethodNode methodNode,
			final IFilterContext context, final IFilterOutput output) {
		if (methodNode.tryCatchBlocks.isEmpty()) {
			return;
		}
		final Matcher matcher = new Matcher();
		for (TryCatchBlockNode t : methodNode.tryCatchBlocks) {
			if ("java/lang/Throwable".equals(t.type)) {
				matcher.match(t.handler, output, true);
				matcher.match(t.handler, output, false);
			}
		}
	}

	/**
	 * <pre>
	 *     r = ...;
	 *     try {
	 *         ...
	 *     } body-only-finally {
	 *         if (r != null)
	 *             r.close();
	 *     } catch (Throwable primaryExc) {
	 *         if (r != null)
	 *             try {
	 *                 r.close();
	 *             } catch (Throwable t) {
	 *                 primaryExc.addSuppressed(t);
	 *             }
	 *         throw primaryExc;
	 *     }
	 * </pre>
	 * 
	 * <code>null</code> check for resource is omitted when it is initialized
	 * using <code>new</code>
	 */
	private class Matcher extends AbstractMatcher {
		private boolean withNullCheck;

		private String expectedOwner;

		void match(final AbstractInsnNode start, final IFilterOutput output,
				final boolean withNullCheck) {
			this.withNullCheck = withNullCheck;
			vars.clear();
			expectedOwner = null;

			cursor = start.getPrevious();
			nextIsVar(Opcodes.ASTORE, "primaryExc");
			nextIsJavacClose();
			nextIs(Opcodes.GOTO);
			nextIsVar(Opcodes.ASTORE, "t");
			nextIsVar(Opcodes.ALOAD, "primaryExc");
			nextIsVar(Opcodes.ALOAD, "t");
			nextIsInvoke(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable",
					"addSuppressed", "(Ljava/lang/Throwable;)V"); // primaryExc.addSuppressed(t)
			nextIsVar(Opcodes.ALOAD, "primaryExc");
			nextIs(Opcodes.ATHROW);
			if (cursor == null) {
				return;
			}
			final AbstractInsnNode end = cursor;

			AbstractInsnNode s = start.getPrevious();
			cursor = start.getPrevious();
			while (!nextIsJavacClose()) {
				s = s.getPrevious();
				cursor = s;
				if (cursor == null) {
					return;
				}
			}
			s = s.getNext();

			final AbstractInsnNode m = cursor;
			next();
			if (cursor.getOpcode() != Opcodes.GOTO) {
				cursor = m;
			}

			output.ignore(s, cursor);
			output.ignore(start, end);
		}

		private boolean nextIsJavacClose() {
			if (withNullCheck) {
				nextIsVar(Opcodes.ALOAD, "r");
				nextIs(Opcodes.IFNULL);
			}
			nextIsClose();
			return cursor != null;
		}

		private void nextIsClose() {
			nextIsVar(Opcodes.ALOAD, "r");
			next();
			if (cursor == null) {
				return;
			}
			if (cursor.getOpcode() != Opcodes.INVOKEINTERFACE
					&& cursor.getOpcode() != Opcodes.INVOKEVIRTUAL) {
				cursor = null;
				return;
			}
			final MethodInsnNode m = (MethodInsnNode) cursor;
			if (!"close".equals(m.name) || !"()V".equals(m.desc)) {
				cursor = null;
				return;
			}
			final String actual = m.owner;
			if (expectedOwner == null) {
				expectedOwner = actual;
			} else if (!expectedOwner.equals(actual)) {
				cursor = null;
			}
		}

	}

}