-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNameUtils.java
More file actions
98 lines (81 loc) · 2.47 KB
/
NameUtils.java
File metadata and controls
98 lines (81 loc) · 2.47 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
package gir2java;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class NameUtils {
public static final Set<String> keywords = new HashSet<String>(Arrays.asList(
"abstract", "continue", "for", "new", "switch",
"assert", "default", "goto", "package", "synchronized",
"boolean", "do", "if", "private", "this",
"break", "double", "implements", "protected", "throw",
"byte", "else", "import", "public", "throws",
"case", "enum", "instanceof", "return", "transient",
"catch", "extends", "int", "short", "try",
"char", "final", "interface", "static", "void",
"class", "finally", "long", "strictfp", "volatile",
"const", "float", "native", "super", "while"
));
public static String javaifyPackageName(String packageName) {
return packageName.replaceAll("[^a-zA-Z0-9_]", "").toLowerCase();
}
public static String toCamel(String name) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
if (name.charAt(i) == '_') {
if (i < name.length() - 1) {
sb.append(Character.toUpperCase(name.charAt(i+1)));
}
i++;
} else if(i == 0) {
sb.append(Character.toUpperCase(name.charAt(i)));
} else {
sb.append(name.charAt(i));
}
}
return sb.toString();
}
public static String typeToNamespace(String typeName) {
int dotIdx = typeName.indexOf('.');
if (dotIdx == -1) {
return null;
}
return typeName.substring(0, dotIdx);
}
public static String typeToSimpleName(String typeName) {
int dotIdx = typeName.indexOf('.');
if (dotIdx == -1) {
return typeName;
}
return typeName.substring(dotIdx + 1);
}
public static int getIndirectionLevel(String cType) {
int firstStar = cType.indexOf('*');
if (firstStar == -1) {
return 0;
}
int lastStar = cType.lastIndexOf('*');
String stars = cType.substring(firstStar, lastStar + 1).trim();
return stars.length();
}
/**
* If the input is a Java keyword, change it in a way that makes it legal as an identifier. If the input is not a
* keyword, it is returned as-is.
* @param input
* @return
*/
public static String neutralizeKeyword(String input) {
if (keywords.contains(input)) {
return "_" + input;
} else {
return input;
}
}
/**
* Convert CamelCase strings to ALL_UPPERCASE_WITH_UNDERSCORES.
* @param camel
* @return
*/
public static String camelToUpper(String camel) {
return camel.replaceAll("([A-Z][a-z0-9])", "_$1").substring(1).toUpperCase();
}
}