- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为我的库编写一个 gradle 插件。 https://github.com/shehabic/sherlock ,我需要在编译时在OkHttp Client的字节码中注入(inject)网络拦截器( https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/OkHttpClient.java )具体来说,我想在 Java 中注入(inject)以下行:
this.interceptors.add(new com.shehabic.sherlock.interceptors(new SherlockOkHttpInterceptor())
https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/OkHttpClient.java#L1068
我已经编写了变压器插件,这是我的类编写器:
public class SherlockClassWriter {
ClassReader reader;
ClassWriter writer;
PublicizeMethodAdapter pubMethAdapter;
final static String CLASSNAME = "okhttp3.OkHttpClient";
public SherlockClassWriter() {
try {
reader = new ClassReader(CLASSNAME);
writer = new ClassWriter(reader, 0);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public SherlockClassWriter(byte[] contents) {
reader = new ClassReader(contents);
writer = new ClassWriter(reader, 0);
}
public static void main(String[] args) {
SherlockClassWriter ccw = new SherlockClassWriter();
ccw.publicizeMethod();
}
public byte[] publicizeMethod() {
pubMethAdapter = new PublicizeMethodAdapter(writer);
reader.accept(pubMethAdapter, 0);
return writer.toByteArray();
}
public class PublicizeMethodAdapter extends ClassVisitor {
TraceClassVisitor tracer;
PrintWriter pw = new PrintWriter(System.out);
public PublicizeMethodAdapter(ClassVisitor cv) {
super(ASM4, cv);
this.cv = cv;
tracer = new TraceClassVisitor(cv, pw);
}
@Override
public MethodVisitor visitMethod(
int access,
String name,
String desc,
String signature,
String[] exceptions
) {
if (name.equals("build")) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
// call method in java:
// this.interceptors.add(new com.shehabic.sherlock.interceptors(new SherlockOkHttpInterceptor())
}
return tracer.visitMethod(access, name, desc, signature, exceptions);
}
}
}
类似的添加拦截器的方法的字节码如下:
aload_0
getfield #4 <okhttp3/OkHttpClient$Builder.interceptors>
aload_1
invokeinterface #117 <java/util/List.add> count 2
pop
aload_0
我的问题是:1.如何向方法中注入(inject)更多代码?即使字节码。
最佳答案
有示例代码可在函数开头插入行
public class YourClassVisitor extends ClassVisitor {
public YourClassVisitor(ClassVisitor cv) {
super(Opcodes.ASM5, cv);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
if (name.equals("targetName")) {
return new YourMethodVisitor(super.visitMethod(access, name, desc, signature, exceptions));
}
return super.visitMethod(access, name, desc, signature, exceptions);
}
private static class YourMethodVisitor extends MethodVisitor {
public YourMethodVisitor(MethodVisitor mv) {
super(Opcodes.ASM5, mv);
}
// This method will be called before almost all instructions
@Override
public void visitCode() {
// Default implementation is empty. So we haven't to call super method
// Puts 'this' on top of the stack. If your method is static just delete it
visitVarInsn(Opcodes.ALOAD, 0);
// Takes instance of class "the/full/name/of/your/Class" from top of the stack and put value of field interceptors
// "Ljava/util/List;" is just internal name of java.util.List
// If your field is static just replace GETFIELD with GETSTATIC
visitFieldInsn(Opcodes.GETFIELD, "the/full/name/of/your/Class", "interceptors", "Ljava/util/List;");
// Before we call add method of list we have to put target value on top of the stack
// New object creation starts with creating not initialized instance of it
visitTypeInsn(Opcodes.NEW, "com/shehabic/sherlock/interceptors");
// Than we just copy it
visitInsn(Opcodes.DUP);
visitTypeInsn(Opcodes.NEW, "example/path/to/class/SherlockOkHttpInterceptor");
visitInsn(Opcodes.DUP);
// We have to call classes constructor
// Internal name of constructor - <init>
// ()V - signature of method. () - method doesn't have parameters. V - method returns void
visitMethodInsn(Opcodes.INVOKESPECIAL, "example/path/to/class/SherlockOkHttpInterceptor", "<init>", "()V", false);
// So on top of the stack we have initialized instance of example/path/to/class/SherlockOkHttpInterceptor
// Now we can call constructor of com/shehabic/sherlock/interceptors
visitMethodInsn(Opcodes.INVOKESPECIAL, "com/shehabic/sherlock/interceptors", "<init>", "(Lexample/path/to/class/SherlockOkHttpInterceptor;)V", false);
// So on top of the stack we have initialized instance of com/shehabic/sherlock/interceptors
// Now we can put it into list
visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
}
}
}
有使用类访问者的示例
byte[] cache = null;
try (FileInputStream in = new FileInputStream("C:\\Users\\JustAGod\\Projects\\gloomymods\\BuildTools\\BytecodeTools\\out\\production\\classes\\gloomyfolken\\Kek.class")) {
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassReader reader = new ClassReader(in);
reader.accept(new YourClassVisitor(writer), ClassReader.EXPAND_FRAMES);
cache = writer.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
try(FileOutputStream out = new FileOutputStream("C:\\Users\\JustAGod\\Projects\\gloomymods\\BuildTools\\BytecodeTools\\out\\production\\classes\\gloomyfolken\\Kek.class")) {
out.write(cache);
} catch (IOException e) {
e.printStackTrace();
}
我真的很抱歉我的英语。
关于java - 如何使用 ClassVisitor/Java 字节码 (ASM) 向 ByteCode 中的方法添加额外指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54165882/
我需要处理来自旧 Mac 时代(旧摩托罗拉 CPU)的文件。字节是大端字节序,所以我有一个函数可以将 Int64 交换为英特尔小端字节序。该函数是 ASM,可在 32 位 CPU 上运行,但不能在 6
1.概述 转载:史上最通俗易懂的ASM教程 一勺思想 We are all in the gutter, but some of us are looking at the stars. (我们都生活
1.概述 转载:ASM 与 Presto 动态代码生成简介 代码生成是很多计算引擎中常用的执行优化技术,比如我们熟悉的 Apache Spark 和 Presto 在表达式等地方就使用到代码生成技术。
我想在 C++ 程序中使用 ASM 调用地址为 774a7fdch 的函数(kernel32.dll 函数) 我正在使用 Visual Studio 2010。 我该怎么做? call 774a7fd
我是否正确转换了它? 原始 VS C++ 版本: _TEB *pTeb = NULL; _asm { mov eax, fs:[0x18];
阅读自howto_add_systemcall "In general, header files for machine architecture independent system calls
在实现无锁数据结构和时序代码时,通常需要抑制编译器的优化。通常人们使用 asm volatile 和 clobber 列表中的 memory 来执行此操作,但有时您只会看到 asm volatile
这个“strcpy”函数的目的是将src的内容复制到dest,结果很好:显示两行“Hello_src”。 #include static inline char * strcpy(char * de
我正在尝试进行一些汇编编码,我从 C 语言调用函数。代码本身运行良好,但我有两个巨大的问题在很长一段时间内无法解决。第一个是语法高亮 - 我安装了两个不同的(当时一个)asm 高亮扩展到 Visual
我正在研究一些类文件分析,并且正在研究使用 ASM 来读取类。在 Javap 中,操作码以及 tagName 和 tagValue 是内联打印的,但在每个 AbstractInsnNode 中,我只看
我正在尝试弄清楚如何将 ASM 中的 DB 变量用于内联 ASM C++ 我有这个 ASM 代码: filename db "C:\imagen.bmp" eti0: mov ah,3dh mov a
这个“strcpy”函数的目的是将src的内容复制到dest,结果很好:显示两行“Hello_src”。 #include static inline char * strcpy(char * de
在 mm/memory.c 中,它包含一个文件: #include tlb.h 是 include/asm-generic/tlb.h或 arch/arm/include/asm/tlb.h ? 最
你好我找到了一个asm代码......它被集成到c++项目中 template T returned; BYTE *tem = buffer; __asm { mov eax, tem
问题:当我运行 @ 命令提示符 >tasm HelloWorld.asm 顺便说一句,我在输入文件名 HelloWorld.asm 时使用 TAB,所以没有错字.我收到这个致命的命令行错误: Turb
尝试通过 eax 从 asm proc 返回一个 long int,后来又尝试通过 dx:ax。两者都不适合我,因为 C printf 打印的数字与所需的 320L 不同。 x.asm: .model
这是 godbolt 生成的代码. 下面是 Visual Studio 在我的 main.asm 文件上生成的相同代码(通过 Project->C/C++->Output Files->Assembl
在构建具有依赖项的 giraph jar 时,我们收到以下警告.. 真的不知道如何解决这些.. 我们已经尝试过了 useProjectArtifact 为 false 和 解压为真 两者似乎都有效 任
我正在使用 gentoo 并尝试编译一个程序来控制并行端口上的位。它的顶部附近有这条线: #include 当我尝试在其上使用 gcc 时,它会产生以下输出: port.c:4:20: error:
(原帖)将 hibernate 依赖项添加到 pom.xml 时显示错误 2011-10-11 10:36:53.710::WARN: failed guiceFilter java.lang.No
我是一名优秀的程序员,十分优秀!