本文整理匯總了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});
}
}
}
}