/*
 * 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 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);
    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) {
    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 unhandled exception was thrown by Error Prone\n",
            source.getFile().getName(),
            line));
    sb.append(snippet).append('\n');
    sb.append(Strings.repeat(" ", column - 1)).append("^\n");
    return sb.toString();
  }
}

@@ -877,16 +877,73 @@
 l.Log;%0A%0A
+import java.io.PrintStream;%0Aimport java.io.PrintWriter;%0A%0A
 import j
@@ -1377,16 +1377,23 @@
 rce, pos
+, cause
 ), cause
@@ -1879,16 +1879,23 @@
 Message(
+%0A      
 JavaFile
@@ -1929,16 +1929,33 @@
 tion pos
+, Throwable cause
 ) %7B%0A    
@@ -2343,18 +2343,8 @@
  An 
-unhandled 
 exce
@@ -2374,16 +2374,20 @@
 or Prone
+: %25s
 %5Cn%22,%0A   
@@ -2439,16 +2439,48 @@
     line
+,%0A            cause.getMessage()
 ));%0A    
@@ -2603,11 +2603,377 @@
 g();%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

Improve ErrorProneError