Skip to content

Commit

Permalink
Changed the way source file reading is done to fix a null pointer bug…
Browse files Browse the repository at this point in the history
…. Now, after reading the line, the bufferedReader & fileReader are immediately closed. Also, changed one of TranslatorView actions to clear the lines.
  • Loading branch information
aemreunal committed Nov 29, 2013
1 parent 1c31e3e commit a9d414a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public void stopListening() {
}

// IDebugEventSetListener interface methods begin

@Override
public void handleDebugEvents(DebugEvent[] debugEvents) {
for (DebugEvent debugEvent : debugEvents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void translate(int debugEventType) {
getCurrentFrameInfo();
if (sourceCodeIsAvailable()) {
getCurrentClassInfo();
model.addElement(getTranslation(debugEventType));
model.addTranslatedLine(getTranslation(debugEventType));
} else {
System.out.println("Undebuggable, closed-source class.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,22 @@ public class SourceFileReader {
private BufferedReader bufferedReader;

public String readLine(IStackFrame stackFrame, String filePath, int lineNumber) {
String processedFilePath = processFilePath(stackFrame, filePath);
if (this.filePath != processedFilePath) {
this.filePath = processedFilePath;
openFileAndReader();
return getLine(lineNumber);
} else {
resetReader();
return getLine(lineNumber);
}
processFilePath(stackFrame, filePath);
openFileAndReader();
String sourceCodeLine = getLine(lineNumber);
closeFileAndReader();
return sourceCodeLine;
}

private String processFilePath(IStackFrame frame, String filePath) {
private void processFilePath(IStackFrame frame, String filePath) {
String command = (frame.getLaunch().getProcesses())[0].getAttribute(org.eclipse.debug.core.model.IProcess.ATTR_CMDLINE);
int workingDirBeginIndex = command.indexOf("-classpath ") + ("-classpath ".length());
int workingDirEndIndex = command.lastIndexOf('/', command.indexOf(':', workingDirBeginIndex));
return command.substring(workingDirBeginIndex, workingDirEndIndex) + filePath.substring(filePath.indexOf('/', 3));
this.filePath = command.substring(workingDirBeginIndex, workingDirEndIndex) + filePath.substring(filePath.indexOf('/', 3));
}

private void openFileAndReader() {
try {
if (fileReader != null) {
closeFileAndReader();
}
fileReader = new FileReader(filePath);
bufferedReader = new BufferedReader(fileReader);
} catch (IOException ioException) {
Expand All @@ -51,15 +44,6 @@ private void openFileAndReader() {
}
}

private void resetReader() {
try {
bufferedReader.reset();
} catch (IOException e) {
System.out.println("Unable to reset the buffered reader stream!");
e.printStackTrace();
}
}

private String getLine(int lineNumber) {
String line = "";
for (int i = 0; i < lineNumber; i++) {
Expand All @@ -68,6 +52,7 @@ private String getLine(int lineNumber) {
} catch (IOException e) {
System.err.println("Unable to read the line from the buffered reader!");
e.printStackTrace();
return "";
}
}
return line.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
Expand All @@ -22,8 +21,7 @@
import org.osgi.framework.BundleException;

import plainenglishjavadebugger.views.translatorView.actions.ListDoubleClickAction;
import plainenglishjavadebugger.views.translatorView.actions.StepIntoTranslateAction;
import plainenglishjavadebugger.views.translatorView.actions.StepOverTranslateAction;
import plainenglishjavadebugger.views.translatorView.actions.ClearTranslatorViewAction;

/**
* This sample class demonstrates how to plug-in a new workbench view. The view shows data obtained
Expand Down Expand Up @@ -122,8 +120,8 @@ private void setHelpSystem() {
}

private void makeActions() {
stepOverTranslateAction = new StepOverTranslateAction(this);
stepIntoTranslateAction = new StepIntoTranslateAction(this);
// stepOverTranslateAction = new StepOverTranslateAction(this);
stepIntoTranslateAction = new ClearTranslatorViewAction(this);
listDoubleClickAction = new ListDoubleClickAction(this);
}

Expand All @@ -149,14 +147,14 @@ private void contributeToActionBars() {

private void fillLocalPullDown(IMenuManager manager) {
// Adds actions to the view's drop down menu, the down-facing triangle to the top right.
manager.add(stepOverTranslateAction);
manager.add(new Separator());
// manager.add(stepOverTranslateAction);
// manager.add(new Separator());
manager.add(stepIntoTranslateAction);
}

private void fillLocalToolBar(IToolBarManager manager) {
// Adds actions to the view's tool bar as small icons on the top right, to the left of the 'minimize' button.
manager.add(stepOverTranslateAction);
// manager.add(stepOverTranslateAction);
manager.add(stepIntoTranslateAction);
}

Expand Down Expand Up @@ -197,9 +195,9 @@ public void showError(String title, String message) {
MessageDialog.openError(viewer.getControl().getShell(), title, message);
}

public synchronized Action getStepOverTranslateAction() {
return stepOverTranslateAction;
}
// public synchronized Action getStepOverTranslateAction() {
// return stepOverTranslateAction;
// }

public synchronized Action getStepIntoTranslateAction() {
return stepIntoTranslateAction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void menuAboutToShow(IMenuManager manager) {
}

private void fillContextMenu(IMenuManager manager) {
manager.add(view.getStepOverTranslateAction());
// manager.add(view.getStepOverTranslateAction());
manager.add(view.getStepIntoTranslateAction());
// Other plug-ins can contribute their actions here
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class TranslatorViewModel {
private boolean isDebugging = false;
private IJavaThread thread;

private ArrayList<TranslatedLine> elements = new ArrayList<TranslatedLine>();
private ArrayList<TranslatedLine> translatedLines = new ArrayList<TranslatedLine>();

public TranslatorViewModel(TranslatorView view) {
this.view = view;
Expand Down Expand Up @@ -73,18 +73,23 @@ public void run() {
}

// TranslatorView actions
public void addElement(TranslatedLine translatedLine) {
elements.add(translatedLine);
public void addTranslatedLine(TranslatedLine translatedLine) {
translatedLines.add(translatedLine);
view.refresh();
}

public void removeElement() {
if (elements.size() > 0) {
elements.remove(elements.size() - 1);
public void removeTranslatedLine() {
if (translatedLines.size() > 0) {
translatedLines.remove(translatedLines.size() - 1);
}
view.refresh();
}

public void removeAllTranslatedLines() {
translatedLines.clear();
view.refresh();
}

public synchronized boolean isDebugging() {
return isDebugging;
}
Expand All @@ -98,6 +103,6 @@ public synchronized IJavaThread getThread() {
}

public ArrayList<TranslatedLine> getElements() {
return elements;
return translatedLines;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,23 @@
* emre.unal@ozu.edu.tr
*/

public class StepIntoTranslateAction extends Action {
public class ClearTranslatorViewAction extends Action {
private final TranslatorView view;
private final TranslatorViewModel model;

private final String buttonText = "Step Into and Translate";

public StepIntoTranslateAction(TranslatorView view) {
public ClearTranslatorViewAction(TranslatorView view) {
this.view = view;
model = view.getModel();

setText(buttonText);
setToolTipText(buttonText);
setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ELCL_REMOVE));
}

@Override
public void run() {
if (model.isDebugging()) {
// model.getThreadInfo();
// try {
// model.getThread().stepInto();
// } catch (DebugException e) {
// System.err.println("Unable to step into the line!");
// e.printStackTrace();
// }
// model.removeElement();
// view.showMessage("Info", "Removed the last translation line.");
}
model.removeAllTranslatedLines();
}

}

0 comments on commit a9d414a

Please sign in to comment.