java 匿名类 反编译,如何将匿名类重新集成到ECD反编译器生成的Java代码中?

博主有一个jar包需反编译、修改和重新评估,使用ECD插件反编译后遇到匿名类提取需合并的问题,如类P和P$1等。询问如何将P$1合并回P以及this.this$0的含义,还给出了原代码可能的样子,并解释了编译器的一些操作。

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

I have a jar for school that is supposed to be decompiled, modifed, and reevaluated. I decompiled all of the class files using the ECD plugin for Eclipse, but I think I have a few anonymous classes that were extracted and need to be merged back into another class. I have a class P, and then five more classes named P$1, P$2, ..., P$5.

Here's the problem parts of P:

public class P {

private ArrayList group;

private int marker;

private Integer primaryElement;

Comparator c;

public P(ArrayList g, Integer i, Comparator c) {

this.marker = -1;

this.group = new ArrayList(g);

this.primaryElement = i;

this.c = c;

}

/* Some unrelated methods */

public String printHeader() {

return this.print(new 1(this));

}

public String printRow(Integer i) {

return this.print(new 2(this, i));

}

public String printPad() {

return this.print(new 3(this));

}

public Object printCost() {

return this.print(new 4(this));

}

public String printLine() {

return this.print(new 5(this));

}

Here is P$1. The others are very similar.

final class P$1 implements PrintCommand {

P$1(P arg0) {

this.this$0 = arg0;

}

public String print(Family f) {

return String.format("%3d", new Object[]{Integer.valueOf(f.getId())});

}

}

In case you're wondering, PrintCommand is a super simple interface:

public interface PrintCommand {

String print(Family arg0);

}

How can I get P$1 merged back into P? Also, what does this.this$0 mean in P$1?

解决方案

In an anonymous class you can reference the this from the enclosing class with P.this. To do that, the java compiler will create a constructor, which will set a field named this$0 to the reference passed to the constructor.

The original code probably looked like this:

public String printHeader() {

return this.print(new PrintCommand() {

public String print(Family f) {

return String.format(%3d", f.getId());

}

);

}

There are other things the compiler does, for example adding accessor methods for private methods/fields from the enclosing class that are accessed in the inner class. Or passing the value of (effectively) final variables used in the inner class to the constructor.

From the perspective of the Java Runtime, there is no anonymous inner class, only named classes.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值