Path: blob/master/src/jdk.compiler/share/classes/com/sun/tools/sjavac/Util.java
41175 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.sjavac;2627import java.io.File;28import java.io.PrintWriter;29import java.io.StringWriter;30import java.nio.file.Path;31import java.util.Arrays;32import java.util.Collection;33import java.util.HashSet;34import java.util.Map;35import java.util.Set;36import java.util.StringTokenizer;37import java.util.function.Function;38import java.util.regex.Pattern;39import java.util.stream.Collectors;40import java.util.stream.Stream;4142/**43* Utilities.44*45* <p><b>This is NOT part of any supported API.46* If you write code that depends on this, you do so at your own risk.47* This code and its internal interfaces are subject to change or48* deletion without notice.</b>49*/50public class Util {5152public static String toFileSystemPath(String pkgId) {53if (pkgId == null || pkgId.length()==0) return null;54String pn;55if (pkgId.charAt(0) == ':') {56// When the module is the default empty module.57// Do not prepend the module directory, because there is none.58// Thus :java.foo.bar translates to java/foo/bar (or \)59pn = pkgId.substring(1).replace('.',File.separatorChar);60} else {61// There is a module. Thus jdk.base:java.foo.bar translates62// into jdk.base/java/foo/bar63int cp = pkgId.indexOf(':');64String mn = pkgId.substring(0,cp);65pn = mn+File.separatorChar+pkgId.substring(cp+1).replace('.',File.separatorChar);66}67return pn;68}6970public static String justPackageName(String pkgName) {71int c = pkgName.indexOf(":");72if (c == -1)73throw new IllegalArgumentException("Expected ':' in package name (" + pkgName + ")");74return pkgName.substring(c+1);75}7677public static String extractStringOption(String opName, String s) {78return extractStringOption(opName, s, null);79}8081private static String extractStringOptionWithDelimiter(String opName, String s, String deflt, char delimiter) {82int p = s.indexOf(opName+"=");83if (p == -1) return deflt;84p+=opName.length()+1;85int pe = s.indexOf(delimiter, p);86if (pe == -1) pe = s.length();87return s.substring(p, pe);88}8990public static String extractStringOption(String opName, String s, String deflt) {91return extractStringOptionWithDelimiter(opName, s, deflt, ',');92}9394public static String extractStringOptionLine(String opName, String s, String deflt) {95return extractStringOptionWithDelimiter(opName, s, deflt, '\n').strip();96}9798public static boolean extractBooleanOption(String opName, String s, boolean deflt) {99String str = extractStringOption(opName, s);100return "true".equals(str) ? true101: "false".equals(str) ? false102: deflt;103}104105public static int extractIntOption(String opName, String s) {106return extractIntOption(opName, s, 0);107}108109public static int extractIntOption(String opName, String s, int deflt) {110int p = s.indexOf(opName+"=");111if (p == -1) return deflt;112p+=opName.length()+1;113int pe = s.indexOf(',', p);114if (pe == -1) pe = s.length();115int v = 0;116try {117v = Integer.parseInt(s.substring(p, pe));118} catch (Exception e) {}119return v;120}121122/**123* Extract the package name from a fully qualified class name.124*125* Example: Given "pkg.subpkg.A" this method returns ":pkg.subpkg".126* Given "C" this method returns ":".127*128* @returns package name of the given class name129*/130public static String pkgNameOfClassName(String fqClassName) {131int i = fqClassName.lastIndexOf('.');132String pkg = i == -1 ? "" : fqClassName.substring(0, i);133return ":" + pkg;134}135136/**137* Clean out unwanted sub options supplied inside a primary option.138* For example to only had portfile remaining from:139* settings="--server:id=foo,portfile=bar"140* do settings = cleanOptions("--server:",Util.set("-portfile"),settings);141* now settings equals "--server:portfile=bar"142*143* @param allowedSubOptions A set of the allowed sub options, id portfile etc.144* @param s The option settings string.145*/146public static String cleanSubOptions(Set<String> allowedSubOptions, String s) {147StringBuilder sb = new StringBuilder();148StringTokenizer st = new StringTokenizer(s, ",");149while (st.hasMoreTokens()) {150String o = st.nextToken();151int p = o.indexOf('=');152if (p>0) {153String key = o.substring(0,p);154String val = o.substring(p+1);155if (allowedSubOptions.contains(key)) {156if (sb.length() > 0) sb.append(',');157sb.append(key+"="+val);158}159}160}161return sb.toString();162}163164/**165* Convenience method to create a set with strings.166*/167public static Set<String> set(String... ss) {168Set<String> set = new HashSet<>();169set.addAll(Arrays.asList(ss));170return set;171}172173/**174* Normalize windows drive letter paths to upper case to enable string175* comparison.176*177* @param file File name to normalize178* @return The normalized string if file has a drive letter at the beginning,179* otherwise the original string.180*/181public static String normalizeDriveLetter(String file) {182if (file.length() > 2 && file.charAt(1) == ':') {183return Character.toUpperCase(file.charAt(0)) + file.substring(1);184} else if (file.length() > 3 && file.charAt(0) == '*'185&& file.charAt(2) == ':') {186// Handle a wildcard * at the beginning of the string.187return file.substring(0, 1) + Character.toUpperCase(file.charAt(1))188+ file.substring(2);189}190return file;191}192193/**194* Locate the setting for the server properties.195*/196public static String findServerSettings(String[] args) {197for (String s : args) {198if (s.startsWith("--server:")) {199return s;200}201}202return null;203}204205public static <E> Set<E> union(Set<? extends E> s1,206Set<? extends E> s2) {207Set<E> union = new HashSet<>();208union.addAll(s1);209union.addAll(s2);210return union;211}212213public static <E> Set<E> subtract(Set<? extends E> orig,214Set<? extends E> toSubtract) {215Set<E> difference = new HashSet<>(orig);216difference.removeAll(toSubtract);217return difference;218}219220public static String getStackTrace(Throwable t) {221StringWriter sw = new StringWriter();222t.printStackTrace(new PrintWriter(sw));223return sw.toString();224}225226// TODO: Remove when refactoring from java.io.File to java.nio.file.Path.227public static File pathToFile(Path path) {228return path == null ? null : path.toFile();229}230231public static <E> Set<E> intersection(Collection<? extends E> c1,232Collection<? extends E> c2) {233Set<E> intersection = new HashSet<E>(c1);234intersection.retainAll(c2);235return intersection;236}237238public static <I, T> Map<I, T> indexBy(Collection<? extends T> c,239Function<? super T, ? extends I> indexFunction) {240return c.stream().collect(Collectors.<T, I, T>toMap(indexFunction, o -> o));241}242243public static String fileSuffix(Path file) {244String fileNameStr = file.getFileName().toString();245int dotIndex = fileNameStr.indexOf('.');246return dotIndex == -1 ? "" : fileNameStr.substring(dotIndex);247}248249public static Stream<String> getLines(String str) {250return str.isEmpty()251? Stream.empty()252: Stream.of(str.split(Pattern.quote(System.lineSeparator())));253}254}255256257