changeset 910:f46c7991c62f

cleanup: introduce Utils.throwableToString().
author shade
date Wed, 16 Jul 2014 22:29:25 +0400
parents dfed92d1c32b
children 5070b7fd46e7
files jmh-core/src/main/java/org/openjdk/jmh/generators/core/SourceThrowableError.java jmh-core/src/main/java/org/openjdk/jmh/runner/BaseRunner.java jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkServer.java jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APGeneratorDestinaton.java
diffstat 5 files changed, 19 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/SourceThrowableError.java	Wed Jul 16 21:38:21 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/SourceThrowableError.java	Wed Jul 16 22:29:25 2014 +0400
@@ -24,8 +24,7 @@
  */
 package org.openjdk.jmh.generators.core;
 
-import java.io.PrintWriter;
-import java.io.StringWriter;
+import org.openjdk.jmh.util.Utils;
 
 public class SourceThrowableError extends SourceError {
 
@@ -38,10 +37,6 @@
 
     @Override
     public String toString() {
-        StringWriter sw = new StringWriter();
-        PrintWriter pw = new PrintWriter(sw);
-        element.printStackTrace(pw);
-        pw.close();
-        return super.toString() + "\n" + sw.toString();
+        return super.toString() + "\n" + Utils.throwableToString(element);
     }
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseRunner.java	Wed Jul 16 21:38:21 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseRunner.java	Wed Jul 16 22:29:25 2014 +0400
@@ -34,9 +34,8 @@
 import org.openjdk.jmh.util.ClassUtils;
 import org.openjdk.jmh.util.Multimap;
 import org.openjdk.jmh.util.TreeMultimap;
+import org.openjdk.jmh.util.Utils;
 
-import java.io.PrintWriter;
-import java.io.StringWriter;
 import java.lang.management.GarbageCollectorMXBean;
 import java.lang.management.ManagementFactory;
 import java.lang.reflect.Method;
@@ -104,13 +103,7 @@
             } catch (BenchmarkException be) {
                 out.println("<failure>");
                 out.println("");
-                StringWriter sw = new StringWriter();
-                {
-                    PrintWriter pw = new PrintWriter(sw, true);
-                    be.getCause().printStackTrace(pw);
-                    pw.close();
-                }
-                out.println(sw.toString());
+                out.println(Utils.throwableToString(be.getCause()));
                 out.println("");
 
                 if (options.shouldFailOnError().orElse(Defaults.FAIL_ON_ERROR)) {
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkServer.java	Wed Jul 16 21:38:21 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkServer.java	Wed Jul 16 22:29:25 2014 +0400
@@ -34,14 +34,13 @@
 import org.openjdk.jmh.runner.options.VerboseMode;
 import org.openjdk.jmh.util.HashMultimap;
 import org.openjdk.jmh.util.Multimap;
+import org.openjdk.jmh.util.Utils;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.OutputStream;
-import java.io.PrintWriter;
-import java.io.StringWriter;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.InetAddress;
@@ -276,13 +275,7 @@
             } catch (Exception e) {
                 out.println("<binary link had failed, forked VM corrupted the stream? Use " + VerboseMode.EXTRA + " verbose to print exception>");
                 if (opts.verbosity().orElse(Defaults.VERBOSITY).equalsOrHigherThan(VerboseMode.EXTRA)) {
-                    StringWriter sw = new StringWriter();
-                    {
-                        PrintWriter pw = new PrintWriter(sw, true);
-                        e.printStackTrace(pw);
-                        pw.close();
-                    }
-                    out.println(sw.toString());
+                    out.println(Utils.throwableToString(e));
                 }
             } finally {
                 close();
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java	Wed Jul 16 21:38:21 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java	Wed Jul 16 22:29:25 2014 +0400
@@ -27,6 +27,8 @@
 import sun.misc.Unsafe;
 
 import java.io.File;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -97,6 +99,15 @@
         }
     }
 
+    public static String throwableToString(Throwable t) {
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        t.printStackTrace(pw);
+        pw.flush();
+        pw.close();
+        return sw.toString();
+    }
+
     public static int[] unmarshalIntArray(String src) {
         String[] ss = src.split("=");
         int[] arr = new int[ss.length];
--- a/jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APGeneratorDestinaton.java	Wed Jul 16 21:38:21 2014 +0400
+++ b/jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APGeneratorDestinaton.java	Wed Jul 16 22:29:25 2014 +0400
@@ -26,14 +26,13 @@
 
 import org.openjdk.jmh.generators.core.GeneratorDestination;
 import org.openjdk.jmh.generators.core.MetadataInfo;
+import org.openjdk.jmh.util.Utils;
 
 import javax.annotation.processing.ProcessingEnvironment;
 import javax.annotation.processing.RoundEnvironment;
 import javax.tools.Diagnostic;
 import javax.tools.StandardLocation;
 import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
 import java.io.Writer;
 
 public class APGeneratorDestinaton implements GeneratorDestination {
@@ -68,12 +67,7 @@
 
     @Override
     public void printError(String message, Throwable throwable) {
-        StringWriter sw = new StringWriter();
-        PrintWriter pw = new PrintWriter(sw);
-        throwable.printStackTrace(pw);
-        pw.flush();
-        pw.close();
-        printError(message + " " + sw.toString());
+        printError(message + " " + Utils.throwableToString(throwable));
     }
 
 }