Various technical articles, IT-related tutorials, software information, and development journals
Tuesday, May 21, 2019
If a class mysteriously doesn't match, consider the loader
Recently I needed to load some Java class files from disk and see if they had certain annotations. I successfully got the classes loaded, but getAnnotation never found an annotation of my specified type. Confusingly, the full list provided by getAnnotations was of the expected size and the annotation class names were as expected, but the classes didn't compare equal to the target class. Eventually I realized the problem: the annotation class was loaded again from disk, and that copy was distinct from the one loaded into my application because they came from different classloaders. The solution was to prevent the duplicate load by providing a parent classloader (e.g. the current class's loader) to the URLClassLoader.
Labels:
java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment