Java Code Examples for org.jf.dexlib2.iface.ClassDef#getMethods()

The following examples show how to use org.jf.dexlib2.iface.ClassDef#getMethods() . 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: SyntheticAccessorResolver.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
@Nullable
public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) {
    String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference);

    AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor);
    if (accessedMember != null) {
        return accessedMember;
    }

    String type = methodReference.getDefiningClass();
    ClassDef classDef = classDefMap.get(type);
    if (classDef == null) {
        return null;
    }

    Method matchedMethod = null;
    MethodImplementation matchedMethodImpl = null;
    for (Method method: classDef.getMethods()) {
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl != null) {
            if (methodReferenceEquals(method, methodReference)) {
                matchedMethod = method;
                matchedMethodImpl = methodImpl;
                break;
            }
        }
    }

    if (matchedMethod == null) {
        return null;
    }

    //A synthetic accessor will be marked synthetic
    if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) {
        return null;
    }

    List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions());

    int accessType = SyntheticAccessorFSM.test(instructions);

    if (accessType >= 0) {
        AccessedMember member =
                new AccessedMember(accessType, ((ReferenceInstruction)instructions.get(0)).getReference());
        resolvedAccessors.put(methodDescriptor, member);
        return member;
    }
    return null;
}
 
Example 3
Source File: AbIClassDef.java    From atlas with Apache License 2.0 4 votes vote down vote up
@Override
    public ClassDef reClassDef(ClassDef classDef) {
        Iterable<? extends Method> methods = classDef.getMethods();
        LinkedHashSet<Method> newMethods = new LinkedHashSet<Method>();
        Iterable<? extends Field> fields = classDef.getFields();
        LinkedHashSet<Field>newFields = new LinkedHashSet<Field>();
        Set<? extends Annotation> annotations = classDef.getAnnotations();
        List<String>interfaces = classDef.getInterfaces();
        Set<String>newInterfaces = new HashSet<String>();
        Set<Annotation>immutableAnnotations = new HashSet<Annotation>();
        String type = classDef.getType();
        reType = reType(type);
        String superClass = classDef.getSuperclass();
        for (String inter:interfaces){
            newInterfaces.add(reInterface(inter));
        }
        String reSuperClass = reSuperClass(superClass);
        for (Annotation annotation:annotations){

            immutableAnnotations.add(reAnnotation(annotation));
        }
        for (Field field:fields){
            newFields.add(reField(field));
        }
        for (Method method:methods){
            if (method.getName().equals("<cinit>")||method.getName().equals("<init>")){
               newMethods.add(reMethod(method));
                continue;
            }
//            if (method.getName().equals("getArchiveFile")) {
                newMethods.add(reMethod(method));
//            }
        }



        return new ImmutableClassDef(
                reType,
                classDef.getAccessFlags(),
                reSuperClass,
                newInterfaces,
                classDef.getSourceFile(),
                immutableAnnotations,
                newFields,
                newMethods);
        }
 
Example 4
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 5
Source File: SyntheticAccessorResolver.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Nullable
public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) {
    String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference);

    AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor);
    if (accessedMember != null) {
        return accessedMember;
    }

    String type = methodReference.getDefiningClass();
    ClassDef classDef = classDefMap.get(type);
    if (classDef == null) {
        return null;
    }

    Method matchedMethod = null;
    MethodImplementation matchedMethodImpl = null;
    for (Method method: classDef.getMethods()) {
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl != null) {
            if (methodReferenceEquals(method, methodReference)) {
                matchedMethod = method;
                matchedMethodImpl = methodImpl;
                break;
            }
        }
    }

    if (matchedMethod == null) {
        return null;
    }

    //A synthetic accessor will be marked synthetic
    if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) {
        return null;
    }

    List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions());

    int accessType = SyntheticAccessorFSM.test(instructions);

    if (accessType >= 0) {
        AccessedMember member =
                new AccessedMember(accessType, ((ReferenceInstruction)instructions.get(0)).getReference());
        resolvedAccessors.put(methodDescriptor, member);
        return member;
    }
    return null;
}
 
Example 6
Source File: SyntheticAccessorResolver.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
@Nullable
public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) {
    String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference);

    AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor);
    if (accessedMember != null) {
        return accessedMember;
    }

    String type = methodReference.getDefiningClass();
    ClassDef classDef = classDefMap.get(type);
    if (classDef == null) {
        return null;
    }

    Method matchedMethod = null;
    MethodImplementation matchedMethodImpl = null;
    for (Method method: classDef.getMethods()) {
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl != null) {
            if (methodReferenceEquals(method, methodReference)) {
                matchedMethod = method;
                matchedMethodImpl = methodImpl;
                break;
            }
        }
    }

    if (matchedMethod == null) {
        return null;
    }

    //A synthetic accessor will be marked synthetic
    if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) {
        return null;
    }

    List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions());

    int accessType = SyntheticAccessorFSM.test(instructions);

    if (accessType >= 0) {
        AccessedMember member =
                new AccessedMember(accessType, ((ReferenceInstruction)instructions.get(0)).getReference());
        resolvedAccessors.put(methodDescriptor, member);
        return member;
    }
    return null;
}
 
Example 7
Source File: SyntheticAccessorResolver.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
@Nullable
public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) {
    String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference);

    AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor);
    if (accessedMember != null) {
        return accessedMember;
    }

    String type = methodReference.getDefiningClass();
    ClassDef classDef = classDefMap.get(type);
    if (classDef == null) {
        return null;
    }

    Method matchedMethod = null;
    MethodImplementation matchedMethodImpl = null;
    for (Method method: classDef.getMethods()) {
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl != null) {
            if (methodReferenceEquals(method, methodReference)) {
                matchedMethod = method;
                matchedMethodImpl = methodImpl;
                break;
            }
        }
    }

    if (matchedMethod == null) {
        return null;
    }

    //A synthetic accessor will be marked synthetic
    if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) {
        return null;
    }

    List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions());

    int accessType = SyntheticAccessorFSM.test(instructions);

    if (accessType >= 0) {
        AccessedMember member =
                new AccessedMember(accessType, ((ReferenceInstruction)instructions.get(0)).getReference());
        resolvedAccessors.put(methodDescriptor, member);
        return member;
    }
    return null;
}