From 66b10a69d37bbf02958735e46535f6a643f8c514 Mon Sep 17 00:00:00 2001 From: Lukas Jungmann Date: Sat, 15 Jul 2023 09:35:19 +0200 Subject: [PATCH] Allow usage of API jar on Android platform Signed-off-by: Lukas Jungmann --- .../java/jakarta/xml/bind/ModuleUtil.java | 73 ++++++++++++------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/api/src/main/java/jakarta/xml/bind/ModuleUtil.java b/api/src/main/java/jakarta/xml/bind/ModuleUtil.java index 8644464..c9b8842 100644 --- a/api/src/main/java/jakarta/xml/bind/ModuleUtil.java +++ b/api/src/main/java/jakarta/xml/bind/ModuleUtil.java @@ -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 @@ -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} @@ -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."); } } }