Search in sources :

Example 1 with ClassDefinition

use of com.taobao.android.baksmali.adaptors.ClassDefinition in project atlas by alibaba.

the class BakSmali method disassembleClass.

public static boolean disassembleClass(ClassDef classDef, ClassFileNameHandler fileNameHandler, baksmaliOptions options) {
    /**
         * The path for the disassembly file is based on the package name
         * The class descriptor will look something like:
         * Ljava/lang/Object;
         * Where the there is leading 'L' and a trailing ';', and the parts of the
         * package name are separated by '/'
         */
    String classDescriptor = classDef.getType();
    //validate that the descriptor is formatted like we expect
    if (classDescriptor.charAt(0) != 'L' || classDescriptor.charAt(classDescriptor.length() - 1) != ';') {
        System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class");
        return false;
    }
    File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor);
    //create and initialize the top level string template
    ClassDefinition classDefinition = new ClassDefinition(options, classDef);
    //write the disassembly
    Writer writer = null;
    try {
        File smaliParent = smaliFile.getParentFile();
        if (!smaliParent.exists()) {
            if (!smaliParent.mkdirs()) {
                // check again, it's likely it was created in a different thread
                if (!smaliParent.exists()) {
                    System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class");
                    return false;
                }
            }
        }
        if (!smaliFile.exists()) {
            if (!smaliFile.createNewFile()) {
                System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class");
                return false;
            }
        }
        BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(smaliFile), "UTF8"));
        writer = new IndentingWriter(bufWriter);
        classDefinition.writeTo((IndentingWriter) writer);
    } catch (Exception ex) {
        System.err.println("\n\nError occurred while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class");
        ex.printStackTrace();
        // noinspection ResultOfMethodCallIgnored
        smaliFile.delete();
        return false;
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Throwable ex) {
                System.err.println("\n\nError occurred while closing file " + smaliFile.toString());
                ex.printStackTrace();
            }
        }
    }
    return true;
}
Also used : IndentingWriter(org.jf.util.IndentingWriter) ClassDefinition(com.taobao.android.baksmali.adaptors.ClassDefinition) DexFile(org.jf.dexlib2.iface.DexFile) IndentingWriter(org.jf.util.IndentingWriter) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 2 with ClassDefinition

use of com.taobao.android.baksmali.adaptors.ClassDefinition in project atlas by alibaba.

the class AfBakSmali method disassembleClass.

/**
     * 生成apatch的smali文件
     * @param classDef
     * @param fileNameHandler
     * @param options
     * @param isScan
     * @param fullMethod
     * @return
     */
