Automated g4 rollback of changelist 174358009. (#219)
*** Reason for rollback ***
Fix forward: avoid JDK 7-incompatible APIs
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=174581473
diff --git a/core/src/main/java/com/google/googlejavaformat/FormattingError.java b/core/src/main/java/com/google/googlejavaformat/FormattingError.java
index 0c64507..50381d0 100644
--- a/core/src/main/java/com/google/googlejavaformat/FormattingError.java
+++ b/core/src/main/java/com/google/googlejavaformat/FormattingError.java
@@ -14,13 +14,8 @@
package com.google.googlejavaformat;
-import static java.util.Locale.ENGLISH;
-
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
-import org.openjdk.javax.tools.Diagnostic;
-import org.openjdk.javax.tools.JavaFileObject;
/** An unchecked formatting error. */
public class FormattingError extends Error {
@@ -39,15 +34,4 @@
public ImmutableList<FormatterDiagnostic> diagnostics() {
return diagnostics;
}
-
- public static FormattingError fromJavacDiagnostics(
- Iterable<Diagnostic<? extends JavaFileObject>> diagnostics) {
- return new FormattingError(
- Iterables.transform(diagnostics, FormattingError::toFormatterDiagnostic));
- }
-
- private static FormatterDiagnostic toFormatterDiagnostic(Diagnostic<?> input) {
- return FormatterDiagnostic.create(
- (int) input.getLineNumber(), (int) input.getColumnNumber(), input.getMessage(ENGLISH));
- }
}
diff --git a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java
index ec9bb93..0077bb7 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java
@@ -108,8 +108,8 @@
* @param javaOutput the {@link JavaOutput}
* @param options the {@link JavaFormatterOptions}
*/
- static void format(
- final JavaInput javaInput, JavaOutput javaOutput, JavaFormatterOptions options) {
+ static void format(final JavaInput javaInput, JavaOutput javaOutput, JavaFormatterOptions options)
+ throws FormatterException {
Context context = new Context();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
context.put(DiagnosticListener.class, diagnostics);
@@ -148,7 +148,7 @@
Iterable<Diagnostic<? extends JavaFileObject>> errorDiagnostics =
Iterables.filter(diagnostics.getDiagnostics(), Formatter::errorDiagnostic);
if (!Iterables.isEmpty(errorDiagnostics)) {
- throw FormattingError.fromJavacDiagnostics(errorDiagnostics);
+ throw FormatterException.fromJavacDiagnostics(errorDiagnostics);
}
OpsBuilder builder = new OpsBuilder(javaInput, javaOutput);
// Output the compilation unit.
diff --git a/core/src/main/java/com/google/googlejavaformat/java/FormatterException.java b/core/src/main/java/com/google/googlejavaformat/java/FormatterException.java
index 2cf567d..972b8ce 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/FormatterException.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/FormatterException.java
@@ -14,9 +14,14 @@
package com.google.googlejavaformat.java;
+import static java.util.Locale.ENGLISH;
+
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
import com.google.googlejavaformat.FormatterDiagnostic;
import java.util.List;
+import org.openjdk.javax.tools.Diagnostic;
+import org.openjdk.javax.tools.JavaFileObject;
/** Checked exception class for formatter errors. */
public final class FormatterException extends Exception {
@@ -39,4 +44,14 @@
public List<FormatterDiagnostic> diagnostics() {
return diagnostics;
}
+
+ public static FormatterException fromJavacDiagnostics(
+ Iterable<Diagnostic<? extends JavaFileObject>> diagnostics) {
+ return new FormatterException(Iterables.transform(diagnostics, d -> toFormatterDiagnostic(d)));
+ }
+
+ private static FormatterDiagnostic toFormatterDiagnostic(Diagnostic<?> input) {
+ return FormatterDiagnostic.create(
+ (int) input.getLineNumber(), (int) input.getColumnNumber(), input.getMessage(ENGLISH));
+ }
}
diff --git a/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java b/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java
index 01cbc72..ac7a24e 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java
@@ -28,7 +28,6 @@
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeMap;
import com.google.common.collect.TreeRangeSet;
-import com.google.googlejavaformat.FormattingError;
import com.google.googlejavaformat.Newlines;
import java.io.IOError;
import java.io.IOException;
@@ -201,11 +200,11 @@
/** @deprecated use {@link removeUnusedImports(String)} instead. */
@Deprecated
public static String removeUnusedImports(
- final String contents, JavadocOnlyImports javadocOnlyImports) {
+ final String contents, JavadocOnlyImports javadocOnlyImports) throws FormatterException {
return removeUnusedImports(contents);
}
- public static String removeUnusedImports(final String contents) {
+ public static String removeUnusedImports(final String contents) throws FormatterException {
Context context = new Context();
// TODO(cushon): this should default to the latest supported source level, same as in Formatter
Options.instance(context).put(Option.SOURCE, "9");
@@ -220,7 +219,8 @@
contents, buildReplacements(contents, unit, scanner.usedNames, scanner.usedInJavadoc));
}
- private static JCCompilationUnit parse(Context context, String javaInput) {
+ private static JCCompilationUnit parse(Context context, String javaInput)
+ throws FormatterException {
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
context.put(DiagnosticListener.class, diagnostics);
Options.instance(context).put("allowStringFolding", "false");
@@ -250,7 +250,7 @@
Iterables.filter(diagnostics.getDiagnostics(), Formatter::errorDiagnostic);
if (!Iterables.isEmpty(errorDiagnostics)) {
// error handling is done during formatting
- throw FormattingError.fromJavacDiagnostics(errorDiagnostics);
+ throw FormatterException.fromJavacDiagnostics(errorDiagnostics);
}
return unit;
}
diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java
index 3d1d5e9..dc31b09 100644
--- a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java
+++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java
@@ -16,6 +16,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
+import static org.junit.Assert.fail;
import com.google.common.base.Joiner;
import com.google.common.io.CharStreams;
@@ -408,4 +409,13 @@
+ " }\n"
+ "}\n");
}
+
+ @Test
+ public void throwsFormatterException() throws Exception {
+ try {
+ new Formatter().formatSourceAndFixImports("package foo; public class {");
+ fail();
+ } catch (FormatterException expected) {
+ }
+ }
}