Java Code Examples for org.jf.dexlib2.iface.DexFile#getClasses()

The following examples show how to use org.jf.dexlib2.iface.DexFile#getClasses() . 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: RootBuilder.java    From android-classyshark with Apache License 2.0 6 votes vote down vote up
private void fillFromDex(File file, ClassNode rootNode) {
    try {
        DexFile dxFile = DexlibLoader.loadDexFile(file);

        Set<? extends ClassDef> classSet = dxFile.getClasses();
        for (ClassDef o : classSet) {
            int methodCount = 0;
            for (Method method : o.getMethods()) {
                methodCount++;
            }

            String translatedClassName = o.getType().replaceAll("\\/", "\\.").substring(1, o.getType().length() - 1);
            ClassInfo classInfo = new ClassInfo(translatedClassName, methodCount);
            rootNode.add(classInfo);
        }

    } catch (Exception ex) {
        System.err.println("Error parsing Dexfile: " + file.getName() + ": " + ex.getMessage());
        ex.printStackTrace(System.err);
    }
}
 
Example 2
Source File: SmaliClassDetailLoader.java    From PATDroid with Apache License 2.0 6 votes vote down vote up
/**
 * Parse an apk file and extract all classes, methods, fields and optionally instructions
 */
public void loadAll(Scope scope) {
    IdentityHashMap<MethodInfo, MethodImplementation> collector = new IdentityHashMap<MethodInfo, MethodImplementation>();
    for (DexFile dexFile: dexFiles) {
        for (final ClassDef classDef : dexFile.getClasses()) {
            ClassInfo ci = Dalvik.findOrCreateClass(scope, classDef.getType());
            ClassDetail detail = translateClassDef(ci, classDef, collector);
            setDetail(ci, detail);
        }
    }
    if (translateInstructions) {
        for (MethodInfo mi: collector.keySet()) {
            final MethodImplementation impl = collector.get(mi);
            // Decode instructions
            if (impl != null) {
                new MethodImplementationTranslator(scope).translate(mi, impl);
            }
        }
    }
}
 
Example 3
Source File: ClassPath.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ClassPath instance that can load classes from the given dex files
 *
 * @param classPath An iterable of DexFile objects. When loading a class, these dex files will be searched in order
 * @param api API level
 */
public ClassPath(@Nonnull Iterable<DexFile> classPath, int api) {
    // add fallbacks for certain special classes that must be present
    Iterable<DexFile> dexFiles = Iterables.concat(classPath, Lists.newArrayList(getBasicClasses()));

    unknownClass = new UnknownClassProto(this);
    loadedClasses.put(unknownClass.getType(), unknownClass);
    this.api = api;

    loadPrimitiveType("Z");
    loadPrimitiveType("B");
    loadPrimitiveType("S");
    loadPrimitiveType("C");
    loadPrimitiveType("I");
    loadPrimitiveType("J");
    loadPrimitiveType("F");
    loadPrimitiveType("D");
    loadPrimitiveType("L");
    //Logger.log("add the classinfo by classpath");
    for (DexFile dexFile: dexFiles) {
        for (ClassDef classDef: dexFile.getClasses()) {
            ClassDef prev = availableClasses.get(classDef.getType());
            if (prev == null) {
                availableClasses.put(classDef.getType(), classDef);
                //Logger.log("add the calldef "+classDef.getType());
            }
        }
    }
    //Logger.log("end the classinfo by classpath");
}
 
Example 4
Source File: ClassPath.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ClassPath instance that can load classes from the given dex files
 *
 * @param classPath An iterable of DexFile objects. When loading a class, these dex files will be searched in order
 * @param api API level
 */
public ClassPath(@Nonnull Iterable<DexFile> classPath, int api) {
    // add fallbacks for certain special classes that must be present
    Iterable<DexFile> dexFiles = Iterables.concat(classPath, Lists.newArrayList(getBasicClasses()));

    unknownClass = new UnknownClassProto(this);
    loadedClasses.put(unknownClass.getType(), unknownClass);
    this.api = api;

    loadPrimitiveType("Z");
    loadPrimitiveType("B");
    loadPrimitiveType("S");
    loadPrimitiveType("C");
    loadPrimitiveType("I");
    loadPrimitiveType("J");
    loadPrimitiveType("F");
    loadPrimitiveType("D");
    loadPrimitiveType("L");
    //Logger.log("add the classinfo by classpath");
    for (DexFile dexFile: dexFiles) {
        for (ClassDef classDef: dexFile.getClasses()) {
            ClassDef prev = availableClasses.get(classDef.getType());
            if (prev == null) {
                availableClasses.put(classDef.getType(), classDef);
                //Logger.log("add the calldef "+classDef.getType());
            }
        }
    }
    //Logger.log("end the classinfo by classpath");
}
 
