-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #867 from scouter-project/feature/866-exception-on…
…-java17 Feature/866 exception on java17
- Loading branch information
Showing
4 changed files
with
171 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
scouter.agent.java/src/main/java/scouter/agent/util/ModuleUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* @https://github.com/scouter-project/scouter | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package scouter.agent.util; | ||
|
||
import java.lang.instrument.Instrumentation; | ||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import scouter.agent.JavaAgent; | ||
import scouter.agent.Logger; | ||
|
||
/** | ||
* refer to glowroot (https://github.com/glowroot/glowroot) | ||
*/ | ||
public class ModuleUtil { | ||
|
||
private static final ModuleUtil instance; | ||
|
||
static { | ||
if (JavaAgent.isJava9plus()) { | ||
instance = new ModuleUtil(); | ||
} else { | ||
instance = null; | ||
} | ||
} | ||
|
||
private final Method getModuleMethod; | ||
private final Class<?> moduleClass; | ||
private final Method redefineModuleMethod; | ||
|
||
private ModuleUtil() { | ||
try { | ||
getModuleMethod = Class.class.getMethod("getModule"); | ||
moduleClass = Class.forName("java.lang.Module"); | ||
redefineModuleMethod = Instrumentation.class.getMethod("redefineModule", | ||
moduleClass, Set.class, Map.class, Map.class, Set.class, Map.class); | ||
} catch (Exception e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
public static Object getModule(Class<?> clazz) throws Exception { | ||
return instance.getModuleInternal(clazz); | ||
} | ||
|
||
public static void grantAccess(Instrumentation instrumentation, String fromClassName, | ||
String toClassName) throws Exception { | ||
instance.grantAccessInternal(instrumentation, fromClassName, null, toClassName, null); | ||
} | ||
|
||
public static void grantAccess(Instrumentation instrumentation, String fromClassName, | ||
ClassLoader fromClassLoader, | ||
String toClassName, ClassLoader toClassLoader) throws Exception { | ||
instance.grantAccessInternal(instrumentation, fromClassName, fromClassLoader, toClassName, | ||
toClassLoader); | ||
} | ||
|
||
private Object getModuleInternal(Class<?> clazz) throws Exception { | ||
// getModule() always returns non-null | ||
return getModuleMethod.invoke(clazz); | ||
} | ||
|
||
private void grantAccessInternal(Instrumentation instrumentation, String fromClassName, | ||
ClassLoader fromClassLoader, | ||
String toClassName, ClassLoader toClassLoader) throws Exception { | ||
Class<?> fromClass; | ||
try { | ||
if (fromClassLoader == null) { | ||
fromClass = Class.forName(fromClassName); | ||
} else { | ||
fromClass = Class.forName(fromClassName, true, fromClassLoader); | ||
} | ||
} catch (ClassNotFoundException e) { | ||
Logger.println("MODULEUTIL-1", e.getMessage() + " : " + fromClassName, e); | ||
return; | ||
} | ||
Class<?> toClass; | ||
try { | ||
if (toClassLoader == null) { | ||
toClass = Class.forName(toClassName); | ||
} else { | ||
toClass = Class.forName(toClassName, true, toClassLoader); | ||
} | ||
} catch (ClassNotFoundException e) { | ||
Logger.println("MODULEUTIL-2", e.getMessage() + " : " + toClassName, e); | ||
return; | ||
} | ||
Map<String, Set<?>> extraOpens = new HashMap<>(); | ||
Package pkg = toClass.getPackage(); | ||
if (pkg != null) { | ||
Set openSet = new HashSet(); | ||
openSet.add(getModuleMethod.invoke(fromClass)); | ||
extraOpens.put(pkg.getName(), openSet); | ||
} | ||
// getModule() always returns non-null | ||
redefineModuleMethod.invoke(instrumentation, getModuleMethod.invoke(toClass), | ||
new HashSet<>(), new HashMap<>(), extraOpens, new HashSet<>(), new HashMap<>()); | ||
Logger.println("extra opens module. from = " + fromClassName + " to = " + toClassName); | ||
} | ||
|
||
} |