如何把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:
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.
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 thecompare(Family o1, Family o2)method. - If it’s a
Runnable, it’ll have arun()method. - Pay attention to references like
this$0.grouporthis$0.marker—those point to the parentPclass’s members, which tells you where the anonymous class was originally defined insideP.
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.xxxwithP.this.xxxto 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
Runnabletask), embed it directly in the field declaration.
P$N Classes Go through each P$1 to P$5 one by one:
- Confirm its purpose and original placement in
P. - Embed its code as an inline anonymous class in the correct spot.
- Fix any references to the parent
Pclass (swapthis$0forP.thiswhere needed).
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 asfinalif 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

