Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow usage of API jar on Android platform #273

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 47 additions & 26 deletions api/src/main/java/jakarta/xml/bind/ModuleUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand Down Expand Up @@ -28,6 +28,21 @@ class ModuleUtil {

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

//Android does not contain JPMS related methods added in SE 9+
private static final boolean JPMS_SUPPORTED;

static {
boolean b = false;
try {
JAXBContext.class.getModule();
b = true;
} catch (NoSuchMethodError nsme) {
//android
b = false;
}
JPMS_SUPPORTED = b;
}

/**
* Resolves classes from context path.
* Only one class per package is needed to access its {@link java.lang.Module}
Expand Down Expand Up @@ -108,38 +123,44 @@ static Class<?> findFirstByJaxbIndex(String pkg, ClassLoader classLoader) throws
* @throws JAXBException if ony of a classes package is not open to {@code jakarta.xml.bind} module.
*/
public static void delegateAddOpensToImplModule(Class<?>[] classes, Class<?> factorySPI) throws JAXBException {
final Module implModule = factorySPI.getModule();
if (JPMS_SUPPORTED) {
final Module implModule = factorySPI.getModule();

Module jaxbModule = JAXBContext.class.getModule();
Module jaxbModule = JAXBContext.class.getModule();

if (!jaxbModule.isNamed()) {
//we are not on the module path, so assume class-path mode
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Using jakarta.xml.bind-api on the class path.");
if (!jaxbModule.isNamed()) {
//we are not on the module path, so assume class-path mode
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Using jakarta.xml.bind-api on the class path.");
}
return;
}
return;
}

for (Class<?> cls : classes) {
Class<?> jaxbClass = cls.isArray() ?
cls.getComponentType() : cls;
for (Class<?> cls : classes) {
Class<?> jaxbClass = cls.isArray() ?
cls.getComponentType() : cls;

final Module classModule = jaxbClass.getModule();
final String packageName = jaxbClass.getPackageName();
//no need for unnamed and java.base types
if (!classModule.isNamed() || classModule.getName().equals("java.base")) {
continue;
}
//report error if they are not open to jakarta.xml.bind
if (!classModule.isOpen(packageName, jaxbModule)) {
throw new JAXBException(Messages.format(Messages.JAXB_CLASSES_NOT_OPEN,
packageName, jaxbClass.getName(), classModule.getName()));
final Module classModule = jaxbClass.getModule();
final String packageName = jaxbClass.getPackageName();
//no need for unnamed and java.base types
if (!classModule.isNamed() || classModule.getName().equals("java.base")) {
continue;
}
//report error if they are not open to jakarta.xml.bind
if (!classModule.isOpen(packageName, jaxbModule)) {
throw new JAXBException(Messages.format(Messages.JAXB_CLASSES_NOT_OPEN,
packageName, jaxbClass.getName(), classModule.getName()));
}
//propagate openness to impl module
classModule.addOpens(packageName, implModule);
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Propagating openness of package {0} in {1} to {2}.",
new String[]{ packageName, classModule.getName(), implModule.getName() });
}
}
//propagate openness to impl module
classModule.addOpens(packageName, implModule);
} else {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Propagating openness of package {0} in {1} to {2}.",
new String[]{ packageName, classModule.getName(), implModule.getName() });
LOGGER.log(Level.FINE, "Using jakarta.xml.bind-api with no JPMS related APIs, such as Class::getModule.");
}
}
}
Expand Down