changeset 46:27376699db35

CODETOOLS-7902337 jcov (JREInstr) hides results of running java commands: jlink, jimage
author lkuskov
date Tue, 23 Oct 2018 15:15:30 -0700
parents dc39eb43dfda
children de986bc9529d
files src/classes/com/sun/tdk/jcov/JREInstr.java src/classes/com/sun/tdk/jcov/util/Utils.java
diffstat 2 files changed, 41 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/src/classes/com/sun/tdk/jcov/JREInstr.java	Thu Oct 11 10:37:27 2018 -0700
+++ b/src/classes/com/sun/tdk/jcov/JREInstr.java	Tue Oct 23 15:15:30 2018 -0700
@@ -39,6 +39,7 @@
 import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Objects;
 import java.util.Properties;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -310,6 +311,25 @@
         }
     }
 
+    private boolean doCommand(String command, File where, String msg) throws IOException, InterruptedException {
+        Objects.requireNonNull(command);
+        Objects.requireNonNull(msg);
+        Process process = Runtime.getRuntime().exec(command, null, where);
+        try (BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
+            process.waitFor();
+            if (process.exitValue() != 0) {
+                msg += command;
+                String s;
+                while ((s = stdInput.readLine()) != null) {
+                    msg += System.lineSeparator() + '\t' + s;
+                }
+                logger.log(Level.SEVERE, msg);
+                return false;
+            }
+        }
+        return true;
+    }
+
     private File extractJMod(File jdk, File from, File to) {
         try {
             String name = from.getName();
@@ -318,12 +338,8 @@
             }
             File modDir = new File(to, name);
             modDir.mkdirs();
-
             String command = jdk.getAbsolutePath() + File.separator + "bin" + File.separator + "jar xf " + from.getAbsolutePath();
-            Process process = Runtime.getRuntime().exec(command, null, modDir);
-            process.waitFor();
-            if (process.exitValue() != 0) {
-                logger.log(Level.SEVERE, "wrong command for unjar jmod: " + command);
+            if (!doCommand(command, modDir, "wrong command for unjar jmod: ")) {
                 return null;
             }
             return modDir;
@@ -346,11 +362,7 @@
             }
             String mods = sb.toString().substring(0, sb.toString().length() - 1);
             command += mods + " --output instr_jimage_dir";
-
-            Process process = Runtime.getRuntime().exec(command, null, jmodDir.getParentFile());
-            process.waitFor();
-            if (process.exitValue() != 0) {
-                logger.log(Level.SEVERE, "wrong command for jlink mods: " + command);
+            if (!doCommand(command, jmodDir.getParentFile(), "wrong command for jlink mods: ")) {
                 return null;
             }
         } catch (Exception e) {
@@ -387,12 +399,7 @@
                 }
             }
             command.append(" " + jmodDir.getName() + ".jmod");
-
-            Process process = Runtime.getRuntime().exec(command.toString(), null, modsDir);
-            process.waitFor();
-            if (process.exitValue() != 0) {
-                logger.log(Level.SEVERE, "wrong command for create jmod: " + command.toString());
-            }
+            doCommand(command.toString(),modsDir,"wrong command for create jmod: ");
         } catch (Exception e) {
             logger.log(Level.SEVERE, "exception in process(create jmod)", e);
         }
@@ -402,34 +409,22 @@
         try {
             String command = jimage.getParentFile().getParentFile().getParent() + File.separator + "bin" + File.separator + "jimage extract --dir " +
                     jimage.getParent() + File.separator + tempDirName + " " + jimage.getAbsolutePath();
-            Process process = Runtime.getRuntime().exec(command);
-            process.waitFor();
-            if (process.exitValue() != 0) {
-                logger.log(Level.SEVERE, "wrong command for expand jimage: " + command);
-                return false;
-            }
+            return doCommand(command,null, "wrong command for expand jimage: ");
         } catch (Exception e) {
             logger.log(Level.SEVERE, "exception in process(expanding jimage)", e);
             return false;
         }
-        return true;
     }
 
     private boolean createJimage(File dir, String new_jimage_path) {
         try {
             String command = dir.getParentFile().getParentFile().getParent() + File.separator + "bin" + File.separator + "jimage recreate --dir " +
                     dir + " " + new_jimage_path;
-            Process process = Runtime.getRuntime().exec(command);
-            process.waitFor();
-            if (process.exitValue() != 0) {
-                logger.log(Level.SEVERE, "wrong command for create jimage: " + command);
-                return false;
-            }
+            return doCommand(command, null, "wrong command for create jimage: ");
         } catch (Exception e) {
             logger.log(Level.SEVERE, "exception in process(expanding jimage)", e);
             return false;
         }
-        return true;
     }
 
     @Override
--- a/src/classes/com/sun/tdk/jcov/util/Utils.java	Thu Oct 11 10:37:27 2018 -0700
+++ b/src/classes/com/sun/tdk/jcov/util/Utils.java	Tue Oct 23 15:15:30 2018 -0700
@@ -100,31 +100,27 @@
             map[chars[i]] = i;
         }
     }
-    public final static int VER11 = 110;
-    public final static int VER12 = 120;
-    public final static int VER122 = 122;
-    public final static int VER13 = 130;
-    public final static int VER14 = 140;
-    public final static int VER15 = 150;
-    public final static int VER16 = 160;
-    public final static int VER9 = 190;
+
+
+    public final static int VER16  = 160;
+    public final static int VER17  = 160;
+    public final static int VER18  = 160;
+    public final static int VER90  = 900;
+    public final static int VER100 = 1000;
     private static int javaVersion = -1;
 
-
     /**
-     * @return JVM version
+     * @return JVM version: java.version * 100
      */
     public static int getJavaVersion() {
         if (javaVersion == -1){
             String ver = System.getProperty("java.version");
-            if (ver.length() > 1) {
-                javaVersion = (ver.charAt(0) - '0') * 100
-                        + (ver.charAt(2) - '0') * 10
-                        + (ver.length() > 4 ? (ver.charAt(4) - '0') : 0);
+            for(int i=1; i<=15; i++) {
+                if( ver.startsWith(String.format( (i <= 8) ? "1.%d" : "%d" , i))) {
+                    return (i <= 8) ? 100 + i*10 : i * 100;
+                }
             }
-            else{
-                javaVersion = VER9;
-            }
+            javaVersion = VER90;
         }
         return javaVersion;
     }
@@ -632,10 +628,10 @@
     }
 
     public static void initLogger() {
+        if (loggerHandler == null) {
+            setLoggerHandler(new ConsoleHandler());
+        }
         if (System.getProperty("java.util.logging.config.file") == null) {
-            if (loggerHandler == null) {
-                setLoggerHandler(new ConsoleHandler());
-            }
             InputStream in = null;
             try {
                 in = Utils.class.getResourceAsStream("/com/sun/tdk/jcov/logging.properties");