Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace constants in annotations of method arguments #2358

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion jadx-core/src/main/java/jadx/core/dex/visitors/ModVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import jadx.api.plugins.input.data.annotations.EncodedValue;
import jadx.api.plugins.input.data.annotations.IAnnotation;
import jadx.api.plugins.input.data.attributes.JadxAttrType;
import jadx.api.plugins.input.data.attributes.types.AnnotationMethodParamsAttr;
import jadx.api.plugins.input.data.attributes.types.AnnotationsAttr;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
Expand Down Expand Up @@ -279,12 +280,27 @@ private void replaceConstInAnnotations(ClassNode cls) {
if (cls.root().getArgs().isReplaceConsts()) {
replaceConstsInAnnotationForAttrNode(cls, cls);
cls.getFields().forEach(f -> replaceConstsInAnnotationForAttrNode(cls, f));
cls.getMethods().forEach(m -> replaceConstsInAnnotationForAttrNode(cls, m));
cls.getMethods().forEach((m) -> {
replaceConstsInAnnotationForAttrNode(cls, m);
replaceConstsInAnnotationForMethodParamsAttr(cls, m);
});
}
}

private void replaceConstsInAnnotationForMethodParamsAttr(ClassNode cls, MethodNode m) {
AnnotationMethodParamsAttr paramsAnnotation = m.get(JadxAttrType.ANNOTATION_MTH_PARAMETERS);
if (paramsAnnotation == null) {
return;
}
paramsAnnotation.getParamList().forEach(annotationsList -> replaceConstsInAnnotationsAttr(cls, annotationsList));
}

private void replaceConstsInAnnotationForAttrNode(ClassNode parentCls, AttrNode attrNode) {
AnnotationsAttr annotationsList = attrNode.get(JadxAttrType.ANNOTATION_LIST);
replaceConstsInAnnotationsAttr(parentCls, annotationsList);
}

private void replaceConstsInAnnotationsAttr(ClassNode parentCls, AnnotationsAttr annotationsList) {
if (annotationsList == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package jadx.tests.integration.android;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import org.junit.jupiter.api.Test;

import jadx.tests.api.IntegrationTest;

import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;

public class TestResConstReplace3 extends IntegrationTest {

@Retention(RetentionPolicy.RUNTIME)
public @interface UsesAndroidResource {
int value() default 0;
}

@UsesAndroidResource(17039370 /* android.R.string.ok */)
public static class TestCls {
public void test(@UsesAndroidResource(17039370 /* android.R.string.ok */) int i) {
}
}

@Test
public void test() {
disableCompilation();
assertThat(getClassNode(TestCls.class))
.code()
.containsOne("import android.R;")
.countString(2, "@TestResConstReplace3.UsesAndroidResource(R.string.ok)");
}
}
Loading