Java Code Examples for com.android.dx.cf.iface.Method#getAttributes()

The following examples show how to use com.android.dx.cf.iface.Method#getAttributes() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 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 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 11
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 12
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 13
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 14
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 15
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 16
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;
}