org.jf.util.ClassFileNameHandler Java Examples

The following examples show how to use org.jf.util.ClassFileNameHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
public static Set<String> buildCode(File smaliDir, File dexFile, DexDiffInfo info) throws IOException,
        RecognitionException {
    Set<String> classes = new HashSet<String>();
    Set<DexBackedClassDef> classDefs = new HashSet<DexBackedClassDef>();
    classDefs.addAll(info.getModifiedClasses());
    classDefs.addAll(info.getAddedClasses());
    final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
    final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
    DexBuilder dexBuilder = new DexBuilder(Opcodes.getDefault());
    File smaliFile;
    String className;
    for (DexBackedClassDef classDef : classDefs) {
        ApkPatch.currentClassType = classDef.getType();
        className = TypeGenUtil.newType(classDef.getType());
        AfBakSmali.disassembleClass(classDef, outFileNameHandler, getBuildOption(classDefs, 19), false, false);
        smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
        classes.add(className.substring(1, className.length() - 1).replace('/', '.'));
        SmaliMod.assembleSmaliFile(smaliFile, dexBuilder, true, true);
    }

    dexBuilder.writeTo(new FileDataStore(dexFile));

    return classes;
}
 
Example #2
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
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, Opcodes.getDefault());
                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;
    }
 
Example #3
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static File disassemble(File smaliDir, DexBackedClassDef dexBackedClassDef) throws PatchException {

        Set<DexBackedClassDef> classes = Sets.newHashSet();
        classes.add(dexBackedClassDef);
        final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
        final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
        String className = dexBackedClassDef.getType();
        AfBakSmali.disassembleClass(dexBackedClassDef, outFileNameHandler, getBuildOption(classes, 19), true, false);
        File smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
        return smaliFile;

    }
 
Example #4
Source File: ApkPatch.java    From atlas with Apache License 2.0 4 votes vote down vote up
private static Set<String> buildPrepareClass(File smaliDir, List<File> newFiles,
                                                 DexDiffInfo info) throws PatchException {
        Set<DexBackedClassDef> classes = Sets.newHashSet();
        classes = SmaliDiffUtils.scanClasses(smaliDir, newFiles);
        ArrayList<String> methods = new ArrayList<String>();
        {
            Set<DexBackedMethod> tempSet = info.getModifiedMethods();
            for (DexBackedMethod methodRef : tempSet) {
                String template = methodRef.getDefiningClass() + "->" + methodRef.getName();
                methods.add(template);
                System.out.println("template: " + template);
                if (superClasses.containsKey(methodRef.getDefiningClass())) {
                    ArrayList<String> derivedClasses = superClasses.get(methodRef.getDefiningClass());
                    for (int i = 0; i < derivedClasses.size(); i++) {
                        template = derivedClasses.get(i) + "->" + methodRef.getName();
                        System.out.println("template: " + template);
                        methods.add(template);
                    }
                }
            }
        }

        Set<String> prepareClasses = new HashSet<String>();
        try {
            final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
            for (DexBackedClassDef classDef : classes) {
                currentClassType = null;
                String className = TypeGenUtil.newType(classDef.getType());
                // baksmali.disassembleClass(classDef, outFileNameHandler, options);
                File smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
                if (!smaliFile.exists()){
                    continue;
                }
                //增加class注解到prepare
                getClassAnnotaionPrepareClasses(classDef,prepareClasses,info);

                BufferedReader br = new BufferedReader(new FileReader(smaliFile));
                String data = br.readLine();// 一次读入一行,直到读入null为文件结束
                while (data != null) {
                    boolean find = false;
                    for (String m : methods) {
                        if (data.contains(m)) {
                            find = true;
                            break;
                        }
                    }
                    if (find) {
                        prepareClasses.add(className.substring(1, className.length() - 1).replace('/', '.'));
                        System.out.println("prepare class: " + className);
                        break;
                    }
                    data = br.readLine(); // 接着读下一行
                }
                br.close();

            }
        } catch (Exception e) {
            throw new PatchException(e);
        }
        for (DexBackedMethod method:info.getModifiedMethods()) {
            prepareClasses.add(method.getDefiningClass().substring(1, method.getDefiningClass().length() - 1).replace("/", "."));
        }
        //增加modify的anatation到prepare
//        getMethodAnnotaionPrepareClasses(info,prepareClasses);
        return prepareClasses;
    }