org.jf.dexlib2.dexbacked.DexBackedMethodImplementation Java Examples

The following examples show how to use org.jf.dexlib2.dexbacked.DexBackedMethodImplementation. 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: ComplexityAnalyzer.java    From apkfile with Apache License 2.0 6 votes vote down vote up
public void analyze() {
    for (DexFile dexFile : dexFiles) {
        for (DexMethod dexMethod : dexFile.getMethodDescriptorToMethod().values()) {
            if (dexMethod.getCyclomaticComplexity() >= 0) {
                continue;
            }

            DexBackedMethod method = dexMethod.getMethod();
            DexBackedMethodImplementation implementation = method.getImplementation();
            if (implementation != null) {
                int complexity = calculateComplexity(implementation, dexFiles, new HashSet<>());
                dexMethod.setCyclomaticComplexity(complexity);
            }
        }

    }

}
 
Example #2
Source File: DebugInfo.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static DebugInfo newOrEmpty(@Nonnull DexBackedDexFile dexFile, int debugInfoOffset,
                                   @Nonnull DexBackedMethodImplementation methodImpl) {
    if (debugInfoOffset == 0) {
        return EmptyDebugInfo.INSTANCE;
    }
    return new DebugInfoImpl(dexFile, debugInfoOffset, methodImpl);
}
 
Example #3
Source File: DebugInfo.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public DebugInfoImpl(@Nonnull DexBackedDexFile dexFile,
                 int debugInfoOffset,
                 @Nonnull DexBackedMethodImplementation methodImpl) {
    this.dexFile = dexFile;
    this.debugInfoOffset = debugInfoOffset;
    this.methodImpl = methodImpl;
}
 
Example #4
Source File: DexMethod.java    From apkfile with Apache License 2.0 5 votes vote down vote up
private void analyzeEntropy() {
    try {
        Field f = DexBackedMethodImplementation.class.getDeclaredField("codeOffset");
        f.setAccessible(true);
        int codeOffset = (Integer) f.get(method.getImplementation());
        DexReaderEntropyCalculator calculator = new DexReaderEntropyCalculator(method.dexFile.getDataBuffer().readerAt(codeOffset));
        int implementationSize = method.getImplementation().getSize();
        calculator.calculate(codeOffset, implementationSize);
        codeEntropy = calculator.entropy();
        codePerplexity = calculator.perplexity();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }
}
 
Example #5
Source File: SmaliCodeUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
/**
 * 将方法的实现转换为smali代码
 *
 * @param dexBackedMethodImplementation
 * @param withLineNo                    是否包含行号
 * @return
 */
