Util.java revision 2958:27da0c3ac83a
1/* 2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 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 26package com.sun.tools.sjavac; 27 28import java.io.File; 29import java.io.PrintWriter; 30import java.io.StringWriter; 31import java.nio.file.Path; 32import java.util.Arrays; 33import java.util.Collection; 34import java.util.HashSet; 35import java.util.Map; 36import java.util.Set; 37import java.util.StringTokenizer; 38import java.util.function.Function; 39import java.util.stream.Collectors; 40 41/** 42 * Utilities. 43 * 44 * <p><b>This is NOT part of any supported API. 45 * If you write code that depends on this, you do so at your own risk. 46 * This code and its internal interfaces are subject to change or 47 * deletion without notice.</b> 48 */ 49public class Util { 50 51 public static String toFileSystemPath(String pkgId) { 52 if (pkgId == null || pkgId.length()==0) return null; 53 String pn; 54 if (pkgId.charAt(0) == ':') { 55 // When the module is the default empty module. 56 // Do not prepend the module directory, because there is none. 57 // Thus :java.foo.bar translates to java/foo/bar (or \) 58 pn = pkgId.substring(1).replace('.',File.separatorChar); 59 } else { 60 // There is a module. Thus jdk.base:java.foo.bar translates 61 // into jdk.base/java/foo/bar 62 int cp = pkgId.indexOf(':'); 63 String mn = pkgId.substring(0,cp); 64 pn = mn+File.separatorChar+pkgId.substring(cp+1).replace('.',File.separatorChar); 65 } 66 return pn; 67 } 68 69 public static String justPackageName(String pkgName) { 70 int c = pkgName.indexOf(":"); 71 if (c == -1) 72 throw new IllegalArgumentException("Expected ':' in package name (" + pkgName + ")"); 73 return pkgName.substring(c+1); 74 } 75 76 public static String extractStringOption(String opName, String s) { 77 return extractStringOption(opName, s, null); 78 } 79 80 public static String extractStringOption(String opName, String s, String deflt) { 81 int p = s.indexOf(opName+"="); 82 if (p == -1) return deflt; 83 p+=opName.length()+1; 84 int pe = s.indexOf(',', p); 85 if (pe == -1) pe = s.length(); 86 return s.substring(p, pe); 87 } 88 89 public static boolean extractBooleanOption(String opName, String s, boolean deflt) { 90 String str = extractStringOption(opName, s); 91 return "true".equals(str) ? true 92 : "false".equals(str) ? false 93 : deflt; 94 } 95 96 public static int extractIntOption(String opName, String s) { 97 return extractIntOption(opName, s, 0); 98 } 99 100 public static int extractIntOption(String opName, String s, int 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 * Extract the package name from a fully qualified class name. 115 * 116 * Example: Given "pkg.subpkg.A" this method returns ":pkg.subpkg". 117 * Given "C" this method returns ":". 118 * 119 * @returns package name of the given class name 120 */ 121 public static String pkgNameOfClassName(String fqClassName) { 122 int i = fqClassName.lastIndexOf('.'); 123 String pkg = i == -1 ? "" : fqClassName.substring(0, i); 124 return ":" + pkg; 125 } 126 127 /** 128 * Clean out unwanted sub options supplied inside a primary option. 129 * For example to only had portfile remaining from: 130 * settings="--server:id=foo,portfile=bar" 131 * do settings = cleanOptions("--server:",Util.set("-portfile"),settings); 132 * now settings equals "--server:portfile=bar" 133 * 134 * @param allowsSubOptions A set of the allowed sub options, id portfile etc. 135 * @param s The option settings string. 136 */ 137 public static String cleanSubOptions(Set<String> allowedSubOptions, String s) { 138 StringBuilder sb = new StringBuilder(); 139 StringTokenizer st = new StringTokenizer(s, ","); 140 while (st.hasMoreTokens()) { 141 String o = st.nextToken(); 142 int p = o.indexOf('='); 143 if (p>0) { 144 String key = o.substring(0,p); 145 String val = o.substring(p+1); 146 if (allowedSubOptions.contains(key)) { 147 if (sb.length() > 0) sb.append(','); 148 sb.append(key+"="+val); 149 } 150 } 151 } 152 return sb.toString(); 153 } 154 155 /** 156 * Convenience method to create a set with strings. 157 */ 158 public static Set<String> set(String... ss) { 159 Set<String> set = new HashSet<>(); 160 set.addAll(Arrays.asList(ss)); 161 return set; 162 } 163 164 /** 165 * Normalize windows drive letter paths to upper case to enable string 166 * comparison. 167 * 168 * @param file File name to normalize 169 * @return The normalized string if file has a drive letter at the beginning, 170 * otherwise the original string. 171 */ 172 public static String normalizeDriveLetter(String file) { 173 if (file.length() > 2 && file.charAt(1) == ':') { 174 return Character.toUpperCase(file.charAt(0)) + file.substring(1); 175 } else if (file.length() > 3 && file.charAt(0) == '*' 176 && file.charAt(2) == ':') { 177 // Handle a wildcard * at the beginning of the string. 178 return file.substring(0, 1) + Character.toUpperCase(file.charAt(1)) 179 + file.substring(2); 180 } 181 return file; 182 } 183 184 /** 185 * Locate the setting for the server properties. 186 */ 187 public static String findServerSettings(String[] args) { 188 for (String s : args) { 189 if (s.startsWith("--server:")) { 190 return s; 191 } 192 } 193 return null; 194 } 195 196 public static <E> Set<E> union(Set<? extends E> s1, 197 Set<? extends E> s2) { 198 Set<E> union = new HashSet<>(); 199 union.addAll(s1); 200 union.addAll(s2); 201 return union; 202 } 203 204 public static <E> Set<E> subtract(Set<? extends E> orig, 205 Set<? extends E> toSubtract) { 206 Set<E> difference = new HashSet<>(orig); 207 difference.removeAll(toSubtract); 208 return difference; 209 } 210 211 public static String getStackTrace(Throwable t) { 212 StringWriter sw = new StringWriter(); 213 t.printStackTrace(new PrintWriter(sw)); 214 return sw.toString(); 215 } 216 217 // TODO: Remove when refactoring from java.io.File to java.nio.file.Path. 218 public static File pathToFile(Path path) { 219 return path == null ? null : path.toFile(); 220 } 221 222 public static <E> Set<E> intersection(Collection<? extends E> c1, 223 Collection<? extends E> c2) { 224 Set<E> intersection = new HashSet<E>(c1); 225 intersection.retainAll(c2); 226 return intersection; 227 } 228 229 public static <I, T> Map<I, T> indexBy(Collection<? extends T> c, 230 Function<? super T, ? extends I> indexFunction) { 231 return c.stream().collect(Collectors.toMap(indexFunction, o -> o)); 232 } 233} 234