Skip to content

Commit 66b10a6

Browse files
committed
Allow usage of API jar on Android platform
Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
1 parent 0b24e82 commit 66b10a6

File tree

1 file changed

+47
-26
lines changed

1 file changed

+47
-26
lines changed

api/src/main/java/jakarta/xml/bind/ModuleUtil.java

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -28,6 +28,21 @@ class ModuleUtil {
2828

2929
private static final Logger LOGGER = Logger.getLogger("jakarta.xml.bind");
3030

31+
//Android does not contain JPMS related methods added in SE 9+
32+
private static final boolean JPMS_SUPPORTED;
33+
34+
static {
35+
boolean b = false;
36+
try {
37+
JAXBContext.class.getModule();
38+
b = true;
39+
} catch (NoSuchMethodError nsme) {
40+
//android
41+
b = false;
42+
}
43+
JPMS_SUPPORTED = b;
44+
}
45+
3146
/**
3247
* Resolves classes from context path.
3348
* Only one class per package is needed to access its {@link java.lang.Module}
@@ -108,38 +123,44 @@ static Class<?> findFirstByJaxbIndex(String pkg, ClassLoader classLoader) throws
108123
* @throws JAXBException if ony of a classes package is not open to {@code jakarta.xml.bind} module.
109124
*/
110125
public static void delegateAddOpensToImplModule(Class<?>[] classes, Class<?> factorySPI) throws JAXBException {
111-
final Module implModule = factorySPI.getModule();
126+
if (JPMS_SUPPORTED) {
127+
final Module implModule = factorySPI.getModule();
112128

113-
Module jaxbModule = JAXBContext.class.getModule();
129+
Module jaxbModule = JAXBContext.class.getModule();
114130

115-
if (!jaxbModule.isNamed()) {
116-
//we are not on the module path, so assume class-path mode
117-
if (LOGGER.isLoggable(Level.FINE)) {
118-
LOGGER.log(Level.FINE, "Using jakarta.xml.bind-api on the class path.");
131+
if (!jaxbModule.isNamed()) {
132+
//we are not on the module path, so assume class-path mode
133+
if (LOGGER.isLoggable(Level.FINE)) {
134+
LOGGER.log(Level.FINE, "Using jakarta.xml.bind-api on the class path.");
135+
}
136+
return;
119137
}
120-
return;
121-
}
122138

123-
for (Class<?> cls : classes) {
124-
Class<?> jaxbClass = cls.isArray() ?
125-
cls.getComponentType() : cls;
139+
for (Class<?> cls : classes) {
140+
Class<?> jaxbClass = cls.isArray() ?
141+
cls.getComponentType() : cls;
126142

127-
final Module classModule = jaxbClass.getModule();
128-
final String packageName = jaxbClass.getPackageName();
129-
//no need for unnamed and java.base types
130-
if (!classModule.isNamed() || classModule.getName().equals("java.base")) {
131-
continue;
132-
}
133-
//report error if they are not open to jakarta.xml.bind
134-
if (!classModule.isOpen(packageName, jaxbModule)) {
135-
throw new JAXBException(Messages.format(Messages.JAXB_CLASSES_NOT_OPEN,
136-
packageName, jaxbClass.getName(), classModule.getName()));
143+
final Module classModule = jaxbClass.getModule();
144+
final String packageName = jaxbClass.getPackageName();
145+
//no need for unnamed and java.base types
146+
if (!classModule.isNamed() || classModule.getName().equals("java.base")) {
147+
continue;
148+
}
149+
//report error if they are not open to jakarta.xml.bind
150+
if (!classModule.isOpen(packageName, jaxbModule)) {
151+
throw new JAXBException(Messages.format(Messages.JAXB_CLASSES_NOT_OPEN,
152+
packageName, jaxbClass.getName(), classModule.getName()));
153+
}
154+
//propagate openness to impl module
155+
classModule.addOpens(packageName, implModule);
156+
if (LOGGER.isLoggable(Level.FINE)) {
157+
LOGGER.log(Level.FINE, "Propagating openness of package {0} in {1} to {2}.",
158+
new String[]{ packageName, classModule.getName(), implModule.getName() });
159+
}
137160
}
138-
//propagate openness to impl module
139-
classModule.addOpens(packageName, implModule);
161+
} else {
140162
if (LOGGER.isLoggable(Level.FINE)) {
141-
LOGGER.log(Level.FINE, "Propagating openness of package {0} in {1} to {2}.",
142-
new String[]{ packageName, classModule.getName(), implModule.getName() });
163+
LOGGER.log(Level.FINE, "Using jakarta.xml.bind-api with no JPMS related APIs, such as Class::getModule.");
143164
}
144165
}
145166
}

0 commit comments

Comments
 (0)