clean up error handling code for exceptions and the runners

This commit is contained in:
benfry
2009-11-29 12:37:38 +00:00
parent faa59d3f55
commit 036c3b809b
6 changed files with 116 additions and 179 deletions

View File

@@ -1147,7 +1147,7 @@ public class Sketch {
* When running from the editor, take care of preparations before running
* the build.
*/
protected void prepare() {
public void prepare() {
// make sure the user didn't hide the sketch folder
ensureExistence();
@@ -1241,7 +1241,6 @@ public class Sketch {
bigCount += sc.getLineCount();
}
}
System.out.println(bigCode.toString());
// Note that the headerOffset isn't applied until compile and run, because
// it only applies to the code after it's been written to the .java file.
@@ -1521,24 +1520,29 @@ public class Sketch {
if (dotJavaFilename.equals(code.getFileName())) {
codeIndex = i;
codeLine = dotJavaLine;
return new RunnerException(message, codeIndex, codeLine);
}
}
}
// If not the preprocessed file at this point, then need to get out
if (!dotJavaFilename.equals(name + ".java")) {
return null;
}
// if it's not a .java file, codeIndex will still be 0
// this section searches through the list of .pde files
if (codeIndex == 0) { // main class, figure out which tab
for (int i = 0; i < getCodeCount(); i++) {
SketchCode code = getCode(i);
codeIndex = 0;
for (int i = 0; i < getCodeCount(); i++) {
SketchCode code = getCode(i);
if (code.isExtension("pde")) {
// System.out.println("preproc offset is " + code.getPreprocOffset());
// System.out.println("looking for line " + dotJavaLine);
if (code.getPreprocOffset() <= dotJavaLine) {
codeIndex = i;
// System.out.println("i'm thinkin file " + i);
codeLine = dotJavaLine - code.getPreprocOffset();
}
if (code.isExtension("pde")) {
// System.out.println("preproc offset is " + code.getPreprocOffset());
// System.out.println("looking for line " + dotJavaLine);
if (code.getPreprocOffset() <= dotJavaLine) {
codeIndex = i;
// System.out.println("i'm thinkin file " + i);
codeLine = dotJavaLine - code.getPreprocOffset();
}
}
}
@@ -1547,10 +1551,10 @@ public class Sketch {
// send the error message through.
// this is necessary because 'import' statements will be at a line
// that has a lower number than the preproc offset, for instance.
if (codeLine == -1 && !dotJavaFilename.equals(name + ".java")) {
return null;
}
return new RunnerException(message, codeIndex, codeLine, 0, false);
// if (codeLine == -1 && !dotJavaFilename.equals(name + ".java")) {
// return null;
// }
return new RunnerException(message, codeIndex, codeLine);
}

View File

@@ -431,7 +431,6 @@ public class Runner implements MessageConsumer {
if (editor != null) {
editor.internalRunnerClosed();
}
} catch (InterruptedException exc) {
// we don't interrupt
}
@@ -440,42 +439,10 @@ public class Runner implements MessageConsumer {
}
/**
* Find a com.sun.jdi.CommandLineLaunch connector
*/
/*
protected LaunchingConnector findLaunchingConnector(String connectorName) {
//VirtualMachineManager mgr = Bootstrap.virtualMachineManager();
// Get the default connector.
// Not useful here since they all need different args.
// System.out.println(Bootstrap.virtualMachineManager().defaultConnector());
// return Bootstrap.virtualMachineManager().defaultConnector();
List connectors = Bootstrap.virtualMachineManager().allConnectors();
// code to list available connectors
// Iterator iter2 = connectors.iterator();
// while (iter2.hasNext()) {
// Connector connector = (Connector)iter2.next();
// System.out.println("connector name is " + connector.name());
// }
Iterator iter = connectors.iterator();
while (iter.hasNext()) {
Connector connector = (Connector)iter.next();
if (connector.name().equals(connectorName)) {
return (LaunchingConnector)connector;
}
}
throw new Error("No launching connector");
}
*/
protected Connector findConnector(String connectorName) {
List connectors = Bootstrap.virtualMachineManager().allConnectors();
// code to list available connectors
// debug: code to list available connectors
// Iterator iter2 = connectors.iterator();
// while (iter2.hasNext()) {
// Connector connector = (Connector)iter2.next();
@@ -555,117 +522,56 @@ public class Runner implements MessageConsumer {
}
// This may be called more than one time per error in the VM,
// TODO: This may be called more than one time per error in the VM,
// presumably because exceptions might be wrapped inside others,
// and this will fire for both.
// and this will fire for both.
protected void reportException(String message, ThreadReference thread) {
try {
RunnerException rex = searchStackForSketchError(message, thread);
if (rex != null) {
listener.statusError(rex);
}
} catch (IncompatibleThreadStateException itse) {
itse.printStackTrace();
}
listener.statusError(message);
listener.statusError(findException(message, thread));
}
RunnerException searchStackForSketchError(String message, ThreadReference thread)
throws IncompatibleThreadStateException {
// Any of the thread.blah() methods can throw an AbsentInformationEx
// if that bit of data is missing. If so, just write out the error
// message to the console.
List<StackFrame> frames = thread.frames();
for (StackFrame frame : frames) {
//System.out.println("frame: " + frame);
Location location = frame.location();
try {
String filename = location.sourceName();
int line = location.lineNumber() - 1;
RunnerException rex = sketch.placeException(message, filename, line);
if (rex != null) {
return rex;
}
} catch (AbsentInformationException e) {
// ignore this and move on
e.printStackTrace();
}
}
return null;
}
protected void reportException_old(String message, ThreadReference thread) {
/**
* Move through a list of stack frames, searching for references to code
* found in the current sketch. Return with a RunnerException that contains
* the location of the error, or if nothing is found, just return with a
* RunnerException that wraps the error message itself.
*/
RunnerException findException(String message, ThreadReference thread) {
try {
// int codeIndex = -1;
// int lineNumber = -1;
// use to dump the stack for debugging
// for (StackFrame frame : thread.frames()) {
// System.out.println("frame: " + frame);
// }
// Any of the thread.blah() methods can throw an AbsentInformationEx
// if that bit of data is missing. If so, just write out the error
// message to the console.
List<StackFrame> frames = thread.frames();
for (StackFrame frame : frames) {
// System.out.println("frame: " + frame);
Location location = frame.location();
String filename = location.sourceName();
int lineNumber = location.lineNumber();
RunnerException rex =
sketch.placeException(message, filename, lineNumber);
if (rex != null) {
listener.statusError(rex);
} else {
listener.statusError(message);
}
/*
String appletJavaFile = appletClassName + ".java";
SketchCode errorCode = null;
if (filename.equals(appletJavaFile)) {
for (SketchCode code : sketch.getCode()) {
if (code.isExtension("pde")) {
if (lineNumber >= code.getPreprocOffset()) {
errorCode = code;
}
}
try {
Location location = frame.location();
String filename = null;
filename = location.sourceName();
int lineNumber = location.lineNumber() - 1;
RunnerException rex =
sketch.placeException(message, filename, lineNumber);
if (rex != null) {
return rex;
}
} else {
for (SketchCode code : sketch.getCode()) {
if (code.isExtension("java")) {
if (filename.equals(code.getFileName())) {
errorCode = code;
}
}
}
}
codeIndex = sketch.getCodeIndex(errorCode);
if (codeIndex != -1) {
//System.out.println("got line num " + lineNumber);
// in case this was a tab that got embedded into the main .java
lineNumber -= sketch.getCode(codeIndex).getPreprocOffset();
// lineNumber is 1-indexed, but editor wants zero-indexed
lineNumber--;
// getMessage() will be what's shown in the editor
exception = new RunnerException(message, codeIndex, lineNumber, -1);
} catch (AbsentInformationException e) {
// Any of the thread.blah() methods can throw an AbsentInformationEx
// if that bit of data is missing. If so, just write out the error
// message to the console.
//e.printStackTrace(); // not useful
exception = new RunnerException(message);
exception.hideStackTrace();
listener.statusError(exception);
return;
}
*/
}
} catch (AbsentInformationException e) {
//e.printStackTrace(); // not useful
exception = new RunnerException(message);
exception.hideStackTrace();
listener.statusError(exception);
} catch (IncompatibleThreadStateException e) {
// This shouldn't happen, but if it does, print the exception in case
// it's something that needs to be debugged separately.
e.printStackTrace();
}
// Give up, nothing found inside the pile of stack frames
return new RunnerException(message);
}

View File

@@ -41,6 +41,11 @@ public class RunnerException extends Exception /*RuntimeException*/ {
}
public RunnerException(String message, int file, int line) {
this(message, file, line, -1, true);
}
public RunnerException(String message, int file, int line, int column) {
this(message, file, line, column, true);
}

View File

@@ -76,12 +76,23 @@ public class Android implements Tool {
public void run() {
editor.statusNotice("Loading Android tools.");
boolean success = checkPath();
if (!success) {
editor.statusNotice("Android mode canceled.");
return;
}
//adb get-state
try {
System.out.print("adb get state: ");
Pavarotti p = new Pavarotti(new String[] { "adb", "get-state" });
p.waitFor();
p.printLines();
} catch (Exception e) {
e.printStackTrace();
}
success = Device.checkDefaults();
if (!success) {
editor.statusError("Could not load Android tools.");
@@ -300,6 +311,16 @@ public class Android implements Tool {
public void installAndRun(String target, String device) {
boolean success;
//adb get-state
try {
System.out.print("(installAndRun) adb get state: ");
Pavarotti p = new Pavarotti(new String[] { "adb", "get-state" });
p.waitFor();
p.printLines();
} catch (Exception e) {
e.printStackTrace();
}
// Simply reset the debug bridge, since it seems so prone to getting
// into bad states and not producing error messages.
//resetServer();
@@ -438,7 +459,7 @@ public class Android implements Tool {
if (result == 0) {
String[] lines = p.getOutputLines();
PApplet.println(lines);
// PApplet.println(lines);
// String last = lines[lines.length - 1];
// if (last.trim().length() == 0) {
// last = lines[lines.length - 2];

View File

@@ -25,9 +25,7 @@ package processing.app.tools.android;
import processing.app.*;
import processing.app.debug.*;
import processing.core.*;
import java.awt.Point;
import java.io.*;
import java.util.*;
@@ -265,13 +263,13 @@ public class AndroidRunner extends Runner {
// ((Connector.Argument) arguments.get("timeout")).setValue("5000");
try {
PApplet.println(connector);
PApplet.println(arguments);
// PApplet.println(connector);
// PApplet.println(arguments);
PApplet.println("attaching now...");
// PApplet.println("attaching now...");
//return connector.attach(arguments);
VirtualMachine machine = connector.attach(arguments);
PApplet.println("attached");
// PApplet.println("attached");
return machine;
} catch (IOException ioe) {
@@ -414,21 +412,16 @@ public class AndroidRunner extends Runner {
// This may be called more than one time per error in the VM,
// presumably because exceptions might be wrapped inside others,
// and this will fire for both.
/*
protected void reportException(String message, ThreadReference thread) {
try {
// int codeIndex = -1;
// int lineNumber = -1;
// Any of the thread.blah() methods can throw an AbsentInformationEx
// if that bit of data is missing. If so, just write out the error
// message to the console.
Sketch sketch = editor.getSketch();
// String appletClassName = sketch.getName(); // TODO * not yet correct! *
// a bit for debugging
// for (StackFrame frame : thread.frames()) {
// System.out.println("frame: " + frame);
// }
List<StackFrame> frames = thread.frames();
for (StackFrame frame : frames) {
// System.out.println("frame: " + frame);
@@ -440,11 +433,16 @@ public class AndroidRunner extends Runner {
sketch.placeException(message, filename, lineNumber);
if (rex != null) {
listener.statusError(rex);
} else {
listener.statusError(message);
return;
}
}
// Give up, nothing found inside the pile of stack frames
listener.statusError(message);
} catch (AbsentInformationException e) {
// Any of the thread.blah() methods can throw an AbsentInformationEx
// if that bit of data is missing. If so, just write out the error
// message to the console.
//e.printStackTrace(); // not useful
exception = new RunnerException(message);
exception.hideStackTrace();
@@ -454,8 +452,10 @@ public class AndroidRunner extends Runner {
e.printStackTrace();
}
}
*/
/*
public void close() {
// TODO make sure stop() has already been called to exit the sketch
@@ -472,11 +472,13 @@ public class AndroidRunner extends Runner {
vm = null;
}
}
*/
// made synchronized for rev 87
// attempted to remove synchronized for 0136 to fix bug #775 (no luck tho)
// http://dev.processing.org/bugs/show_bug.cgi?id=775
/*
synchronized public void message(String s) {
//System.out.println("M" + s.length() + ":" + s.trim()); // + "MMM" + s.length());
@@ -509,30 +511,27 @@ public class AndroidRunner extends Runner {
// Removed while doing cleaning for 0145,
// it seems that this is never actually printed out.
/*
// this is PApplet sending a message saying "i'm about to spew
// a stack trace because an error occurred during PApplet.run()"
if (s.indexOf(PApplet.LEECH_WAKEUP) == 0) {
// newMessage being set to 'true' means that the next time
// message() is called, expect the first line of the actual
// error message & stack trace to be sent from the applet.
newMessage = true;
return; // this line ignored
}
*/
// if (s.indexOf(PApplet.LEECH_WAKEUP) == 0) {
// // newMessage being set to 'true' means that the next time
// // message() is called, expect the first line of the actual
// // error message & stack trace to be sent from the applet.
// newMessage = true;
// return; // this line ignored
// }
// these are used for debugging, in case there are concerns
// that some errors aren't coming through properly
/*
if (s.length() > 2) {
System.err.println(newMessage);
System.err.println("message " + s.length() + ":" + s);
}
*/
// if (s.length() > 2) {
// System.err.println(newMessage);
// System.err.println("message " + s.length() + ":" + s);
// }
// always shove out the mesage, since it might not fall under
// the same setup as we're expecting
System.err.print(s);
//System.err.println("[" + s.length() + "] " + s);
System.err.flush();
}
*/
}

View File

@@ -164,6 +164,8 @@ public class Build {
try {
// need to change to a better set of imports here
// grab code from current editing window
sketch.prepare();
className = sketch.preprocess(buildPath, new Preproc());
if (className != null) {
File androidXML = new File(androidFolder, "AndroidManifest.xml");