fix: rename parameters in annotations (#504)

This commit is contained in:
Skylot
2019-03-24 16:59:55 +03:00
parent a848eab407
commit 29d3ce15a8
12 changed files with 161 additions and 46 deletions
@@ -0,0 +1,49 @@
package jadx.tests.integration.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class TestAnnotationsRename extends IntegrationTest {
public static class TestCls {
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface A {
int x();
}
@A(x = 5)
void test() {
}
public void check() throws NoSuchMethodException {
Method test = TestCls.class.getDeclaredMethod("test");
A annotation = test.getAnnotation(A.class);
assertThat(annotation.x(), is(5));
}
}
@Test
public void test() {
enableDeobfuscation();
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, containsString("public @interface "));
assertThat(code, not(containsString("(x = 5)")));
}
}
@@ -7,7 +7,6 @@ import java.lang.annotation.Target;
import org.junit.jupiter.api.Test;
import jadx.NotYetImplemented;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;
@@ -15,30 +14,32 @@ import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
public class AnnotationsRenaming extends IntegrationTest {
public class TestAnnotationsRenameDef extends IntegrationTest {
public static class TestCls {
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public static @interface A {
int x();
public @interface A {
int value();
}
@A(x = 5)
@A(5)
void test() {
}
}
@Test
@NotYetImplemented
public void test504() {
public void test() {
enableDeobfuscation();
// force rename 'value' method
args.setDeobfuscationMinLength(20);
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, containsString("public static @interface "));
assertThat(code, not(containsString("(x = 5)")));
assertThat(code, containsString("public @interface "));
assertThat(code, not(containsString("int value();")));
assertThat(code, not(containsString("(5)")));
}
}
@@ -46,11 +46,11 @@ public class TestLineNumbers extends IntegrationTest {
String code = cls.getCode().toString();
FieldNode field = cls.searchFieldByName("field");
MethodNode func = cls.searchMethodByName("func()V");
MethodNode func = cls.searchMethodByShortId("func()V");
ClassNode inner = cls.getInnerClasses().get(0);
MethodNode innerFunc = inner.searchMethodByName("innerFunc()V");
MethodNode innerFunc2 = inner.searchMethodByName("innerFunc2()V");
MethodNode innerFunc3 = inner.searchMethodByName("innerFunc3()V");
MethodNode innerFunc = inner.searchMethodByShortId("innerFunc()V");
MethodNode innerFunc2 = inner.searchMethodByShortId("innerFunc2()V");
MethodNode innerFunc3 = inner.searchMethodByShortId("innerFunc3()V");
FieldNode innerField = inner.searchFieldByName("innerField");
// check source lines (available only for instructions and methods)
@@ -1,10 +1,5 @@
package jadx.tests.integration.debuginfo;
import static jadx.tests.api.utils.JadxMatchers.containsOne;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
import jadx.NotYetImplemented;
@@ -14,6 +9,11 @@ import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.tests.api.IntegrationTest;
import static jadx.tests.api.utils.JadxMatchers.containsOne;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class TestReturnSourceLine extends IntegrationTest {
public static class TestCls {
@@ -55,10 +55,10 @@ public class TestReturnSourceLine extends IntegrationTest {
String code = codeWriter.toString();
String[] lines = code.split(CodeWriter.NL);
MethodNode test1 = cls.searchMethodByName("test1(Z)I");
MethodNode test1 = cls.searchMethodByShortId("test1(Z)I");
checkLine(lines, codeWriter, test1, 3, "return 1;");
MethodNode test2 = cls.searchMethodByName("test2(I)I");
MethodNode test2 = cls.searchMethodByShortId("test2(I)I");
checkLine(lines, codeWriter, test2, 3, "return v - 1;");
}
@@ -70,7 +70,7 @@ public class TestReturnSourceLine extends IntegrationTest {
String code = codeWriter.toString();
String[] lines = code.split(CodeWriter.NL);
MethodNode test3 = cls.searchMethodByName("test3(I)I");
MethodNode test3 = cls.searchMethodByShortId("test3(I)I");
checkLine(lines, codeWriter, test3, 3, "return v;");
}
@@ -30,10 +30,7 @@ public class TestMthRename extends IntegrationTest {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, containsString("public abstract void mo1a();"));
assertThat(code, not(containsString("public abstract void a();")));
assertThat(code, containsString(".mo1a();"));
assertThat(code, not(containsString(".a();")));
}
}