如何检查一个类是否属于 Java JDK
我使用一个返回一些 List
的外部库。 我需要检查此列表中的每个对象是否是 JDK 的对象(String、int、Integer...)。 这是一个正确的解决方案吗?
List<?> list = externalLibrary.search(...);
for(clazz : list) {
if (clazz.getPackage().getName().startsWith("java.lang"))
// do something different
}
还有更好的吗?
I use an external library which return some List<?>
.
I need to check if each object of this list is an Object of the JDK (String, int, Integer...).
Is this a proper solution?
List<?> list = externalLibrary.search(...);
for(clazz : list) {
if (clazz.getPackage().getName().startsWith("java.lang"))
// do something different
}
Is there a better one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据您对“JDK 对象”的定义(它的边缘可能会变得相当模糊),不,这是行不通的。
java.lang
包只是 JDK 中包含的所有类的一小部分。您可以检查每个对象是否由加载 java.lang.String 的同一个 ClassLoader 加载 - 即,
通常是不同的 ClassLoader将用于系统类与应用程序类。
Depending on your definition of "object of the JDK" -- which could get quite fuzzy around the edges -- no, this isn't going to do it. The
java.lang
package is only a tiny part of all the classes included in the JDK.You might check whether each object was loaded by the same
ClassLoader
that loadedjava.lang.String
-- i.e.,In general, a different
ClassLoader
will be used for system classes vs. application classes.可能没问题,只是您必须检查以下软件包:
可能是其他软件包......
It is probably OK, just you have to check the following packages:
probably others...
我个人喜欢类加载器基本答案。但在 StringBuilder 上它也会返回 true。如果您想要更狭窄的定义,仅是“内置”类型,您可以尝试评估这是原始类型(例如 int)还是包装类型(例如 Integer)或 String。您可以编写如下内容:
您还可以选择添加对 java.math.BigDecimal 、 java.util.Date 、 java.sql 等类的支持。时间戳。但请注意,它们不是最终的,因此我假设如果有人即使以微不足道的方式扩展它们,它也不会被视为 JDK 类。
Personally I like class loader base answer. But it will return true also on StringBuilder. If you want to more narrow definition that is only "built-in" types, you can try to evaluate whether this is primitive type (such as int) or wrapper type (such as Integer) or String. You can write something like this:
You can also optionally add support for such classes like
java.math.BigDecimal
,java.util.Date
,java.sql.Timestamp
. Note, however, that they are not final, so I assumed that if somebody extended them even in the trivial way, it will not be considered as JDK class.我们使用下面的类来检查这些类是否属于 JDK
We use the below class to check if the classes belongs to JDK
您可以使用 ClassLoader.getSystemResources ,然后检查从哪个 jar 加载的类(如果来自 rt.jar 则为 fg )。
您将获得如下 URL:
取自 SLF4j 的示例代码:
You can use
ClassLoader.getSystemResources
and then check from what jar is the class loaded (f.g. if it comes from rt.jar).You will get URL's such as:
Example code taken from SLF4j:
我认为更简单的解决方案是这样解决问题:
编写一个方法来识别您定义的所有类。在大多数情况下,所有用户定义的类都遵循 com.something.something 之类的模式。那么如果它们不属于com.something.something,那么它就是一个JDK类
I think an easier solution is to thing of the problem this way:
write a method to identify all classes that are defined by you. In most cases, all user defined classes follow a pattern like com.something.something. Then if they do not belong to com.something.something, it is a JDK class