com.android.dx.cf.iface.Method Java Examples

The following examples show how to use com.android.dx.cf.iface.Method. 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: BlockDumper.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void endParsingMember(ByteArray bytes, int offset, String name,
        String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    if ((member.getAccessFlags() & (AccessFlags.ACC_ABSTRACT |
            AccessFlags.ACC_NATIVE)) != 0) {
        return;
    }

    ConcreteMethod meth =
        new ConcreteMethod((Method) member, classFile, true, true);

    if (rop) {
        ropDump(meth);
    } else {
        regularDump(meth);
    }
}
 
Example #2
Source File: BlockDumper.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void endParsingMember(ByteArray bytes, int offset, String name,
        String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    if ((member.getAccessFlags() & (AccessFlags.ACC_ABSTRACT |
            AccessFlags.ACC_NATIVE)) != 0) {
        return;
    }

    ConcreteMethod meth =
        new ConcreteMethod((Method) member, classFile, true, true);

    if (rop) {
        ropDump(meth);
    } else {
        regularDump(meth);
    }
}
 
Example #3
Source File: BlockDumper.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void endParsingMember(ByteArray bytes, int offset, String name,
        String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    if ((member.getAccessFlags() & (AccessFlags.ACC_ABSTRACT |
            AccessFlags.ACC_NATIVE)) != 0) {
        return;
    }

    ConcreteMethod meth =
        new ConcreteMethod((Method) member, classFile, true, true);

    if (rop) {
        ropDump(meth);
    } else {
        regularDump(meth);
    }
}
 
