Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a class belongs to Java JDK

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?

like image 911
yves amsellem Avatar asked Jan 02 '12 17:01

yves amsellem


2 Answers

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 loaded java.lang.String -- i.e.,

if (theObject.getClass().getClassLoader() == "".getClass().getClassLoader()) ...

In general, a different ClassLoader will be used for system classes vs. application classes.

like image 182
Ernest Friedman-Hill Avatar answered Oct 13 '22 20:10

Ernest Friedman-Hill


It is probably OK, just you have to check the following packages:

java
javax
com.sun
sun

probably others...

like image 36
AlexR Avatar answered Oct 13 '22 18:10

AlexR