package org.apache.logging.log4j.core.util;

import java.io.Closeable;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.status.StatusLogger;

public final class Throwables {
   private static final Method ADD_SUPPRESSED;
   private static final Method GET_SUPPRESSED;

   /** @deprecated */
   @Deprecated
   public static void addSuppressed(Throwable throwable, Throwable suppressedThrowable) {
      if (ADD_SUPPRESSED != null) {
         try {
            ADD_SUPPRESSED.invoke(throwable, suppressedThrowable);
         } catch (IllegalAccessException var3) {
            StatusLogger.getLogger().error(var3);
         } catch (IllegalArgumentException var4) {
            StatusLogger.getLogger().error(var4);
         } catch (InvocationTargetException var5) {
            StatusLogger.getLogger().error(var5);
         }
      }

   }

   /** @deprecated */
   @Deprecated
   public static Throwable[] getSuppressed(Throwable throwable) {
      if (GET_SUPPRESSED != null) {
         try {
            return (Throwable[])((Throwable[])GET_SUPPRESSED.invoke(throwable));
         } catch (Exception var2) {
            StatusLogger.getLogger().error(var2);
            return null;
         }
      } else {
         return null;
      }
   }

   public static boolean isGetSuppressedAvailable() {
      return GET_SUPPRESSED != null;
   }

   public static List<String> toStringList(Throwable throwable) {
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);

      try {
         throwable.printStackTrace(pw);
      } catch (RuntimeException var10) {
      }

      pw.flush();
      List<String> lines = new ArrayList();
      LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));

      try {
         for(String line = reader.readLine(); line != null; line = reader.readLine()) {
            lines.add(line);
         }
      } catch (IOException var11) {
         if (var11 instanceof InterruptedIOException) {
            Thread.currentThread().interrupt();
         }

         lines.add(var11.toString());
      } finally {
         Closer.closeSilently((Closeable)reader);
      }

      return lines;
   }

   private Throwables() {
   }

   static {
      Method getSuppressed = null;
      Method addSuppressed = null;
      Method[] methods = Throwable.class.getMethods();
      Method[] arr$ = methods;
      int len$ = methods.length;

      for(int i$ = 0; i$ < len$; ++i$) {
         Method method = arr$[i$];
         if (method.getName().equals("getSuppressed")) {
            getSuppressed = method;
         } else if (method.getName().equals("addSuppressed")) {
            addSuppressed = method;
         }
      }

      GET_SUPPRESSED = getSuppressed;
      ADD_SUPPRESSED = addSuppressed;
   }
}
