|
1 | 1 | /*
|
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. |
3 | 3 | *
|
4 | 4 | * This program and the accompanying materials are made available under the
|
5 | 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at
|
@@ -28,6 +28,21 @@ class ModuleUtil {
|
28 | 28 |
|
29 | 29 | private static final Logger LOGGER = Logger.getLogger("jakarta.xml.bind");
|
30 | 30 |
|
| 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 | + |
31 | 46 | /**
|
32 | 47 | * Resolves classes from context path.
|
33 | 48 | * 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
|
108 | 123 | * @throws JAXBException if ony of a classes package is not open to {@code jakarta.xml.bind} module.
|
109 | 124 | */
|
110 | 125 | 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(); |
112 | 128 |
|
113 |
| - Module jaxbModule = JAXBContext.class.getModule(); |
| 129 | + Module jaxbModule = JAXBContext.class.getModule(); |
114 | 130 |
|
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; |
119 | 137 | }
|
120 |
| - return; |
121 |
| - } |
122 | 138 |
|
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; |
126 | 142 |
|
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 | + } |
137 | 160 | }
|
138 |
| - //propagate openness to impl module |
139 |
| - classModule.addOpens(packageName, implModule); |
| 161 | + } else { |
140 | 162 | 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."); |
143 | 164 | }
|
144 | 165 | }
|
145 | 166 | }
|
|
0 commit comments