Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7282185c5a408333a5854e4a285de81a9df0fc4f (plain) (blame)
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
/**********************************************************************
 * This file is part of the "Object Teams Runtime Environment"
 *
 * Copyright 2003-2009 Berlin Institute of Technology, Germany.
 *
 * 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
 * $Id: DebugUtil.java 23408 2010-02-03 18:07:35Z stephan $
 *
 * Please visit http://www.objectteams.org for updates and contact.
 *
 * Contributors:
 * Berlin Institute of Technology - Initial API and implementation
 **********************************************************************/
package org.eclipse.objectteams.otre.util;

import de.fub.bytecode.generic.*;
import de.fub.bytecode.Constants;
import java.util.Enumeration;

import org.eclipse.objectteams.otre.OTConstants;

/**
 * @author Stephan Herrmann
 */
@SuppressWarnings("nls")
public class DebugUtil {

	// -----------------------------------------
	// -------- Development utilities  ---------
	// -----------------------------------------

    /**
    *  Creates an the instructions necessary to "System.out.println" 
	*  the given string.
    *
    *  @param cpg     the constant pool of the class where this instructions 
	*                 will be inserted
    *  @param string  the String to be printed
    *  @return        an InstructionList containing the necessary instructions
    */
	public static InstructionList createPrintln(ConstantPoolGen cpg, 
												InstructionFactory factory,
												String string) 
	{
        InstructionList il  = new InstructionList();
        ObjectType p_stream = new ObjectType("java.io.PrintStream");

        il.append(factory.createFieldAccess("java.lang.System", "out", 
											p_stream,
											Constants.GETSTATIC));
        il.append(new PUSH(cpg, string + "\n"));
        il.append(factory.createInvoke("java.io.PrintStream", "print", 
									   Type.VOID, 
									   new Type[] { Type.STRING }, 
									   Constants.INVOKEVIRTUAL));
        return il;
    }

	/**
	 * Create a <tt>System.out.println</tt> call for an <tt>Object</tt>
	 * argument. The argument is assumed to be on the stack and will not
	 * be consumed.
	 */
    public static InstructionList createPrintlnObj(InstructionFactory factory) {
        InstructionList il  = new InstructionList();
        ObjectType p_stream = new ObjectType("java.io.PrintStream");

		il.append(new DUP());
        il.append(factory.createFieldAccess("java.lang.System", "out", 
											p_stream,
											Constants.GETSTATIC));
		il.append(new SWAP());
        il.append(factory.createInvoke("java.io.PrintStream", "print", 
									   Type.VOID, 
									   new Type[] { OTConstants.object }, 
									   Constants.INVOKEVIRTUAL));
        return il;
    }

	/**
	 * Create a <tt>System.out.println</tt> call for an <tt>Exception</tt>
	 * argument. The argument is assumed to be on the stack and _will_
	 * be consumed. This effect is however only performed, if 
	 * the property ot.log.lift is set.
	 */
    public static InstructionList createReportExc(InstructionFactory factory) {
        InstructionList il  = new InstructionList();
		if (System.getProperty("ot.log.lift") == null) {
			il.append(new POP());
		} else {
			ObjectType p_stream = new ObjectType("java.io.PrintStream");
			
			il.append(factory.createFieldAccess("java.lang.System", "out", 
												p_stream,
												Constants.GETSTATIC));
			il.append(new SWAP());
			il.append(factory.createInvoke("java.io.PrintStream", "println", 
										   Type.VOID, 
										   new Type[] { OTConstants.object }, 
										   Constants.INVOKEVIRTUAL));
		}
        return il;
    }

	/**
	 * Create a <tt>System.out.println</tt> call for an <tt>int</tt>
	 * argument. The argument is assumed to be on the stack and will not
	 * be consumed.
	 */
    public static InstructionList createPrintlnInt(InstructionFactory factory) {
        InstructionList il  = new InstructionList();
        ObjectType p_stream = new ObjectType("java.io.PrintStream");

		il.append(new DUP());
        il.append(factory.createFieldAccess("java.lang.System", "out", 
											p_stream,
											Constants.GETSTATIC));
		il.append(new SWAP());
        il.append(factory.createInvoke("java.io.PrintStream", "print", 
									   Type.VOID, 
									   new Type[] { Type.INT }, 
									   Constants.INVOKEVIRTUAL));
        return il;
    }
    
    public static InstructionList createPrintlnBool(ConstantPoolGen cp) {
    	InstructionList il= new InstructionList();
    	int             out     = cp.addFieldref("java.lang.System", "out",
    											 "Ljava/io/PrintStream;");
    	int             println = cp.addMethodref("java.io.PrintStream", "println",
    											  "(Z)V");
    	il.append(new DUP());
    	il.append(new GETSTATIC(out));
    	il.append(new SWAP());
    	il.append(new INVOKEVIRTUAL(println));
    	return il;
    }

    @SuppressWarnings("unchecked")
	public static void printIL (InstructionList il, ConstantPoolGen cpg) {
        int off = 0;
        Enumeration en =  il.elements();
        while(en.hasMoreElements()) {
            Instruction i = ((InstructionHandle)en.nextElement()).getInstruction();
            off += i.produceStack(cpg);
            off -= i.consumeStack(cpg);
            System.out.print(off);
            System.out.println("  = "+i);
        }
    }

}

Back to the top