KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > Step


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 import gov.nasa.jpf.*;
22 import gov.nasa.jpf.jvm.bytecode.Instruction;
23 import gov.nasa.jpf.util.Left;
24
25 import java.io.PrintWriter JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import gov.nasa.jpf.util.SourceRef;
28 import gov.nasa.jpf.util.Source;
29
30
31 /**
32  * this corresponds to an executed instruction. Note that we can have a
33  * potentially huge number of Steps, hence we want to save objects here
34  * (e.g. Collection overhead)
35  */

36 public class Step extends SourceRef implements TransitionStep {
37     
38   Instruction insn;
39   String JavaDoc comment;
40   Step next;
41
42   // report options - statics are suboptimal, but we don't have a good path
43
// to the VM here
44
static boolean showBytecode;
45   static boolean showMissingLines;
46   
47   public String JavaDoc description; // on demand (cache)
48

49   public Step (String JavaDoc f, int l) {
50     super(f, l);
51   }
52
53   public Step (String JavaDoc file, int line, Instruction insn) {
54     this(file, line);
55     this.insn = insn;
56   }
57
58   static boolean init (Config config) {
59     showBytecode = config.getBoolean("vm.report.show_bytecode");
60     showMissingLines = config.getBoolean("vm.report.show_missing_lines");
61     return true;
62   }
63   
64   public Step getNext() {
65     return next;
66   }
67   
68   public SourceRef getSourceRef () {
69     return this;
70   }
71
72   public String JavaDoc getDescription () {
73     if (description != null) {
74       StringWriter JavaDoc sw = new StringWriter JavaDoc();
75       PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
76       
77       printStepOn(pw, null, null);
78       description = sw.toString();
79     }
80     
81     return description;
82   }
83   
84   public void setComment (String JavaDoc s) {
85     comment = s;
86   }
87
88   public String JavaDoc getComment () {
89     return comment;
90   }
91
92   public void printStepOn (PrintWriter JavaDoc pw, SourceRef lastPrinted, String JavaDoc prefix) {
93     Source source = Source.getSource(fileName);
94     boolean isLineMissing = source.isLineMissing(line);
95
96     if (!this.equals(lastPrinted)) {
97       if (!isLineMissing) {
98         pw.print( (prefix != null) ? prefix : " ");
99         pw.print(Left.format(fileName + ":" + line, 20));
100         pw.print(' ');
101         pw.println(source.getLine(line));
102       } else {
103         if (!fileName.equals(lastPrinted.getFileName()) && !showMissingLines) {
104           pw.print( (prefix != null) ? prefix : " ");
105           pw.print("[no source for: ");
106           pw.print(fileName);
107           pw.println(']');
108         }
109       }
110       
111       lastPrinted.set(this);
112     }
113
114     if (showBytecode || (showMissingLines && isLineMissing) ) {
115       if (insn != null) {
116         MethodInfo mi = insn.getMethod();
117         pw.print('\t');
118         pw.print(Left.format(mi.getClassInfo().getName() + "." +
119                                mi.getUniqueName() + "." + insn.getPosition() + ":", 40));
120         pw.println(insn);
121       }
122     }
123     
124     if (comment != null) {
125       pw.print(" // ");
126       pw.println(comment);
127     }
128   }
129
130   public void toXML (PrintWriter JavaDoc pw) {
131     pw.print("\t<Instruction File=\"");
132     pw.print( fileName);
133     pw.print("\" Line=\"");
134     pw.print(line);
135     pw.println("\"/>");
136
137     if (comment != null) {
138       pw.print("\t<Comment>");
139       pw.print( comment);
140       pw.println("</Comment>");
141     }
142   }
143 }
144
Popular Tags