我已经从这篇文章中了解了如何在运行时使用ASM操作类。
但是我还有一个关于如何修改常量池的问题。以下是我要修改的示例Java程序
主jar文件:
public class test {
private static final String a = "Hello World";
private static final String b = "ASM is awasome";
public static void main(String[] args) {
int x = 10;
int y = 25;
int z = x * y;
System.out.println(a);
System.out.println(z);
System.out.println(b);
}
}
Run Code Online (Sandbox Code Playgroud)
我想将变量a从修改"Hello World"为"Multiply Of x*y is: "
我的特工班
import java.lang.instrument.*;
import java.security.ProtectionDomain;
import org.objectweb.asm.*;
public class ExampleAgent implements ClassFileTransformer {
private static final String TRANSFORM_CLASS …Run Code Online (Sandbox Code Playgroud) 总结:使用 ASM,给定一个字节码类,对于每个方法指令(MethodInsnNode),我需要获取正在使用的引用。
考虑以下类:
public void myMethod(){
String str1 = "str12";
String str2 = str1;
String str3 = "str3";
Boolean myBool = true;
Boolean myBool2 = true;
Cemo cemo = new Cemo();
assertTrue(cemo.isTrue());
assertTrue(cemo.isTrue());
Run Code Online (Sandbox Code Playgroud)
}
考虑以下生成的字节码指令:
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public void myMethod();
Code:
0: ldc #2 // String str12
2: astore_1
3: aload_1
4: astore_2
5: ldc #3 // String str3
7: astore_3
8: iconst_1
9: invokestatic #4 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean; …Run Code Online (Sandbox Code Playgroud) java bytecode bytecode-manipulation java-bytecode-asm jvm-bytecode
我试图通过检查它在Java方面的外观来了解Kotlin的一些功能.
所以作为一个实验,我尝试了这个:
val printKotlin = fun () {
print("Hello Kotlin")
}
Run Code Online (Sandbox Code Playgroud)
所以上面代码片段的输出是:
public final class FunAsVariableKt {
private static final Function0 printKotlin;
public static final Function0 getPrintKotlin() {
return printKotlin;
}
static {
printKotlin = (Function0)null.INSTANCE;
}
}
Run Code Online (Sandbox Code Playgroud)
我如何理解上面反编译代码的静态块?为什么它会生成这个非工作代码?
对于以下Java类:
public class ArtClass {
public boolean foo(int x) {
if(x == 3956681)
return true;
else if(x == 9855021)
return true;
else if(x == 63085561)
return true;
else
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
它的JVM指令是:
I4 Branch 1 IF_ICMPNE L3
I13 Branch 2 IF_ICMPNE L5
I22 Branch 3 IF_ICMPNE L7
Run Code Online (Sandbox Code Playgroud)
据我所知,第一个分支是在第三行与同为第二和第三分支但到底是什么IF_ICMPNE意思,也做什么I4,I13以及I22是什么意思?
哪个具有更大的字节码大小或在 Java 中相同?
if (a > b) a = b;
Run Code Online (Sandbox Code Playgroud)
对比
if (a > b) {
a = b;
}
Run Code Online (Sandbox Code Playgroud)