/
GUtil.java
558 lines (506 loc) · 19.9 KB
/
GUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.util.internal;
import org.apache.commons.lang.StringUtils;
import org.gradle.api.Transformer;
import org.gradle.api.UncheckedIOException;
import org.gradle.api.specs.Spec;
import org.gradle.internal.Cast;
import org.gradle.internal.Factory;
import org.gradle.internal.IoActions;
import org.gradle.internal.UncheckedException;
import org.jetbrains.annotations.Contract;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
public class GUtil {
private static final Pattern WORD_SEPARATOR = Pattern.compile("\\W+");
private static final Pattern UPPER_LOWER = Pattern.compile("(?m)([A-Z]*)([a-z0-9]*)");
public static <T extends Collection<?>> T flatten(Object[] elements, T addTo, boolean flattenMaps) {
return flatten(asList(elements), addTo, flattenMaps);
}
public static <T extends Collection<?>> T flatten(Object[] elements, T addTo) {
return flatten(asList(elements), addTo);
}
public static <T extends Collection<?>> T flatten(Collection<?> elements, T addTo) {
return flatten(elements, addTo, true);
}
public static <T extends Collection<?>> T flattenElements(Object... elements) {
Collection<T> out = new LinkedList<T>();
flatten(elements, out, true);
return Cast.uncheckedNonnullCast(out);
}
public static <T extends Collection<?>> T flatten(Collection<?> elements, T addTo, boolean flattenMapsAndArrays) {
return flatten(elements, addTo, flattenMapsAndArrays, flattenMapsAndArrays);
}
public static <T extends Collection<?>> T flatten(Collection<?> elements, T addTo, boolean flattenMaps, boolean flattenArrays) {
Iterator<?> iter = elements.iterator();
while (iter.hasNext()) {
Object element = iter.next();
if (element instanceof Collection) {
flatten((Collection<?>) element, addTo, flattenMaps, flattenArrays);
} else if ((element instanceof Map) && flattenMaps) {
flatten(((Map<?, ?>) element).values(), addTo, flattenMaps, flattenArrays);
} else if ((element.getClass().isArray()) && flattenArrays) {
flatten(asList((Object[]) element), addTo, flattenMaps, flattenArrays);
} else {
(Cast.<Collection<Object>>uncheckedNonnullCast(addTo)).add(element);
}
}
return addTo;
}
/**
* Flattens input collections (including arrays *but* not maps). If input is not a collection wraps it in a collection and returns it.
*
* @param input any object
* @return collection of flattened input or single input wrapped in a collection.
*/
public static Collection<?> collectionize(Object input) {
if (input == null) {
return emptyList();
} else if (input instanceof Collection) {
Collection<?> out = new LinkedList<Object>();
flatten((Collection<?>) input, out, false, true);
return out;
} else if (input.getClass().isArray()) {
Collection<?> out = new LinkedList<Object>();
flatten(asList((Object[]) input), out, false, true);
return out;
} else {
return asList(input);
}
}
public static List<Object> flatten(Collection<Object> elements, boolean flattenMapsAndArrays) {
return flatten(elements, new ArrayList<Object>(), flattenMapsAndArrays);
}
public static List<Object> flatten(Collection<Object> elements) {
return flatten(elements, new ArrayList<Object>());
}
public static String asPath(Iterable<?> collection) {
return CollectionUtils.join(File.pathSeparator, collection);
}
public static List<String> prefix(String prefix, Collection<String> strings) {
List<String> prefixed = new ArrayList<String>();
for (String string : strings) {
prefixed.add(prefix + string);
}
return prefixed;
}
public static boolean isTrue(@Nullable Object object) {
if (object == null) {
return false;
}
if (object instanceof Collection) {
return ((Collection) object).size() > 0;
} else if (object instanceof String) {
return ((String) object).length() > 0;
}
return true;
}
/**
* Prefer {@link #getOrDefault(Object, Factory)} if the value is expensive to compute or
* would trigger early configuration.
*/
@Nullable
@Contract("!null, _ -> param1; null, !null -> param2; null, null -> null")
public static <T> T elvis(@Nullable T object, @Nullable T defaultValue) {
return isTrue(object) ? object : defaultValue;
}
@Nullable
public static <T> T getOrDefault(@Nullable T object, Factory<T> defaultValueSupplier) {
return isTrue(object) ? object : defaultValueSupplier.create();
}
public static <V, T extends Collection<? super V>> T addToCollection(T dest, boolean failOnNull, Iterable<? extends V> src) {
for (V v : src) {
if (failOnNull && v == null) {
throw new IllegalArgumentException("Illegal null value provided in this collection: " + src);
}
dest.add(v);
}
return dest;
}
public static <V, T extends Collection<? super V>> T addToCollection(T dest, Iterable<? extends V> src) {
return addToCollection(dest, false, src);
}
@Deprecated
public static <V, T extends Collection<? super V>> T addToCollection(T dest, boolean failOnNull, Iterable<? extends V>... srcs) {
for (Iterable<? extends V> src : srcs) {
for (V v : src) {
if (failOnNull && v == null) {
throw new IllegalArgumentException("Illegal null value provided in this collection: " + src);
}
dest.add(v);
}
}
return dest;
}
@Deprecated
public static <V, T extends Collection<? super V>> T addToCollection(T dest, Iterable<? extends V>... srcs) {
return addToCollection(dest, false, srcs);
}
public static Comparator<String> caseInsensitive() {
return new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int diff = o1.compareToIgnoreCase(o2);
if (diff != 0) {
return diff;
}
return o1.compareTo(o2);
}
};
}
public static <K, V> Map<K, V> addMaps(Map<K, V> map1, Map<K, V> map2) {
HashMap<K, V> map = new HashMap<K, V>();
map.putAll(map1);
map.putAll(map2);
return map;
}
public static void addToMap(Map<String, String> dest, Map<?, ?> src) {
for (Map.Entry<?, ?> entry : src.entrySet()) {
dest.put(entry.getKey().toString(), entry.getValue().toString());
}
}
public static Properties loadProperties(File propertyFile) {
try {
FileInputStream inputStream = new FileInputStream(propertyFile);
try {
return loadProperties(inputStream);
} finally {
inputStream.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static Properties loadProperties(URL url) {
try {
URLConnection uc = url.openConnection();
uc.setUseCaches(false);
return loadProperties(uc.getInputStream());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static Properties loadProperties(InputStream inputStream) {
Properties properties = new Properties();
try {
properties.load(inputStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
IoActions.closeQuietly(inputStream);
}
return properties;
}
public static void saveProperties(Properties properties, File propertyFile) {
try {
FileOutputStream propertiesFileOutputStream = new FileOutputStream(propertyFile);
try {
properties.store(propertiesFileOutputStream, null);
} finally {
propertiesFileOutputStream.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static void saveProperties(Properties properties, OutputStream outputStream) {
try {
try {
properties.store(outputStream, null);
} finally {
outputStream.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static Map<Object, Object> map(Object... objects) {
Map<Object, Object> map = new HashMap<Object, Object>();
assert objects.length % 2 == 0;
for (int i = 0; i < objects.length; i += 2) {
map.put(objects[i], objects[i + 1]);
}
return map;
}
public static String toString(Iterable<?> names) {
Formatter formatter = new Formatter();
boolean first = true;
for (Object name : names) {
if (first) {
formatter.format("'%s'", name);
first = false;
} else {
formatter.format(", '%s'", name);
}
}
return formatter.toString();
}
/**
* Converts an arbitrary string to a camel-case string which can be used in a Java identifier. Eg, with_underscores -> withUnderscores
*/
public static String toCamelCase(CharSequence string) {
return toCamelCase(string, false);
}
public static String toLowerCamelCase(CharSequence string) {
return toCamelCase(string, true);
}
private static String toCamelCase(CharSequence string, boolean lower) {
if (string == null) {
return null;
}
StringBuilder builder = new StringBuilder();
Matcher matcher = WORD_SEPARATOR.matcher(string);
int pos = 0;
boolean first = true;
while (matcher.find()) {
String chunk = string.subSequence(pos, matcher.start()).toString();
pos = matcher.end();
if (chunk.isEmpty()) {
continue;
}
if (lower && first) {
chunk = StringUtils.uncapitalize(chunk);
first = false;
} else {
chunk = StringUtils.capitalize(chunk);
}
builder.append(chunk);
}
String rest = string.subSequence(pos, string.length()).toString();
if (lower && first) {
rest = StringUtils.uncapitalize(rest);
} else {
rest = StringUtils.capitalize(rest);
}
builder.append(rest);
return builder.toString();
}
/**
* Converts an arbitrary string to upper case identifier with words separated by _. Eg, camelCase -> CAMEL_CASE
*/
public static String toConstant(CharSequence string) {
if (string == null) {
return null;
}
return toWords(string, '_').toUpperCase();
}
/**
* Converts an arbitrary string to space-separated words. Eg, camelCase -> camel case, with_underscores -> with underscores
*/
public static String toWords(CharSequence string) {
return toWords(string, ' ');
}
public static String toWords(CharSequence string, char separator) {
if (string == null) {
return null;
}
StringBuilder builder = new StringBuilder();
int pos = 0;
Matcher matcher = UPPER_LOWER.matcher(string);
while (pos < string.length()) {
matcher.find(pos);
if (matcher.end() == pos) {
// Not looking at a match
pos++;
continue;
}
if (builder.length() > 0) {
builder.append(separator);
}
String group1 = matcher.group(1).toLowerCase();
String group2 = matcher.group(2);
if (group2.length() == 0) {
builder.append(group1);
} else {
if (group1.length() > 1) {
builder.append(group1.substring(0, group1.length() - 1));
builder.append(separator);
builder.append(group1.substring(group1.length() - 1));
} else {
builder.append(group1);
}
builder.append(group2);
}
pos = matcher.end();
}
return builder.toString();
}
public static <T> Comparator<T> last(final Comparator<? super T> comparator, final T lastValue) {
return new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
boolean o1Last = comparator.compare(o1, lastValue) == 0;
boolean o2Last = comparator.compare(o2, lastValue) == 0;
if (o1Last && o2Last) {
return 0;
}
if (o1Last && !o2Last) {
return 1;
}
if (!o1Last && o2Last) {
return -1;
}
return comparator.compare(o1, o2);
}
};
}
/**
* Calls the given callable converting any thrown exception to an unchecked exception via {@link UncheckedException#throwAsUncheckedException(Throwable)}
*
* @param callable The callable to call
* @param <T> Callable's return type
* @return The value returned by {@link Callable#call()}
*/
@Nullable
public static <T> T uncheckedCall(Callable<T> callable) {
try {
return callable.call();
} catch (Exception e) {
throw UncheckedException.throwAsUncheckedException(e);
}
}
public static <T extends Enum<T>> T toEnum(Class<? extends T> enumType, Object value) {
if (enumType.isInstance(value)) {
return enumType.cast(value);
}
if (value instanceof CharSequence) {
final String literal = value.toString();
T match = findEnumValue(enumType, literal);
if (match != null) {
return match;
}
final String alternativeLiteral = toWords(literal, '_');
match = findEnumValue(enumType, alternativeLiteral);
if (match != null) {
return match;
}
throw new IllegalArgumentException(
String.format("Cannot convert string value '%s' to an enum value of type '%s' (valid case insensitive values: %s)",
literal, enumType.getName(), CollectionUtils.join(", ", CollectionUtils.collect(Arrays.asList(enumType.getEnumConstants()), new Transformer<String, T>() {
@Override
public String transform(T t) {
return t.name();
}
}))
)
);
}
throw new IllegalArgumentException(String.format("Cannot convert value '%s' of type '%s' to enum type '%s'",
value, value.getClass().getName(), enumType.getName()));
}
@Nullable
private static <T extends Enum<T>> T findEnumValue(Class<? extends T> enumType, final String literal) {
List<? extends T> enumConstants = Arrays.asList(enumType.getEnumConstants());
return CollectionUtils.findFirst(enumConstants, new Spec<T>() {
@Override
public boolean isSatisfiedBy(T enumValue) {
return enumValue.name().equalsIgnoreCase(literal);
}
});
}
public static <T extends Enum<T>> EnumSet<T> toEnumSet(Class<T> enumType, Object[] values) {
return toEnumSet(enumType, Arrays.asList(values));
}
public static <T extends Enum<T>> EnumSet<T> toEnumSet(Class<T> enumType, Iterable<?> values) {
EnumSet<T> result = EnumSet.noneOf(enumType);
for (Object value : values) {
result.add(toEnum(enumType, value));
}
return result;
}
/**
* Checks whether the fist {@link CharSequence} ends with the second.
*
* If the {@link CharSequence#charAt(int)} method of both sequences is fast,
* this check is faster than converting them to Strings and using {@link String#endsWith(String)}.
*/
public static boolean endsWith(CharSequence longer, CharSequence shorter) {
if (longer instanceof String && shorter instanceof String) {
return ((String) longer).endsWith((String) shorter);
}
int longerLength = longer.length();
int shorterLength = shorter.length();
if (longerLength < shorterLength) {
return false;
}
for (int i = shorterLength; i > 0; i--) {
if (longer.charAt(longerLength - i) != shorter.charAt(shorterLength - i)) {
return false;
}
}
return true;
}
public static URI toSecureUrl(URI scriptUri) {
try {
return new URI("https", null, scriptUri.getHost(), scriptUri.getPort(), scriptUri.getPath(), scriptUri.getQuery(), scriptUri.getFragment());
} catch (URISyntaxException e) {
throw new IllegalArgumentException("could not make url use https", e);
}
}
public static boolean isSecureUrl(URI url) {
/*
* TL;DR: http://127.0.0.1 will bypass this validation, http://localhost will fail this validation.
*
* Hundreds of Gradle's integration tests use a local web-server to test logic that relies upon
* this behavior.
*
* Changing all of those tests so that they use a KeyStore would have been impractical.
* Instead, the test fixture was updated to use 127.0.0.1 when making HTTP requests.
*
* This allows tests that still want to exercise the deprecation logic to use http://localhost
* which will bypass this check and trigger the validation.
*
* It's important to note that the only way to create a certificate for an IP address is to bind
* the IP address as a 'Subject Alternative Name' which was deemed far too complicated for our test
* use case.
*
* Additionally, in the rare case that a user or a plugin author truly needs to test with a localhost
* server, they can use http://127.0.0.1
*/
if ("127.0.0.1".equals(url.getHost())) {
return true;
}
final String scheme = url.getScheme();
return !"http".equalsIgnoreCase(scheme);
}
}