Example 5
Source File: ClassPath.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ClassPath instance that can load classes from the given dex files
 *
 * @param classPath An iterable of DexFile objects. When loading a class, these dex files will be searched in order
 * @param api API level
 */
public ClassPath(@Nonnull Iterable<DexFile> classPath, int api) {
    // add fallbacks for certain special classes that must be present
    Iterable<DexFile> dexFiles = Iterables.concat(classPath, Lists.newArrayList(getBasicClasses()));

    unknownClass = new UnknownClassProto(this);
    loadedClasses.put(unknownClass.getType(), unknownClass);
    this.api = api;

    loadPrimitiveType("Z");
    loadPrimitiveType("B");
    loadPrimitiveType("S");
    loadPrimitiveType("C");
    loadPrimitiveType("I");
    loadPrimitiveType("J");
    loadPrimitiveType("F");
    loadPrimitiveType("D");
    loadPrimitiveType("L");
    //Logger.log("add the classinfo by classpath");
    for (DexFile dexFile: dexFiles) {
        for (ClassDef classDef: dexFile.getClasses()) {
            ClassDef prev = availableClasses.get(classDef.getType());
            if (prev == null) {
                availableClasses.put(classDef.getType(), classDef);
                //Logger.log("add the calldef "+classDef.getType());
            }
        }
    }
    //Logger.log("end the classinfo by classpath");
}
 
