提交 26bad4a1 编写于 作者: S Skylot

fix: replace constants for arrays in annotations (#831)

上级 fa0a38d3
......@@ -200,13 +200,25 @@ public class ModVisitor extends AbstractVisitor {
continue;
}
for (Map.Entry<String, Object> entry : annotation.getValues().entrySet()) {
Object value = entry.getValue();
FieldNode constField = parentCls.getConstField(value);
if (constField != null) {
entry.setValue(constField.getFieldInfo());
}
entry.setValue(replaceConstValue(parentCls, entry.getValue()));
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object replaceConstValue(ClassNode parentCls, @Nullable Object value) {
if (value instanceof List) {
List listVal = (List) value;
if (!listVal.isEmpty()) {
listVal.replaceAll(v -> replaceConstValue(parentCls, v));
}
return listVal;
}
FieldNode constField = parentCls.getConstField(value);
if (constField != null) {
return constField.getFieldInfo();
}
return value;
}
private static void replaceConst(MethodNode mth, ClassNode parentClass, BlockNode block, int i, InsnNode insn) {
......
package jadx.tests.integration.inner;
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 TestReplaceConstsInAnnotations2 extends IntegrationTest {
public static class TestCls {
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface A {
int[] value();
}
@A(C.INT_CONST)
public static class C {
public static final int INT_CONST = 23412342;
}
@A({ C.INT_CONST, C2.INT_CONST })
public static class C2 {
public static final int INT_CONST = 34563456;
}
}
@Test
public void test() {
assertThat(getClassNode(TestCls.class))
.code()
// .containsOne("@A(C.INT_CONST)") // TODO: remove brackets for single element
.containsOne("@A({C.INT_CONST}")
.containsOne("@A({C.INT_CONST, C2.INT_CONST})")
.containsOne("23412342")
.containsOne("34563456");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册