-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathModuleNameUtil.java
More file actions
149 lines (132 loc) · 4.48 KB
/
ModuleNameUtil.java
File metadata and controls
149 lines (132 loc) · 4.48 KB
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
// SPDX-License-Identifier: Apache-2.0
package org.gradlex.javamodule.moduleinfo;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jspecify.annotations.NullMarked;
/**
* Implementation based on 'jdk.internal.module.ModulePath#deriveModuleDescriptor' and related methods.
*/
@NullMarked
class ModuleNameUtil {
private static final Pattern DASH_VERSION = Pattern.compile("-(\\d+(\\.|$))");
private static final Pattern NON_ALPHANUM = Pattern.compile("[^A-Za-z0-9]");
private static final Pattern REPEATING_DOTS = Pattern.compile("(\\.)(\\1)+");
private static final Pattern LEADING_DOTS = Pattern.compile("^\\.");
private static final Pattern TRAILING_DOTS = Pattern.compile("\\.$");
static String automaticModulNameFromFileName(File jarFile) {
// Derive the version, and the module name if needed, from JAR file name
String fn = jarFile.getName();
int i = fn.lastIndexOf(File.separator);
if (i != -1) fn = fn.substring(i + 1);
// drop ".jar"
String name = fn.substring(0, fn.length() - 4);
// find first occurrence of -${NUMBER}. or -${NUMBER}$
Matcher matcher = DASH_VERSION.matcher(name);
if (matcher.find()) {
name = name.substring(0, matcher.start());
}
return validateModuleName(cleanModuleName(name));
}
static String validateModuleName(String name) {
int next;
int off = 0;
while ((next = name.indexOf('.', off)) != -1) {
String id = name.substring(off, next);
if (!isJavaIdentifier(id)) {
throw new IllegalArgumentException(
name + ": Invalid module name" + ": '" + id + "' is not a Java identifier");
}
off = next + 1;
}
String last = name.substring(off);
if (!isJavaIdentifier(last)) {
throw new IllegalArgumentException(
name + ": Invalid module name" + ": '" + last + "' is not a Java identifier");
}
return name;
}
private static String cleanModuleName(String mn) {
// replace non-alphanumeric
mn = NON_ALPHANUM.matcher(mn).replaceAll(".");
// collapse repeating dots
mn = REPEATING_DOTS.matcher(mn).replaceAll(".");
// drop leading dots
if (!mn.isEmpty() && mn.charAt(0) == '.') mn = LEADING_DOTS.matcher(mn).replaceAll("");
// drop trailing dots
int len = mn.length();
if (len > 0 && mn.charAt(len - 1) == '.') mn = TRAILING_DOTS.matcher(mn).replaceAll("");
return mn;
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private static boolean isJavaIdentifier(String str) {
if (str.isEmpty() || RESERVED.contains(str)) return false;
int first = Character.codePointAt(str, 0);
if (!Character.isJavaIdentifierStart(first)) return false;
int i = Character.charCount(first);
while (i < str.length()) {
int cp = Character.codePointAt(str, i);
if (!Character.isJavaIdentifierPart(cp)) return false;
i += Character.charCount(cp);
}
return true;
}
// keywords, boolean and null literals, not allowed in identifiers
private static final List<String> RESERVED = Arrays.asList(
"abstract",
"assert",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"default",
"do",
"double",
"else",
"enum",
"extends",
"final",
"finally",
"float",
"for",
"goto",
"if",
"implements",
"import",
"instanceof",
"int",
"interface",
"long",
"native",
"new",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"strictfp",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"try",
"void",
"volatile",
"while",
"true",
"false",
"null",
"_");
}