本文整理汇总了Java中java.lang.instrument.Instrumentation.addTransformer方法的典型用法代码示例。如果您正苦于以下问题:Java Instrumentation.addTransformer方法的具体用法?Java Instrumentation.addTransformer怎么用?Java Instrumentation.addTransformer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.instrument.Instrumentation
的用法示例。
在下文中一共展示了Instrumentation.addTransformer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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});
}
}
}
}
示例4: startUp
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static synchronized void startUp(){
if(!isLoaded){
try {
loadAgent();
Instrumentation inst = instrumentation();
inst.addTransformer(new CodeTransformer(), true);
isLoaded = true;
}catch (Throwable e){
logger.warn("start agentTool failed",e);
}
}
}
示例5: 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();
}
示例6: premain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(final String agentArgs,
final Instrumentation inst) throws Exception {
System.out.printf("Starting %s\n",
MetricsCollectionJavassistAgent.class.getSimpleName());
inst.addTransformer(new MetricsCollectionTransformer());
}
示例7: premain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String args, Instrumentation instrumentation) throws ClassNotFoundException { // magic
instrumentation.addTransformer(new TailRecClassFileTransformer());
}
示例8: agentmain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception {
Agent transformer = new Agent();
instrumentation.addTransformer(transformer, true);
redefine(agentArgs, instrumentation, Test.class);
}
示例9: premain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String args, Instrumentation instr){
System.out.format("[%s] [logging agent]\n", Thread.currentThread().getName());
instr.addTransformer(new ClassLoadingLogger());
}
示例10: premain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String agentArgs, Instrumentation inst) {
final SimpleClassTransformer transformer = new SimpleClassTransformer();
inst.addTransformer(transformer);
}
示例11: premain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String agentArgs, Instrumentation inst) {
inst.addTransformer(new DummyAgent(), false);
}
示例12: make
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void make(final AgentArgs agentArgs, Instrumentation instrumentation) {
try {
if (!AgentArgs.valida(agentArgs)) {
throw new InterceptorException("JavaAgent参数设置错误");
}
ConnectorMatcher<Type> readBeforeMatcher;
switch (agentArgs.getNameMatcherRule()) {
case NAME_ALL:
readBeforeMatcher = TypeMatchers.any();
break;
case NAME_START_WITH:
readBeforeMatcher = TypeMatchers.nameStartsWith(agentArgs.getMatchRule());
break;
case NAME_END_WITH:
readBeforeMatcher = TypeMatchers.nameEndsWith(agentArgs.getMatchRule());
break;
case NAMED:
readBeforeMatcher = TypeMatchers.named(agentArgs.getMatchRule());
break;
case NAME_CONTAINS:
readBeforeMatcher = TypeMatchers.nameContains(agentArgs.getMatchRule());
break;
default:
throw new InterceptorException("JavaAgent必须设置正确的代理实现方式.");
}
ConnectorMatcher<ClassNode> readAfterMather = ClassMatchers.not(ClassMatchers.isInterface());
ConnectorMatcher<MethodNode> methodMather = MethodMatchers.not(MethodMatchers.isAbstract())
.and(MethodMatchers.not(MethodMatchers.isConstructor()))
.and(MethodMatchers.not(MethodMatchers.isTypeInitializer()))
.and(MethodMatchers.not(MethodMatchers.named("main")))
.and(MethodMatchers.withAnnotation(ForExample.class)
.or(MethodMatchers.withAnnotation(ForExample1.class)));
Interceptor interceptor = new ExampleInterceptor(
readBeforeMatcher,
readAfterMather,
methodMather);
String interceptorId = Interceptor.registerInterceptor(interceptor);
instrumentation.addTransformer((loader, className, classBeingRedefined, protectionDomain, classfileBuffer) -> {
if (!isAncestor(TransformerMaker.class.getClassLoader(), loader) && !isTarget(readBeforeMatcher,className)) {
return classfileBuffer;
}
return AccessController.doPrivileged((PrivilegedAction<byte[]>) () -> ClassModifier.modify(className, classfileBuffer, interceptorId));
});
} catch (Throwable th) {
th.printStackTrace(System.err);
}
}
示例13: premain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(final String agentArgs, final Instrumentation inst) {
System.out.println("premain...");
instrumentation = inst;
inst.addTransformer(new SleepTransformer());
}
示例14: premain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String args, Instrumentation inst)
{
inst.addTransformer(new Transformer(args));
}
示例15: premain
import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String agentArguments, Instrumentation instrumentation) {
log("premain() is called, arguments = " + agentArguments);
classesToTransform = agentArguments.split(",");
instrumentation.addTransformer(new SimpleTransformer(), /*canRetransform=*/true);
}