src/share/classes/com/sun/tools/sjavac/Util.java

Print this page

        

@@ -28,12 +28,15 @@
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.nio.file.Path;
 import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.List;
+import java.util.Map;
 import java.util.StringTokenizer;
 
 /**
  * Utilities.
  *

@@ -71,10 +74,11 @@
     public static String extractStringOption(String opName, String s) {
         return extractStringOption(opName, s, null);
     }
 
     public static String extractStringOption(String opName, String s, String deflt) {
+        if (s == null) return deflt;
         int p = s.indexOf(opName+"=");
         if (p == -1) return deflt;
         p+=opName.length()+1;
         int pe = s.indexOf(',', p);
         if (pe == -1) pe = s.length();

@@ -91,10 +95,11 @@
     public static int extractIntOption(String opName, String s) {
         return extractIntOption(opName, s, 0);
     }
 
     public static int extractIntOption(String opName, String s, int deflt) {
+        if (s == null) return deflt;
         int p = s.indexOf(opName+"=");
         if (p == -1) return deflt;
         p+=opName.length()+1;
         int pe = s.indexOf(',', p);
         if (pe == -1) pe = s.length();

@@ -176,6 +181,48 @@
 
     // TODO: Remove when refactoring from java.io.File to java.nio.file.Path.
     public static File pathToFile(Path path) {
         return path == null ? null : path.toFile();
     }
+
+    /**
+     * Extract the jar/zip/cym archive file name from a classfile tosttring.
+     */
+    public static String extractArchive(String c) {
+        // Example:
+        // ZipFileIndexFileObject[/home/fredrik/bin/jdk1.8.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Runnable.class)]
+        if (c.startsWith("ZipFileIndexFileObject[")) {
+            int p = c.indexOf('(', 23);
+            if (p == -1) {
+                return null;
+            }
+            return c.substring(23, p);
+        }
+        return null;
+    }
+
+    /**
+     * Utility to add to a Map<String,Set<String>>
+     */
+    public static void addToMapSet(String k, String v, Map<String,Set<String>> m) {
+        Set<String> set = m.get(k);
+        if (set == null) {
+            set = new HashSet<String>();
+            m.put(k, set);
+        }
+        set.add(v);
+    }
+
+    /**
+     * Utility to add to a Map<String,List<String>>
+     */
+    public static void addToMapList(String k, String v, Map<String,List<String>> m) {
+        List<String> list = m.get(k);
+        if (list == null) {
+            list = new ArrayList<String>();
+            m.put(k, list);
+        }
+        list.add(v);
+    }
+
+        
 }