Skip to content

Commit

Permalink
Added primitive statement type check. Removed redundant action class.…
Browse files Browse the repository at this point in the history
… Heckload of refactoring & bug fixes.
  • Loading branch information
aemreunal committed Dec 2, 2013
1 parent a9d414a commit 4d83e8c
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 141 deletions.
12 changes: 12 additions & 0 deletions src/plainenglishjavadebugger/translationModule/StatementType.java
@@ -0,0 +1,12 @@
package plainenglishjavadebugger.translationModule;

/*
* This code belongs to:
* Ahmet Emre Unal
* S001974
* emre.unal@ozu.edu.tr
*/

public enum StatementType {
METHOD_CALL, IF, SWITCH, WHILE_LOOP, FOR_LOOP, INSTANTIATION, RETURN, METHOD_ENTER, OTHER
}
119 changes: 119 additions & 0 deletions src/plainenglishjavadebugger/translationModule/TranslatedLine.java
@@ -0,0 +1,119 @@
package plainenglishjavadebugger.translationModule;

import org.eclipse.debug.core.DebugEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

/*
* This code belongs to:
* Ahmet Emre Unal
* S001974
* emre.unal@ozu.edu.tr
*/

public class TranslatedLine {
private static int itemNumber = 0;

private final int translationStepNumber;
private String translatedCode;
private String shortDescription;
private String longDescription;
private String debugEventType;
private String popupMessage = "";

private StatementType statementType;

public TranslatedLine(String translatedCode, int debugEventType) {
this(translatedCode, "", "", debugEventType);
}

public TranslatedLine(String translatedCode, String shortDescription, String longDescription, int debugEventType) {
TranslatedLine.itemNumber++;
translationStepNumber = TranslatedLine.itemNumber;
this.translatedCode = translatedCode;
this.shortDescription = shortDescription;
this.longDescription = longDescription;
this.debugEventType = getDebugEventTypeName(debugEventType);
}

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.";
}
}

private void createPopupMessage() {
popupMessage = "";
popupMessage += "Step #" + translationStepNumber + "\n\n";
popupMessage += "Debug event: " + debugEventType + "\n\n";
popupMessage += "The translated line of code:\n\t" + translatedCode + "\n\n";
popupMessage += getLongDescription();
}

public int getTranslationNumber() {
return translationStepNumber;
}

public String getTranslatedCode() {
return translatedCode;
}

public String getShortDescription() {
return shortDescription;
}

public String getLongDescription() {
return longDescription;
}

public String getDebugEventType() {
return debugEventType;
}

public String getPopupMessage() {
createPopupMessage();
return popupMessage;
}

public Image getImage() {
// Returns a dummy image for now. Later on, it should return an icon depending on the translation.
return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
}

/**
* @param shortDescription
* the shortDescription to set
*/
public synchronized void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}

/**
* @param longDescription
* the longDescription to set
*/
public synchronized void setLongDescription(String longDescription) {
this.longDescription = longDescription;
}

/**
* @return the statementType
*/
public StatementType getStatementType() {
return statementType;
}

/**
* @param statementType
* the statementType to set
*/
public synchronized void setStatementType(StatementType statementType) {
this.statementType = statementType;
}
}
121 changes: 109 additions & 12 deletions src/plainenglishjavadebugger/translationModule/Translator.java
Expand Up @@ -6,7 +6,6 @@
import org.eclipse.jdt.debug.core.IJavaThread;

import plainenglishjavadebugger.translationModule.fileReader.SourceFileReader;
import plainenglishjavadebugger.views.translatorView.TranslatedLine;
import plainenglishjavadebugger.views.translatorView.TranslatorViewModel;

/*
Expand Down Expand Up @@ -34,14 +33,32 @@ public class Translator {
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) {
Expand All @@ -52,12 +69,14 @@ 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();
model.addTranslatedLine(getTranslation(debugEventType));
generateTranslatedLine();
} else {
System.out.println("Undebuggable, closed-source class.");
}
Expand All @@ -67,28 +86,106 @@ public void translate(int debugEventType) {
}
}

private boolean sourceCodeIsAvailable() {
/* Which means it's not a built-in, closed-source library class */
return debuggedClassSourceElement != null;
}

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 TranslatedLine getTranslation(int debugEventType) {
String executedSourceLine = sourceFileReader.readLine(topStackFrame, debuggedClassPath, debuggedLineNumber);
TranslatedLine translatedLine = new TranslatedLine(executedSourceLine, executedSourceLine, debuggedClassPath + " " + debuggedLineNumber, getDebugEventTypeName(debugEventType));
return translatedLine;
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;
}

public String getDebugEventTypeName(int debugEventType) {
private String getDebugEventTypeName(int debugEventType) {
switch (debugEventType) {
case DebugEvent.STEP_OVER:
return "Stepped over the line";
Expand Down

This file was deleted.

Expand Up @@ -4,6 +4,8 @@
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import plainenglishjavadebugger.translationModule.TranslatedLine;

/*
* This code belongs to:
* Ahmet Emre Unal
Expand Down

0 comments on commit 4d83e8c

Please sign in to comment.