如何检查类名是否有效? [英] How to check if the class name is valid?

查看:106
本文介绍了如何检查类名是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中有一个方法来检查一个字符串是否可以用作类名?

Is there a method in Java to check if a string can be used as a class name?

推荐答案

SourceVersion.isName 可用于检查完全限定名。

SourceVersion.isName can be used to check fully qualified names.

如果没有可以通过这种方式进行检查:

If no .s should be allowed, the check can be done this way:

boolean isValidName (String className) {
    return SourceVersion.isIdentifier(className) && !SourceVersion.isKeyword(className);
}

这篇关于如何检查类名是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