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

Print this page




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.sjavac;
  27 
  28 import java.io.File;
  29 import java.io.FileWriter;
  30 import java.io.IOException;
  31 import java.nio.file.Path;
  32 import java.util.Arrays;

  33 import java.util.HashSet;
  34 import java.util.Set;


  35 import java.util.StringTokenizer;
  36 
  37 /**
  38  * Utilities.
  39  *
  40  * <p><b>This is NOT part of any supported API.
  41  * If you write code that depends on this, you do so at your own
  42  * risk.  This code and its internal interfaces are subject to change
  43  * or deletion without notice.</b></p>
  44  */
  45 public class Util {
  46 
  47     public static String toFileSystemPath(String pkgId) {
  48         if (pkgId == null || pkgId.length()==0) return null;
  49         String pn;
  50         if (pkgId.charAt(0) == ':') {
  51             // When the module is the default empty module.
  52             // Do not prepend the module directory, because there is none.
  53             // Thus :java.foo.bar translates to java/foo/bar (or \)
  54             pn = pkgId.substring(1).replace('.',File.separatorChar);


  56             // There is a module. Thus jdk.base:java.foo.bar translates
  57             // into jdk.base/java/foo/bar
  58             int cp = pkgId.indexOf(':');
  59             String mn = pkgId.substring(0,cp);
  60             pn = mn+File.separatorChar+pkgId.substring(cp+1).replace('.',File.separatorChar);
  61         }
  62         return pn;
  63     }
  64 
  65     public static String justPackageName(String pkgName) {
  66         int c = pkgName.indexOf(":");
  67         assert(c != -1);
  68         return pkgName.substring(c+1);
  69     }
  70 
  71     public static String extractStringOption(String opName, String s) {
  72         return extractStringOption(opName, s, null);
  73     }
  74 
  75     public static String extractStringOption(String opName, String s, String deflt) {

  76         int p = s.indexOf(opName+"=");
  77         if (p == -1) return deflt;
  78         p+=opName.length()+1;
  79         int pe = s.indexOf(',', p);
  80         if (pe == -1) pe = s.length();
  81         return s.substring(p, pe);
  82     }
  83 
  84     public static boolean extractBooleanOption(String opName, String s, boolean deflt) {
  85        String str = extractStringOption(opName, s);
  86         return "true".equals(str) ? true
  87              : "false".equals(str) ? false
  88              : deflt;
  89     }
  90 
  91     public static int extractIntOption(String opName, String s) {
  92         return extractIntOption(opName, s, 0);
  93     }
  94 
  95     public static int extractIntOption(String opName, String s, int deflt) {

  96         int p = s.indexOf(opName+"=");
  97         if (p == -1) return deflt;
  98         p+=opName.length()+1;
  99         int pe = s.indexOf(',', p);
 100         if (pe == -1) pe = s.length();
 101         int v = 0;
 102         try {
 103             v = Integer.parseInt(s.substring(p, pe));
 104         } catch (Exception e) {}
 105         return v;
 106     }
 107 
 108     /**
 109      * Clean out unwanted sub options supplied inside a primary option.
 110      * For example to only had portfile remaining from:
 111      *    settings="--server:id=foo,portfile=bar"
 112      * do settings = cleanOptions("--server:",Util.set("-portfile"),settings);
 113      *    now settings equals "--server:portfile=bar"
 114      *
 115      * @param allowsSubOptions A set of the allowed sub options, id portfile etc.


 161         }
 162         return file;
 163     }
 164 
 165     /**
 166      * Locate the setting for the server properties.
 167      */
 168     public static String findServerSettings(String[] args) {
 169         for (String s : args) {
 170             if (s.startsWith("--server:")) {
 171                 return s;
 172             }
 173         }
 174         return null;
 175     }
 176 
 177     // TODO: Remove when refactoring from java.io.File to java.nio.file.Path.
 178     public static File pathToFile(Path path) {
 179         return path == null ? null : path.toFile();
 180     }










































 181 }


  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.sjavac;
  27 
  28 import java.io.File;
  29 import java.io.FileWriter;
  30 import java.io.IOException;
  31 import java.nio.file.Path;
  32 import java.util.Arrays;
  33 import java.util.ArrayList;
  34 import java.util.HashSet;
  35 import java.util.Set;
  36 import java.util.List;
  37 import java.util.Map;
  38 import java.util.StringTokenizer;
  39 
  40 /**
  41  * Utilities.
  42  *
  43  * <p><b>This is NOT part of any supported API.
  44  * If you write code that depends on this, you do so at your own
  45  * risk.  This code and its internal interfaces are subject to change
  46  * or deletion without notice.</b></p>
  47  */
  48 public class Util {
  49 
  50     public static String toFileSystemPath(String pkgId) {
  51         if (pkgId == null || pkgId.length()==0) return null;
  52         String pn;
  53         if (pkgId.charAt(0) == ':') {
  54             // When the module is the default empty module.
  55             // Do not prepend the module directory, because there is none.
  56             // Thus :java.foo.bar translates to java/foo/bar (or \)
  57             pn = pkgId.substring(1).replace('.',File.separatorChar);


  59             // There is a module. Thus jdk.base:java.foo.bar translates
  60             // into jdk.base/java/foo/bar
  61             int cp = pkgId.indexOf(':');
  62             String mn = pkgId.substring(0,cp);
  63             pn = mn+File.separatorChar+pkgId.substring(cp+1).replace('.',File.separatorChar);
  64         }
  65         return pn;
  66     }
  67 
  68     public static String justPackageName(String pkgName) {
  69         int c = pkgName.indexOf(":");
  70         assert(c != -1);
  71         return pkgName.substring(c+1);
  72     }
  73 
  74     public static String extractStringOption(String opName, String s) {
  75         return extractStringOption(opName, s, null);
  76     }
  77 
  78     public static String extractStringOption(String opName, String s, String deflt) {
  79         if (s == null) return deflt;
  80         int p = s.indexOf(opName+"=");
  81         if (p == -1) return deflt;
  82         p+=opName.length()+1;
  83         int pe = s.indexOf(',', p);
  84         if (pe == -1) pe = s.length();
  85         return s.substring(p, pe);
  86     }
  87 
  88     public static boolean extractBooleanOption(String opName, String s, boolean deflt) {
  89        String str = extractStringOption(opName, s);
  90         return "true".equals(str) ? true
  91              : "false".equals(str) ? false
  92              : deflt;
  93     }
  94 
  95     public static int extractIntOption(String opName, String s) {
  96         return extractIntOption(opName, s, 0);
  97     }
  98 
  99     public static int extractIntOption(String opName, String s, int deflt) {
 100         if (s == null) return deflt;
 101         int p = s.indexOf(opName+"=");
 102         if (p == -1) return deflt;
 103         p+=opName.length()+1;
 104         int pe = s.indexOf(',', p);
 105         if (pe == -1) pe = s.length();
 106         int v = 0;
 107         try {
 108             v = Integer.parseInt(s.substring(p, pe));
 109         } catch (Exception e) {}
 110         return v;
 111     }
 112 
 113     /**
 114      * Clean out unwanted sub options supplied inside a primary option.
 115      * For example to only had portfile remaining from:
 116      *    settings="--server:id=foo,portfile=bar"
 117      * do settings = cleanOptions("--server:",Util.set("-portfile"),settings);
 118      *    now settings equals "--server:portfile=bar"
 119      *
 120      * @param allowsSubOptions A set of the allowed sub options, id portfile etc.


 166         }
 167         return file;
 168     }
 169 
 170     /**
 171      * Locate the setting for the server properties.
 172      */
 173     public static String findServerSettings(String[] args) {
 174         for (String s : args) {
 175             if (s.startsWith("--server:")) {
 176                 return s;
 177             }
 178         }
 179         return null;
 180     }
 181 
 182     // TODO: Remove when refactoring from java.io.File to java.nio.file.Path.
 183     public static File pathToFile(Path path) {
 184         return path == null ? null : path.toFile();
 185     }
 186 
 187     /**
 188      * Extract the jar/zip/cym archive file name from a classfile tosttring.
 189      */
 190     public static String extractArchive(String c) {
 191         // Example:
 192         // ZipFileIndexFileObject[/home/fredrik/bin/jdk1.8.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Runnable.class)]
 193         if (c.startsWith("ZipFileIndexFileObject[")) {
 194             int p = c.indexOf('(', 23);
 195             if (p == -1) {
 196                 return null;
 197             }
 198             return c.substring(23, p);
 199         }
 200         return null;
 201     }
 202 
 203     /**
 204      * Utility to add to a Map<String,Set<String>>
 205      */
 206     public static void addToMapSet(String k, String v, Map<String,Set<String>> m) {
 207         Set<String> set = m.get(k);
 208         if (set == null) {
 209             set = new HashSet<String>();
 210             m.put(k, set);
 211         }
 212         set.add(v);
 213     }
 214 
 215     /**
 216      * Utility to add to a Map<String,List<String>>
 217      */
 218     public static void addToMapList(String k, String v, Map<String,List<String>> m) {
 219         List<String> list = m.get(k);
 220         if (list == null) {
 221             list = new ArrayList<String>();
 222             m.put(k, list);
 223         }
 224         list.add(v);
 225     }
 226 
 227         
 228 }