-
Notifications
You must be signed in to change notification settings - Fork 1
/
Translator.java
198 lines (175 loc) · 7.54 KB
/
Translator.java
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package plainenglishjavadebugger.translationModule;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IStackFrame;
import org.eclipse.jdt.debug.core.IJavaThread;
import plainenglishjavadebugger.translationModule.fileReader.SourceFileReader;
import plainenglishjavadebugger.views.translatorView.TranslatorViewModel;
/*
* This code belongs to:
* Ahmet Emre Unal
* S001974
* emre.unal@ozu.edu.tr
*/
/*
* Scheduling a translation job: http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/guide/runtime_jobs.htm?cp=2_0_3_5
*
* When the status of the debugger changes, the translation job can be started, joined, executed and terminated.
* Can get other info about other jobs through IJobManager.
* Set a listener to the debugger, when it's status changes, start the translation job.
*
* Translation job should be a system job:
* (http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/guide/runtime_jobs_progress.htm?cp=2_0_3_5_0)
*
* This, most probably, is a SHORT job (Job Scheduling).
*/
public class Translator {
private final TranslatorViewModel model;
private final SourceFileReader sourceFileReader;
private IJavaThread thread;
private TranslatedLine translatedLine;
private IStackFrame topStackFrame;
private Object debuggedClassSourceElement;
private int debuggedLineNumber;
private String debuggedClassPath;
private int debugEventType;
private String executedSourceLine;
private String[] ignoredLines = { "}", "});" };
// Java regex info: http://docs.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html
private final String javaNameRegex = "[a-zA-Z]*[a-zA-Z_0-9]*";
private final String argumentRegex = "['('].*[')']";
private final String messageSendRegex = javaNameRegex + "['.']";
private final String assignmentRegex = "(" + messageSendRegex + ")?" + javaNameRegex + "[ ]=[ ]";
private final String assignmentPossibilityRegex = "(" + assignmentRegex + ")?";
private final String methodCallStatementRegex = assignmentPossibilityRegex + "(" + messageSendRegex + ")*" + javaNameRegex + "[ ]?" + argumentRegex + "[';']";
private final String instantiationStatementRegex = assignmentPossibilityRegex + "new[ ]" + javaNameRegex + "[ ]?" + argumentRegex + "[';']";
private final String visibilityDeclarationRegex = "\bprivate\b|\bpublic\b|\bprotected\b";
private final String returnTypeRegex = javaNameRegex;
private final String methodEnterStatementRegex = "(" + visibilityDeclarationRegex + ")?" + "[ ]" + returnTypeRegex + "[ ]" + javaNameRegex + "[ ]?" + argumentRegex + "[ ]?['{']";
public Translator(TranslatorViewModel model) {
this.model = model;
sourceFileReader = new SourceFileReader();
resetTranslator();
}
public void setThread(IJavaThread thread) {
this.thread = thread;
}
public void clearThread() {
thread = null;
}
// The method that does the translation work
public void translate(int debugEventType) {
try {
getCurrentFrameInfo();
if (sourceCodeIsAvailable()) {
this.debugEventType = debugEventType;
getCurrentClassInfo();
generateTranslatedLine();
} else {
System.out.println("Undebuggable, closed-source class.");
}
} catch (DebugException e) {
System.err.println("Unable to get stack frame info from thread!");
e.printStackTrace();
}
}
private void getCurrentFrameInfo() throws DebugException {
topStackFrame = thread.getTopStackFrame();
debuggedClassSourceElement = topStackFrame.getLaunch().getSourceLocator().getSourceElement(topStackFrame);
}
private boolean sourceCodeIsAvailable() {
/* Which means it's not a built-in, closed-source library class */
return debuggedClassSourceElement != null;
}
private void getCurrentClassInfo() throws DebugException {
debuggedLineNumber = topStackFrame.getLineNumber();
debuggedClassPath = debuggedClassSourceElement.toString();
}
private void generateTranslatedLine() {
executedSourceLine = sourceFileReader.readLine(topStackFrame, debuggedClassPath, debuggedLineNumber);
if (!lineIsIgnored()) {
createTranslatedLineObject();
setTranslationInfo();
returnLine();
resetTranslator();
}
}
private void createTranslatedLineObject() {
translatedLine = new TranslatedLine(executedSourceLine, debugEventType);
}
private void setTranslationInfo() {
identifyStatementType();
}
private void identifyStatementType() {
if (executedSourceLine.startsWith("if")) {
translatedLine.setStatementType(StatementType.IF);
translatedLine.setShortDescription("This is an if-statement.");
translatedLine.setLongDescription(debuggedClassPath + " " + debuggedLineNumber);
} else if (executedSourceLine.startsWith("for")) {
translatedLine.setStatementType(StatementType.FOR_LOOP);
translatedLine.setShortDescription("This is a for-loop.");
translatedLine.setLongDescription(debuggedClassPath + " " + debuggedLineNumber);
} else if (executedSourceLine.startsWith("while")) {
translatedLine.setStatementType(StatementType.WHILE_LOOP);
translatedLine.setShortDescription("This is a while-loop.");
translatedLine.setLongDescription(debuggedClassPath + " " + debuggedLineNumber);
} else if (executedSourceLine.startsWith("switch")) {
translatedLine.setStatementType(StatementType.SWITCH);
translatedLine.setShortDescription("This is a switch-statement.");
translatedLine.setLongDescription(debuggedClassPath + " " + debuggedLineNumber);
} else if (executedSourceLine.startsWith("return")) {
translatedLine.setStatementType(StatementType.RETURN);
translatedLine.setShortDescription("This is a return-statement.");
translatedLine.setLongDescription(debuggedClassPath + " " + debuggedLineNumber);
} else if (executedSourceLine.matches(instantiationStatementRegex)) {
translatedLine.setStatementType(StatementType.INSTANTIATION);
translatedLine.setShortDescription("This is an instantiation.");
translatedLine.setLongDescription(debuggedClassPath + " " + debuggedLineNumber);
} else if (executedSourceLine.matches(methodCallStatementRegex)) {
translatedLine.setStatementType(StatementType.METHOD_CALL);
translatedLine.setShortDescription("This is a method call.");
translatedLine.setLongDescription(debuggedClassPath + " " + debuggedLineNumber);
} /*
* else if (executedSourceLine.matches(methodEnterStatementRegex)) {
* // Can this ever be reached? Does the debugger highlight method entrances?
* translatedLine.setStatementType(StatementType.METHOD_ENTER);
* translatedLine.setShortDescription("This is a method entrance.");
* translatedLine.setLongDescription(debuggedClassPath + " " + debuggedLineNumber);
* }
*/else {
translatedLine.setStatementType(StatementType.OTHER);
translatedLine.setShortDescription("This is something else.");
translatedLine.setLongDescription(debuggedClassPath + " " + debuggedLineNumber);
}
}
private void returnLine() {
model.addTranslatedLine(translatedLine);
}
private void resetTranslator() {
translatedLine = null;
topStackFrame = null;
debuggedClassSourceElement = null;
debuggedLineNumber = -1;
debuggedClassPath = "";
debugEventType = -1;
executedSourceLine = "";
}
private boolean lineIsIgnored() {
for (String ignoredLine : ignoredLines) {
if (ignoredLine.equals(executedSourceLine)) {
return true;
}
}
return false;
}
private String getDebugEventTypeName(int debugEventType) {
switch (debugEventType) {
case DebugEvent.STEP_OVER:
return "Stepped over the line";
case DebugEvent.STEP_INTO:
return "Stepped into the line";
default:
return "Undetermined debug event.";
}
}
}