diff --git a/tools/product_config/src/com/android/build/config/ErrorReporter.java b/tools/product_config/src/com/android/build/config/ErrorReporter.java index f382b4e1b..5d87636d1 100644 --- a/tools/product_config/src/com/android/build/config/ErrorReporter.java +++ b/tools/product_config/src/com/android/build/config/ErrorReporter.java @@ -49,6 +49,16 @@ public class ErrorReporter { */ private boolean mHadError; + public static class FatalException extends RuntimeException { + FatalException(String message) { + super(message); + } + + FatalException(String message, Throwable chain) { + super(message, chain); + } + } + /** * Whether errors are errors, warnings or hidden. */ @@ -127,6 +137,35 @@ public class ErrorReporter { public String getHelp() { return mHelp; } + + /** + * Add an error with no source position. + */ + public void add(String message) { + ErrorReporter.this.add(this, false, new Position(), message); + } + + /** + * Add an error. + */ + public void add(Position pos, String message) { + ErrorReporter.this.add(this, false, pos, message); + } + + /** + * Add an error with no source position, and throw a FatalException, stopping processing + * immediately. + */ + public void fatal(String message) { + ErrorReporter.this.add(this, true, new Position(), message); + } + + /** + * Add an error, and throw a FatalException, stopping processing immediately. + */ + public void fatal(Position pos, String message) { + ErrorReporter.this.add(this, true, pos, message); + } } /** @@ -154,6 +193,13 @@ public class ErrorReporter { public String getMessage() { return mMessage; } + + @Override + public String toString() { + return mPosition + + "[" + mCategory.getLevel().getLabel() + " " + mCategory.getCode() + "] " + + mMessage; + } } private void initLocked() { @@ -190,23 +236,17 @@ public class ErrorReporter { } } - /** - * Add an error with no source position. - */ - public void add(Category category, String message) { - add(category, new Position(), message); - } - /** * Add an error. */ - public void add(Category category, Position pos, String message) { + private void add(Category category, boolean fatal, Position pos, String message) { synchronized (mEntries) { initLocked(); if (mCategories.get(category.getCode()) != category) { throw new RuntimeException("Errors.Category used from the wrong Errors object."); } - mEntries.add(new Entry(category, pos, message)); + final Entry entry = new Entry(category, pos, message); + mEntries.add(entry); final Level level = category.getLevel(); if (level == Level.WARNING || level == Level.ERROR) { mHadWarningOrError = true; @@ -214,6 +254,9 @@ public class ErrorReporter { if (level == Level.ERROR) { mHadError = true; } + if (fatal) { + throw new FatalException(entry.toString()); + } } } @@ -250,13 +293,10 @@ public class ErrorReporter { public void printErrors(PrintStream out) { synchronized (mEntries) { for (Entry entry: mEntries) { - final Category category = entry.getCategory(); - final Level level = category.getLevel(); - if (level == Level.HIDDEN) { + if (entry.getCategory().getLevel() == Level.HIDDEN) { continue; } - out.println(entry.getPosition() + "[" + level.getLabel() + " " - + category.getCode() + "] " + entry.getMessage()); + out.println(entry.toString()); } } } diff --git a/tools/product_config/src/com/android/build/config/Main.java b/tools/product_config/src/com/android/build/config/Main.java index 6d6214d14..b79219305 100644 --- a/tools/product_config/src/com/android/build/config/Main.java +++ b/tools/product_config/src/com/android/build/config/Main.java @@ -38,7 +38,7 @@ public class Main { // TODO: Get the variables that were defined in starlark and use that to write // out the make, soong and bazel input files. - mErrors.add(mErrors.ERROR_COMMAND_LINE, "asdf"); + mErrors.ERROR_COMMAND_LINE.add("asdf"); throw new RuntimeException("poop"); } diff --git a/tools/product_config/src/com/android/build/config/Options.java b/tools/product_config/src/com/android/build/config/Options.java index 494b947f3..48146cb73 100644 --- a/tools/product_config/src/com/android/build/config/Options.java +++ b/tools/product_config/src/com/android/build/config/Options.java @@ -96,14 +96,14 @@ public class Options { mIndex++; } } catch (ParseException ex) { - mErrors.add(mErrors.ERROR_COMMAND_LINE, ex.getMessage()); + mErrors.ERROR_COMMAND_LINE.add(ex.getMessage()); } return mResult; } private void addWarning(Errors.Category category, String message) { - mErrors.add(category, message); + category.add(message); } private String getNextNonFlagArg() { @@ -133,12 +133,11 @@ public class Options { final int code = requireNextNumberArg(arg); final Errors.Category category = mErrors.getCategories().get(code); if (category == null) { - mErrors.add(mErrors.WARNING_UNKNOWN_COMMAND_LINE_ERROR, - "Unknown error code: " + code); + mErrors.WARNING_UNKNOWN_COMMAND_LINE_ERROR.add("Unknown error code: " + code); return; } if (!category.isLevelSettable()) { - mErrors.add(mErrors.ERROR_COMMAND_LINE, "Can't set level for error " + code); + mErrors.ERROR_COMMAND_LINE.add("Can't set level for error " + code); return; } category.setLevel(level); diff --git a/tools/product_config/test/com/android/build/config/ErrorReporterTest.java b/tools/product_config/test/com/android/build/config/ErrorReporterTest.java index 2cde4769a..b9b25b407 100644 --- a/tools/product_config/test/com/android/build/config/ErrorReporterTest.java +++ b/tools/product_config/test/com/android/build/config/ErrorReporterTest.java @@ -30,7 +30,7 @@ public class ErrorReporterTest { public void testAdding() { TestErrors errors = new TestErrors(); - errors.add(errors.ERROR, new Position("a", 12), "Errrororrrr"); + errors.ERROR.add(new Position("a", 12), "Errrororrrr"); Assert.assertTrue(errors.hadWarningOrError()); Assert.assertTrue(errors.hadError()); @@ -66,7 +66,7 @@ public class ErrorReporterTest { public void testWarning() { TestErrors errors = new TestErrors(); - errors.add(errors.WARNING, "Waaaaarninggggg"); + errors.WARNING.add("Waaaaarninggggg"); Assert.assertTrue(errors.hadWarningOrError()); Assert.assertFalse(errors.hadError()); @@ -80,7 +80,7 @@ public class ErrorReporterTest { public void testHidden() { TestErrors errors = new TestErrors(); - errors.add(errors.HIDDEN, "Hidddeennn"); + errors.HIDDEN.add("Hidddeennn"); Assert.assertFalse(errors.hadWarningOrError()); Assert.assertFalse(errors.hadError());