|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.commons.lang3.builder; |
| 18 | + |
| 19 | +import java.lang.reflect.InvocationTargetException; |
| 20 | +import java.lang.reflect.Method; |
| 21 | + |
| 22 | +/** |
| 23 | + * Utility class to determine whether reflective access to a target module is permitted from the current module. |
| 24 | + * This class assumes all reflective accesses are allowed in JVM where JPMS is not supported (i.e. JDK8 or earlier). |
| 25 | + */ |
| 26 | +class ReflectiveAccessUtil { |
| 27 | + |
| 28 | + private static Method initializeGetModuleMethod() { |
| 29 | + try { |
| 30 | + return Class.class.getMethod("getModule"); |
| 31 | + } catch (NoSuchMethodException e) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private static Method initializeIsOpenMethod() { |
| 37 | + if (getModule == null) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + try { |
| 41 | + final Object module = getModule.invoke(ReflectiveAccessUtil.class); |
| 42 | + final Class<?> moduleClass = module.getClass(); |
| 43 | + return moduleClass.getMethod("isOpen", String.class, moduleClass); |
| 44 | + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static final Method getModule = initializeGetModuleMethod(); |
| 50 | + private static final Method isOpen = initializeIsOpenMethod(); |
| 51 | + |
| 52 | + /** |
| 53 | + * Determines whether {@code targetClass} is accessible via reflection from the module that defines this class. |
| 54 | + * On JVMs that do not support the Java Platform Module System, the method always returns {@code true}. |
| 55 | + * Otherwise, it checks whether the module that declares {@code targetClass} is open to the module containing |
| 56 | + * this class at runtime. |
| 57 | + * |
| 58 | + * @param targetClass the {@link Class} object to inspect. |
| 59 | + * @return {@code true} if {@code targetClass} can be reflected on from this module, |
| 60 | + * {@code false} otherwise. |
| 61 | + */ |
| 62 | + boolean isAllowed(Class<?> targetClass) { |
| 63 | + if (!isJpmsSupported()) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + if (targetClass.isArray()) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + try { |
| 70 | + final Object targetModule = getModule.invoke(targetClass); |
| 71 | + final Object selfModule = getModule.invoke(getClass()); |
| 72 | + return (Boolean) isOpen.invoke(targetModule, targetClass.getPackage().getName(), selfModule); |
| 73 | + } catch (IllegalAccessException | InvocationTargetException e) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns {@code }true} if the current JVM supports JPMS. |
| 80 | + */ |
| 81 | + boolean isJpmsSupported() { |
| 82 | + return getModule != null && isOpen != null; |
| 83 | + } |
| 84 | +} |
0 commit comments