Search in sources :

Example 6 with EncodedValue

use of org.jf.dexlib2.iface.value.EncodedValue in project atlas by alibaba.

the class DexDiffer method compareFields.

/**
     * 比较二个dex中的类的字段差异性
     *
     * @param baseClassDef
     * @param newClassDef
     * @param classDiffInfo
     */
private boolean compareFields(DexBackedClassDef baseClassDef, DexBackedClassDef newClassDef, ClassDiffInfo classDiffInfo) throws PatchException {
    Map<String, DexBackedField> baseFieldMaps = Maps.newHashMap();
    for (DexBackedField backedField : baseClassDef.getFields()) {
        baseFieldMaps.put(ReferenceUtil.getFieldDescriptor(backedField), backedField);
    }
    for (DexBackedField newField : newClassDef.getFields()) {
        FieldDiffInfo fieldDiffInfo = new FieldDiffInfo();
        fieldDiffInfo.setBackedField(newField);
        String fieldDesc = ReferenceUtil.getFieldDescriptor(newField);
        DexBackedField baseField = baseFieldMaps.get(fieldDesc);
        if (null == baseField) {
            fieldDiffInfo.setType(DiffType.ADD);
            classDiffInfo.getModifyFields().add(fieldDiffInfo);
            continue;
        }
        // 首先判断字段的声明,包括access flag等是否一致
        // 判断初始化值
        EncodedValue baseInitaValue = baseField.getInitialValue();
        EncodedValue newInitaValue = newField.getInitialValue();
        if (!Objects.equal(baseInitaValue, newInitaValue)) {
            fieldDiffInfo.setType(DiffType.MODIFY);
            classDiffInfo.getModifyFields().add(fieldDiffInfo);
            baseFieldMaps.remove(fieldDesc);
            continue;
        }
        //注解
        Set<? extends DexBackedAnnotation> backedAnnotations = baseField.getAnnotations();
        if (!equalsImpl(backedAnnotations, newField.getAnnotations())) {
            fieldDiffInfo.setType(DiffType.MODIFY);
            classDiffInfo.getModifyFields().add(fieldDiffInfo);
            baseFieldMaps.remove(fieldDesc);
            continue;
        }
        //type
        if (!baseField.getType().equals(newField.getType())) {
            fieldDiffInfo.setType(DiffType.MODIFY);
            classDiffInfo.getModifyFields().add(fieldDiffInfo);
            baseFieldMaps.remove(fieldDesc);
            continue;
        }
        if (baseField.getAccessFlags() != newField.getAccessFlags()) {
            fieldDiffInfo.setType(DiffType.MODIFY);
            classDiffInfo.getModifyFields().add(fieldDiffInfo);
            baseFieldMaps.remove(fieldDesc);
            continue;
        }
        baseFieldMaps.remove(fieldDesc);
    }
    // 如果字段已经移除
    if (baseFieldMaps.size() > 0) {
        for (Map.Entry<String, DexBackedField> entry : baseFieldMaps.entrySet()) {
            FieldDiffInfo fieldDiffInfo = new FieldDiffInfo();
            fieldDiffInfo.setBackedField(entry.getValue());
            fieldDiffInfo.setType(DiffType.REMOVE);
            classDiffInfo.getModifyFields().add(fieldDiffInfo);
        }
    }
    if (classDiffInfo.getModifyFields().size() > 0) {
        return true;
    }
    return false;
}
Also used : EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with EncodedValue

use of org.jf.dexlib2.iface.value.EncodedValue in project atlas by alibaba.

the class ClassReIClassDef method reAnnotation.

