jakartaee / jakartaee/jaxb-api
NPE in jakarta/xml/bind/ServiceLoaderUtil.java method lookupUsingOSGiServiceLoader
- Dominant language
- Java
- Stars
- 79
- Forks
- 48
- Avg merge
- 7h 12m
- Merged PRs (30d)
- 2
Description
The ServiceLoaderUtil#lookupUsingOSGiServiceLoader is defined as follows:
```
static T lookupUsingOSGiServiceLoader(String factoryId, Logger logger) {
try {
// Use reflection to avoid having any dependency on ServiceLoader class
@SuppressWarnings("unchecked")
Class serviceClass = (Class) Class.forName(factoryId);
Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
Method m = target.getMethod(OSGI_SERVICE_LOADER_METHOD_NAME, Class.class);
@SuppressWarnings("unchecked")
Iterator iter = ((Iterable) m.invoke(null, serviceClass)).iterator();
...
} catch (...) {}
}
```
it's not guaranteed that
`m.invoke(null, serviceClass))`, that `.iterator()` is executed on, will return non null value.
In case of the default
`org.glassfish.hk2.osgiresourcelocator.ServiceLoader#lookupProviderClasses`: https://github.com/eclipse-ee4j/glassfish-hk2-extra/blob/master/osgi-resource-locator/src/main/java/org/glassfish/hk2/osgiresourcelocator/ServiceLoader.java#L107
```
public static Iterable lookupProviderClasses(Class serviceClass) {
if (_me == null) return null;
return _me.lookupProviderClasses1(serviceClass);
}
```
and `_me` can be null in cases where the jar is not properly initialized which can lead to NPE
reference: https://github.com/jakartaee/jaxb-api/blob/main/api/src/main/java/jakarta/xml/bind/ServiceLoaderUtil.java#L61
Contributor guide
Research direction
Start in api/src/main/java/jakarta/xml/bind/ServiceLoaderUtil.java at lookupUsingOSGiServiceLoader and inspect the reflective call to OSGI_SERVICE_LOADER_METHOD_NAME. Compare its behavior with hk2-extra's ServiceLoader.lookupProviderClasses, especially when _me is null, then verify that an uninitialized service loader no longer causes an NPE.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100