1 /* 2 * Copyright 2015 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.googlejavaformat.java; 16 17 import static java.util.Locale.ENGLISH; 18 19 import com.google.common.collect.ImmutableList; 20 import com.google.common.collect.Iterables; 21 import com.google.googlejavaformat.FormatterDiagnostic; 22 import java.util.List; 23 import javax.tools.Diagnostic; 24 import javax.tools.JavaFileObject; 25 26 /** Checked exception class for formatter errors. */ 27 public final class FormatterException extends Exception { 28 29 private final ImmutableList<FormatterDiagnostic> diagnostics; 30 FormatterException(String message)31 public FormatterException(String message) { 32 this(FormatterDiagnostic.create(message)); 33 } 34 FormatterException(FormatterDiagnostic diagnostic)35 public FormatterException(FormatterDiagnostic diagnostic) { 36 this(ImmutableList.of(diagnostic)); 37 } 38 FormatterException(Iterable<FormatterDiagnostic> diagnostics)39 public FormatterException(Iterable<FormatterDiagnostic> diagnostics) { 40 super(diagnostics.iterator().next().toString()); 41 this.diagnostics = ImmutableList.copyOf(diagnostics); 42 } 43 diagnostics()44 public List<FormatterDiagnostic> diagnostics() { 45 return diagnostics; 46 } 47 fromJavacDiagnostics( Iterable<Diagnostic<? extends JavaFileObject>> diagnostics)48 public static FormatterException fromJavacDiagnostics( 49 Iterable<Diagnostic<? extends JavaFileObject>> diagnostics) { 50 return new FormatterException( 51 Iterables.transform(diagnostics, FormatterException::toFormatterDiagnostic)); 52 } 53 toFormatterDiagnostic(Diagnostic<?> input)54 private static FormatterDiagnostic toFormatterDiagnostic(Diagnostic<?> input) { 55 return FormatterDiagnostic.create( 56 (int) input.getLineNumber(), (int) input.getColumnNumber(), input.getMessage(ENGLISH)); 57 } 58 } 59