Example #4
Source File: AttributeTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the annotations out of a given method, similar to {@link
 * #getAnnotations}, also including an annotation for the translation
 * of the method-specific attribute {@code Exceptions}.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getMethodAnnotations(Method method) {
    Annotations result = getAnnotations(method.getAttributes());
    TypeList exceptions = getExceptions(method);

    if (exceptions.size() != 0) {
        Annotation throwsAnnotation =
            AnnotationUtils.makeThrows(exceptions);
        result = Annotations.combine(result, throwsAnnotation);
    }

    return result;
}
 
Example #5
Source File: AttributeTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the {@code AnnotationDefault} attributes out of a
 * given class, if any, reforming them as an
 * {@code AnnotationDefault} annotation.
 *
 * @param cf {@code non-null;} the class in question
 * @return {@code null-ok;} an appropriately-constructed
 * {@code AnnotationDefault} annotation, if there were any
 * annotation defaults in the class, or {@code null} if not
 */
private static Annotation translateAnnotationDefaults(DirectClassFile cf) {
    CstType thisClass = cf.getThisClass();
    MethodList methods = cf.getMethods();
    int sz = methods.size();
    Annotation result =
        new Annotation(thisClass, AnnotationVisibility.EMBEDDED);
    boolean any = false;

    for (int i = 0; i < sz; i++) {
        Method one = methods.get(i);
        AttributeList attribs = one.getAttributes();
        AttAnnotationDefault oneDefault = (AttAnnotationDefault)
            attribs.findFirst(AttAnnotationDefault.ATTRIBUTE_NAME);

        if (oneDefault != null) {
            NameValuePair pair = new NameValuePair(
                    one.getNat().getName(),
                    oneDefault.getValue());
            result.add(pair);
            any = true;
        }
    }

    if (! any) {
        return null;
    }

    result.setImmutable();
    return AnnotationUtils.makeAnnotationDefault(result);
}
 
Example #6
Source File: AttributeTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the parameter annotations out of a given method. This
 * combines both visible and invisible annotations into a single
 * result set.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the list of annotation sets, which may be
 * empty
 */
public static AnnotationsList getParameterAnnotations(Method method) {
    AttributeList attribs = method.getAttributes();
    AttRuntimeVisibleParameterAnnotations visible =
        (AttRuntimeVisibleParameterAnnotations)
        attribs.findFirst(
                AttRuntimeVisibleParameterAnnotations.ATTRIBUTE_NAME);
    AttRuntimeInvisibleParameterAnnotations invisible =
        (AttRuntimeInvisibleParameterAnnotations)
        attribs.findFirst(
                AttRuntimeInvisibleParameterAnnotations.ATTRIBUTE_NAME);

    if (visible == null) {
        if (invisible == null) {
            return AnnotationsList.EMPTY;
        }
        return invisible.getParameterAnnotations();
    }

    if (invisible == null) {
        return visible.getParameterAnnotations();
    }

    // Both are non-null, so combine them.

    return AnnotationsList.combine(visible.getParameterAnnotations(),
            invisible.getParameterAnnotations());
}
 
Example #7
Source File: AttributeTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the annotations out of a given method, similar to {@link
 * #getAnnotations}, also including an annotation for the translation
 * of the method-specific attribute {@code Exceptions}.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getMethodAnnotations(Method method) {
    Annotations result = getAnnotations(method.getAttributes());
    TypeList exceptions = getExceptions(method);

    if (exceptions.size() != 0) {
        Annotation throwsAnnotation =
            AnnotationUtils.makeThrows(exceptions);
        result = Annotations.combine(result, throwsAnnotation);
    }

    return result;
}
 
Example #8
Source File: AttributeTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the list of thrown exceptions for a given method.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the list of thrown exceptions
 */
public static TypeList getExceptions(Method method) {
    AttributeList attribs = method.getAttributes();
    AttExceptions exceptions = (AttExceptions)
        attribs.findFirst(AttExceptions.ATTRIBUTE_NAME);

    if (exceptions == null) {
        return StdTypeList.EMPTY;
    }

    return exceptions.getExceptions();
}
 
Example #9
Source File: AttributeTranslator.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the {@code AnnotationDefault} attributes out of a
 * given class, if any, reforming them as an
 * {@code AnnotationDefault} annotation.
 *
 * @param cf {@code non-null;} the class in question
 * @return {@code null-ok;} an appropriately-constructed
 * {@code AnnotationDefault} annotation, if there were any
 * annotation defaults in the class, or {@code null} if not
 */
private static Annotation translateAnnotationDefaults(DirectClassFile cf) {
    CstType thisClass = cf.getThisClass();
    MethodList methods = cf.getMethods();
    int sz = methods.size();
    Annotation result =
        new Annotation(thisClass, AnnotationVisibility.EMBEDDED);
    boolean any = false;

    for (int i = 0; i < sz; i++) {
        Method one = methods.get(i);
        AttributeList attribs = one.getAttributes();
        AttAnnotationDefault oneDefault = (AttAnnotationDefault)
            attribs.findFirst(AttAnnotationDefault.ATTRIBUTE_NAME);

        if (oneDefault != null) {
            NameValuePair pair = new NameValuePair(
                    one.getNat().getName(),
                    oneDefault.getValue());
            result.add(pair);
            any = true;
        }
    }

    if (! any) {
        return null;
    }

    result.setImmutable();
    return AnnotationUtils.makeAnnotationDefault(result);
}
 
Example #10
Source File: AttributeTranslator.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the parameter annotations out of a given method. This
 * combines both visible and invisible annotations into a single
 * result set.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the list of annotation sets, which may be
 * empty
 */
public static AnnotationsList getParameterAnnotations(Method method) {
    AttributeList attribs = method.getAttributes();
    AttRuntimeVisibleParameterAnnotations visible =
        (AttRuntimeVisibleParameterAnnotations)
        attribs.findFirst(
                AttRuntimeVisibleParameterAnnotations.ATTRIBUTE_NAME);
    AttRuntimeInvisibleParameterAnnotations invisible =
        (AttRuntimeInvisibleParameterAnnotations)
        attribs.findFirst(
                AttRuntimeInvisibleParameterAnnotations.ATTRIBUTE_NAME);

    if (visible == null) {
        if (invisible == null) {
            return AnnotationsList.EMPTY;
        }
        return invisible.getParameterAnnotations();
    }

    if (invisible == null) {
        return visible.getParameterAnnotations();
    }

    // Both are non-null, so combine them.

    return AnnotationsList.combine(visible.getParameterAnnotations(),
            invisible.getParameterAnnotations());
}
 
Example #11
Source File: AttributeTranslator.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the annotations out of a given method, similar to {@link
 * #getAnnotations}, also including an annotation for the translation
 * of the method-specific attribute {@code Exceptions}.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getMethodAnnotations(Method method) {
    Annotations result = getAnnotations(method.getAttributes());
    TypeList exceptions = getExceptions(method);

    if (exceptions.size() != 0) {
        Annotation throwsAnnotation =
            AnnotationUtils.makeThrows(exceptions);
        result = Annotations.combine(result, throwsAnnotation);
    }

    return result;
}
 
Example #12
Source File: AttributeTranslator.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the list of thrown exceptions for a given method.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the list of thrown exceptions
 */
public static TypeList getExceptions(Method method) {
    AttributeList attribs = method.getAttributes();
    AttExceptions exceptions = (AttExceptions)
        attribs.findFirst(AttExceptions.ATTRIBUTE_NAME);

    if (exceptions == null) {
        return StdTypeList.EMPTY;
    }

    return exceptions.getExceptions();
}
 
Example #13
Source File: AttributeTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the {@code AnnotationDefault} attributes out of a
 * given class, if any, reforming them as an
 * {@code AnnotationDefault} annotation.
 *
 * @param cf {@code non-null;} the class in question
 * @return {@code null-ok;} an appropriately-constructed
 * {@code AnnotationDefault} annotation, if there were any
 * annotation defaults in the class, or {@code null} if not
 */
private static Annotation translateAnnotationDefaults(DirectClassFile cf) {
    CstType thisClass = cf.getThisClass();
    MethodList methods = cf.getMethods();
    int sz = methods.size();
    Annotation result =
        new Annotation(thisClass, AnnotationVisibility.EMBEDDED);
    boolean any = false;

    for (int i = 0; i < sz; i++) {
        Method one = methods.get(i);
        AttributeList attribs = one.getAttributes();
        AttAnnotationDefault oneDefault = (AttAnnotationDefault)
            attribs.findFirst(AttAnnotationDefault.ATTRIBUTE_NAME);

        if (oneDefault != null) {
            NameValuePair pair = new NameValuePair(
                    one.getNat().getName(),
                    oneDefault.getValue());
            result.add(pair);
            any = true;
        }
    }

    if (! any) {
        return null;
    }

    result.setImmutable();
    return AnnotationUtils.makeAnnotationDefault(result);
}
 
Example #14
Source File: AttributeTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the parameter annotations out of a given method. This
 * combines both visible and invisible annotations into a single
 * result set.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the list of annotation sets, which may be
 * empty
 */
public static AnnotationsList getParameterAnnotations(Method method) {
    AttributeList attribs = method.getAttributes();
    AttRuntimeVisibleParameterAnnotations visible =
        (AttRuntimeVisibleParameterAnnotations)
        attribs.findFirst(
                AttRuntimeVisibleParameterAnnotations.ATTRIBUTE_NAME);
    AttRuntimeInvisibleParameterAnnotations invisible =
        (AttRuntimeInvisibleParameterAnnotations)
        attribs.findFirst(
                AttRuntimeInvisibleParameterAnnotations.ATTRIBUTE_NAME);

    if (visible == null) {
        if (invisible == null) {
            return AnnotationsList.EMPTY;
        }
        return invisible.getParameterAnnotations();
    }

    if (invisible == null) {
        return visible.getParameterAnnotations();
    }

    // Both are non-null, so combine them.

    return AnnotationsList.combine(visible.getParameterAnnotations(),
            invisible.getParameterAnnotations());
}
 
Example #15
Source File: AttributeTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the annotations out of a given method, similar to {@link
 * #getAnnotations}, also including an annotation for the translation
 * of the method-specific attribute {@code Exceptions}.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getMethodAnnotations(Method method) {
    Annotations result = getAnnotations(method.getAttributes());
    TypeList exceptions = getExceptions(method);

    if (exceptions.size() != 0) {
        Annotation throwsAnnotation =
            AnnotationUtils.makeThrows(exceptions);
        result = Annotations.combine(result, throwsAnnotation);
    }

    return result;
}
 
Example #16
Source File: AttributeTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the list of thrown exceptions for a given method.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the list of thrown exceptions
 */
public static TypeList getExceptions(Method method) {
    AttributeList attribs = method.getAttributes();
    AttExceptions exceptions = (AttExceptions)
        attribs.findFirst(AttExceptions.ATTRIBUTE_NAME);

    if (exceptions == null) {
        return StdTypeList.EMPTY;
    }

    return exceptions.getExceptions();
}
 
Example #17
Source File: AttributeTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the {@code AnnotationDefault} attributes out of a
 * given class, if any, reforming them as an
 * {@code AnnotationDefault} annotation.
 *
 * @param cf {@code non-null;} the class in question
 * @return {@code null-ok;} an appropriately-constructed
 * {@code AnnotationDefault} annotation, if there were any
 * annotation defaults in the class, or {@code null} if not
 */
private static Annotation translateAnnotationDefaults(DirectClassFile cf) {
    CstType thisClass = cf.getThisClass();
    MethodList methods = cf.getMethods();
    int sz = methods.size();
    Annotation result =
        new Annotation(thisClass, AnnotationVisibility.EMBEDDED);
    boolean any = false;

    for (int i = 0; i < sz; i++) {
        Method one = methods.get(i);
        AttributeList attribs = one.getAttributes();
        AttAnnotationDefault oneDefault = (AttAnnotationDefault)
            attribs.findFirst(AttAnnotationDefault.ATTRIBUTE_NAME);

        if (oneDefault != null) {
            NameValuePair pair = new NameValuePair(
                    one.getNat().getName(),
                    oneDefault.getValue());
            result.add(pair);
            any = true;
        }
    }

    if (! any) {
        return null;
    }

    result.setImmutable();
    return AnnotationUtils.makeAnnotationDefault(result);
}
 
Example #18
Source File: AttributeTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the parameter annotations out of a given method. This
 * combines both visible and invisible annotations into a single
 * result set.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the list of annotation sets, which may be
 * empty
 */
public static AnnotationsList getParameterAnnotations(Method method) {
    AttributeList attribs = method.getAttributes();
    AttRuntimeVisibleParameterAnnotations visible =
        (AttRuntimeVisibleParameterAnnotations)
        attribs.findFirst(
                AttRuntimeVisibleParameterAnnotations.ATTRIBUTE_NAME);
    AttRuntimeInvisibleParameterAnnotations invisible =
        (AttRuntimeInvisibleParameterAnnotations)
        attribs.findFirst(
                AttRuntimeInvisibleParameterAnnotations.ATTRIBUTE_NAME);

    if (visible == null) {
        if (invisible == null) {
            return AnnotationsList.EMPTY;
        }
        return invisible.getParameterAnnotations();
    }

    if (invisible == null) {
        return visible.getParameterAnnotations();
    }

    // Both are non-null, so combine them.

    return AnnotationsList.combine(visible.getParameterAnnotations(),
            invisible.getParameterAnnotations());
}
 
Example #19
Source File: AttributeTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the list of thrown exceptions for a given method.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the list of thrown exceptions
 */
public static TypeList getExceptions(Method method) {
    AttributeList attribs = method.getAttributes();
    AttExceptions exceptions = (AttExceptions)
        attribs.findFirst(AttExceptions.ATTRIBUTE_NAME);

    if (exceptions == null) {
        return StdTypeList.EMPTY;
    }

    return exceptions.getExceptions();
}
 
Example #20
Source File: ConcreteMethod.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param method {@code non-null;} the method to be based on
 * @param classFile {@code non-null;} the class file that contains this method
 * @param keepLines whether to keep the line number information
 * (if any)
 * @param keepLocals whether to keep the local variable
 * information (if any)
 */
public ConcreteMethod(Method method, ClassFile classFile,
        boolean keepLines, boolean keepLocals) {
    this.method = method;
    this.classFile = classFile;

    AttributeList attribs = method.getAttributes();
    this.attCode = (AttCode) attribs.findFirst(AttCode.ATTRIBUTE_NAME);

    AttributeList codeAttribs = attCode.getAttributes();

    /*
     * Combine all LineNumberTable attributes into one, with the
     * combined result saved into the instance. The following code
     * isn't particularly efficient for doing merges, but as far
     * as I know, this situation rarely occurs "in the
     * wild," so there's not much point in optimizing for it.
     */
    LineNumberList lnl = LineNumberList.EMPTY;
    if (keepLines) {
        for (AttLineNumberTable lnt = (AttLineNumberTable)
                 codeAttribs.findFirst(AttLineNumberTable.ATTRIBUTE_NAME);
             lnt != null;
             lnt = (AttLineNumberTable) codeAttribs.findNext(lnt)) {
            lnl = LineNumberList.concat(lnl, lnt.getLineNumbers());
        }
    }
    this.lineNumbers = lnl;

    LocalVariableList lvl = LocalVariableList.EMPTY;
    if (keepLocals) {
        /*
         * Do likewise (and with the same caveat) for
         * LocalVariableTable and LocalVariableTypeTable attributes.
         * This combines both of these kinds of attribute into a
         * single LocalVariableList.
         */
        for (AttLocalVariableTable lvt = (AttLocalVariableTable)
                 codeAttribs.findFirst(
                         AttLocalVariableTable.ATTRIBUTE_NAME);
             lvt != null;
             lvt = (AttLocalVariableTable) codeAttribs.findNext(lvt)) {

            lvl = LocalVariableList.concat(lvl, lvt.getLocalVariables());
        }

        LocalVariableList typeList = LocalVariableList.EMPTY;
        for (AttLocalVariableTypeTable lvtt = (AttLocalVariableTypeTable)
                 codeAttribs.findFirst(
                         AttLocalVariableTypeTable.ATTRIBUTE_NAME);
             lvtt != null;
             lvtt = (AttLocalVariableTypeTable) codeAttribs.findNext(lvtt)) {
            typeList = LocalVariableList.concat(typeList, lvtt.getLocalVariables());
        }

        if (typeList.size() != 0) {

            lvl = LocalVariableList.mergeDescriptorsAndSignatures(lvl, typeList);
        }
    }
    this.localVariables = lvl;
}
 
Example #21
Source File: DotDumper.java    From Box with Apache License 2.0 4 votes vote down vote up
@Override
public void endParsingMember(ByteArray bytes, int offset, String name,
                             String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    ConcreteMethod meth = new ConcreteMethod((Method) member, classFile,
                                             true, true);

    TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
    RopMethod rmeth =
        Ropper.convert(meth, advice, classFile.getMethods(), dexOptions);

    if (optimize) {
        boolean isStatic = AccessFlags.isStatic(meth.getAccessFlags());
        rmeth = Optimizer.optimize(rmeth,
                BaseDumper.computeParamWidth(meth, isStatic), isStatic,
                true, advice);
    }

    System.out.println("digraph "  + name + "{");

    System.out.println("\tfirst -> n"
            + Hex.u2(rmeth.getFirstLabel()) + ";");

    BasicBlockList blocks = rmeth.getBlocks();

    int sz = blocks.size();
    for (int i = 0; i < sz; i++) {
        BasicBlock bb = blocks.get(i);
        int label = bb.getLabel();
        IntList successors = bb.getSuccessors();

        if (successors.size() == 0) {
            System.out.println("\tn" + Hex.u2(label) + " -> returns;");
        } else if (successors.size() == 1) {
            System.out.println("\tn" + Hex.u2(label) + " -> n"
                    + Hex.u2(successors.get(0)) + ";");
        } else {
            System.out.print("\tn" + Hex.u2(label) + " -> {");
            for (int j = 0; j < successors.size(); j++ ) {
                int successor = successors.get(j);

                if (successor != bb.getPrimarySuccessor()) {
                    System.out.print(" n" + Hex.u2(successor) + " ");
                }

            }
            System.out.println("};");

            System.out.println("\tn" + Hex.u2(label) + " -> n"
                    + Hex.u2(bb.getPrimarySuccessor())
                    + " [label=\"primary\"];");


        }
    }

    System.out.println("}");
}
 
Example #22
Source File: ConcreteMethod.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
public ConcreteMethod(Method method, CstString sourceFile,
        boolean keepLines, boolean keepLocals) {
    this.method = method;
    this.sourceFile = sourceFile;

    AttributeList attribs = method.getAttributes();
    this.attCode = (AttCode) attribs.findFirst(AttCode.ATTRIBUTE_NAME);

    AttributeList codeAttribs = attCode.getAttributes();

    /*
     * Combine all LineNumberTable attributes into one, with the
     * combined result saved into the instance. The following code
     * isn't particularly efficient for doing merges, but as far
     * as I know, this situation rarely occurs "in the
     * wild," so there's not much point in optimizing for it.
     */
    LineNumberList lineNumbers = LineNumberList.EMPTY;
    if (keepLines) {
        for (AttLineNumberTable lnt = (AttLineNumberTable)
                 codeAttribs.findFirst(AttLineNumberTable.ATTRIBUTE_NAME);
             lnt != null;
             lnt = (AttLineNumberTable) codeAttribs.findNext(lnt)) {
            lineNumbers = LineNumberList.concat(lineNumbers,
                    lnt.getLineNumbers());
        }
    }
    this.lineNumbers = lineNumbers;

    LocalVariableList localVariables = LocalVariableList.EMPTY;
    if (keepLocals) {
        /*
         * Do likewise (and with the same caveat) for
         * LocalVariableTable and LocalVariableTypeTable attributes.
         * This combines both of these kinds of attribute into a
         * single LocalVariableList.
         */
        for (AttLocalVariableTable lvt = (AttLocalVariableTable)
                 codeAttribs.findFirst(
                         AttLocalVariableTable.ATTRIBUTE_NAME);
             lvt != null;
             lvt = (AttLocalVariableTable) codeAttribs.findNext(lvt)) {
            localVariables =
                LocalVariableList.concat(localVariables,
                        lvt.getLocalVariables());
        }

        LocalVariableList typeList = LocalVariableList.EMPTY;
        for (AttLocalVariableTypeTable lvtt = (AttLocalVariableTypeTable)
                 codeAttribs.findFirst(
                         AttLocalVariableTypeTable.ATTRIBUTE_NAME);
             lvtt != null;
             lvtt =
                 (AttLocalVariableTypeTable) codeAttribs.findNext(lvtt)) {
            typeList =
                LocalVariableList.concat(typeList,
                        lvtt.getLocalVariables());
        }

        if (typeList.size() != 0) {
            localVariables =
                LocalVariableList.mergeDescriptorsAndSignatures(
                        localVariables, typeList);
        }
    }
    this.localVariables = localVariables;
}
 
Example #23
Source File: DotDumper.java    From Box with Apache License 2.0 4 votes vote down vote up
@Override
public void endParsingMember(ByteArray bytes, int offset, String name,
                             String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    ConcreteMethod meth = new ConcreteMethod((Method) member, classFile,
                                             true, true);

    TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
    RopMethod rmeth =
        Ropper.convert(meth, advice, classFile.getMethods(), dexOptions);

    if (optimize) {
        boolean isStatic = AccessFlags.isStatic(meth.getAccessFlags());
        rmeth = Optimizer.optimize(rmeth,
                BaseDumper.computeParamWidth(meth, isStatic), isStatic,
                true, advice);
    }

    System.out.println("digraph "  + name + "{");

    System.out.println("\tfirst -> n"
            + Hex.u2(rmeth.getFirstLabel()) + ";");

    BasicBlockList blocks = rmeth.getBlocks();

    int sz = blocks.size();
    for (int i = 0; i < sz; i++) {
        BasicBlock bb = blocks.get(i);
        int label = bb.getLabel();
        IntList successors = bb.getSuccessors();

        if (successors.size() == 0) {
            System.out.println("\tn" + Hex.u2(label) + " -> returns;");
        } else if (successors.size() == 1) {
            System.out.println("\tn" + Hex.u2(label) + " -> n"
                    + Hex.u2(successors.get(0)) + ";");
        } else {
            System.out.print("\tn" + Hex.u2(label) + " -> {");
            for (int j = 0; j < successors.size(); j++ ) {
                int successor = successors.get(j);

                if (successor != bb.getPrimarySuccessor()) {
                    System.out.print(" n" + Hex.u2(successor) + " ");
                }

            }
            System.out.println("};");

            System.out.println("\tn" + Hex.u2(label) + " -> n"
                    + Hex.u2(bb.getPrimarySuccessor())
                    + " [label=\"primary\"];");


        }
    }

    System.out.println("}");
}
 
Example #24
Source File: ConcreteMethod.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param method {@code non-null;} the method to be based on
 * @param classFile {@code non-null;} the class file that contains this method
 * @param keepLines whether to keep the line number information
 * (if any)
 * @param keepLocals whether to keep the local variable
 * information (if any)
 */
public ConcreteMethod(Method method, ClassFile classFile,
        boolean keepLines, boolean keepLocals) {
    this.method = method;
    this.classFile = classFile;

    AttributeList attribs = method.getAttributes();
    this.attCode = (AttCode) attribs.findFirst(AttCode.ATTRIBUTE_NAME);

    AttributeList codeAttribs = attCode.getAttributes();

    /*
     * Combine all LineNumberTable attributes into one, with the
     * combined result saved into the instance. The following code
     * isn't particularly efficient for doing merges, but as far
     * as I know, this situation rarely occurs "in the
     * wild," so there's not much point in optimizing for it.
     */
    LineNumberList lnl = LineNumberList.EMPTY;
    if (keepLines) {
        for (AttLineNumberTable lnt = (AttLineNumberTable)
                 codeAttribs.findFirst(AttLineNumberTable.ATTRIBUTE_NAME);
             lnt != null;
             lnt = (AttLineNumberTable) codeAttribs.findNext(lnt)) {
            lnl = LineNumberList.concat(lnl, lnt.getLineNumbers());
        }
    }
    this.lineNumbers = lnl;

    LocalVariableList lvl = LocalVariableList.EMPTY;
    if (keepLocals) {
        /*
         * Do likewise (and with the same caveat) for
         * LocalVariableTable and LocalVariableTypeTable attributes.
         * This combines both of these kinds of attribute into a
         * single LocalVariableList.
         */
        for (AttLocalVariableTable lvt = (AttLocalVariableTable)
                 codeAttribs.findFirst(
                         AttLocalVariableTable.ATTRIBUTE_NAME);
             lvt != null;
             lvt = (AttLocalVariableTable) codeAttribs.findNext(lvt)) {

            lvl = LocalVariableList.concat(lvl, lvt.getLocalVariables());
        }

        LocalVariableList typeList = LocalVariableList.EMPTY;
        for (AttLocalVariableTypeTable lvtt = (AttLocalVariableTypeTable)
                 codeAttribs.findFirst(
                         AttLocalVariableTypeTable.ATTRIBUTE_NAME);
             lvtt != null;
             lvtt = (AttLocalVariableTypeTable) codeAttribs.findNext(lvtt)) {
            typeList = LocalVariableList.concat(typeList, lvtt.getLocalVariables());
        }

        if (typeList.size() != 0) {

            lvl = LocalVariableList.mergeDescriptorsAndSignatures(lvl, typeList);
        }
    }
    this.localVariables = lvl;
}
 
Example #25
Source File: ConcreteMethod.java    From buck with Apache License 2.0 4 votes vote down vote up
public ConcreteMethod(Method method, int accessFlags, CstString sourceFile,
        boolean keepLines, boolean keepLocals) {
    this.method = method;
    this.accSuper = (accessFlags & AccessFlags.ACC_SUPER) != 0;
    this.sourceFile = sourceFile;

    AttributeList attribs = method.getAttributes();
    this.attCode = (AttCode) attribs.findFirst(AttCode.ATTRIBUTE_NAME);

    AttributeList codeAttribs = attCode.getAttributes();

    /*
     * Combine all LineNumberTable attributes into one, with the
     * combined result saved into the instance. The following code
     * isn't particularly efficient for doing merges, but as far
     * as I know, this situation rarely occurs "in the
     * wild," so there's not much point in optimizing for it.
     */
    LineNumberList lineNumbers = LineNumberList.EMPTY;
    if (keepLines) {
        for (AttLineNumberTable lnt = (AttLineNumberTable)
                 codeAttribs.findFirst(AttLineNumberTable.ATTRIBUTE_NAME);
             lnt != null;
             lnt = (AttLineNumberTable) codeAttribs.findNext(lnt)) {
            lineNumbers = LineNumberList.concat(lineNumbers,
                    lnt.getLineNumbers());
        }
    }
    this.lineNumbers = lineNumbers;

    LocalVariableList localVariables = LocalVariableList.EMPTY;
    if (keepLocals) {
        /*
         * Do likewise (and with the same caveat) for
         * LocalVariableTable and LocalVariableTypeTable attributes.
         * This combines both of these kinds of attribute into a
         * single LocalVariableList.
         */
        for (AttLocalVariableTable lvt = (AttLocalVariableTable)
                 codeAttribs.findFirst(
                         AttLocalVariableTable.ATTRIBUTE_NAME);
             lvt != null;
             lvt = (AttLocalVariableTable) codeAttribs.findNext(lvt)) {
            localVariables =
                LocalVariableList.concat(localVariables,
                        lvt.getLocalVariables());
        }

        LocalVariableList typeList = LocalVariableList.EMPTY;
        for (AttLocalVariableTypeTable lvtt = (AttLocalVariableTypeTable)
                 codeAttribs.findFirst(
                         AttLocalVariableTypeTable.ATTRIBUTE_NAME);
             lvtt != null;
             lvtt =
                 (AttLocalVariableTypeTable) codeAttribs.findNext(lvtt)) {
            typeList =
                LocalVariableList.concat(typeList,
                        lvtt.getLocalVariables());
        }

        if (typeList.size() != 0) {
            localVariables =
                LocalVariableList.mergeDescriptorsAndSignatures(
                        localVariables, typeList);
        }
    }
    this.localVariables = localVariables;
}
 
Example #26
Source File: DotDumper.java    From buck with Apache License 2.0 4 votes vote down vote up
public void endParsingMember(ByteArray bytes, int offset, String name,
                             String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    ConcreteMethod meth = new ConcreteMethod((Method) member, classFile,
                                             true, true);

    TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
    RopMethod rmeth =
        Ropper.convert(meth, advice, classFile.getMethods());

    if (optimize) {
        boolean isStatic = AccessFlags.isStatic(meth.getAccessFlags());
        rmeth = Optimizer.optimize(rmeth,
                BaseDumper.computeParamWidth(meth, isStatic), isStatic,
                true, advice);
    }

    System.out.println("digraph "  + name + "{");

    System.out.println("\tfirst -> n"
            + Hex.u2(rmeth.getFirstLabel()) + ";");

    BasicBlockList blocks = rmeth.getBlocks();

    int sz = blocks.size();
    for (int i = 0; i < sz; i++) {
        BasicBlock bb = blocks.get(i);
        int label = bb.getLabel();
        IntList successors = bb.getSuccessors();

        if (successors.size() == 0) {
            System.out.println("\tn" + Hex.u2(label) + " -> returns;");
        } else if (successors.size() == 1) {
            System.out.println("\tn" + Hex.u2(label) + " -> n"
                    + Hex.u2(successors.get(0)) + ";");
        } else {
            System.out.print("\tn" + Hex.u2(label) + " -> {");
            for (int j = 0; j < successors.size(); j++ ) {
                int successor = successors.get(j);

                if (successor != bb.getPrimarySuccessor()) {
                    System.out.print(" n" + Hex.u2(successor) + " ");
                }

            }
            System.out.println("};");

            System.out.println("\tn" + Hex.u2(label) + " -> n"
                    + Hex.u2(bb.getPrimarySuccessor())
                    + " [label=\"primary\"];");


        }
    }

    System.out.println("}");
}
 
Example #27
Source File: ConcreteMethod.java    From J2ME-Loader with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param method {@code non-null;} the method to be based on
 * @param cf {@code non-null;} the class file that contains this method
 * @param keepLines whether to keep the line number information
 * (if any)
 * @param keepLocals whether to keep the local variable
 * information (if any)
 */
public ConcreteMethod(Method method, ClassFile cf, boolean keepLines, boolean keepLocals) {
    this(method, cf.getSourceFile(), keepLines, keepLocals);
}
 
Example #28
Source File: ConcreteMethod.java    From buck with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param method {@code non-null;} the method to be based on
 * @param cf {@code non-null;} the class file that contains this method
 * @param keepLines whether to keep the line number information
 * (if any)
 * @param keepLocals whether to keep the local variable
 * information (if any)
 */
public ConcreteMethod(Method method, ClassFile cf, boolean keepLines, boolean keepLocals) {
    this(method, cf.getAccessFlags(), cf.getSourceFile(), keepLines, keepLocals);
}