@Override
protected Annotation reAnnotation(Annotation annotation) {
    String type = annotation.getType();
    boolean isArray = false;
    if (type.startsWith("[")) {
        isArray = true;
    }
    String newType = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(type)).className, isArray);
    Set<? extends AnnotationElement> sets = annotation.getElements();
    Set<ImmutableAnnotationElement> newAnnotationElement = new HashSet<ImmutableAnnotationElement>();
    for (AnnotationElement annotationElement : sets) {
        String name = annotationElement.getName();
        EncodedValue encodedValue = annotationElement.getValue();
        if (encodedValue instanceof ArrayEncodedValue) {
            List<EncodedValue> lists = new ArrayList<EncodedValue>();
            for (EncodedValue encodedValueSub : ((ArrayEncodedValue) encodedValue).getValue()) {
                if (encodedValueSub instanceof StringEncodedValue) {
                    String newValue = null;
                    boolean isArray1 = false;
                    String value = ((StringEncodedValue) encodedValueSub).getValue();
                    if (value.startsWith("[")) {
                        isArray1 = true;
                    }
                    if (basicValue.contains(value)) {
                        newValue = value;
                    } else if (value.startsWith("Ljava/util/") || value.startsWith("Ljava/lang/") || !value.endsWith(";")) {
                        newValue = value;
                    } else {
                        newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray1);
                    }
                    ImmutableStringEncodedValue immutableStringEncodedValue = new ImmutableStringEncodedValue(newValue);
                    lists.add(immutableStringEncodedValue);
                } else if (encodedValueSub instanceof TypeEncodedValue) {
                    String newValueSub = null;
                    String value = ((TypeEncodedValue) encodedValueSub).getValue();
                    boolean isArray2 = false;
                    if (value.startsWith("[")) {
                        isArray2 = true;
                    }
                    if (basicValue.contains(value)) {
                        newValueSub = value;
                    } else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
                        newValueSub = value;
                    } else {
                        newValueSub = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray2);
                    }
                    ImmutableTypeEncodedValue immutableTypeEncodedValue = new ImmutableTypeEncodedValue(newValueSub);
                    lists.add(immutableTypeEncodedValue);
                } else {
                    lists.add(encodedValue);
                }
            }
            ImmutableArrayEncodedValue immutableArrayEncodedValue = new ImmutableArrayEncodedValue(lists);
            ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableArrayEncodedValue);
            newAnnotationElement.add(immutableAnnotationElement);
        } else if (encodedValue instanceof StringEncodedValue) {
            String value = ((StringEncodedValue) encodedValue).getValue();
            String newValue = null;
            isArray = false;
            if (value.startsWith("[")) {
                isArray = true;
            }
            if (basicValue.contains(value)) {
                newValue = value;
            } else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
                newValue = value;
            } else {
                newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray);
            }
            ImmutableStringEncodedValue immutableStringEncodedValue = new ImmutableStringEncodedValue(newValue);
            ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableStringEncodedValue);
            newAnnotationElement.add(immutableAnnotationElement);
        } else if (encodedValue instanceof TypeEncodedValue) {
            String newValue = null;
            String value = ((TypeEncodedValue) encodedValue).getValue();
            boolean isArray2 = false;
            if (value.startsWith("[")) {
                isArray2 = true;
            }
            if (basicValue.contains(value)) {
                newValue = value;
            } else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
                newValue = value;
            } else {
                newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray2);
            }
            ImmutableTypeEncodedValue immutableTypeEncodedValue = new ImmutableTypeEncodedValue(newValue);
            ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableTypeEncodedValue);
            newAnnotationElement.add(immutableAnnotationElement);
        } else if (encodedValue instanceof MethodEncodedValue) {
            MethodReference methodReference = ((MethodEncodedValue) encodedValue).getValue();
            String returnType = methodReference.getReturnType();
            boolean isBasic = false;
            List<? extends CharSequence> paramTypes = methodReference.getParameterTypes();
            List<CharSequence> dalvikParamTypes = new ArrayList<CharSequence>();
            List<CharSequence> newParamTypes = new ArrayList<CharSequence>();
            for (CharSequence charSequence : paramTypes) {
                if (basicType.containsKey(charSequence.toString())) {
                    newParamTypes.add(charSequence);
                    dalvikParamTypes.add(basicType.get(charSequence.toString()));
                    continue;
                }
                dalvikParamTypes.add(DefineUtils.getDalvikClassName(charSequence.toString()) + (isArray ? "[]" : ""));
                newParamTypes.add(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(charSequence.toString())).className, isArray));
            }
            final ImmutableMethodReference immutableReference = new ImmutableMethodReference(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass())).className, false), classProcessor.methodProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass()), methodReference.getName(), isBasic ? basicType.get(methodReference.getReturnType()) : DefineUtils.getDalvikClassName(methodReference.getReturnType()) + (isArray ? "[]" : ""), StringUtils.join(dalvikParamTypes.toArray(), ",")).methodName, newParamTypes, isBasic ? returnType : DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getReturnType())).className, methodReference.getReturnType().startsWith("[")));
            ImmutableMethodEncodedValue immutableMethodEncodedValue = new ImmutableMethodEncodedValue(immutableReference);
            ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableMethodEncodedValue);
            newAnnotationElement.add(immutableAnnotationElement);
        } else {
            newAnnotationElement.add(ImmutableAnnotationElement.of(annotationElement));
        }
    }
    return new ImmutableAnnotation(annotation.getVisibility(), newType, newAnnotationElement);
}
Also used : ImmutableAnnotationElement(org.jf.dexlib2.immutable.ImmutableAnnotationElement) ImmutableTypeEncodedValue(org.jf.dexlib2.immutable.value.ImmutableTypeEncodedValue) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) ArrayList(java.util.ArrayList) MethodEncodedValue(org.jf.dexlib2.iface.value.MethodEncodedValue) ImmutableMethodEncodedValue(org.jf.dexlib2.immutable.value.ImmutableMethodEncodedValue) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) ImmutableArrayEncodedValue(org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue) ImmutableTypeEncodedValue(org.jf.dexlib2.immutable.value.ImmutableTypeEncodedValue) ImmutableMethodEncodedValue(org.jf.dexlib2.immutable.value.ImmutableMethodEncodedValue) ImmutableAnnotation(org.jf.dexlib2.immutable.ImmutableAnnotation) MethodEncodedValue(org.jf.dexlib2.iface.value.MethodEncodedValue) ImmutableTypeEncodedValue(org.jf.dexlib2.immutable.value.ImmutableTypeEncodedValue) StringEncodedValue(org.jf.dexlib2.iface.value.StringEncodedValue) ImmutableMethodEncodedValue(org.jf.dexlib2.immutable.value.ImmutableMethodEncodedValue) ImmutableStringEncodedValue(org.jf.dexlib2.immutable.value.ImmutableStringEncodedValue) EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) ImmutableArrayEncodedValue(org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue) ImmutableAnnotationElement(org.jf.dexlib2.immutable.ImmutableAnnotationElement) AnnotationElement(org.jf.dexlib2.iface.AnnotationElement) ImmutableStringEncodedValue(org.jf.dexlib2.immutable.value.ImmutableStringEncodedValue) StringEncodedValue(org.jf.dexlib2.iface.value.StringEncodedValue) ImmutableStringEncodedValue(org.jf.dexlib2.immutable.value.ImmutableStringEncodedValue) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableArrayEncodedValue(org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue) HashSet(java.util.HashSet)

Example 8 with EncodedValue

use of org.jf.dexlib2.iface.value.EncodedValue in project smali by JesusFreke.

the class ClassPool method intern.

public void intern(@Nonnull ClassDef classDef) {
    PoolClassDef poolClassDef = new PoolClassDef(classDef);
    PoolClassDef prev = internedItems.put(poolClassDef.getType(), poolClassDef);
    if (prev != null) {
        throw new ExceptionWithContext("Class %s has already been interned", poolClassDef.getType());
    }
    dexPool.typeSection.intern(poolClassDef.getType());
    dexPool.typeSection.internNullable(poolClassDef.getSuperclass());
    dexPool.typeListSection.intern(poolClassDef.getInterfaces());
    dexPool.stringSection.internNullable(poolClassDef.getSourceFile());
    HashSet<String> fields = new HashSet<String>();
    for (Field field : poolClassDef.getFields()) {
        String fieldDescriptor = ReferenceUtil.getShortFieldDescriptor(field);
        if (!fields.add(fieldDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for field %s->%s", poolClassDef.getType(), fieldDescriptor);
        }
        dexPool.fieldSection.intern(field);
        EncodedValue initialValue = field.getInitialValue();
        if (initialValue != null) {
            dexPool.internEncodedValue(initialValue);
        }
        dexPool.annotationSetSection.intern(field.getAnnotations());
    }
    HashSet<String> methods = new HashSet<String>();
    for (PoolMethod method : poolClassDef.getMethods()) {
        String methodDescriptor = ReferenceUtil.getMethodDescriptor(method, true);
        if (!methods.add(methodDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for method %s->%s", poolClassDef.getType(), methodDescriptor);
        }
        dexPool.methodSection.intern(method);
        internCode(method);
        internDebug(method);
        dexPool.annotationSetSection.intern(method.getAnnotations());
        for (MethodParameter parameter : method.getParameters()) {
            dexPool.annotationSetSection.intern(parameter.getAnnotations());
        }
    }
    dexPool.annotationSetSection.intern(poolClassDef.getAnnotations());
}
Also used : EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) ExceptionWithContext(org.jf.util.ExceptionWithContext)

Example 9 with EncodedValue

use of org.jf.dexlib2.iface.value.EncodedValue in project smali by JesusFreke.

the class BaseMethodParameter method getSignature.

@Nullable
@Override
public String getSignature() {
    Annotation signatureAnnotation = null;
    for (Annotation annotation : getAnnotations()) {
        if (annotation.getType().equals("Ldalvik/annotation/Signature;")) {
            signatureAnnotation = annotation;
            break;
        }
    }
    if (signatureAnnotation == null) {
        return null;
    }
    ArrayEncodedValue signatureValues = null;
    for (AnnotationElement annotationElement : signatureAnnotation.getElements()) {
        if (annotationElement.getName().equals("value")) {
            EncodedValue encodedValue = annotationElement.getValue();
            if (encodedValue.getValueType() != ValueType.ARRAY) {
                return null;
            }
            signatureValues = (ArrayEncodedValue) encodedValue;
            break;
        }
    }
    if (signatureValues == null) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    for (EncodedValue signatureValue : signatureValues.getValue()) {
        if (signatureValue.getValueType() != ValueType.STRING) {
            return null;
        }
        sb.append(((StringEncodedValue) signatureValue).getValue());
    }
    return sb.toString();
}
Also used : StringEncodedValue(org.jf.dexlib2.iface.value.StringEncodedValue) EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) AnnotationElement(org.jf.dexlib2.iface.AnnotationElement) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) Annotation(org.jf.dexlib2.iface.Annotation) Nullable(javax.annotation.Nullable)

Example 10 with EncodedValue

use of org.jf.dexlib2.iface.value.EncodedValue in project smali by JesusFreke.

the class BaseAnnotationEncodedValue method compareTo.

@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0)
        return res;
    AnnotationEncodedValue other = (AnnotationEncodedValue) o;
    res = getType().compareTo(other.getType());
    if (res != 0)
        return res;
    return CollectionUtils.compareAsSet(getElements(), other.getElements());
}
Also used : AnnotationEncodedValue(org.jf.dexlib2.iface.value.AnnotationEncodedValue)

Aggregations

EncodedValue (org.jf.dexlib2.iface.value.EncodedValue)11 AnnotationElement (org.jf.dexlib2.iface.AnnotationElement)5 ArrayEncodedValue (org.jf.dexlib2.iface.value.ArrayEncodedValue)5 TypeEncodedValue (org.jf.dexlib2.iface.value.TypeEncodedValue)4 Annotation (org.jf.dexlib2.iface.Annotation)3 MethodEncodedValue (org.jf.dexlib2.iface.value.MethodEncodedValue)3 StringEncodedValue (org.jf.dexlib2.iface.value.StringEncodedValue)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 DexBackedAnnotation (org.jf.dexlib2.dexbacked.DexBackedAnnotation)2 DexBackedAnnotationElement (org.jf.dexlib2.dexbacked.DexBackedAnnotationElement)2 DexBackedAnnotationEncodedValue (org.jf.dexlib2.dexbacked.value.DexBackedAnnotationEncodedValue)2 DexBackedArrayEncodedValue (org.jf.dexlib2.dexbacked.value.DexBackedArrayEncodedValue)2 DexBackedTypeEncodedValue (org.jf.dexlib2.dexbacked.value.DexBackedTypeEncodedValue)2 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)2 ImmutableAnnotation (org.jf.dexlib2.immutable.ImmutableAnnotation)2 ImmutableAnnotationElement (org.jf.dexlib2.immutable.ImmutableAnnotationElement)2 ImmutableMethodReference (org.jf.dexlib2.immutable.reference.ImmutableMethodReference)2 ImmutableArrayEncodedValue (org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue)2 ImmutableMethodEncodedValue (org.jf.dexlib2.immutable.value.ImmutableMethodEncodedValue)2