Search in sources :

Example 6 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class DexAnnotationsConvert method extractSignature.

@SuppressWarnings({ "unchecked", "ConstantConditions" })
private static String extractSignature(IAnnotation annotation) {
    List<EncodedValue> values = (List<EncodedValue>) annotation.getDefaultValue().getValue();
    if (values.size() == 1) {
        return (String) values.get(0).getValue();
    }
    StringBuilder sb = new StringBuilder();
    for (EncodedValue part : values) {
        sb.append((String) part.getValue());
    }
    return sb.toString();
}
Also used : EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class DexAnnotationsConvert method convertSystemAnnotations.

@SuppressWarnings("unchecked")
private static void convertSystemAnnotations(@Nullable String cls, List<IJadxAttribute> attributes, IAnnotation annotation) {
    switch(annotation.getAnnotationClass()) {
        case "Ldalvik/annotation/Signature;":
            attributes.add(new SignatureAttr(extractSignature(annotation)));
            break;
        case "Ldalvik/annotation/InnerClass;":
            try {
                String name = AnnotationsUtils.getValue(annotation, "name", EncodedType.ENCODED_STRING, null);
                int accFlags = AnnotationsUtils.getValue(annotation, "accessFlags", EncodedType.ENCODED_INT, 0);
                if (name != null || accFlags != 0) {
                    InnerClsInfo innerClsInfo = new InnerClsInfo(cls, null, name, accFlags);
                    attributes.add(new InnerClassesAttr(Collections.singletonMap(cls, innerClsInfo)));
                }
            } catch (Exception e) {
                LOG.warn("Failed to parse annotation: " + annotation, e);
            }
            break;
        case "Ldalvik/annotation/AnnotationDefault;":
            EncodedValue annValue = annotation.getDefaultValue();
            if (annValue != null && annValue.getType() == EncodedType.ENCODED_ANNOTATION) {
                IAnnotation defAnnotation = (IAnnotation) annValue.getValue();
                attributes.add(new AnnotationDefaultClassAttr(defAnnotation.getValues()));
            }
            break;
        case "Ldalvik/annotation/Throws;":
            try {
                EncodedValue defaultValue = annotation.getDefaultValue();
                if (defaultValue != null) {
                    List<String> excs = ((List<EncodedValue>) defaultValue.getValue()).stream().map(ev -> ((String) ev.getValue())).collect(Collectors.toList());
                    attributes.add(new ExceptionsAttr(excs));
                }
            } catch (Exception e) {
                LOG.warn("Failed to convert dalvik throws annotation", e);
            }
            break;
        case "Ldalvik/annotation/MethodParameters;":
            try {
                List<EncodedValue> names = AnnotationsUtils.getArray(annotation, "names");
                List<EncodedValue> accFlags = AnnotationsUtils.getArray(annotation, "accessFlags");
                if (!names.isEmpty() && names.size() == accFlags.size()) {
                    int size = names.size();
                    List<MethodParametersAttr.Info> list = new ArrayList<>(size);
                    for (int i = 0; i < size; i++) {
                        String name = (String) names.get(i).getValue();
                        int accFlag = (int) accFlags.get(i).getValue();
                        list.add(new MethodParametersAttr.Info(accFlag, name));
                    }
                    attributes.add(new MethodParametersAttr(list));
                }
            } catch (Exception e) {
                LOG.warn("Failed to parse annotation: " + annotation, e);
            }
            break;
    }
}
Also used : IAnnotation(jadx.api.plugins.input.data.annotations.IAnnotation) ExceptionsAttr(jadx.api.plugins.input.data.attributes.types.ExceptionsAttr) InnerClassesAttr(jadx.api.plugins.input.data.attributes.types.InnerClassesAttr) Logger(org.slf4j.Logger) AnnotationsAttr(jadx.api.plugins.input.data.attributes.types.AnnotationsAttr) SignatureAttr(jadx.api.plugins.input.data.attributes.types.SignatureAttr) LoggerFactory(org.slf4j.LoggerFactory) AnnotationsUtils(jadx.plugins.input.dex.sections.annotations.AnnotationsUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable) EncodedType(jadx.api.plugins.input.data.annotations.EncodedType) InnerClsInfo(jadx.api.plugins.input.data.attributes.types.InnerClsInfo) List(java.util.List) AnnotationVisibility(jadx.api.plugins.input.data.annotations.AnnotationVisibility) IJadxAttribute(jadx.api.plugins.input.data.attributes.IJadxAttribute) MethodParametersAttr(jadx.api.plugins.input.data.attributes.types.MethodParametersAttr) Utils(jadx.api.plugins.utils.Utils) AnnotationDefaultClassAttr(jadx.api.plugins.input.data.attributes.types.AnnotationDefaultClassAttr) Collections(java.util.Collections) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) IAnnotation(jadx.api.plugins.input.data.annotations.IAnnotation) InnerClsInfo(jadx.api.plugins.input.data.attributes.types.InnerClsInfo) ArrayList(java.util.ArrayList) AnnotationDefaultClassAttr(jadx.api.plugins.input.data.attributes.types.AnnotationDefaultClassAttr) InnerClsInfo(jadx.api.plugins.input.data.attributes.types.InnerClsInfo) ExceptionsAttr(jadx.api.plugins.input.data.attributes.types.ExceptionsAttr) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) MethodParametersAttr(jadx.api.plugins.input.data.attributes.types.MethodParametersAttr) SignatureAttr(jadx.api.plugins.input.data.attributes.types.SignatureAttr) ArrayList(java.util.ArrayList) List(java.util.List) InnerClassesAttr(jadx.api.plugins.input.data.attributes.types.InnerClassesAttr)

