首页 > 编程语言 >javassist改类

javassist改类

时间:2023-09-20 17:25:40浏览次数:39  
标签:java CtBehavior 改类 Test javassist MemberResolver compiler

package agent;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtBehavior;
import javassist.CtClass;

import java.io.*;

public class Test {
    public static void main(String []f) throws IOException, CannotCompileException {


        ClassPool classPool = new ClassPool(true);

        InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("agent/Test.class");
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        byte [] bytes = new byte[1000];
        int n = 0;
        while(-1 != (n=inputStream.read(bytes))) {
            byteArrayOutputStream.write(bytes, 0, n);
        }
        CtClass ctClass = classPool.makeClass(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
        for(CtBehavior ctBehavior : ctClass.getDeclaredBehaviors()) {
            System.out.println("start handle medthod: " + ctBehavior.getLongName());
            try {
                ctBehavior.insertBefore("System.out.println(\"b\");");
                ctBehavior.insertAfter("System.out.println(\"a\");");

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        byte [] out =  ctClass.toBytecode();
        FileOutputStream fileOutputStream = new FileOutputStream("tmp.class");
        fileOutputStream.write(out);
        fileOutputStream.close();

  

假如:

ClassPool classPool = new ClassPool();

start handle medthod: agent.Test()
javassist.CannotCompileException: [source error] no such class: System.out
at javassist.CtBehavior.insertBefore(CtBehavior.java:774)
at javassist.CtBehavior.insertBefore(CtBehavior.java:734)
at agent.Test.main(Test.java:28)
Caused by: compile error: no such class: System.out
at javassist.compiler.MemberResolver.searchImports(MemberResolver.java:470)
at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:414)
at javassist.compiler.MemberResolver.lookupClassByJvmName(MemberResolver.java:321)
at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:683)
at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:157)
at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:242)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:330)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
at javassist.compiler.Javac.compileStmnt(Javac.java:567)
at javassist.CtBehavior.insertBefore(CtBehavior.java:754)
... 2 more
start handle medthod: agent.Test.main(java.lang.String[])
javassist.CannotCompileException: cannot find java.lang.String[]
at javassist.CtBehavior.insertBefore(CtBehavior.java:771)
at javassist.CtBehavior.insertBefore(CtBehavior.java:734)
at agent.Test.main(Test.java:28)
Caused by: javassist.NotFoundException: java.lang.String[]
at javassist.ClassPool.get(ClassPool.java:445)
at javassist.bytecode.Descriptor.toCtClass(Descriptor.java:592)
at javassist.bytecode.Descriptor.getParameterTypes(Descriptor.java:439)
at javassist.CtBehavior.getParameterTypes(CtBehavior.java:298)
at javassist.CtBehavior.insertBefore(CtBehavior.java:749)
... 2 more
start handle medthod: agent.Test.print(int)
javassist.CannotCompileException: [source error] no such class: System.out
at javassist.CtBehavior.insertBefore(CtBehavior.java:774)
at javassist.CtBehavior.insertBefore(CtBehavior.java:734)
at agent.Test.main(Test.java:28)
Caused by: compile error: no such class: System.out
at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:402)
at javassist.compiler.MemberResolver.lookupClassByJvmName(MemberResolver.java:321)
at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:683)
at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:157)
at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:242)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:330)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
at javassist.compiler.Javac.compileStmnt(Javac.java:567)
at javassist.CtBehavior.insertBefore(CtBehavior.java:754)
... 2 more

标签:java,CtBehavior,改类,Test,javassist,MemberResolver,compiler
From: https://www.cnblogs.com/silyvin/p/17717824.html

相关文章

  • NET7下EFCORE的通用增删查改类
    NET7下EFCORE的通用增删查改类代码摘录自《深入浅出ASP.NETCORE》 ///<summary>///所有仓储的约定,此接口仅作为约定,用于标识他们///</summary>///<typeparamname="TEntity">传入仓储的实体模型</typeparam>///<typeparamname="TPrimaryKey&quo......
  • jdk代理 cglib代理 asm javassist
    jdk代理实现代码:publicclassJdkProxyCimplementsInvocationHandler{privateObjecttarget;publicJdkProxyC(Objecttarget){this.target=target;}publicObjectgetProxy()throwsClassNotFoundException,InvocationTargetExceptio......
  • java~字节码操作Javassist
    Javassist是一个开源的Java字节码操作库,它提供了一组简单而强大的API,用于在运行时修改和生成Java字节码。Javassist的名称是"JavaProgrammingAssistant"的缩写,它的目标是简化对字节码的操作,使开发人员能够更轻松地实现动态代码生成和修改。Javassist的主要特点包括:简单易用:J......
  • QT修改类名
    QT修改类名前言由于QT的自动生成,QT中修改类名往往导致各种编译错误。首先明确,修改类名的基本操作是使用QtCreater,右键->Refactor->RenameSymbolunderCursor情形1给普通类改名修改完类名后,直接清理项目重新构建即可情形2给界面类改名修改完类名后,注意修改在命名空间......
  • javassist动态生成类
    1. 使⽤javassist⽣成类   58来⾃百度百科:Javassist是⼀个开源的分析、编辑和创建Java字节码的类库。是由东京⼯业⼤学的数学和计算机科学系的 Shigeru Chiba (千叶 滋)所创建的。它已加⼊了开放源代码JBoss 应⽤服务器项⽬,通过使⽤Javassist对字节码操作为JBoss实现动态"AO......
  • 字节码增强javassist 使用javassist运行时动态修改字节码对象
    java程序什么时候需要在运行的时候动态修改字节码对象?如何在运行的时候动态修改字节码对象?修改字节码对象的时候会发生哪些错误,又该如何解决这些问题?一、java程序什么时候需要在运行的时候动态修改字节码对象我认为有两种场景,一种是无法修改源代码的时候;另外一种是功能增强的......
  • MySQL - SQL语句增加字段/修改字段/修改类型/修改默认值
    1.添加字段,比如我在数据表中添加一个age字段,类型为int(11)ALTERTABLEplayerADDCOLUMNageint(11);2.修改字段名,将age字段改成player_ageALTERTABLEplayerRENAMECOLUMNagetoplayer_age3.修改字段的数据类型,将player_age的数据类型设置为float(3,1)ALT......
  • javassist 报错 ; is missing
    在通过javassist编写的时候,主要代码如下:Stringcode="for(Stringaa:tt){....}";启动后报 Exceptioninthread"main"javassist.CannotCompileException:[sourceerror];ismissingatjavassist.CtNewMethod.make(CtNewMethod.java:84)atjava......
  • java字节码编程技术(8/10) -Javassist
    Javassist这个库和asm经常使用,它的性能稍差一点<dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.27.0-GA</version></dependency>获取一个类加载器(ClassLoader),以加载指定的.jar或.class文件privateClassLoade......
  • 在Javassist学习时遇到的javassist.NotFoundException问题的解决办法
    在Javassist学习时遇到的javassist.NotFoundException问题的解决办法//运行时会报错的原代码:ClassPoolpool=ClassPool.getDefault();CtClasscc=pool.get(Emp.clas......