1 /* 2 * Copyright (c) 2012, 2014, 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 26 package com.sun.tools.sjavac; 27 28 import java.io.File; 29 import java.io.PrintWriter; 30 import java.io.StringWriter; 31 import java.nio.file.Path; 32 import java.util.Arrays; 33 import java.util.Collection; 34 import java.util.HashSet; 35 import java.util.Map; 36 import java.util.Set; 37 import java.util.StringTokenizer; 38 import java.util.function.Function; 39 import 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 */ 49 public 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 * 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. 121 * @param s The option settings string. 122 */ 123 public static String cleanSubOptions(Set<String> allowedSubOptions, String s) { 124 StringBuilder sb = new StringBuilder(); 125 StringTokenizer st = new StringTokenizer(s, ","); 126 while (st.hasMoreTokens()) { 127 String o = st.nextToken(); 128 int p = o.indexOf('='); 129 if (p>0) { 130 String key = o.substring(0,p); 131 String val = o.substring(p+1); 132 if (allowedSubOptions.contains(key)) { 133 if (sb.length() > 0) sb.append(','); 134 sb.append(key+"="+val); 135 } 136 } 137 } 138 return sb.toString(); 139 } 140 141 /** 142 * Convenience method to create a set with strings. 143 */ 144 public static Set<String> set(String... ss) { 145 Set<String> set = new HashSet<>(); 146 set.addAll(Arrays.asList(ss)); 147 return set; 148 } 149 150 /** 151 * Normalize windows drive letter paths to upper case to enable string 152 * comparison. 153 * 154 * @param file File name to normalize 155 * @return The normalized string if file has a drive letter at the beginning, 156 * otherwise the original string. 157 */ 158 public static String normalizeDriveLetter(String file) { 159 if (file.length() > 2 && file.charAt(1) == ':') { 160 return Character.toUpperCase(file.charAt(0)) + file.substring(1); 161 } else if (file.length() > 3 && file.charAt(0) == '*' 162 && file.charAt(2) == ':') { 163 // Handle a wildcard * at the beginning of the string. 164 return file.substring(0, 1) + Character.toUpperCase(file.charAt(1)) 165 + file.substring(2); 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 public static <E> Set<E> union(Set<? extends E> s1, 183 Set<? extends E> s2) { 184 Set<E> union = new HashSet<>(); 185 union.addAll(s1); 186 union.addAll(s2); 187 return union; 188 } 189 190 public static <E> Set<E> subtract(Set<? extends E> orig, 191 Set<? extends E> toSubtract) { 192 Set<E> difference = new HashSet<>(orig); 193 difference.removeAll(toSubtract); 194 return difference; 195 } 196 197 public static String getStackTrace(Throwable t) { 198 StringWriter sw = new StringWriter(); 199 t.printStackTrace(new PrintWriter(sw)); 200 return sw.toString(); 201 } 202 203 // TODO: Remove when refactoring from java.io.File to java.nio.file.Path. 204 public static File pathToFile(Path path) { 205 return path == null ? null : path.toFile(); 206 } 207 208 public static <E> Set<E> intersection(Collection<? extends E> c1, 209 Collection<? extends E> c2) { 210 Set<E> intersection = new HashSet<E>(c1); 211 intersection.retainAll(c2); 212 return intersection; 213 } 214 215 public static <I, T> Map<I, T> indexBy(Collection<? extends T> c, 216 Function<? super T, ? extends I> indexFunction) { 217 return c.stream().collect(Collectors.toMap(indexFunction, o -> o)); 218 } 219 }