mirror of
https://github.com/skylot/jadx.git
synced 2024-11-22 20:29:51 +00:00
fix: check for annotations before remove empty default constructor (#1863)
This commit is contained in:
parent
e29011eb95
commit
0fd9a9df29
@ -14,14 +14,14 @@ trim_trailing_whitespace = true
|
||||
[*.java]
|
||||
ij_java_continuation_indent_size = 8
|
||||
ij_java_use_single_class_imports = true
|
||||
ij_java_class_count_to_use_import_on_demand = 999
|
||||
ij_java_names_count_to_use_import_on_demand = 999
|
||||
ij_java_class_count_to_use_import_on_demand = 99
|
||||
ij_java_names_count_to_use_import_on_demand = 99
|
||||
ij_java_packages_to_use_import_on_demand = *
|
||||
|
||||
[*.kt]
|
||||
ij_kotlin_continuation_indent_size = 8
|
||||
ij_kotlin_name_count_to_use_star_import = 999
|
||||
ij_kotlin_name_count_to_use_star_import_for_members = 999
|
||||
ij_kotlin_name_count_to_use_star_import = 99
|
||||
ij_kotlin_name_count_to_use_star_import_for_members = 99
|
||||
ij_kotlin_packages_to_use_import_on_demand = *
|
||||
|
||||
[*.yml]
|
||||
|
@ -8,6 +8,7 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import jadx.api.plugins.input.data.AccessFlags;
|
||||
import jadx.api.plugins.input.data.attributes.JadxAttrType;
|
||||
import jadx.core.Consts;
|
||||
import jadx.core.dex.attributes.AFlag;
|
||||
import jadx.core.dex.attributes.AType;
|
||||
@ -306,19 +307,24 @@ public class ClassModifier extends AbstractVisitor {
|
||||
* Remove public empty constructors (static or default)
|
||||
*/
|
||||
private static void removeEmptyMethods(MethodNode mth) {
|
||||
if (!mth.getArgRegs().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
AccessInfo af = mth.getAccessFlags();
|
||||
boolean publicConstructor = af.isConstructor() && af.isPublic();
|
||||
boolean publicConstructor = mth.isConstructor() && af.isPublic();
|
||||
boolean clsInit = mth.getMethodInfo().isClassInit() && af.isStatic();
|
||||
if ((publicConstructor || clsInit) && mth.getArgRegs().isEmpty()) {
|
||||
List<BlockNode> bb = mth.getBasicBlocks();
|
||||
if (bb == null || bb.isEmpty() || BlockUtils.isAllBlocksEmpty(bb)) {
|
||||
if (clsInit) {
|
||||
if (publicConstructor || clsInit) {
|
||||
if (!BlockUtils.isAllBlocksEmpty(mth.getBasicBlocks())) {
|
||||
return;
|
||||
}
|
||||
if (clsInit) {
|
||||
mth.add(AFlag.DONT_GENERATE);
|
||||
} else {
|
||||
// don't remove default constructor if other constructors exists or constructor has annotations
|
||||
if (mth.isDefaultConstructor()
|
||||
&& !isNonDefaultConstructorExists(mth)
|
||||
&& !mth.contains(JadxAttrType.ANNOTATION_LIST)) {
|
||||
mth.add(AFlag.DONT_GENERATE);
|
||||
} else {
|
||||
// don't remove default constructor if other constructors exists
|
||||
if (mth.isDefaultConstructor() && !isNonDefaultConstructorExists(mth)) {
|
||||
mth.add(AFlag.DONT_GENERATE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -981,6 +981,9 @@ public class BlockUtils {
|
||||
}
|
||||
|
||||
public static boolean isAllBlocksEmpty(List<BlockNode> blocks) {
|
||||
if (Utils.isEmpty(blocks)) {
|
||||
return true;
|
||||
}
|
||||
for (BlockNode block : blocks) {
|
||||
if (!block.getInstructions().isEmpty()) {
|
||||
return false;
|
||||
|
@ -0,0 +1,33 @@
|
||||
package jadx.tests.integration.others;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jadx.tests.api.IntegrationTest;
|
||||
|
||||
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
|
||||
|
||||
public class TestDefConstructorWithAnnotation extends IntegrationTest {
|
||||
|
||||
public static class TestCls {
|
||||
@AnnotationTest
|
||||
public TestCls() {
|
||||
}
|
||||
|
||||
@Target(ElementType.CONSTRUCTOR)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AnnotationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
assertThat(getClassNode(TestCls.class))
|
||||
.code()
|
||||
.containsOne("@AnnotationTest");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user