Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.compiler/share/classes/com/sun/tools/sjavac/Util.java
41175 views
1
/*
2
* Copyright (c) 2012, 2020, 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.regex.Pattern;
40
import java.util.stream.Collectors;
41
import java.util.stream.Stream;
42
43
/**
44
* Utilities.
45
*
46
* <p><b>This is NOT part of any supported API.
47
* If you write code that depends on this, you do so at your own risk.
48
* This code and its internal interfaces are subject to change or
49
* deletion without notice.</b>
50
*/
51
public class Util {
52
53
public static String toFileSystemPath(String pkgId) {
54
if (pkgId == null || pkgId.length()==0) return null;
55
String pn;
56
if (pkgId.charAt(0) == ':') {
57
// When the module is the default empty module.
58
// Do not prepend the module directory, because there is none.
59
// Thus :java.foo.bar translates to java/foo/bar (or \)
60
pn = pkgId.substring(1).replace('.',File.separatorChar);
61
} else {
62
// There is a module. Thus jdk.base:java.foo.bar translates
63
// into jdk.base/java/foo/bar
64
int cp = pkgId.indexOf(':');
65
String mn = pkgId.substring(0,cp);
66
pn = mn+File.separatorChar+pkgId.substring(cp+1).replace('.',File.separatorChar);
67
}
68
return pn;
69
}
70
71
public static String justPackageName(String pkgName) {
72
int c = pkgName.indexOf(":");
73
if (c == -1)
74
throw new IllegalArgumentException("Expected ':' in package name (" + pkgName + ")");
75
return pkgName.substring(c+1);
76
}
77
78
public static String extractStringOption(String opName, String s) {
79
return extractStringOption(opName, s, null);
80
}
81
82
private static String extractStringOptionWithDelimiter(String opName, String s, String deflt, char delimiter) {
83
int p = s.indexOf(opName+"=");
84
if (p == -1) return deflt;
85
p+=opName.length()+1;
86
int pe = s.indexOf(delimiter, p);
87
if (pe == -1) pe = s.length();
88
return s.substring(p, pe);
89
}
90
91
public static String extractStringOption(String opName, String s, String deflt) {
92
return extractStringOptionWithDelimiter(opName, s, deflt, ',');
93
}
94
95
public static String extractStringOptionLine(String opName, String s, String deflt) {
96
return extractStringOptionWithDelimiter(opName, s, deflt, '\n').strip();
97
}
98
99
public static boolean extractBooleanOption(String opName, String s, boolean deflt) {
100
String str = extractStringOption(opName, s);
101
return "true".equals(str) ? true
102
: "false".equals(str) ? false
103
: deflt;
104
}
105
106
public static int extractIntOption(String opName, String s) {
107
return extractIntOption(opName, s, 0);
108
}
109
110
public static int extractIntOption(String opName, String s, int deflt) {
111
int p = s.indexOf(opName+"=");
112
if (p == -1) return deflt;
113
p+=opName.length()+1;
114
int pe = s.indexOf(',', p);
115
if (pe == -1) pe = s.length();
116
int v = 0;
117
try {
118
v = Integer.parseInt(s.substring(p, pe));
119
} catch (Exception e) {}
120
return v;
121
}
122
123
/**
124
* Extract the package name from a fully qualified class name.
125
*
126
* Example: Given "pkg.subpkg.A" this method returns ":pkg.subpkg".
127
* Given "C" this method returns ":".
128
*
129
* @returns package name of the given class name
130
*/
131
public static String pkgNameOfClassName(String fqClassName) {
132
int i = fqClassName.lastIndexOf('.');
133
String pkg = i == -1 ? "" : fqClassName.substring(0, i);
134
return ":" + pkg;
135
}
136
137
/**
138
* Clean out unwanted sub options supplied inside a primary option.
139
* For example to only had portfile remaining from:
140
* settings="--server:id=foo,portfile=bar"
141
* do settings = cleanOptions("--server:",Util.set("-portfile"),settings);
142
* now settings equals "--server:portfile=bar"
143
*
144
* @param allowedSubOptions A set of the allowed sub options, id portfile etc.
145
* @param s The option settings string.
146
*/
147
public static String cleanSubOptions(Set<String> allowedSubOptions, String s) {
148
StringBuilder sb = new StringBuilder();
149
StringTokenizer st = new StringTokenizer(s, ",");
150
while (st.hasMoreTokens()) {
151
String o = st.nextToken();
152
int p = o.indexOf('=');
153
if (p>0) {
154
String key = o.substring(0,p);
155
String val = o.substring(p+1);
156
if (allowedSubOptions.contains(key)) {
157
if (sb.length() > 0) sb.append(',');
158
sb.append(key+"="+val);
159
}
160
}
161
}
162
return sb.toString();
163
}
164
165
/**
166
* Convenience method to create a set with strings.
167
*/
168
public static Set<String> set(String... ss) {
169
Set<String> set = new HashSet<>();
170
set.addAll(Arrays.asList(ss));
171
return set;
172
}
173
174
/**
175
* Normalize windows drive letter paths to upper case to enable string
176
* comparison.
177
*
178
* @param file File name to normalize
179
* @return The normalized string if file has a drive letter at the beginning,
180
* otherwise the original string.
181
*/
182
public static String normalizeDriveLetter(String file) {
183
if (file.length() > 2 && file.charAt(1) == ':') {
184
return Character.toUpperCase(file.charAt(0)) + file.substring(1);
185
} else if (file.length() > 3 && file.charAt(0) == '*'
186
&& file.charAt(2) == ':') {
187
// Handle a wildcard * at the beginning of the string.
188
return file.substring(0, 1) + Character.toUpperCase(file.charAt(1))
189
+ file.substring(2);
190
}
191
return file;
192
}
193
194
/**
195
* Locate the setting for the server properties.
196
*/
197
public static String findServerSettings(String[] args) {
198
for (String s : args) {
199
if (s.startsWith("--server:")) {
200
return s;
201
}
202
}
203
return null;
204
}
205
206
public static <E> Set<E> union(Set<? extends E> s1,
207
Set<? extends E> s2) {
208
Set<E> union = new HashSet<>();
209
union.addAll(s1);
210
union.addAll(s2);
211
return union;
212
}
213
214
public static <E> Set<E> subtract(Set<? extends E> orig,
215
Set<? extends E> toSubtract) {
216
Set<E> difference = new HashSet<>(orig);
217
difference.removeAll(toSubtract);
218
return difference;
219
}
220
221
public static String getStackTrace(Throwable t) {
222
StringWriter sw = new StringWriter();
223
t.printStackTrace(new PrintWriter(sw));
224
return sw.toString();
225
}
226
227
// TODO: Remove when refactoring from java.io.File to java.nio.file.Path.
228
public static File pathToFile(Path path) {
229
return path == null ? null : path.toFile();
230
}
231
232
public static <E> Set<E> intersection(Collection<? extends E> c1,
233
Collection<? extends E> c2) {
234
Set<E> intersection = new HashSet<E>(c1);
235
intersection.retainAll(c2);
236
return intersection;
237
}
238
239
public static <I, T> Map<I, T> indexBy(Collection<? extends T> c,
240
Function<? super T, ? extends I> indexFunction) {
241
return c.stream().collect(Collectors.<T, I, T>toMap(indexFunction, o -> o));
242
}
243
244
public static String fileSuffix(Path file) {
245
String fileNameStr = file.getFileName().toString();
246
int dotIndex = fileNameStr.indexOf('.');
247
return dotIndex == -1 ? "" : fileNameStr.substring(dotIndex);
248
}
249
250
public static Stream<String> getLines(String str) {
251
return str.isEmpty()
252
? Stream.empty()
253
: Stream.of(str.split(Pattern.quote(System.lineSeparator())));
254
}
255
}
256
257