Example 6
Source File: ProcessManifest.java    From DroidRA with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Set<String> getDexClasses()
{	
		Set<String> dexClasses = new HashSet<String>();
		
		try {
			DexFile dexFile = DexFileFactory.loadDexFile(new File(apk.getAbsolutePath()), targetSdkVersion());
			for (ClassDef classDef: dexFile.getClasses()) 
			{
				String cls = classDef.getType();
				if (cls.contains("$"))
				{
					//do not consider sub-classes
					continue;
				}
				
				cls = cls.replace("/", ".").substring(1, cls.length()-1);
				
				dexClasses.add(cls);
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		return dexClasses;
 	}
 
Example 7
Source File: DexlibAdapter.java    From android-classyshark with Apache License 2.0 5 votes vote down vote up
public static ClassDef getClassDefByName(String className, DexFile dexFile)
        throws Exception {
    ClassDef result = null;
    String dexName;

    for (ClassDef currentClassDef : dexFile.getClasses()) {
        dexName = currentClassDef.getType();
        if (isMatchFromDex(className, dexName)) {
            result = currentClassDef;
            break;
        }
    }

    return result;
}
 
Example 8
Source File: StressTest.java    From android-classyshark with Apache License 2.0 5 votes vote down vote up
public static void runAllClassesInDex(String jarCanonicalPath) throws Exception {
    DexFile dexFile = DexlibLoader.loadDexFile(new File(jarCanonicalPath));
    Set<? extends ClassDef> allClassesInDex = dexFile.getClasses();

    for (ClassDef currentClass : allClassesInDex) {
        String normType = DexlibAdapter.getClassStringFromDex(currentClass.getType());
        Translator sourceGenerator = TranslatorFactory.createTranslator(
                normType, new File(jarCanonicalPath));
        sourceGenerator.apply();
        System.out.println(sourceGenerator.toString());
    }
}
 
Example 9
Source File: DexReader.java    From android-classyshark with Apache License 2.0 5 votes vote down vote up
public static List<String> readClassNamesFromDex(File binaryArchiveFile) throws Exception {
    DexFile dexFile = DexlibLoader.loadDexFile(binaryArchiveFile);
    List<String> result = new ArrayList<>();

    for (ClassDef classDef : dexFile.getClasses()) {
        result.add(classDef.getType().replaceAll("/", ".").
                substring(1, classDef.getType().length() - 1));
    }

    Collections.sort(result);
    return result;
}
 
Example 10
Source File: ClassPath.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ClassPath instance that can load classes from the given dex files
 *
 * @param classPath An iterable of DexFile objects. When loading a class, these dex files will be searched in order
 * @param api API level
 */
public ClassPath(@Nonnull Iterable<DexFile> classPath, int api) {
    // add fallbacks for certain special classes that must be present
    Iterable<DexFile> dexFiles = Iterables.concat(classPath, Lists.newArrayList(getBasicClasses()));

    unknownClass = new UnknownClassProto(this);
    loadedClasses.put(unknownClass.getType(), unknownClass);
    this.api = api;

    loadPrimitiveType("Z");
    loadPrimitiveType("B");
    loadPrimitiveType("S");
    loadPrimitiveType("C");
    loadPrimitiveType("I");
    loadPrimitiveType("J");
    loadPrimitiveType("F");
    loadPrimitiveType("D");
    loadPrimitiveType("L");
    //Logger.log("add the classinfo by classpath");
    for (DexFile dexFile: dexFiles) {
        for (ClassDef classDef: dexFile.getClasses()) {
            ClassDef prev = availableClasses.get(classDef.getType());
            if (prev == null) {
                availableClasses.put(classDef.getType(), classDef);
                //Logger.log("add the calldef "+classDef.getType());
            }
        }
    }
    //Logger.log("end the classinfo by classpath");
}
 
Example 11
Source File: ImmutableDexFile.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
public static ImmutableDexFile of(DexFile dexFile) {
    if (dexFile instanceof ImmutableDexFile) {
        return (ImmutableDexFile)dexFile;
    }
    return new ImmutableDexFile(dexFile.getClasses());
}
 
Example 12
Source File: PatchMethodTool.java    From atlas with Apache License 2.0 4 votes vote down vote up
public static void modifyMethod(String srcDexFile, String outDexFile, boolean isAndFix) throws IOException {

        DexFile dexFile = DexFileFactory.loadDexFile(srcDexFile, Opcodes.getDefault());

        final Set<ClassDef> classes = Sets.newConcurrentHashSet();

        for (ClassDef classDef : dexFile.getClasses()) {
            Set<Method> methods = Sets.newConcurrentHashSet();
            boolean modifiedMethod = false;
            for (Method method : classDef.getMethods()) {
                    MethodImplementation implementation = method.getImplementation();
                    if (implementation != null&&(methodNeedsModification(classDef, method, isAndFix))) {
                        modifiedMethod = true;
                        methods.add(new ImmutableMethod(
                                method.getDefiningClass(),
                                method.getName(),
                                method.getParameters(),
                                method.getReturnType(),
                                method.getAccessFlags(),
                                method.getAnnotations(),
                                isAndFix ?
                                        modifyMethodAndFix(implementation, method) : modifyMethodTpatch(implementation, method)));
                    } else {
                        methods.add(method);
                    }
                }
            if (!modifiedMethod) {
                classes.add(classDef);
            } else {
                classes.add(new ImmutableClassDef(
                        classDef.getType(),
                        classDef.getAccessFlags(),
                        classDef.getSuperclass(),
                        classDef.getInterfaces(),
                        classDef.getSourceFile(),
                        classDef.getAnnotations(),
                        classDef.getFields(),
                        methods));
            }

        }

        DexFileFactory.writeDexFile(outDexFile, new DexFile() {
            @Nonnull
            @Override
            public Set<? extends ClassDef> getClasses() {
                return new AbstractSet<ClassDef>() {
                    @Nonnull
                    @Override
                    public Iterator<ClassDef> iterator() {
                        return classes.iterator();
                    }

                    @Override
                    public int size() {
                        return classes.size();
                    }
                };
            }

            @Nonnull
            @Override
            public Opcodes getOpcodes() {
                return Opcodes.getDefault();
            }
        });
    }
 
Example 13
Source File: ImmutableDexFile.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
public static ImmutableDexFile of(DexFile dexFile) {
    if (dexFile instanceof ImmutableDexFile) {
        return (ImmutableDexFile)dexFile;
    }
    return new ImmutableDexFile(dexFile.getClasses());
}
 
Example 14
Source File: ImmutableDexFile.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
public static ImmutableDexFile of(DexFile dexFile) {
    if (dexFile instanceof ImmutableDexFile) {
        return (ImmutableDexFile)dexFile;
    }
    return new ImmutableDexFile(dexFile.getClasses());
}
 
Example 15
Source File: ImmutableDexFile.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
public static ImmutableDexFile of(DexFile dexFile) {
    if (dexFile instanceof ImmutableDexFile) {
        return (ImmutableDexFile)dexFile;
    }
    return new ImmutableDexFile(dexFile.getClasses());
}