public static boolean disassembleClass(ClassDef classDef, ClassFileNameHandler fileNameHandler, baksmaliOptions options, boolean isScan, boolean fullMethod) {
    /**
         * The path for the disassembly file is based on the package name
         * The class descriptor will look something like:
         * Ljava/lang/Object;
         * Where the there is leading 'L' and a trailing ';', and the parts of the
         * package name are separated by '/'
         */
    String classDescriptor = TypeGenUtil.newType(classDef.getType());
    //validate that the descriptor is formatted like we expect
    if (classDescriptor.charAt(0) != 'L' || classDescriptor.charAt(classDescriptor.length() - 1) != ';') {
        System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class");
        return false;
    }
    File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor);
    //create and initialize the top level string template
    ClassDefinition classDefinition = new ClassDefinition(options, classDef, isScan, fullMethod);
    //write the disassembly
    Writer writer = null;
    try {
        File smaliParent = smaliFile.getParentFile();
        if (!smaliParent.exists()) {
            if (!smaliParent.mkdirs()) {
                // check again, it's likely it was created in a different thread
                if (!smaliParent.exists()) {
                    System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class");
                    return false;
                }
            }
        }
        if (!smaliFile.exists()) {
            if (!smaliFile.createNewFile()) {
                System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class");
                return false;
            }
        }
        BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(smaliFile), "UTF8"));
        writer = new IndentingWriter(bufWriter);
        classDefinition.writeTo((IndentingWriter) writer);
    } catch (Exception ex) {
        System.err.println("\n\nError occurred while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class");
        ex.printStackTrace();
        // noinspection ResultOfMethodCallIgnored
        smaliFile.delete();
        return false;
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Throwable ex) {
                System.err.println("\n\nError occurred while closing file " + smaliFile.toString());
                ex.printStackTrace();
            }
        }
    }
    return true;
}
Also used : FileOutputStream(java.io.FileOutputStream) IndentingWriter(org.jf.util.IndentingWriter) OutputStreamWriter(java.io.OutputStreamWriter) ClassDefinition(com.taobao.android.baksmali.adaptors.ClassDefinition) File(java.io.File) IndentingWriter(org.jf.util.IndentingWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 3 with ClassDefinition

use of com.taobao.android.baksmali.adaptors.ClassDefinition in project atlas by alibaba.

the class AfBakSmali method disassembleClass.

/**
 * 生成apatch的smali文件
 * @param classDef
 * @param fileNameHandler
 * @param options
 * @param isScan
 * @param fullMethod
 * @return
 */
public static boolean disassembleClass(ClassDef classDef, ClassFileNameHandler fileNameHandler, BaksmaliOptions options, boolean isScan, boolean fullMethod) {
    /**
     * The path for the disassembly file is based on the package name
     * The class descriptor will look something like:
     * Ljava/lang/Object;
     * Where the there is leading 'L' and a trailing ';', and the parts of the
     * package name are separated by '/'
     */
    String classDescriptor = TypeGenUtil.newType(classDef.getType());
    // validate that the descriptor is formatted like we expect
    if (classDescriptor.charAt(0) != 'L' || classDescriptor.charAt(classDescriptor.length() - 1) != ';') {
        System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class");
        return false;
    }
    File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor);
    // create and initialize the top level string template
    ClassDefinition classDefinition = new ClassDefinition(options, classDef, isScan, fullMethod);
    // write the disassembly
    Writer writer = null;
    try {
        File smaliParent = smaliFile.getParentFile();
        if (!smaliParent.exists()) {
            if (!smaliParent.mkdirs()) {
                // check again, it's likely it was created in a different thread
                if (!smaliParent.exists()) {
                    System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class");
                    return false;
                }
            }
        }
        if (!smaliFile.exists()) {
            if (!smaliFile.createNewFile()) {
                System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class");
                return false;
            }
        }
        BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(smaliFile), "UTF8"));
        writer = new IndentingWriter(bufWriter);
        classDefinition.writeTo((IndentingWriter) writer);
    } catch (Exception ex) {
        System.err.println("\n\nError occurred while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class");
        ex.printStackTrace();
        // noinspection ResultOfMethodCallIgnored
        smaliFile.delete();
        return false;
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Throwable ex) {
                System.err.println("\n\nError occurred while closing file " + smaliFile.toString());
                ex.printStackTrace();
            }
        }
    }
    return true;
}
Also used : FileOutputStream(java.io.FileOutputStream) IndentingWriter(org.jf.util.IndentingWriter) OutputStreamWriter(java.io.OutputStreamWriter) ClassDefinition(com.taobao.android.baksmali.adaptors.ClassDefinition) File(java.io.File) IndentingWriter(org.jf.util.IndentingWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 4 with ClassDefinition

use of com.taobao.android.baksmali.adaptors.ClassDefinition in project atlas by alibaba.

the class BakSmali method disassembleClass.

public static boolean disassembleClass(ClassDef classDef, ClassFileNameHandler fileNameHandler, BaksmaliOptions options) {
    /**
     * The path for the disassembly file is based on the package name
     * The class descriptor will look something like:
     * Ljava/lang/Object;
     * Where the there is leading 'L' and a trailing ';', and the parts of the
     * package name are separated by '/'
     */
    String classDescriptor = classDef.getType();
    // validate that the descriptor is formatted like we expect
    if (classDescriptor.charAt(0) != 'L' || classDescriptor.charAt(classDescriptor.length() - 1) != ';') {
        System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class");
        return false;
    }
    File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor);
    // create and initialize the top level string template
    ClassDefinition classDefinition = new ClassDefinition(options, classDef);
    // write the disassembly
    Writer writer = null;
    try {
        File smaliParent = smaliFile.getParentFile();
        if (!smaliParent.exists()) {
            if (!smaliParent.mkdirs()) {
                // check again, it's likely it was created in a different thread
                if (!smaliParent.exists()) {
                    System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class");
                    return false;
                }
            }
        }
        if (!smaliFile.exists()) {
            if (!smaliFile.createNewFile()) {
                System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class");
                return false;
            }
        }
        BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(smaliFile), "UTF8"));
        writer = new IndentingWriter(bufWriter);
        classDefinition.writeTo((IndentingWriter) writer);
    } catch (Exception ex) {
        System.err.println("\n\nError occurred while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class");
        ex.printStackTrace();
        // noinspection ResultOfMethodCallIgnored
        smaliFile.delete();
        return false;
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Throwable ex) {
                System.err.println("\n\nError occurred while closing file " + smaliFile.toString());
                ex.printStackTrace();
            }
        }
    }
    return true;
}
Also used : IndentingWriter(org.jf.util.IndentingWriter) ClassDefinition(com.taobao.android.baksmali.adaptors.ClassDefinition) DexFile(org.jf.dexlib2.iface.DexFile) IndentingWriter(org.jf.util.IndentingWriter) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Aggregations

ClassDefinition (com.taobao.android.baksmali.adaptors.ClassDefinition)4 IndentingWriter (org.jf.util.IndentingWriter)4 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 DexFile (org.jf.dexlib2.iface.DexFile)2 SAXException (org.xml.sax.SAXException)2