當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。