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 * $Id: $
12 *******************************************************************************/
13package org.jacoco.core.instr;
14
15import org.objectweb.asm.MethodVisitor;
16import org.objectweb.asm.Opcodes;
17import org.objectweb.asm.commons.GeneratorAdapter;
18
19/**
20 * Constants and utilities for byte code instrumentation.
21 *
22 * @author Marc R. Hoffmann
23 * @version $Revision: $
24 */
25public final class InstrSupport {
26
27 private InstrSupport() {
28 }
29
30 // === Data Field ===
31
32 /**
33 * Name of the field that stores coverage information of a class.
34 */
35 public static final String DATAFIELD_NAME = "$jacocoData";
36
37 /**
38 * Access modifiers of the field that stores coverage information of a
39 * class.
40 */
41 public static final int DATAFIELD_ACC = Opcodes.ACC_SYNTHETIC
42 | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
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}