Does anybody knows if it’s possible (is there a library) to know if a Class<?> variable is included in the JRE or not ?
Here is what I would want :
Class<String> stringClass = String.class;
System.out.println(TheMagickLibrary.isJREClass(stringClass)); // should display true
Class<AnyClass> anotherClass = AnyClass.class;
System.out.println(TheMagickLibrary.isJREClass(anotherClass)); // should display false
I can offer you 2 solutions.
java.,sun.,com.sun.As you can see they say “some implmentations may return null”. It means that for these implementations
clazz.getClassLoader() == nullmeans that the class is loaded by bootstrap class loader and therefore belongs to JRE. BTW this works on my system (Java(TM) SE Runtime Environment (build 1.6.0_30-b12)).If not check the documentation of
ClassLoader#getParent():Again, some implementations will return null if current class loader is a bootstrap.
Finally I’d recommend the following strategy:
I believe this is good enough for 99% of cases.