|
| 1 | +/* |
| 2 | + * Copyright 2016-2022 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.java; |
| 17 | + |
| 18 | +import java.lang.invoke.MethodHandle; |
| 19 | +import java.lang.invoke.MethodHandles; |
| 20 | +import java.lang.reflect.Field; |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.lang.reflect.Modifier; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import javax.annotation.Nullable; |
| 30 | + |
| 31 | +import com.diffplug.spotless.Jvm; |
| 32 | + |
| 33 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 34 | +import sun.misc.Unsafe; |
| 35 | + |
| 36 | +final class ModuleHelper { |
| 37 | + // prevent direct instantiation |
| 38 | + private ModuleHelper() {} |
| 39 | + |
| 40 | + private static final Map<String, String> REQUIRED_PACKAGES_TO_TEST_CLASSES = new HashMap<>(); |
| 41 | + |
| 42 | + static { |
| 43 | + REQUIRED_PACKAGES_TO_TEST_CLASSES.putIfAbsent("com.sun.tools.javac.util", "Context"); |
| 44 | + REQUIRED_PACKAGES_TO_TEST_CLASSES.putIfAbsent("com.sun.tools.javac.file", "CacheFSInfo"); |
| 45 | + REQUIRED_PACKAGES_TO_TEST_CLASSES.putIfAbsent("com.sun.tools.javac.tree", "TreeTranslator"); |
| 46 | + REQUIRED_PACKAGES_TO_TEST_CLASSES.putIfAbsent("com.sun.tools.javac.parser", "Tokens$TokenKind"); |
| 47 | + REQUIRED_PACKAGES_TO_TEST_CLASSES.putIfAbsent("com.sun.tools.javac.api", "DiagnosticFormatter$PositionKind"); |
| 48 | + } |
| 49 | + |
| 50 | + private static boolean checkDone = false; |
| 51 | + |
| 52 | + public static synchronized void doOpenInternalPackagesIfRequired() { |
| 53 | + if (Jvm.version() < 16 || checkDone) { |
| 54 | + return; |
| 55 | + } |
| 56 | + try { |
| 57 | + checkDone = true; |
| 58 | + final List<String> unavailableRequiredPackages = unavailableRequiredPackages(); |
| 59 | + if (!unavailableRequiredPackages.isEmpty()) { |
| 60 | + openPackages(unavailableRequiredPackages); |
| 61 | + final List<String> failedToOpen = unavailableRequiredPackages(); |
| 62 | + if (!failedToOpen.isEmpty()) { |
| 63 | + final StringBuilder message = new StringBuilder(); |
| 64 | + message.append("WARNING: Some required internal classes are unavailable. Please consider adding the following JVM arguments\n"); |
| 65 | + message.append("WARNING: "); |
| 66 | + for (String name : failedToOpen) { |
| 67 | + message.append(String.format("--add-opens jdk.compiler/%s=ALL-UNNAMED", name)); |
| 68 | + } |
| 69 | + System.err.println(message); |
| 70 | + } |
| 71 | + } |
| 72 | + } catch (Throwable e) { |
| 73 | + System.err.println("WARNING: Failed to check for unavailable JDK packages. Reason: " + e.getMessage()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @SuppressFBWarnings("REC_CATCH_EXCEPTION") // workaround JDK11 |
| 78 | + private static List<String> unavailableRequiredPackages() { |
| 79 | + final List<String> packages = new ArrayList<>(); |
| 80 | + for (Map.Entry<String, String> e : REQUIRED_PACKAGES_TO_TEST_CLASSES.entrySet()) { |
| 81 | + final String key = e.getKey(); |
| 82 | + final String value = e.getValue(); |
| 83 | + try { |
| 84 | + final Class<?> clazz = Class.forName(key + "." + value); |
| 85 | + if (clazz.isEnum()) { |
| 86 | + clazz.getMethod("values").invoke(null); |
| 87 | + } else { |
| 88 | + clazz.getDeclaredConstructor().newInstance(); |
| 89 | + } |
| 90 | + } catch (IllegalAccessException ex) { |
| 91 | + packages.add(key); |
| 92 | + } catch (Exception ignore) { |
| 93 | + // in old versions of JDK some classes could be unavailable |
| 94 | + } |
| 95 | + } |
| 96 | + return packages; |
| 97 | + } |
| 98 | + |
| 99 | + @SuppressWarnings("unchecked") |
| 100 | + private static void openPackages(Collection<String> packagesToOpen) throws Throwable { |
| 101 | + final Collection<?> modules = allModules(); |
| 102 | + if (modules == null) { |
| 103 | + return; |
| 104 | + } |
| 105 | + final Unsafe unsafe = Unsafe.getUnsafe(); |
| 106 | + final Field implLookupField = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP"); |
| 107 | + final MethodHandles.Lookup lookup = (MethodHandles.Lookup) unsafe.getObject( |
| 108 | + unsafe.staticFieldBase(implLookupField), |
| 109 | + unsafe.staticFieldOffset(implLookupField)); |
| 110 | + final MethodHandle modifiers = lookup.findSetter(Method.class, "modifiers", Integer.TYPE); |
| 111 | + final Method exportMethod = Class.forName("java.lang.Module").getDeclaredMethod("implAddOpens", String.class); |
| 112 | + modifiers.invokeExact(exportMethod, Modifier.PUBLIC); |
| 113 | + for (Object module : modules) { |
| 114 | + final Collection<String> packages = (Collection<String>) module.getClass().getMethod("getPackages").invoke(module); |
| 115 | + for (String name : packages) { |
| 116 | + if (packagesToOpen.contains(name)) { |
| 117 | + exportMethod.invoke(module, name); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Nullable |
| 124 | + @SuppressFBWarnings("REC_CATCH_EXCEPTION") // workaround JDK11 |
| 125 | + private static Collection<?> allModules() { |
| 126 | + // calling ModuleLayer.boot().modules() by reflection |
| 127 | + try { |
| 128 | + final Object boot = Class.forName("java.lang.ModuleLayer").getMethod("boot").invoke(null); |
| 129 | + if (boot == null) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + final Object modules = boot.getClass().getMethod("modules").invoke(boot); |
| 133 | + return (Collection<?>) modules; |
| 134 | + } catch (Exception ignore) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments