1/*******************************************************************************
2 * Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *    Marc R. Hoffmann - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.core.internal.flow;
13
14import org.jacoco.core.internal.instr.InstrSupport;
15import org.objectweb.asm.Label;
16import org.objectweb.asm.MethodVisitor;
17import org.objectweb.asm.tree.MethodNode;
18
19/**
20 * A {@link MethodVisitor} with additional methods to get probe insertion
21 * information.
22 */
23public abstract class MethodProbesVisitor extends MethodVisitor {
24
25	/**
26	 * New visitor instance without delegate visitor.
27	 */
28	public MethodProbesVisitor() {
29		this(null);
30	}
31
32	/**
33	 * New visitor instance that delegates to the given visitor.
34	 *
35	 * @param mv
36	 *            optional next visitor in chain
37	 */
38	public MethodProbesVisitor(final MethodVisitor mv) {
39		super(InstrSupport.ASM_API_VERSION, mv);
40	}
41
42	/**
43	 * Visits an unconditional probe that should be inserted at the current
44	 * position.
45	 *
46	 * @param probeId
47	 *            id of the probe to insert
48	 */
49	@SuppressWarnings("unused")
50	public void visitProbe(final int probeId) {
51	}
52
53	/**
54	 * Visits a jump instruction. A probe with the given id should be inserted
55	 * in a way that it is executed only when the jump to the given label is
56	 * executed.
57	 *
58	 * @param opcode
59	 *            the opcode of the type instruction to be visited. This opcode
60	 *            is either IFEQ, IFNE, IFLT, IFGE, IFGT, IFLE, IF_ICMPEQ,
61	 *            IF_ICMPNE, IF_ICMPLT, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE,
62	 *            IF_ACMPEQ, IF_ACMPNE, GOTO, IFNULL or IFNONNULL.
63	 * @param label
64	 *            the operand of the instruction to be visited. This operand is
65	 *            a label that designates the instruction to which the jump
66	 *            instruction may jump.
67	 * @param probeId
68	 *            id of the probe
69	 * @param frame
70	 *            stackmap frame status after the execution of the jump
71	 *            instruction. The instance is only valid with the call of this
72	 *            method.
73	 * @see MethodVisitor#visitJumpInsn(int, Label)
74	 */
75	@SuppressWarnings("unused")
76	public void visitJumpInsnWithProbe(final int opcode, final Label label,
77			final int probeId, final IFrame frame) {
78	}
79
80	/**
81	 * Visits a zero operand instruction with a probe. This event is used only
82	 * for instructions that terminate the method. Therefore the probe must be
83	 * inserted before the actual instruction.
84	 *
85	 * @param opcode
86	 *            the opcode of the instruction to be visited. This opcode is
87	 *            either IRETURN, LRETURN, FRETURN, DRETURN, ARETURN, RETURN or
88	 *            ATHROW.
89	 * @param probeId
90	 *            id of the probe
91	 * @see MethodVisitor#visitInsn(int)
92	 */
93	@SuppressWarnings("unused")
94	public void visitInsnWithProbe(final int opcode, final int probeId) {
95	}
96
97	/**
98	 * Visits a TABLESWITCH instruction with optional probes for each target
99	 * label. Implementations can be optimized based on the fact that the same
100	 * target labels will always have the same probe id within a call to this
101	 * method. The probe id for each label can be obtained with
102	 * {@link LabelInfo#getProbeId(Label)}.
103	 *
104	 * @param min
105	 *            the minimum key value.
106	 * @param max
107	 *            the maximum key value.
108	 * @param dflt
109	 *            beginning of the default handler block.
110	 * @param labels
111	 *            beginnings of the handler blocks. <code>labels[i]</code> is
112	 *            the beginning of the handler block for the
113	 *            <code>min + i</code> key.
114	 * @param frame
115	 *            stackmap frame status after the execution of the switch
116	 *            instruction. The instance is only valid with the call of this
117	 *            method.
118	 * @see MethodVisitor#visitTableSwitchInsn(int, int, Label, Label[])
119	 */
120	@SuppressWarnings("unused")
121	public void visitTableSwitchInsnWithProbes(final int min, final int max,
122			final Label dflt, final Label[] labels, final IFrame frame) {
123	}
124
125	/**
126	 * Visits a LOOKUPSWITCH instruction with optional probes for each target
127	 * label. Implementations can be optimized based on the fact that the same
128	 * target labels will always have the same probe id within a call to this
129	 * method. The probe id for each label can be obtained with
130	 * {@link LabelInfo#getProbeId(Label)}.
131	 *
132	 * @param dflt
133	 *            beginning of the default handler block.
134	 * @param keys
135	 *            the values of the keys.
136	 * @param labels
137	 *            beginnings of the handler blocks. <code>labels[i]</code> is
138	 *            the beginning of the handler block for the
139	 *            <code>keys[i]</code> key.
140	 * @param frame
141	 *            stackmap frame status after the execution of the switch
142	 *            instruction. The instance is only valid with the call of this
143	 *            method.
144	 * @see MethodVisitor#visitLookupSwitchInsn(Label, int[], Label[])
145	 */
146	@SuppressWarnings("unused")
147	public void visitLookupSwitchInsnWithProbes(final Label dflt,
148			final int[] keys, final Label[] labels, final IFrame frame) {
149	}
150
151	/**
152	 * This method can be overwritten to hook into the process of emitting the
153	 * instructions of this method as <code>visitX()</code> events.
154	 *
155	 * @param methodNode
156	 *            the content to emit
157	 * @param methodVisitor
158	 *            A visitor to emit the content to. Note that this is not
159	 *            necessarily this visitor instance but some wrapper which
160	 *            calculates the probes.
161	 */
162	public void accept(final MethodNode methodNode,
163			final MethodVisitor methodVisitor) {
164		methodNode.accept(methodVisitor);
165	}
166
167}
168