fix: check for annotations before remove empty default constructor (#1863)

This commit is contained in:
Skylot 2023-05-11 18:11:02 +01:00
parent e29011eb95
commit 0fd9a9df29
No known key found for this signature in database
GPG Key ID: 1E23F5B52567AA39
4 changed files with 56 additions and 14 deletions

View File

@ -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]

View File

@ -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);
}
}
}
}

View File

@ -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;

View File

@ -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");
}
}