当前位置: 首页>>代码示例>>Java>>正文


Java Instrumentation.addTransformer方法代码示例

本文整理汇总了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);
    }
}
 
开发者ID:baidu,项目名称:openrasp,代码行数:27,代码来源:Agent.java

示例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);
            }
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:TestLambdaFormRetransformation.java

示例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});
            }
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:RedefineClassTest.java

示例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);
        }
    }
}
 
开发者ID:ctripcorp,项目名称:cornerstone,代码行数:13,代码来源:AgentTool.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:RedefineLeak.java

示例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());
}
 
开发者ID:ivanyu,项目名称:java-agents-demo,代码行数:7,代码来源:MetricsCollectionJavassistAgent.java

示例7: premain

import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String args, Instrumentation instrumentation) throws ClassNotFoundException { // magic
    instrumentation.addTransformer(new TailRecClassFileTransformer());
}
 
开发者ID:ibessonov,项目名称:java-tailrec-agent,代码行数:4,代码来源:TailRecAgentPreMain.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:7,代码来源:Agent.java

示例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());
}
 
开发者ID:kslisenko,项目名称:java-performance,代码行数:5,代码来源:LoggingAgent.java

示例10: premain

import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String agentArgs, Instrumentation inst) {
    final SimpleClassTransformer transformer = new SimpleClassTransformer();
    inst.addTransformer(transformer);
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Programming-By-Example,代码行数:5,代码来源:SimpleAgent.java

示例11: premain

import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String agentArgs, Instrumentation inst) {
    inst.addTransformer(new DummyAgent(), false);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:4,代码来源:DummyAgent.java

示例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);
    }
}
 
开发者ID:yutian-tianpl,项目名称:byte-cobweb,代码行数:51,代码来源:TransformerMaker.java

示例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());
}
 
开发者ID:vitaly-chibrikov,项目名称:otus_java_2017_10,代码行数:7,代码来源:SimpleAgent.java

示例14: premain

import java.lang.instrument.Instrumentation; //导入方法依赖的package包/类
public static void premain(String args, Instrumentation inst)
{
    inst.addTransformer(new Transformer(args));
}
 
开发者ID:Zelldon,项目名称:zb-profiler,代码行数:5,代码来源:ProfilerMain.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:TransformerAgent.java


注:本文中的java.lang.instrument.Instrumentation.addTransformer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。