Search in sources :

Example 6 with Method

use of org.jf.dexlib2.iface.Method in project atlas by alibaba.

the class MethodDefinition method writeTo.

public void writeTo(IndentingWriter writer) throws IOException {
    int parameterRegisterCount = 0;
    if (!AccessFlags.STATIC.isSet(method.getAccessFlags())) {
        parameterRegisterCount++;
    }
    writer.write(".method ");
    writeAccessFlags(writer, method.getAccessFlags());
    writer.write(method.getName());
    writer.write("(");
    for (MethodParameter parameter : methodParameters) {
        String type = parameter.getType();
        writer.write(type);
        parameterRegisterCount++;
        if (TypeUtils.isWideType(type)) {
            parameterRegisterCount++;
        }
    }
    writer.write(")");
    writer.write(method.getReturnType());
    writer.write('\n');
    writer.indent(4);
    if (classDef.options.useLocalsDirective) {
        writer.write(".locals ");
        int registerCount = methodImpl.getRegisterCount() - parameterRegisterCount;
        writer.printSignedIntAsDec(registerCount);
    } else {
        writer.write(".registers ");
        writer.printSignedIntAsDec(methodImpl.getRegisterCount());
    }
    writer.write('\n');
    writeParameters(writer, method, methodParameters, classDef.options);
    if (registerFormatter == null) {
        registerFormatter = new RegisterFormatter(classDef.options, methodImpl.getRegisterCount(), parameterRegisterCount);
    }
    String containingClass = null;
    if (classDef.options.useImplicitReferences) {
        containingClass = method.getDefiningClass();
    }
    //如果是修改的方法,需要添加ReplaceAnnotation
    if (DexDiffInfo.modifiedMethods.contains(method)) {
        MethodReplaceAnnotation replaceAnnotation = new MethodReplaceAnnotation(method.getDefiningClass(), method.getName());
        AnnotationFormatter.writeTo(writer, method.getAnnotations(), containingClass, replaceAnnotation);
    } else {
        AnnotationFormatter.writeTo(writer, method.getAnnotations(), containingClass);
    }
    writer.write('\n');
    boolean first = true;
    boolean writeCheckCast = false;
    List<MethodItem> methodItems = getMethodItems();
    for (MethodItem methodItem : methodItems) {
        if (first && APatchTool.isApatch) {
            first = false;
            if (!fullMethod && !(methodItem instanceof EndPrologueMethodItem) && !DexDiffInfo.addedClasses.contains(classDef.getClassDef())) {
                if (!AccessFlags.STATIC.isSet(method.getAccessFlags())) {
                    writer.write("check-cast p0, " + method.getDefiningClass());
                    writer.write('\n');
                    writeCheckCast = true;
                }
            }
        }
        if (methodItem.writeTo(writer)) {
            writer.write('\n');
        }
        if (!writeCheckCast && !fullMethod && APatchTool.isApatch && !DexDiffInfo.addedClasses.contains(classDef.getClassDef())) {
            if (!AccessFlags.STATIC.isSet(method.getAccessFlags())) {
                if (methodItem instanceof EndPrologueMethodItem) {
                    writer.write("check-cast p0, " + method.getDefiningClass());
                    writer.write('\n');
                    writeCheckCast = true;
                }
            }
        }
    }
    writer.deindent(4);
    writer.write(".end method\n");
}
Also used : MethodReplaceAnnotation(com.taobao.android.apatch.annotation.MethodReplaceAnnotation) EndPrologueMethodItem(com.taobao.android.baksmali.adaptors.Debug.EndPrologueMethodItem) DebugMethodItem(com.taobao.android.baksmali.adaptors.Debug.DebugMethodItem) EndPrologueMethodItem(com.taobao.android.baksmali.adaptors.Debug.EndPrologueMethodItem) MethodParameter(org.jf.dexlib2.iface.MethodParameter)

Example 7 with Method

use of org.jf.dexlib2.iface.Method in project atlas by alibaba.

the class MethodReIClassDef method reMethod.