public static String methodImplementionToCode(DexBackedMethodImplementation dexBackedMethodImplementation, boolean withLineNo) {
    if (null == dexBackedMethodImplementation) {
        return null;
    }
    StringWriter stringWriter = new StringWriter();
    IndentingWriter writer = new IndentingWriter(stringWriter);
    MethodDefinition methodDefinition = new MethodDefinition(toClassDefinition(dexBackedMethodImplementation),
            dexBackedMethodImplementation.method,
            dexBackedMethodImplementation);
    try {
        methodDefinition.writeTo(writer);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    if (withLineNo) {
        return stringWriter.toString();
    } else {
        List<String> codes = Lists.newArrayList();
        String[] lines = StringUtils.split(stringWriter.toString(), "\n");
        for (String line : lines) {
            if (StringUtils.isNoneBlank(line) && !line.matches("\\s+\\.line\\s+[0-9]+$")) {
                codes.add(line);
            }
        }
        return StringUtils.join(codes, "\n");
    }

}
 
Example #6
Source File: DexCompareUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
/**
     * 判断2个方法的实现是否equal
     *
     * @param baseMethod
     * @param newMethod
     * @return
     */
    public static boolean compareMethod(DexBackedMethodImplementation baseMethod, DexBackedMethodImplementation newMethod) {
        String baseMethodStr = SmaliCodeUtils.methodImplementionToCode(baseMethod, false);
        String newMethodStr = SmaliCodeUtils.methodImplementionToCode(newMethod, false);
        if (StringUtils.equals(baseMethodStr, newMethodStr)) {
            return true;
        } else {
            return false;
        }
//        if (null != baseMethod && null != newMethod) {
//            List< Instruction> a = Lists.newArrayList();
//            List<Instruction> b = Lists.newArrayList();
//            for(Instruction instruction:baseMethod.getInstructions()){
//                a.add(instruction);
//            }
//            for(Instruction instruction:newMethod.getInstructions()){
//                b.add(instruction);
//            }
//
//            return baseMethod.getRegisterCount() == newMethod.getRegisterCount()
//                    && equalTryBlocks(baseMethod.getTryBlocks(), newMethod.getTryBlocks()) && equalsInstruction(a,b);
//        } else if (null == baseMethod && null == newMethod) {
//            return true;
//        } else {
//            return false;
//        }

    }
 
Example #7
Source File: DebugInfo.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
public static DebugInfo newOrEmpty(@Nonnull DexBackedDexFile dexFile, int debugInfoOffset,
                                   @Nonnull DexBackedMethodImplementation methodImpl) {
    if (debugInfoOffset == 0) {
        return EmptyDebugInfo.INSTANCE;
    }
    return new DebugInfoImpl(dexFile, debugInfoOffset, methodImpl);
}
 
Example #8
Source File: DebugInfo.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
public DebugInfoImpl(@Nonnull DexBackedDexFile dexFile,
                 int debugInfoOffset,
                 @Nonnull DexBackedMethodImplementation methodImpl) {
    this.dexFile = dexFile;
    this.debugInfoOffset = debugInfoOffset;
    this.methodImpl = methodImpl;
}
 
Example #9
Source File: DebugInfo.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
public static DebugInfo newOrEmpty(@Nonnull DexBackedDexFile dexFile, int debugInfoOffset,
                                   @Nonnull DexBackedMethodImplementation methodImpl) {
    if (debugInfoOffset == 0) {
        return EmptyDebugInfo.INSTANCE;
    }
    return new DebugInfoImpl(dexFile, debugInfoOffset, methodImpl);
}
 
Example #10
Source File: DebugInfo.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
public DebugInfoImpl(@Nonnull DexBackedDexFile dexFile,
                 int debugInfoOffset,
                 @Nonnull DexBackedMethodImplementation methodImpl) {
    this.dexFile = dexFile;
    this.debugInfoOffset = debugInfoOffset;
    this.methodImpl = methodImpl;
}
 
Example #11
Source File: DebugInfo.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static DebugInfo newOrEmpty(@Nonnull DexBackedDexFile dexFile, int debugInfoOffset,
                                   @Nonnull DexBackedMethodImplementation methodImpl) {
    if (debugInfoOffset == 0) {
        return EmptyDebugInfo.INSTANCE;
    }
    return new DebugInfoImpl(dexFile, debugInfoOffset, methodImpl);
}
 
Example #12
Source File: DebugInfo.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public DebugInfoImpl(@Nonnull DexBackedDexFile dexFile,
                 int debugInfoOffset,
                 @Nonnull DexBackedMethodImplementation methodImpl) {
    this.dexFile = dexFile;
    this.debugInfoOffset = debugInfoOffset;
    this.methodImpl = methodImpl;
}
 
Example #13
Source File: DexMethod.java    From apkfile with Apache License 2.0 4 votes vote down vote up
private void analyze(@Nonnull DexBackedMethodImplementation implementation) {
    debugItemCount = Utils.makeCollection(implementation.getDebugItems()).size();
    registerCount = implementation.getRegisterCount();
    tryCatchCount = implementation.getTryBlocks().size();

    for (Instruction instruction : implementation.getInstructions()) {
        instructionCount++;
        Opcode op = instruction.getOpcode();
        opCounts.adjustOrPutValue(op.apiToValueMap.get(DexFile.TARGET_API), 1, 1);

        if (instruction instanceof ReferenceInstruction) {
            ReferenceInstruction refInstr = (ReferenceInstruction) instruction;
            if (op.referenceType == ReferenceType.METHOD || op.referenceType == ReferenceType.FIELD) {
                boolean isApiPackage = false;
                for (String apiPackage : API_PACKAGES) {
                    String refStr = ReferenceUtil.getReferenceString(refInstr.getReference());
                    if (refStr.startsWith(apiPackage)) {
                        isApiPackage = true;
                        break;
                    }
                }
                if (!isApiPackage) {
                    continue;
                }
            }

            switch (op.referenceType) {
                case ReferenceType.METHOD:
                    MethodReference methodRef = (MethodReference) refInstr.getReference();
                    if (shortMethodSignatures) {
                        ShortMethodReference shortMethodRef = new ShortMethodReference(methodRef);
                        frameworkApiCounts.adjustOrPutValue(shortMethodRef, 1, 1);
                    } else {
                        frameworkApiCounts.adjustOrPutValue(methodRef, 1, 1);
                    }
                    break;
                case ReferenceType.FIELD:
                    FieldReference fieldRef = (FieldReference) refInstr.getReference();
                    frameworkFieldReferenceCounts.adjustOrPutValue(fieldRef, 1, 1);
                    break;
                case ReferenceType.STRING:
                    StringReference stringRef = (StringReference) refInstr.getReference();
                    stringReferenceCounts.adjustOrPutValue(stringRef, 1, 1);
                    break;
            }
        }
    }

    analyzeEntropy();
}
 
Example #14
Source File: SmaliCodeUtils.java    From atlas with Apache License 2.0 4 votes vote down vote up
private static ClassDefinition toClassDefinition(DexBackedMethodImplementation dexBackedMethodImplementation) {
    ClassDefinition classDefinition = new ClassDefinition(createBaksmaliOptions(dexBackedMethodImplementation.method.classDef),
            dexBackedMethodImplementation.method.classDef);
    return classDefinition;
}