Skip to content

Commit

Permalink
fix: check for annotations before remove empty default constructor (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed May 11, 2023
1 parent e29011e commit 0fd9a9d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
8 changes: 4 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
26 changes: 16 additions & 10 deletions jadx-core/src/main/java/jadx/core/dex/visitors/ClassModifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -306,19 +307,24 @@ private static boolean registersAndCastsOnly(InsnArg arg) {
* 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);
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions jadx-core/src/main/java/jadx/core/utils/BlockUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,9 @@ public static BlockNode skipSyntheticPredecessor(BlockNode block) {
}

public static boolean isAllBlocksEmpty(List<BlockNode> blocks) {
if (Utils.isEmpty(blocks)) {
return true;
}
for (BlockNode block : blocks) {
if (!block.getInstructions().isEmpty()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 0fd9a9d

Please sign in to comment.