fix: check for annotations before remove empty default constructor (#1863)
This commit is contained in:
+4
-4
@@ -14,14 +14,14 @@ trim_trailing_whitespace = true
|
|||||||
[*.java]
|
[*.java]
|
||||||
ij_java_continuation_indent_size = 8
|
ij_java_continuation_indent_size = 8
|
||||||
ij_java_use_single_class_imports = true
|
ij_java_use_single_class_imports = true
|
||||||
ij_java_class_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 = 999
|
ij_java_names_count_to_use_import_on_demand = 99
|
||||||
ij_java_packages_to_use_import_on_demand = *
|
ij_java_packages_to_use_import_on_demand = *
|
||||||
|
|
||||||
[*.kt]
|
[*.kt]
|
||||||
ij_kotlin_continuation_indent_size = 8
|
ij_kotlin_continuation_indent_size = 8
|
||||||
ij_kotlin_name_count_to_use_star_import = 999
|
ij_kotlin_name_count_to_use_star_import = 99
|
||||||
ij_kotlin_name_count_to_use_star_import_for_members = 999
|
ij_kotlin_name_count_to_use_star_import_for_members = 99
|
||||||
ij_kotlin_packages_to_use_import_on_demand = *
|
ij_kotlin_packages_to_use_import_on_demand = *
|
||||||
|
|
||||||
[*.yml]
|
[*.yml]
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import java.util.Map;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import jadx.api.plugins.input.data.AccessFlags;
|
import jadx.api.plugins.input.data.AccessFlags;
|
||||||
|
import jadx.api.plugins.input.data.attributes.JadxAttrType;
|
||||||
import jadx.core.Consts;
|
import jadx.core.Consts;
|
||||||
import jadx.core.dex.attributes.AFlag;
|
import jadx.core.dex.attributes.AFlag;
|
||||||
import jadx.core.dex.attributes.AType;
|
import jadx.core.dex.attributes.AType;
|
||||||
@@ -306,19 +307,24 @@ public class ClassModifier extends AbstractVisitor {
|
|||||||
* Remove public empty constructors (static or default)
|
* Remove public empty constructors (static or default)
|
||||||
*/
|
*/
|
||||||
private static void removeEmptyMethods(MethodNode mth) {
|
private static void removeEmptyMethods(MethodNode mth) {
|
||||||
|
if (!mth.getArgRegs().isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
AccessInfo af = mth.getAccessFlags();
|
AccessInfo af = mth.getAccessFlags();
|
||||||
boolean publicConstructor = af.isConstructor() && af.isPublic();
|
boolean publicConstructor = mth.isConstructor() && af.isPublic();
|
||||||
boolean clsInit = mth.getMethodInfo().isClassInit() && af.isStatic();
|
boolean clsInit = mth.getMethodInfo().isClassInit() && af.isStatic();
|
||||||
if ((publicConstructor || clsInit) && mth.getArgRegs().isEmpty()) {
|
if (publicConstructor || clsInit) {
|
||||||
List<BlockNode> bb = mth.getBasicBlocks();
|
if (!BlockUtils.isAllBlocksEmpty(mth.getBasicBlocks())) {
|
||||||
if (bb == null || bb.isEmpty() || BlockUtils.isAllBlocksEmpty(bb)) {
|
return;
|
||||||
if (clsInit) {
|
}
|
||||||
|
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);
|
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) {
|
public static boolean isAllBlocksEmpty(List<BlockNode> blocks) {
|
||||||
|
if (Utils.isEmpty(blocks)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
for (BlockNode block : blocks) {
|
for (BlockNode block : blocks) {
|
||||||
if (!block.getInstructions().isEmpty()) {
|
if (!block.getInstructions().isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
+33
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user