/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.errorprone;

import com.google.common.base.Strings;
import com.google.common.base.Throwables;

import com.sun.tools.javac.util.DiagnosticSource;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.Log;

import java.io.PrintStream;
import java.io.PrintWriter;

import javax.tools.JavaFileObject;

/**
 * Wraps an unrecoverable error that occurs during analysis with the source
 * position that triggered the crash.
 */
public class ErrorProneError extends Error {

  private final Throwable cause;
  private final DiagnosticPosition pos;
  private final JavaFileObject source;

  public ErrorProneError(Throwable cause, DiagnosticPosition pos, JavaFileObject source) {
    super(formatMessage(source, pos, cause), cause);
    this.cause = cause;
    this.pos = pos;
    this.source = source;
  }

  public void logFatalError(Log log) {
    String version = ErrorProneCompiler.loadVersionFromPom().or("unknown version");
    JavaFileObject prev = log.currentSourceFile();
    try {
      log.useSource(source);
      log.error(
          pos, "error.prone.crash", Throwables.getStackTraceAsString(cause), version);
    } finally {
      log.useSource(prev);
    }
  }

  private static String formatMessage(
      JavaFileObject file, DiagnosticPosition pos, Throwable cause) {
    DiagnosticSource source = new DiagnosticSource(file, /*log=*/ null);
    int column = source.getColumnNumber(pos.getStartPosition(), /*expandTabs=*/ true);
    int line = source.getLineNumber(pos.getStartPosition());
    String snippet = source.getLine(pos.getStartPosition());
    StringBuilder sb = new StringBuilder();
    sb.append(
        String.format(
            "\n%s:%d: An exception was thrown by Error Prone: %s\n",
            source.getFile().getName(),
            line,
            cause.getMessage()));
    sb.append(snippet).append('\n');
    sb.append(Strings.repeat(" ", column - 1)).append("^\n");
    return sb.toString();
  }

  @Override
  public void printStackTrace() {
    cause.printStackTrace();
  }

  @Override
  public void printStackTrace(PrintStream s) {
    cause.printStackTrace(s);
  }

  @Override
  public void printStackTrace(PrintWriter s) {
    cause.printStackTrace(s);
  }

  @Override
  public StackTraceElement[] getStackTrace() {
    return cause.getStackTrace();
  }
}

@@ -881,65 +881,8 @@
 g;%0A%0A
-import java.io.PrintStream;%0Aimport java.io.PrintWriter;%0A%0A
 impo
@@ -1295,16 +1295,25 @@
   super(
+%0A        
 formatMe
@@ -1338,20 +1338,104 @@
  cause),
- cau
+%0A        cause,%0A        /*enableSuppression=*/ true,%0A        /*writableStackTrace=*/ fal
 se);%0A   
@@ -2643,373 +2643,7 @@
 %0A  %7D
-%0A%0A  @Override%0A  public void printStackTrace() %7B%0A    cause.printStackTrace();%0A  %7D%0A%0A  @Override%0A  public void printStackTrace(PrintStream s) %7B%0A    cause.printStackTrace(s);%0A  %7D%0A%0A  @Override%0A  public void printStackTrace(PrintWriter s) %7B%0A    cause.printStackTrace(s);%0A  %7D%0A%0A  @Override%0A  public StackTraceElement%5B%5D getStackTrace() %7B%0A    return cause.getStackTrace();%0A  %7D
 %0A%7D%0A

Make ErrorProneError quieter