-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to disable anonymous class inline (#633)
- Loading branch information
Showing
16 changed files
with
152 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
jadx-core/src/main/java/jadx/core/dex/visitors/ProcessAnonymous.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package jadx.core.dex.visitors; | ||
|
||
import jadx.core.dex.attributes.AFlag; | ||
import jadx.core.dex.nodes.ClassNode; | ||
import jadx.core.dex.nodes.FieldNode; | ||
import jadx.core.dex.nodes.MethodNode; | ||
import jadx.core.dex.nodes.RootNode; | ||
import jadx.core.dex.visitors.regions.RegionMakerVisitor; | ||
|
||
@JadxVisitor( | ||
name = "ProcessAnonymous", | ||
desc = "Mark anonymous and lambda classes (for future inline)", | ||
runAfter = RegionMakerVisitor.class | ||
) | ||
public class ProcessAnonymous extends AbstractVisitor { | ||
|
||
@Override | ||
public void init(RootNode root) { | ||
if (!root.getArgs().isInlineAnonymousClasses()) { | ||
return; | ||
} | ||
|
||
for (ClassNode cls : root.getClasses(true)) { | ||
markAnonymousClass(cls); | ||
} | ||
} | ||
|
||
private static boolean markAnonymousClass(ClassNode cls) { | ||
if (isAnonymous(cls) || isLambdaCls(cls)) { | ||
cls.add(AFlag.ANONYMOUS_CLASS); | ||
cls.add(AFlag.DONT_GENERATE); | ||
|
||
for (MethodNode mth : cls.getMethods()) { | ||
if (mth.isConstructor()) { | ||
mth.add(AFlag.ANONYMOUS_CONSTRUCTOR); | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean isAnonymous(ClassNode cls) { | ||
return cls.getClassInfo().isInner() | ||
&& Character.isDigit(cls.getClassInfo().getShortName().charAt(0)) | ||
&& cls.getMethods().stream().filter(MethodNode::isConstructor).count() == 1; | ||
} | ||
|
||
private static boolean isLambdaCls(ClassNode cls) { | ||
return cls.getAccessFlags().isSynthetic() | ||
&& cls.getAccessFlags().isFinal() | ||
&& cls.getClassInfo().getRawName().contains(".-$$Lambda$") | ||
&& countStaticFields(cls) == 0; | ||
} | ||
|
||
private static int countStaticFields(ClassNode cls) { | ||
int c = 0; | ||
for (FieldNode field : cls.getFields()) { | ||
if (field.getAccessFlags().isStatic()) { | ||
c++; | ||
} | ||
} | ||
return c; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.