当前位置:   article > 正文

java agent重新定义类 redefineClasses_javaagent redfine

javaagent redfine

instrumentation.redefineClasses 可以重新定义类文件

  • 但是对于已经正在使用的类不起作用,对于新加载的类可以生效。如果要对当前使用的类生效可以使用retransformClasses。详细说明可以查看jdk注释
  • redefineClasses 会触发 transform操作

import java.lang.instrument.*;
import java.lang.reflect.Method;
import java.security.ProtectionDomain;
import java.util.*;


public class TestTransformer implements ClassFileTransformer, Runnable {


    private static Map<String, List<TransformerLoad>> enhanceModels = new HashMap<>();

    private static List<String> list = new ArrayList();

    private Instrumentation instrumentation = null;

    static {
        //后面可以改成spi扩展的方式

        list.add("org/apache/logging/log4j/core/layout/PatternLayout$Builder");
        list.add("ch/qos/logback/core/pattern/PatternLayoutBase");
        list.add("ch/qos/logback/classic/PatternLayout");
        list.add("sun/net/www/http/HttpClient");
    }

    public TestTransformer(Instrumentation instrumentation) {
        this.instrumentation = instrumentation;
    }


    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
        if (list.contains(className)) {
            try{
                List<TransformerLoad> transformerLoadList = null;
                String key = "-";
                if (loader != null){
                    key = loader.toString();
                }
                if (! enhanceModels.containsKey(key)){
                    transformerLoadList = new ArrayList<>();
                    enhanceModels.put(key, transformerLoadList);
                }else {
                    transformerLoadList = enhanceModels.get(key);
                }

                TransformerLoad transformerLoad = new TransformerLoad();
                transformerLoad.setClassLoader(loader);
                transformerLoad.setClassBeingRedefined(classBeingRedefined);
                transformerLoad.setClassfileBuffer(classfileBuffer);
                transformerLoad.setClassName(className);
                transformerLoad.setProtectionDomain(protectionDomain);
                transformerLoadList.add(transformerLoad);
            } catch (Throwable e){
                e.printStackTrace();
            }

        }

        return classfileBuffer;
    }


    public void start() {
        Thread thread = new Thread(this::run);
        thread.setName("tTransformer thread");
        thread.setDaemon(true);
        thread.start();
    }

    @Override
    public void run() {

        try {
            Thread.sleep(40000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        List<TransformerLoad> initiatedTransformerLoadList = new ArrayList<>();


        System.out.println("---------------redefineClasses start:");

        enhanceModels.forEach((k, v) -> {
            List<TransformerLoad> list = v;
            if (list != null) {
//                ClassLoader classLoader = list.get(0).getClassLoader();
//                Class[] initiatedClasses = instrumentation.getInitiatedClasses(classLoader);

                for (TransformerLoad transformerLoad : list) {
                    initiatedTransformerLoadList.add(transformerLoad);
//                    for (Class cla : initiatedClasses) {
//                        //加载过的class
//                        if (cla.getName().equals(transformerLoad.getClassName())) {
//                            initiatedTransformerLoadList.add(transformerLoad);
//                        }
//                    }
                }
            }
        });

        for (TransformerLoad transformerLoad : initiatedTransformerLoadList){
            ClassLoader loader = transformerLoad.getClassLoader();
            if (loader == null){
                loader = Thread.currentThread().getContextClassLoader();
            }
            try {
                String className = transformerLoad.className.replaceAll("/",".");
                Class cla = loader.loadClass(className);
                ClassDefinition classDefinition = new ClassDefinition(cla,transformerLoad.classfileBuffer);
                if (instrumentation.isRedefineClassesSupported() && instrumentation.isModifiableClass(cla)){
                    try {
                        instrumentation.redefineClasses(classDefinition);
                        System.out.println("---------------redefineClasses class:"+transformerLoad.className);
                    } catch (UnmodifiableClassException e) {
                        e.printStackTrace();
                    }
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 转换加载模型
     */
    public static class TransformerLoad {

        private ClassLoader classLoader;

        private String className;

        private byte[] classfileBuffer;

        private Class<?> classBeingRedefined;

        private ProtectionDomain protectionDomain;

        public ClassLoader getClassLoader() {
            return classLoader;
        }

        public void setClassLoader(ClassLoader classLoader) {
            this.classLoader = classLoader;
        }

        public String getClassName() {
            return className;
        }

        public void setClassName(String className) {
            this.className = className;
        }

        public byte[] getClassfileBuffer() {
            return classfileBuffer;
        }

        public void setClassfileBuffer(byte[] classfileBuffer) {
            this.classfileBuffer = classfileBuffer;
        }

        public Class<?> getClassBeingRedefined() {
            return classBeingRedefined;
        }

        public void setClassBeingRedefined(Class<?> classBeingRedefined) {
            this.classBeingRedefined = classBeingRedefined;
        }

        public ProtectionDomain getProtectionDomain() {
            return protectionDomain;
        }

        public void setProtectionDomain(ProtectionDomain protectionDomain) {
            this.protectionDomain = protectionDomain;
        }
    }


}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/450763
推荐阅读
相关标签
  

闽ICP备14008679号