Example 8 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class InvokeCustomBuilder method build.

public static InsnNode build(MethodNode mth, InsnData insn, boolean isRange) {
    try {
        ICallSite callSite = InsnDataUtils.getCallSite(insn);
        if (callSite == null) {
            throw new JadxRuntimeException("Failed to get call site for insn: " + insn);
        }
        callSite.load();
        List<EncodedValue> values = callSite.getValues();
        if (CustomLambdaCall.isLambdaInvoke(values)) {
            return CustomLambdaCall.buildLambdaMethodCall(mth, insn, isRange, values);
        }
        if (CustomStringConcat.isStringConcat(values)) {
            return CustomStringConcat.buildStringConcat(insn, isRange, values);
        }
        // TODO: output raw dynamic call
        throw new JadxRuntimeException("Failed to process invoke-custom instruction: " + callSite);
    } catch (Exception e) {
        throw new JadxRuntimeException("'invoke-custom' instruction processing error: " + e.getMessage(), e);
    }
}
Also used : EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) ICallSite(jadx.api.plugins.input.data.ICallSite) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Example 9 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class KotlinMetadataUtils method getClassAlias.

/**
 * Try to get class info from Kotlin Metadata annotation
 */
@Nullable
public static ClsAliasPair getClassAlias(ClassNode cls) {
    IAnnotation metadataAnnotation = cls.getAnnotation(KOTLIN_METADATA_ANNOTATION);
    List<EncodedValue> d2Param = getParamAsList(metadataAnnotation, KOTLIN_METADATA_D2_PARAMETER);
    if (d2Param == null || d2Param.isEmpty()) {
        return null;
    }
    EncodedValue firstValue = d2Param.get(0);
    if (firstValue == null || firstValue.getType() != EncodedType.ENCODED_STRING) {
        return null;
    }
    try {
        String rawClassName = ((String) firstValue.getValue()).trim();
        if (rawClassName.isEmpty()) {
            return null;
        }
        String clsName = Utils.cleanObjectName(rawClassName);
        ClsAliasPair alias = splitAndCheckClsName(cls, clsName);
        if (alias != null) {
            RenameReasonAttr.forNode(cls).append("from Kotlin metadata");
            return alias;
        }
    } catch (Exception e) {
        LOG.error("Failed to parse kotlin metadata", e);
    }
    return null;
}
Also used : IAnnotation(jadx.api.plugins.input.data.annotations.IAnnotation) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) ClsAliasPair(jadx.core.deobf.ClsAliasPair) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class TestRFieldRestore method test.

@Test
public void test() {
    // unknown R class
    disableCompilation();
    Map<Integer, String> map = new HashMap<>();
    int buttonConstValue = 2131230730;
    map.put(buttonConstValue, "id.Button");
    setResMap(map);
    ClassNode cls = getClassNode(TestCls.class);
    String code = cls.getCode().toString();
    assertThat(code, containsOne("return R.id.Button;"));
    assertThat(code, not(containsString("import R;")));
    // check 'R' class
    ClassNode rCls = cls.root().searchClassByFullAlias("R");
    assertThat(rCls, notNullValue());
    // check inner 'id' class
    List<ClassNode> innerClasses = rCls.getInnerClasses();
    assertThat(innerClasses, hasSize(1));
    ClassNode idCls = innerClasses.get(0);
    assertThat(idCls.getShortName(), is("id"));
    // check 'Button' field
    FieldNode buttonField = idCls.searchFieldByName("Button");
    assertThat(buttonField, notNullValue());
    EncodedValue constVal = buttonField.get(JadxAttrType.CONSTANT_VALUE);
    Integer buttonValue = (Integer) constVal.getValue();
    assertThat(buttonValue, is(buttonConstValue));
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) FieldNode(jadx.core.dex.nodes.FieldNode) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test) IntegrationTest(jadx.tests.api.IntegrationTest)

Aggregations

EncodedValue (jadx.api.plugins.input.data.annotations.EncodedValue)17 FieldInfo (jadx.core.dex.info.FieldInfo)4 FieldNode (jadx.core.dex.nodes.FieldNode)4 ArrayList (java.util.ArrayList)4 IAnnotation (jadx.api.plugins.input.data.annotations.IAnnotation)3 ClassNode (jadx.core.dex.nodes.ClassNode)3 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 ICallSite (jadx.api.plugins.input.data.ICallSite)2 IFieldRef (jadx.api.plugins.input.data.IFieldRef)2 AnnotationVisibility (jadx.api.plugins.input.data.annotations.AnnotationVisibility)2 JadxAnnotation (jadx.api.plugins.input.data.annotations.JadxAnnotation)2 AnnotationsAttr (jadx.api.plugins.input.data.attributes.types.AnnotationsAttr)2 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)2 ConstPoolReader (jadx.plugins.input.java.data.ConstPoolReader)2 JavaClassParseException (jadx.plugins.input.java.utils.JavaClassParseException)2 LinkedHashMap (java.util.LinkedHashMap)2 EncodedType (jadx.api.plugins.input.data.annotations.EncodedType)1 IJadxAttribute (jadx.api.plugins.input.data.attributes.IJadxAttribute)1 AnnotationDefaultClassAttr (jadx.api.plugins.input.data.attributes.types.AnnotationDefaultClassAttr)1