本文整理汇总了Java中java.lang.instrument.Instrumentation.retransformClasses方法的典型用法代码示例。如果您正苦于以下问题:Java Instrumentation.retransformClasses方法的具体用法?Java Instrumentation.retransformClasses怎么用?Java Instrumentation.retransformClasses使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.instrument.Instrumentation
的用法示例。
在下文中一共展示了Instrumentation.retransformClasses方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initTransformer
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
/**
* 初始化类字节码的转换器
*
* @param inst 用于管理字节码转换器
*/
private static void initTransformer(Instrumentation inst) throws UnmodifiableClassException {
LinkedList<Class> retransformClasses = new LinkedList<Class>();
CustomClassTransformer customClassTransformer = new CustomClassTransformer();
inst.addTransformer(customClassTransformer, true);
Class[] loadedClasses = inst.getAllLoadedClasses();
for (Class clazz : loadedClasses) {
for (final AbstractClassHook hook : customClassTransformer.getHooks()) {
if (hook.isClassMatched(clazz.getName().replace(".", "/"))) {
if (inst.isModifiableClass(clazz) && !clazz.getName().startsWith("java.lang.invoke.LambdaForm")) {
retransformClasses.add(clazz);
}
}
}
}
// hook已经加载的类
Class[] classes = new Class[retransformClasses.size()];
retransformClasses.toArray(classes);
if (classes.length > 0) {
inst.retransformClasses(classes);
}
}
示例2: premain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String args, Instrumentation instrumentation) {
if (!instrumentation.isRetransformClassesSupported()) {
System.out.println("Class retransformation is not supported.");
return;
}
System.out.println("Calling lambda to ensure that lambda forms were created");
Agent.lambda.run();
System.out.println("Registering class file transformer");
instrumentation.addTransformer(new Agent());
for (Class c : instrumentation.getAllLoadedClasses()) {
if (c.getName().contains("LambdaForm") &&
instrumentation.isModifiableClass(c)) {
System.out.format("We've found a modifiable lambda form: %s%n", c.getName());
try {
instrumentation.retransformClasses(c);
} catch (UnmodifiableClassException e) {
throw new AssertionError("Modification of modifiable class " +
"caused UnmodifiableClassException", e);
}
}
}
}
示例3: reloadClass
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
private static void reloadClass(Class seleClass) throws Exception {
if (!isLoaded) {
startUp();
}
Instrumentation inst = instrumentation();
inst.retransformClasses(seleClass);
}
示例4: removeMetricsClass
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public synchronized static void removeMetricsClass(String name) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, UnmodifiableClassException {
Class seleClass = Class.forName(name.replace('/', '.'));
needRecoverClasses.put(name, needMetricsClasses.remove(name));
Instrumentation inst = instrumentation();
inst.retransformClasses(seleClass);
MetricsCollector.getCollector().removeClassMetrics(name);
}
示例5: removeDebugClassByTraceId
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public synchronized static Map<String, Object> removeDebugClassByTraceId(String traceId,boolean isForce) throws NotFoundException {
String className = null;
logger.debug("ready remove "+traceId);
Map<String, Object> rtn = DebugTool.removeTraceInfo(traceId);
for (Map.Entry<String, DebugInfo> entry : debugClassInfos.entrySet()) {
if (traceId.equals(entry.getValue().getTraceId())) {
className = entry.getKey();
break;
}
}
if (className == null) {
throw new NotFoundException("no debug class found by traceid "+traceId);
}
if(rtn != null || isForce) {
try {
Class seleClass = Class.forName(className.replace('/', '.'));
debugClassInfos.remove(className);
needRecoverClasses.put(className, needDebugClasses.remove(className));
Instrumentation inst = instrumentation();
inst.retransformClasses(seleClass);
} catch (Throwable e) {
logger.error("recover debug class failed!", e);
}
}
return rtn;
}
示例6: rewriteIfPossible
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
@Override
public void rewriteIfPossible(Class<?> declaringClass) throws IllegalStateException {
Instrumentation instrumentation = checkInstrumentation();
/*
byte[] bytecode;
ClassLoader loader = declaringClass.getClassLoader();
try(InputStream input = loader.getResourceAsStream(declaringClass.getName().replace('.', '/') + ".class")) {
if (input == null) {
throw new IllegalStateException("no input");
}
Function<String, Optional<InputStream>> classFileFinder = internalName -> Optional.ofNullable(loader.getResourceAsStream(internalName + ".class"));
bytecode = Rewriter.rewrite(input, classFileFinder);
} catch (IOException e) {
throw new IllegalStateException(e);
}
try {
instrumentation.redefineClasses(new ClassDefinition(declaringClass, bytecode));
} catch (ClassNotFoundException | UnmodifiableClassException e) {
throw new IllegalStateException(e);
}*/
try {
instrumentation.retransformClasses(declaringClass);
} catch (UnmodifiableClassException e) {
throw new IllegalStateException(e);
}
}
示例7: agentmain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception {
if (inst.isRedefineClassesSupported() && inst.isRetransformClassesSupported()) {
inst.addTransformer(new Redefiner(), true);
Class<?>[] allClasses = inst.getAllLoadedClasses();
for (int i = 0; i < allClasses.length; i++) {
Class<?> c = allClasses[i];
if (c == Intrinsic.class) {
inst.retransformClasses(new Class<?>[]{c});
}
}
}
}
示例8: premain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String agentArgs, Instrumentation inst) throws Exception {
LoggingTransformer t = new LoggingTransformer();
inst.addTransformer(t, true);
{
Class demoClass = Class.forName("RedefineLeak$Tester");
for (int i = 0; i < 10000; i++) {
inst.retransformClasses(demoClass);
}
}
System.gc();
}
示例9: redefine
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) {
try {
instrumentation.retransformClasses(to_redefine);
} catch (Exception e) {
e.printStackTrace();
}
}
示例10: agentmain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception {
if (inst.isRedefineClassesSupported() && inst.isRetransformClassesSupported()) {
inst.addTransformer(new FooTransformer(), true);
Class<?>[] allClasses = inst.getAllLoadedClasses();
for (int i = 0; i < allClasses.length; i++) {
Class<?> c = allClasses[i];
if (c == Foo.class) {
inst.retransformClasses(new Class<?>[]{c});
}
}
}
}