|
| 1 | +/* |
| 2 | + * Copyright 2015 the original author or authors. |
| 3 | + * @https://github.com/scouter-project/scouter |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://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 | + |
| 18 | +package scouter.agent.util; |
| 19 | + |
| 20 | +import java.lang.instrument.Instrumentation; |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | +import scouter.agent.JavaAgent; |
| 27 | +import scouter.agent.Logger; |
| 28 | + |
| 29 | +/** |
| 30 | + * refer to glowroot (https://github.com/glowroot/glowroot) |
| 31 | + */ |
| 32 | +public class ModuleUtil { |
| 33 | + |
| 34 | + private static final ModuleUtil instance; |
| 35 | + |
| 36 | + static { |
| 37 | + if (JavaAgent.isJava9plus()) { |
| 38 | + instance = new ModuleUtil(); |
| 39 | + } else { |
| 40 | + instance = null; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private final Method getModuleMethod; |
| 45 | + private final Class<?> moduleClass; |
| 46 | + private final Method redefineModuleMethod; |
| 47 | + |
| 48 | + private ModuleUtil() { |
| 49 | + try { |
| 50 | + getModuleMethod = Class.class.getMethod("getModule"); |
| 51 | + moduleClass = Class.forName("java.lang.Module"); |
| 52 | + redefineModuleMethod = Instrumentation.class.getMethod("redefineModule", |
| 53 | + moduleClass, Set.class, Map.class, Map.class, Set.class, Map.class); |
| 54 | + } catch (Exception e) { |
| 55 | + throw new IllegalStateException(e); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static Object getModule(Class<?> clazz) throws Exception { |
| 60 | + return instance.getModuleInternal(clazz); |
| 61 | + } |
| 62 | + |
| 63 | + public static void grantAccess(Instrumentation instrumentation, String fromClassName, |
| 64 | + String toClassName) throws Exception { |
| 65 | + instance.grantAccessInternal(instrumentation, fromClassName, null, toClassName, null); |
| 66 | + } |
| 67 | + |
| 68 | + public static void grantAccess(Instrumentation instrumentation, String fromClassName, |
| 69 | + ClassLoader fromClassLoader, |
| 70 | + String toClassName, ClassLoader toClassLoader) throws Exception { |
| 71 | + instance.grantAccessInternal(instrumentation, fromClassName, fromClassLoader, toClassName, |
| 72 | + toClassLoader); |
| 73 | + } |
| 74 | + |
| 75 | + private Object getModuleInternal(Class<?> clazz) throws Exception { |
| 76 | + // getModule() always returns non-null |
| 77 | + return getModuleMethod.invoke(clazz); |
| 78 | + } |
| 79 | + |
| 80 | + private void grantAccessInternal(Instrumentation instrumentation, String fromClassName, |
| 81 | + ClassLoader fromClassLoader, |
| 82 | + String toClassName, ClassLoader toClassLoader) throws Exception { |
| 83 | + Class<?> fromClass; |
| 84 | + try { |
| 85 | + if (fromClassLoader == null) { |
| 86 | + fromClass = Class.forName(fromClassName); |
| 87 | + } else { |
| 88 | + fromClass = Class.forName(fromClassName, true, fromClassLoader); |
| 89 | + } |
| 90 | + } catch (ClassNotFoundException e) { |
| 91 | + Logger.println("MODULEUTIL-1", e.getMessage() + " : " + fromClassName, e); |
| 92 | + return; |
| 93 | + } |
| 94 | + Class<?> toClass; |
| 95 | + try { |
| 96 | + if (toClassLoader == null) { |
| 97 | + toClass = Class.forName(toClassName); |
| 98 | + } else { |
| 99 | + toClass = Class.forName(toClassName, true, toClassLoader); |
| 100 | + } |
| 101 | + } catch (ClassNotFoundException e) { |
| 102 | + Logger.println("MODULEUTIL-2", e.getMessage() + " : " + toClassName, e); |
| 103 | + return; |
| 104 | + } |
| 105 | + Map<String, Set<?>> extraOpens = new HashMap<>(); |
| 106 | + Package pkg = toClass.getPackage(); |
| 107 | + if (pkg != null) { |
| 108 | + Set openSet = new HashSet(); |
| 109 | + openSet.add(getModuleMethod.invoke(fromClass)); |
| 110 | + extraOpens.put(pkg.getName(), openSet); |
| 111 | + } |
| 112 | + // getModule() always returns non-null |
| 113 | + redefineModuleMethod.invoke(instrumentation, getModuleMethod.invoke(toClass), |
| 114 | + new HashSet<>(), new HashMap<>(), extraOpens, new HashSet<>(), new HashMap<>()); |
| 115 | + Logger.println("extra opens module. from = " + fromClassName + " to = " + toClassName); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments