Search in sources :

Example 6 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.

the class ClassDefinition method writeInstanceFields.

private void writeInstanceFields(IndentingWriter writer, Set<String> staticFields) throws IOException {
    if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
        return;
    }
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();
    Iterable<? extends Field> instanceFields;
    if (classDef instanceof DexBackedClassDef) {
        instanceFields = ((DexBackedClassDef) classDef).getInstanceFields(false);
    } else {
        instanceFields = classDef.getInstanceFields();
    }
    for (Field field : instanceFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# instance fields");
            wroteHeader = true;
        }
        writer.write('\n');
        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
        } else if (staticFields.contains(fieldString)) {
            System.err.println(String.format("Duplicate static+instance field found: %s->%s", classDef.getType(), fieldString));
            System.err.println("You will need to rename one of these fields, including all references.");
            writer.write("# There is both a static and instance field with this signature.\n" + "# You will need to rename one of these fields, including all references.\n");
        }
        FieldDefinition.writeTo(options, fieldWriter, field, false);
    }
}
Also used : Field(org.jf.dexlib2.iface.Field) IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) HashSet(java.util.HashSet)

Example 7 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.

the class ClassDefinition method writeStaticFields.

private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
    if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
        return null;
    }
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();
    Iterable<? extends Field> staticFields;
    if (classDef instanceof DexBackedClassDef) {
        staticFields = ((DexBackedClassDef) classDef).getStaticFields(false);
    } else {
        staticFields = classDef.getStaticFields();
    }
    for (Field field : staticFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# static fields");
            wroteHeader = true;
        }
        writer.write('\n');
        boolean setInStaticConstructor;
        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
            setInStaticConstructor = false;
        } else {
            setInStaticConstructor = fieldsSetInStaticConstructor.contains(fieldString);
        }
        FieldDefinition.writeTo(options, fieldWriter, field, setInStaticConstructor);
    }
    return writtenFields;
}
Also used : Field(org.jf.dexlib2.iface.Field) IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) HashSet(java.util.HashSet)

Example 8 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef 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 9 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.

the class SmaliDiffUtils method scanClasses.

public static Set<DexBackedClassDef> scanClasses(File smaliDir, List<File> newFiles) throws PatchException {
    Set<DexBackedClassDef> classes = Sets.newHashSet();
    try {
        for (File newFile : newFiles) {
            DexBackedDexFile newDexFile = DexFileFactory.loadDexFile(newFile, 19, true);
            Set<? extends DexBackedClassDef> dexClasses = newDexFile.getClasses();
            classes.addAll(dexClasses);
        }
        final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
        final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
        for (DexBackedClassDef classDef : classes) {
            String className = classDef.getType();
            ApkPatch.currentClassType = null;
            AfBakSmali.disassembleClass(classDef, outFileNameHandler, getBuildOption(classes, 19), true, true);
            File smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
        }
    } catch (Exception e) {
        throw new PatchException(e);
    }
    return classes;
}
Also used : DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) ClassFileNameHandler(org.jf.util.ClassFileNameHandler) PatchException(com.taobao.android.differ.dex.PatchException) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) File(java.io.File) RecognitionException(org.antlr.runtime.RecognitionException) PatchException(com.taobao.android.differ.dex.PatchException) IOException(java.io.IOException)

Example 10 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project dex2jar by pxb1988.

the class SmaliTest method baksmali.

private static String baksmali(DexBackedClassDef def) throws IOException {
    baksmaliOptions opts = new baksmaliOptions();
    opts.outputDebugInfo = false;
    opts.syntheticAccessorResolver = new SyntheticAccessorResolver(Collections.EMPTY_LIST);
    ClassDefinition classDefinition = new ClassDefinition(opts, def);
    StringWriter bufWriter = new StringWriter();
    IndentingWriter writer = new IndentingWriter(bufWriter);
    classDefinition.writeTo((IndentingWriter) writer);
    writer.flush();
    return bufWriter.toString();
}
Also used : org.jf.baksmali.baksmaliOptions(org.jf.baksmali.baksmaliOptions) IndentingWriter(org.jf.util.IndentingWriter) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver)

Aggregations

DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)19 IndentingWriter (org.jf.util.IndentingWriter)9 HashSet (java.util.HashSet)7 File (java.io.File)6 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)6 ClassFileNameHandler (org.jf.util.ClassFileNameHandler)4 IOException (java.io.IOException)3 DexBackedMethod (org.jf.dexlib2.dexbacked.DexBackedMethod)3 Method (org.jf.dexlib2.iface.Method)3 MethodReplaceAnnotation (com.taobao.android.apatch.annotation.MethodReplaceAnnotation)2 PatchException (com.taobao.android.differ.dex.PatchException)2 Set (java.util.Set)2 Nonnull (javax.annotation.Nonnull)2 NotNull (org.jetbrains.annotations.NotNull)2 Field (org.jf.dexlib2.iface.Field)2 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)2 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)2 EncodedValue (org.jf.dexlib2.iface.value.EncodedValue)2 DexClassNode (com.googlecode.d2j.node.DexClassNode)1 AndFixFilterImpl (com.taobao.android.apatch.AndFixFilterImpl)1