1.添加maven依赖
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javaassist.version}</version>
</dependency>
<properties>
<javaassist.version>3.21.0-GA</javaassist.version>
</properties>
2.编写修改方法
public static void removeWatermark() throws NotFoundException, CannotCompileException, IOException {
ClassPool.getDefault().insertClassPath("D:\\software\\57-file\\fast-preview-master\\code\\lib\\aspose-words.jar"); // jar包的绝对路径
CtClass targetClass = ClassPool.getDefault().getCtClass("com.aspose.words.zzZE0"); //类路径
//这里是因为我要修改多个方法得返回值,所以填写多个修改就好
CtMethod targetMethod = targetClass.getDeclaredMethod("zzZ4h"); //方法路径
CtMethod targetMethod1 = targetClass.getDeclaredMethod("zzZ4g"); //方法路径
targetMethod.setBody("{return 1;}"); //修改方法体,始终返回1
targetMethod1.setBody("{return 1;}"); //修改方法体,始终返回1
targetClass.writeFile("D:\\aaaaa"); //保存修改,内容为保存的路径。
}
public static void main(String[] args) throws IOException, CannotCompileException, NotFoundException {
removeWatermark();
}
3.最后将生成得class文件替换jar包中得文件即可。