Search in sources :

Example 1 with IAnnotation

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

the class AnnotationGen method add.

private void add(IAttributeNode node, ICodeWriter code) {
    AnnotationsAttr aList = node.get(JadxAttrType.ANNOTATION_LIST);
    if (aList == null || aList.isEmpty()) {
        return;
    }
    for (IAnnotation a : aList.getAll()) {
        String aCls = a.getAnnotationClass();
        if (!aCls.equals(Consts.OVERRIDE_ANNOTATION)) {
            code.startLine();
            formatAnnotation(code, a);
        }
    }
}
Also used : AnnotationsAttr(jadx.api.plugins.input.data.attributes.types.AnnotationsAttr) IAnnotation(jadx.api.plugins.input.data.annotations.IAnnotation)

Example 2 with IAnnotation

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

the class AnnotationGen method addForParameter.

public void addForParameter(ICodeWriter code, AnnotationMethodParamsAttr paramsAnnotations, int n) {
    List<AnnotationsAttr> paramList = paramsAnnotations.getParamList();
    if (n >= paramList.size()) {
        return;
    }
    AnnotationsAttr aList = paramList.get(n);
    if (aList == null || aList.isEmpty()) {
        return;
    }
    for (IAnnotation a : aList.getAll()) {
        formatAnnotation(code, a);
        code.add(' ');
    }
}
Also used : AnnotationsAttr(jadx.api.plugins.input.data.attributes.types.AnnotationsAttr) IAnnotation(jadx.api.plugins.input.data.annotations.IAnnotation)

Example 3 with IAnnotation

use of jadx.api.plugins.input.data.annotations.IAnnotation 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 4 with IAnnotation

use of jadx.api.plugins.input.data.annotations.IAnnotation 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 5 with IAnnotation

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

the class Smali method writeClass.

private void writeClass(SmaliWriter smali, ClassNode cls) {
    IClassData clsData = cls.getClsData();
    if (clsData == null) {
        smali.startLine(String.format("###### Class %s is created by jadx", cls.getFullName()));
        return;
    }
    AttributeStorage attributes = new AttributeStorage();
    attributes.add(clsData.getAttributes());
    smali.startLine("Class: " + clsData.getType()).startLine("AccessFlags: " + AccessFlags.format(clsData.getAccessFlags(), AccessFlagsScope.CLASS)).startLine("SuperType: " + clsData.getSuperType()).startLine("Interfaces: " + clsData.getInterfacesTypes()).startLine("SourceFile: " + attributes.get(JadxAttrType.SOURCE_FILE));
    AnnotationsAttr annotationsAttr = attributes.get(JadxAttrType.ANNOTATION_LIST);
    if (annotationsAttr != null) {
        Collection<IAnnotation> annos = annotationsAttr.getList();
        if (!annos.isEmpty()) {
            smali.startLine(String.format("# %d annotations", annos.size()));
            writeAnnotations(smali, new ArrayList<>(annos));
            smali.startLine();
        }
    }
    List<RawField> fields = new ArrayList<>();
    // first is access flag, second is name
    int[] colWidths = new int[] { 0, 0 };
    int[] mthIndex = new int[] { 0 };
    LineInfo line = new LineInfo();
    clsData.visitFieldsAndMethods(f -> {
        RawField fld = RawField.make(f);
        fields.add(fld);
        if (fld.accessFlag.length() > colWidths[0]) {
            colWidths[0] = fld.accessFlag.length();
        }
        if (fld.name.length() > colWidths[1]) {
            colWidths[1] = fld.name.length();
        }
    }, m -> {
        if (!fields.isEmpty()) {
            writeFields(smali, clsData, fields, colWidths);
            fields.clear();
        }
        writeMethod(smali, cls.getMethods().get(mthIndex[0]++), m, line);
        line.reset();
    });
    if (!fields.isEmpty()) {
        // in case there's no methods.
        writeFields(smali, clsData, fields, colWidths);
    }
    for (ClassNode innerClass : cls.getInnerClasses()) {
        writeClass(smali, innerClass);
    }
}
Also used : AnnotationsAttr(jadx.api.plugins.input.data.attributes.types.AnnotationsAttr) IAnnotation(jadx.api.plugins.input.data.annotations.IAnnotation) ClassNode(jadx.core.dex.nodes.ClassNode) AttributeStorage(jadx.core.dex.attributes.AttributeStorage) ArrayList(java.util.ArrayList) IClassData(jadx.api.plugins.input.data.IClassData)

Aggregations

IAnnotation (jadx.api.plugins.input.data.annotations.IAnnotation)6 AnnotationsAttr (jadx.api.plugins.input.data.attributes.types.AnnotationsAttr)4 EncodedValue (jadx.api.plugins.input.data.annotations.EncodedValue)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Nullable (org.jetbrains.annotations.Nullable)2 IClassData (jadx.api.plugins.input.data.IClassData)1 IFieldRef (jadx.api.plugins.input.data.IFieldRef)1 AnnotationVisibility (jadx.api.plugins.input.data.annotations.AnnotationVisibility)1 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 ExceptionsAttr (jadx.api.plugins.input.data.attributes.types.ExceptionsAttr)1 InnerClassesAttr (jadx.api.plugins.input.data.attributes.types.InnerClassesAttr)1 InnerClsInfo (jadx.api.plugins.input.data.attributes.types.InnerClsInfo)1 MethodParametersAttr (jadx.api.plugins.input.data.attributes.types.MethodParametersAttr)1 SignatureAttr (jadx.api.plugins.input.data.attributes.types.SignatureAttr)1 Utils (jadx.api.plugins.utils.Utils)1 ClsAliasPair (jadx.core.deobf.ClsAliasPair)1 AttributeStorage (jadx.core.dex.attributes.AttributeStorage)1