@Override
public Method reMethod(Method method) {
    String newType = null;
    boolean isBasic = false;
    boolean isInit = false;
    String methodName = method.getName();
    if (methodName.equals("<init>") || methodName.equals("<cinit>")) {
        isInit = true;
    }
    String returnType = method.getReturnType();
    if (basicType.containsKey(returnType)) {
        newType = returnType;
        isBasic = true;
    } else {
        newType = classProcessor.classProcess(DefineUtils.getDalvikClassName(returnType)).className;
    }
    MethodImplementation methodImplementation = method.getImplementation();
    List<? extends MethodParameter> paramters = method.getParameters();
    String[] argsOringn = new String[paramters.size()];
    String[] args = new String[paramters.size()];
    for (int i = 0; i < paramters.size(); i++) {
        //型参数不混淆
        if (basicType.containsKey(paramters.get(i).getType())) {
            argsOringn[i] = basicType.get(paramters.get(i).getType());
            args[i] = argsOringn[i];
            continue;
        }
        argsOringn[i] = DefineUtils.getDalvikClassName(paramters.get(i).getType());
        args[i] = classProcessor.classProcess(DefineUtils.getDalvikClassName(paramters.get(i).getType())).className;
    }
    String type = method.getReturnType();
    return new ImmutableMethod(reType, classProcessor.methodProcess(isInit ? methodName : DefineUtils.getDalvikClassName(method.getDefiningClass()), methodName, isBasic ? basicType.get(returnType) : DefineUtils.getDalvikClassName(returnType), StringUtils.join(argsOringn, ",")).methodName, reParameters(paramters), isBasic ? newType : DefineUtils.getDefineClassName(newType, type.startsWith("[")), method.getAccessFlags(), getAnnotation(method.getAnnotations()), reMethodImpl(methodImplementation));
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod)

Example 8 with Method

use of org.jf.dexlib2.iface.Method in project atlas by alibaba.

the class AbIClassDef method reClassDef.

@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();
    Set<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) {
        if (type.startsWith("Landroid/taobao/atlas/bundleInfo/AtlasBundleInfoManager;")) {
            System.out.println("xxxx");
        }
        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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) Method(org.jf.dexlib2.iface.Method) Annotation(org.jf.dexlib2.iface.Annotation) Field(org.jf.dexlib2.iface.Field) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 9 with Method

use of org.jf.dexlib2.iface.Method in project atlas by alibaba.

the class DexCompareUtils method equalTryBlocks.

/**
     * 比较method重的tryBlock块
     *
     * @param a
     * @param b
     * @return
     */
private static boolean equalTryBlocks(List<? extends DexBackedTryBlock> a, List<? extends DexBackedTryBlock> b) {
    if (a.size() != b.size()) {
        return false;
    }
    DexBackedTryBlock at, bt;
    for (int i = 0; i < a.size(); i++) {
        at = a.get(i);
        bt = b.get(i);
        if (!at.equals(bt)) {
            return false;
        }
    }
    return true;
}
Also used : DexBackedTryBlock(org.jf.dexlib2.dexbacked.DexBackedTryBlock)

Example 10 with Method

use of org.jf.dexlib2.iface.Method in project android-classyshark by google.

the class MetaObjectDex method getDeclaredConstructors.

@Override
public ConstructorInfo[] getDeclaredConstructors() {
    Iterable<? extends Method> implConstructors = classDef.getMethods();
    List<ConstructorInfo> result = new ArrayList<>();
    for (Method constructor : implConstructors) {
        if (isConstructor(constructor)) {
            ConstructorInfo ci = new ConstructorInfo();
            ci.parameterTypes = convertParameters(constructor.getParameters());
            ci.annotations = convertAnnotations(constructor.getAnnotations());
            ci.modifiers = constructor.getAccessFlags();
            result.add(ci);
        }
    }
    ConstructorInfo[] array = new ConstructorInfo[result.size()];
    return result.toArray(array);
}
Also used : ArrayList(java.util.ArrayList) Method(org.jf.dexlib2.iface.Method)

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)21 Method (org.jf.dexlib2.iface.Method)21 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)19 Test (org.junit.Test)16 Instruction (org.jf.dexlib2.iface.instruction.Instruction)15 DexFile (org.jf.dexlib2.iface.DexFile)13 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)13 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)11 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)10 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 MutableMethodImplementation (org.jf.dexlib2.builder.MutableMethodImplementation)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)7 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 Nonnull (javax.annotation.Nonnull)6 Opcode (org.jf.dexlib2.Opcode)6 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)6 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)6 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)6 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)6