InstrSupport.java
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 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.instr;
13
14import org.objectweb.asm.MethodVisitor;
15import org.objectweb.asm.Opcodes;
16import org.objectweb.asm.commons.GeneratorAdapter;
17
18/**
19 * Constants and utilities for byte code instrumentation.
20 *
21 * @author Marc R. Hoffmann
22 * @version 0.4.1.20101007204400
23 */
24public final class InstrSupport {
25
26 private InstrSupport() {
27 }
28
29 // === Data Field ===
30
31 /**
32 * Name of the field that stores coverage information of a class.
33 */
34 public static final String DATAFIELD_NAME = "$jacocoData";
35
36 /**
37 * Access modifiers of the field that stores coverage information of a
38 * class.
39 */
40 public static final int DATAFIELD_ACC = Opcodes.ACC_SYNTHETIC
41 | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL
42 | Opcodes.ACC_TRANSIENT;
43
44 /**
45 * Data type of the field that stores coverage information for a class (
46 * <code>boolean[]</code>).
47 */
48 public static final String DATAFIELD_DESC = "[Z";
49
50 // === Init Method ===
51
52 /**
53 * Name of the initialization method.
54 */
55 public static final String INITMETHOD_NAME = "$jacocoInit";
56
57 /**
58 * Descriptor of the initialization method.
59 */
60 public static final String INITMETHOD_DESC = "()[Z";
61
62 /**
63 * Access modifiers of the initialization method.
64 */
65 public static final int INITMETHOD_ACC = Opcodes.ACC_SYNTHETIC
66 | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
67
68 // === Utilities ===
69
70 /**
71 * Generates the instruction to push the given int value on the stack.
72 * Implementation taken from {@link GeneratorAdapter#push(int)}.
73 *
74 * @param mv
75 * visitor to emit the instruction
76 * @param value
77 * the value to be pushed on the stack.
78 */
79 public static void push(final MethodVisitor mv, final int value) {
80 if (value >= -1 && value <= 5) {
81 mv.visitInsn(Opcodes.ICONST_0 + value);
82 } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
83 mv.visitIntInsn(Opcodes.BIPUSH, value);
84 } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
85 mv.visitIntInsn(Opcodes.SIPUSH, value);
86 } else {
87 mv.visitLdcInsn(new Integer(value));
88 }
89 }
90
91}