Search in sources :

Example 16 with IndentingWriter

use of org.jf.util.IndentingWriter in project atlas by alibaba.

the class ClassDefinition method writeDirectMethods.

private Set<String> writeDirectMethods(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();
    Iterable<? extends Method> directMethods;
    Set<? extends Method> modifieds = null;
    if (classDef instanceof DexBackedClassDef) {
        directMethods = ((DexBackedClassDef) classDef).getDirectMethods(false);
        modifieds = (Set<? extends Method>) DexDiffInfo.modifiedMethods;
    } else {
        directMethods = classDef.getDirectMethods();
    }
    MethodReplaceAnnotation replaceAnnotaion;
    for (Method method : directMethods) {
        if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
            if (!modifieds.contains(method) && !DexDiffInfo.addedMethods.contains(method)) {
                continue;
            }
        }
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# direct methods");
            wroteHeader = true;
        }
        writer.write('\n');
        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getMethodDescriptor(method, true);
        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(writer);
        }
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.setFullMethod(fullMethod);
            methodDefinition.writeTo(methodWriter);
        }
    }
    return writtenMethods;
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) Method(org.jf.dexlib2.iface.Method) MethodReplaceAnnotation(com.taobao.android.apatch.annotation.MethodReplaceAnnotation) IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) HashSet(java.util.HashSet)

Example 17 with IndentingWriter

use of org.jf.util.IndentingWriter in project atlas by alibaba.

the class ClassDefinition method writeVirtualMethods.

private void writeVirtualMethods(IndentingWriter writer, Set<String> directMethods) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();
    Iterable<? extends Method> virtualMethods;
    Set<? extends Method> modifieds = null;
    if (classDef instanceof DexBackedClassDef) {
        virtualMethods = ((DexBackedClassDef) classDef).getVirtualMethods(false);
        modifieds = (Set<? extends Method>) DexDiffInfo.modifiedMethods;
    } else {
        virtualMethods = classDef.getVirtualMethods();
    }
    MethodReplaceAnnotation replaceAnnotaion;
    for (Method method : virtualMethods) {
        if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
            if (!modifieds.contains(method) && !DexDiffInfo.addedMethods.contains(method)) {
                continue;
            }
        }
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# virtual methods");
            wroteHeader = true;
        }
        writer.write('\n');
        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getMethodDescriptor(method, true);
        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(writer);
        } else if (directMethods.contains(methodString)) {
            writer.write("# There is both a direct and virtual method with this signature.\n" + "# You will need to rename one of these methods, including all references.\n");
            System.err.println(String.format("Duplicate direct+virtual method found: %s->%s", classDef.getType(), methodString));
            System.err.println("You will need to rename one of these methods, including all references.");
        }
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) Method(org.jf.dexlib2.iface.Method) MethodReplaceAnnotation(com.taobao.android.apatch.annotation.MethodReplaceAnnotation) IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) HashSet(java.util.HashSet)

Example 18 with IndentingWriter

use of org.jf.util.IndentingWriter in project smali by JesusFreke.

the class AnalysisTest method runTest.

public void runTest(String test, boolean registerInfo, boolean isArt) throws IOException, URISyntaxException {
    String dexFilePath = String.format("%s%sclasses.dex", test, File.separatorChar);
    DexFile dexFile = DexFileFactory.loadDexFile(findResource(dexFilePath), Opcodes.getDefault());
    BaksmaliOptions options = new BaksmaliOptions();
    if (registerInfo) {
        options.registerInfo = BaksmaliOptions.ALL | BaksmaliOptions.FULLMERGE;
        if (isArt) {
            options.classPath = new ClassPath(new ArrayList<ClassProvider>(), true, 56);
        } else {
            options.classPath = new ClassPath();
        }
    }
    options.implicitReferences = false;
    for (ClassDef classDef : dexFile.getClasses()) {
        StringWriter stringWriter = new StringWriter();
        IndentingWriter writer = new IndentingWriter(stringWriter);
        ClassDefinition classDefinition = new ClassDefinition(options, classDef);
        classDefinition.writeTo(writer);
        writer.close();
        String className = classDef.getType();
        String smaliPath = String.format("%s%s%s.smali", test, File.separatorChar, className.substring(1, className.length() - 1));
        String smaliContents = readResource(smaliPath);
        Assert.assertEquals(TextUtils.normalizeWhitespace(smaliContents), TextUtils.normalizeWhitespace((stringWriter.toString())));
    }
}
Also used : ClassPath(org.jf.dexlib2.analysis.ClassPath) ClassDef(org.jf.dexlib2.iface.ClassDef) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) IndentingWriter(org.jf.util.IndentingWriter) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) DexFile(org.jf.dexlib2.iface.DexFile)

Example 19 with IndentingWriter

use of org.jf.util.IndentingWriter in project smali by JesusFreke.

the class ClassDefinition method writeStaticFields.

private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
    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 : IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef)

Example 20 with IndentingWriter

use of org.jf.util.IndentingWriter in project otertool by wuntee.

the class SmaliWorkshop method getSmaliSource.

public static Map<String, File> getSmaliSource(File sourceSmaliOrDexFile, File destinationDirectory) throws IOException {
    Map<String, File> ret = new HashMap<String, File>();
    DexFile dexFile = new DexFile(sourceSmaliOrDexFile);
    IndentingWriter idWriter;
    for (ClassDefItem c : dexFile.ClassDefsSection.getItems()) {
        File classFile = SmaliWorkshop.createSmaliClassFile(destinationDirectory, c);
        String className = SmaliWorkshop.classDefItemToFilename(c);
        logger.debug("Got class: " + className + " [" + classFile + "]");
        BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(classFile)));
        idWriter = new IndentingWriter(fileWriter);
        ClassDefinition cd = new ClassDefinition(c);
        cd.writeTo(idWriter);
        ret.put(className, classFile);
        idWriter.close();
    }
    return (sortMapByKey(ret));
}
Also used : ClassDefItem(org.jf.dexlib.ClassDefItem) HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) IndentingWriter(org.jf.util.IndentingWriter) OutputStreamWriter(java.io.OutputStreamWriter) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) File(java.io.File) DexFile(org.jf.dexlib.DexFile) DexFile(org.jf.dexlib.DexFile) BufferedWriter(java.io.BufferedWriter)

Aggregations

IndentingWriter (org.jf.util.IndentingWriter)20 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)8 IOException (java.io.IOException)5 ClassDefinition (org.jf.baksmali.Adaptors.ClassDefinition)5 HashSet (java.util.HashSet)4 AnalyzedInstruction (org.jf.dexlib2.analysis.AnalyzedInstruction)4 StringWriter (java.io.StringWriter)3 DexFile (org.jf.dexlib2.iface.DexFile)3 MethodReplaceAnnotation (com.taobao.android.apatch.annotation.MethodReplaceAnnotation)2 ClassDefinition (com.taobao.android.baksmali.adaptors.ClassDefinition)2 DebugMethodItem (com.taobao.android.baksmali.adaptors.Debug.DebugMethodItem)2 EndPrologueMethodItem (com.taobao.android.baksmali.adaptors.Debug.EndPrologueMethodItem)2 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 DebugMethodItem (org.jf.baksmali.Adaptors.Debug.DebugMethodItem)2 Opcode (org.jf.dexlib2.Opcode)2 AnalysisException (org.jf.dexlib2.analysis.AnalysisException)2 MethodAnalyzer (org.jf.dexlib2.analysis.MethodAnalyzer)2