java static 块 反编译,Java反编译

当使用 Java 反编译器时,可能会遇到类似于 `Foo.access$004(Foo.this)`、`this.Bar.access$006(Bar.this)` 或 `Baz.access$102(Baz.this, true)` 的代码。这些方法是合成方法,用于支持内部类访问其私有方法。由于内部类在最初的 JVM 版本中未被考虑,所以无法直接使用访问修饰符处理这种情况。编译器会创建额外的包可见方法,将调用委托给私有实现。这种技术使得外部类能够访问内部类的私有方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When decompiling a specific jar using java decompiler (http://java.decompiler.free.fr/) I got some strange code I cannot identify what is. can someone help me? the code is something like:

Foo.access$004(Foo.this);

or this

Bar.access$006(Bar.this);

or else

Baz.access$102(Baz.this, true)

What are these methods access$004, access$006 and access$102?

解决方案

Synthetic methods like this get created to support acessing private methods of inner classes. Since inner classes were not part of the initial jvm version, the access modifiers could not really handle this case. The solution was to create additional package-visible methods that delegate to the private implementation.

public class Example {

private static class Inner {

private void innerMethod() { ... }

}

public void test() {

Inner inner = ...

inner.innerMethod():

}

}

The compile would create a new method of the Inner class like this:

static void access$000(Inner inner) {

inner.innerMethod();

}

And replace the call in the test method like this:

Inner.access$000(inner);

The static access$000 is package visible and so accessible from the outer class, and being inside the same Inner class it can delegate to the private innerMethod.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值