You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何把ECD反编译生成的匿名类(如P$1-P$5)整合回原Java类?

Hey there! Let's work through merging those split anonymous classes back into your P class—this is a super common hiccup when decompiling JARs for school tasks, so I’ve got your back. Here's a step-by-step approach to get this sorted:

Step 1: First, Understand What Those P$N Classes Are

Those P$1 to P$5 files are anonymous inner classes that were originally defined inside your P class. Decompilers split them into separate files because that’s how they’re stored in the JAR, but they’re meant to be embedded directly within P’s code. They’re usually used to implement interfaces (like your Comparator<Family>), define event handlers, or create short-lived logic blocks.

Step 2: Figure Out What Each P$N Class Does

Open each P$N file and scan its code to identify its purpose:

  • If it implements Comparator<Family>, you’ll see it overrides the compare(Family o1, Family o2) method.
  • If it’s a Runnable, it’ll have a run() method.
  • Pay attention to references like this$0.group or this$0.marker—those point to the parent P class’s members, which tells you where the anonymous class was originally defined inside P.
Step 3: Merge Each Anonymous Class Back into P

Let’s use a concrete example with a Comparator-based P$1 class:

Example P$1 Code (Decompiled):

public class P$1 implements Comparator<Family> {
    private final P this$0;

    P$1(P var1) {
        this.this$0 = var1;
    }

    public int compare(Family o1, Family o2) {
        // Logic that uses the parent P class's marker field
        return this.this$0.marker - o1.getAge();
    }
}

Merge It Into P:

Find where this comparator was used in P (maybe a sorting method, or a field initialization) and replace the external class reference with an inline anonymous inner class:

public class P {
    private ArrayList<Family> group;
    private int marker;
    private Integer primaryElement;

    // Example: A method that uses the comparator
    public void sortFamilyGroup() {
        Collections.sort(group, new Comparator<Family>() {
            @Override
            public int compare(Family o1, Family o2) {
                // Replace this$0 with P.this to reference the parent class's members
                return P.this.marker - o1.getAge();
            }
        });
    }
}

Key notes here:

  • Replace this$0.xxx with P.this.xxx to access the parent class’s private members (inner classes have full access to their outer class’s fields).
  • If the anonymous class was used to initialize a field (like a Runnable task), embed it directly in the field declaration.
Step 4: Repeat for All P$N Classes

Go through each P$1 to P$5 one by one:

  1. Confirm its purpose and original placement in P.
  2. Embed its code as an inline anonymous class in the correct spot.
  3. Fix any references to the parent P class (swap this$0 for P.this where needed).
Step 5: Compile and Validate

Once all merges are done, compile P in Eclipse to check for errors:

  • You might run into issues with effectively final variables (if the anonymous class references a local variable from P’s method—just mark that variable as final if needed).
  • Double-check that all private member access works (inner classes can access outer class private fields, so this should be fine).

内容的提问来源于stack exchange,提问作者Troy D

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:16:47