是否有一些很好的方法可以检查某些字符串是否代表有效的(完全限定的)Java类名?例如,org.comPAny.ClassName
甚至mYcRAZYcLASSnAME
都是有效的类名,但org..package.MyClass
或org.ClassName.
之类的名称不是。我想在不加载该类的情况下检查类名有效性。
Java中是否有一些方便的方法来检查?或者,您是否可以提供涵盖所有案例的正则表达式?
编辑:请不要建议第三方图书馆。
答案 0 :(得分:9)
如果您真的需要防弹解决方案,请将字符串拆分为"\\."
并使用第一个
Character.isJavaIdentifierStart
和循环
Character.isJavaIdentifierPart
检查部件是否是有效的标识符名称。
编辑: #split(String)
将字符串解释为正则表达式,因此请确保将字符串拆分为"\\."
,而不仅仅是"."
,正如您所期望的那样最不惊讶的原则。多么糟糕的API设计,哎呀!这就是你用你的语言没有正则表达式文字所得到的......
答案 1 :(得分:3)
不同版本的Java平台支持不同的Unicode版本。
因此,不同版本将支持不同的有效标识符。
以下说明了BMP之外的标识符和Java保留字。
import java.util.Arrays;
public enum PackageName {
SIMPLE, QUALIFIED, INVALID;
public static final PackageName check(String name) {
PackageName ret = PackageName.INVALID;
int[] codePoint;
int index = 0, dotex = -1;
boolean needStart = true;
escape: {
if(name == null || name.isEmpty()) break escape;
if(name.codePointAt(0) == '.') break escape;
codePoint = name.codePoints().toArray();
while (index <= codePoint.length) {
if(index == codePoint.length) {
if(codePoint[index - 1] == '.'){
ret = PackageName.INVALID; break escape;}
int start = dotex + 1;
int end = index;
start = name.offsetByCodePoints(0, start);
end = name.offsetByCodePoints(0, end);
String test = name.substring(start, end);
if(!(Arrays.binarySearch(reserved, test) < 0)){
ret = PackageName.INVALID; break escape;}
if(!(ret == PackageName.QUALIFIED)) ret = PackageName.SIMPLE;
break escape;
}
if(codePoint[index] == '.') {
if(codePoint[index - 1] == '.'){
ret = PackageName.INVALID; break escape;}
else {
needStart = true;
int start = dotex + 1;
int end = index;
start = name.offsetByCodePoints(0, start);
end = name.offsetByCodePoints(0, end);
String test = name.substring(start, end);
if(!(Arrays.binarySearch(reserved, test) < 0)) break escape;
dotex = index;
ret = PackageName.QUALIFIED;
}
} else if(Character.isJavaIdentifierStart(codePoint[index])) {
if(needStart) needStart = false;
} else if((!Character.isJavaIdentifierPart(codePoint[index]))){
ret = PackageName.INVALID; break escape;
}
index++;
}
}
return ret;
}
private static final String[] reserved;
static {
reserved = new String[] { "abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const", "continue", "default", "do",
"double", "else", "enum", "extends", "false", "final", "finally",
"float", "for", "if", "goto", "implements", "import", "instanceof",
"int", "interface", "long", "native", "new", "null", "package",
"private", "protected", "public", "return", "short", "static",
"strictfp", "super", "switch", "synchronized", "this", "throw",
"throws", "transient", "true", "try", "void", "volatile", "while" };
}
}
答案 2 :(得分:2)
public static boolean isValidJavaIdentifier(String s)
{
// an empty or null string cannot be a valid identifier
if (s == null || s.length() == 0)
{
return false;
}
char[] c = s.toCharArray();
if (!Character.isJavaIdentifierStart(c[0]))
{
return false;
}
for (int i = 1; i < c.length; i++)
{
if (!Character.isJavaIdentifierPart(c[i]))
{
return false;
}
}
return true;
}
答案 3 :(得分:2)
您的问题有很好的解决方案:
public static boolean isFullyQualifiedClassname( String classname ) {
if (classname == null) return false;
String[] parts = classname.split("[\\.]");
if (parts.length == 0) return false;
for (String part : parts) {
CharacterIterator iter = new StringCharacterIterator(part);
// Check first character (there should at least be one character for each part) ...
char c = iter.first();
if (c == CharacterIterator.DONE) return false;
if (!Character.isJavaIdentifierStart(c) && !Character.isIdentifierIgnorable(c)) return false;
c = iter.next();
// Check the remaining characters, if there are any ...
while (c != CharacterIterator.DONE) {
if (!Character.isJavaIdentifierPart(c) && !Character.isIdentifierIgnorable(c)) return false;
c = iter.next();
}
}
return true;
}
答案 4 :(得分:2)
正确的方法是使用javax.lang.model.SourceVersion.isName方法(Java标准库的一部分)。
答案 5 :(得分:1)
你见过这个吗?
此方法isValidIdentifier
* 检查提供的名称是否是有效的java包名称部分或类标识符。
答案 6 :(得分:0)
使用正则表达式可以轻松实现防弹解决方案:
String NAME = "\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*";
String DOT = "\\.";
String DOTTED_NAME = NAME + "(?:" + DOT + NAME + ")*";
Pattern DOTTED_NAME_PATTERN = Pattern.compile(DOTTED_NAME);
boolean isJavaName(String name) {
Matcher matcher = DOTTED_NAME_PATTERN.matcher(name);
return matcher.matches();
}