Java Code Examples for org.jf.dexlib2.iface.ClassDef

Following code examples demonstrate how to use org.jf.dexlib2.iface.ClassDeffrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.jf.dexlib2.iface.ClassDefand various code implementation of this class.

    public DexFileWrapper(DexFile dexFile) {
        this.dexFile = dexFile;

        ClassDefSet set = new ClassDefSet();
        Set<? extends ClassDef> classDefs = dexFile.getClasses();
        for (ClassDef classDef : classDefs) {
            set.addClassDef(classDef);
        }
        classes = set;
    } 


    public void replaceClassDef (ClassDef oldClassDef, ClassDef newClassDef) {
        list.remove(oldClassDef);
        list.add(newClassDef);
    } 

    public List<String> getSyntheticAccessors() {

        LinkedList<String> result = new LinkedList<>();

        Set<? extends ClassDef> allClasses = dxFile.getClasses();

        for (ClassDef classDef : allClasses) {


            Iterator<? extends Method> allMethodsIter = classDef.getMethods().iterator();

            while (allMethodsIter.hasNext()) {

                Method element = allMethodsIter.next();

                String name = element.getName();

                String nClassName = classDef.getType();

                if (name.contains("access$")) {

                    String cleanClassName = DexlibAdapter.getClassStringFromDex(nClassName);

                    if (!result.contains(cleanClassName)) {
                        result.add(cleanClassName);
                    }
                }
            }
        }

        return result;
    } 

    public Method read(String className, String member) throws Exception {
        org.jf.dexlib2.iface.ClassDef classDef = (org.jf.dexlib2.iface.ClassDef) reader.read(className,null);
        for(Method method:classDef.getMethods()){
            if (method.getName().equals(member)){
                return method;
            }
        }
        return null;
    } 

    public Set<ClassDef> createModifyClasses() throws IOException, PatchException {
            dexDiffer.setTpatch(true);
        return super.createModifyClasses();
    } 

    public Field read(String className,String member) throws Exception {
        if (reader == null){
            return null;
        }
        classDef = (ClassDef) reader.read(className,null);
        Iterable<? extends Field> fields = classDef.getFields();
        for (Field field:fields){
            if (field.getName().equals(member)){
                return field;
            }
        }
    return null;
    } 

    @Nullable @Override public ClassDef getClassDef(String type) {
        ResolveScopeManager manager = ResolveScopeManager.getInstance(project);
        PsiClass psiClass = NameUtils.resolveSmaliType(project, manager.getDefaultResolveScope(file), type);
        if (psiClass != null) {
            return new SmalideaClassDef(psiClass);
        }
        return null;
    } 

    public String getClassSimpleName(ClassDef classDef) {
        final String type = classDef.getType();
        final int lastSlash = type.lastIndexOf('/');

        final String name;
        if (lastSlash == -1) {
            name = type;
        } else {
            name = type.substring(lastSlash + 1, type.length() - 1);
        }

        return name.endsWith(";") ? name.substring(0, name.length() - 2) : name;
    } 

    public String getPackageName(ClassDef classDef) {
        final String type = classDef.getType();
        final int lastSlash = type.lastIndexOf('/');

        final String path;
        if (lastSlash == -1) {
            path = type;
        } else {
            path = type.substring(0, lastSlash);
        }

        final String packagePath = path.replaceAll("/", ".");
        return packagePath.startsWith("L") ? packagePath.substring(1) : path;
    } 

    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;
    } 

    public ClassReader(Reader reader) throws Exception {
        if (reader!= null && reader instanceof DexReader){
            classDefs = (Set<ClassDef>) reader.read(null,null);
        }
    } 

    public ClassDef read(String className,String memberName){
        if (classDefs == null){
            return null;
        }
        for (ClassDef classDef:classDefs){
            if (classDef.getType().equals(className)){
                return classDef;
            }
        }
        return null;
    } 

Advertisement
Javadoc
This class represents a class definition. It also acts as a TypeReference to itself. Any equality/co

mparison is based on its identity as a TypeReference, and shouldn't take into account anything other than the type of this class.

Read More
Advertisement