Search in sources :

Example 1 with MethodDefinition

use of org.jf.baksmali.Adaptors.MethodDefinition in project atlas by alibaba.

the class SmaliCodeUtils method methodImplementionToCode.

/**
     * 将方法的实现转换为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");
    }
}
Also used : StringWriter(java.io.StringWriter) MethodDefinition(org.jf.baksmali.Adaptors.MethodDefinition) IndentingWriter(org.jf.util.IndentingWriter) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 MethodDefinition (org.jf.baksmali.Adaptors.MethodDefinition)1 IndentingWriter (org.jf.util.IndentingWriter)1