Java Agent不允许我修改java.lang.StringBuilder,为什么?

时间:2018-02-06 18:28:18

标签: java bytecode instrumentation javaagents

我试图根据这个SO答案更改java.lang.StringBuilder的字节码:

Replace a class within the Java class library with a custom version

然而,它似乎没有拦截java.lang.StringBuilder。有谁知道为什么?

代理人:

import java.lang.instrument.*;
import java.security.*;
import java.io.*;

public class BugFixAgent {
  public static void premain(String args, Instrumentation inst) {
    inst.addTransformer(new ClassFileTransformer() {
      @Override
      public byte[] transform(ClassLoader loader,
                              String className,
                              Class<?> classBeingRedefined,
                              ProtectionDomain protectionDomain,
                              byte[] classfileBuffer) {

                System.out.println("---- ClassName: " + className);
                return null;
      }
    }, true);
  }
}

测试代码:

package com.test;

public class Test {

        public static void main(String[] args) {
                StringBuilder sb = new StringBuilder();
                sb.append("Blah");
                sb.append(34.4f);
                System.out.println(sb);
        }
}

但是当我执行时:

java -javaagent:agentclass/bugfixagent.jar -cp classes com.test.Test

我明白了:

---- ClassName: java/lang/invoke/MethodHandleImpl
---- ClassName: java/lang/invoke/MethodHandleImpl$1
---- ClassName: java/lang/invoke/MethodHandleImpl$2
---- ClassName: java/util/function/Function
---- ClassName: java/lang/invoke/MethodHandleImpl$3
---- ClassName: java/lang/invoke/MethodHandleImpl$4
---- ClassName: java/lang/ClassValue
---- ClassName: java/lang/ClassValue$Entry
---- ClassName: java/lang/ClassValue$Identity
---- ClassName: java/lang/ClassValue$Version
---- ClassName: java/lang/invoke/MemberName$Factory
---- ClassName: java/lang/invoke/MethodHandleStatics
---- ClassName: java/lang/invoke/MethodHandleStatics$1
---- ClassName: sun/misc/PostVMInitHook
---- ClassName: sun/usagetracker/UsageTrackerClient
---- ClassName: java/util/concurrent/atomic/AtomicBoolean
---- ClassName: sun/usagetracker/UsageTrackerClient$1
---- ClassName: sun/usagetracker/UsageTrackerClient$4
---- ClassName: sun/usagetracker/UsageTrackerClient$3
---- ClassName: java/io/FileOutputStream$1
---- ClassName: sun/launcher/LauncherHelper
---- ClassName: java/util/concurrent/ConcurrentHashMap$ForwardingNode
---- ClassName: sun/misc/URLClassPath$FileLoader$1
---- ClassName: java/lang/Package
---- ClassName: com/test/Test
---- ClassName: sun/launcher/LauncherHelper$FXHelper
---- ClassName: java/lang/Class$MethodArray
---- ClassName: java/lang/Void
---- ClassName: sun/misc/FloatingDecimal
---- ClassName: sun/misc/FloatingDecimal$ExceptionalBinaryToASCIIBuffer
---- ClassName: sun/misc/FloatingDecimal$BinaryToASCIIConverter
---- ClassName: sun/misc/FloatingDecimal$BinaryToASCIIBuffer
---- ClassName: sun/misc/FloatingDecimal$1
---- ClassName: sun/misc/FloatingDecimal$PreparedASCIIToBinaryBuffer
---- ClassName: sun/misc/FloatingDecimal$ASCIIToBinaryConverter
---- ClassName: sun/misc/FDBigInteger
Blah34.4
---- ClassName: java/lang/Shutdown
---- ClassName: java/lang/Shutdown$Lock

java.lang.StringBuilder去哪了?为什么不存在?

java.lang.String也不存在,可能是因为之前已加载过。如何拦截java.lang.String?不可能?

0 个答案:

没有答案