All Downloads are FREE. Search and download functionalities are using the official Maven repository.

framework.src.org.checkerframework.framework.util.element.IndexedElementAnnotationApplier Maven / Gradle / Ivy

Go to download

The Checker Framework enhances Java’s type system to make it more powerful and useful. This lets software developers detect and prevent errors in their Java programs. The Checker Framework includes compiler plug-ins ("checkers") that find bugs or verify their absence. It also permits you to write your own compiler plug-ins.

There is a newer version: 3.42.0
Show newest version
package org.checkerframework.framework.util.element;

import org.checkerframework.framework.type.AnnotatedTypeMirror;

import java.util.List;
import java.util.Map;

import javax.lang.model.element.Element;

import com.sun.tools.javac.code.Attribute;

/**
 * Some Elements are members of a list (formal method parameters and type parameters).
 * This class ensures that the targeted annotations passed by handleTargeted - see
 * {@link TargetedElementAnnotationApplier} -
 * only include those with a position that matches the index returned by getElementIndex.
 */
abstract class IndexedElementAnnotationApplier extends TargetedElementAnnotationApplier {

    public IndexedElementAnnotationApplier(AnnotatedTypeMirror type, Element element) {
        super(type, element);
    }

    /**
     * The index of element in the list of elements that contains it
     */
    public abstract int getElementIndex();


    /**
     * A TypeAnnotationPosition has a number of different indexes (type_index, bound_index, param_index)
     * Return the index we are interested in.  If offsetting needs to be done it should be done in getElementIndex
     * not here. (see ElementAnnotationUtils.getBoundIndexOffset )
     * @param anno an annotation we might wish to apply
     * @return the index value this applier compares against the getElementIndex
     */
    public abstract int getTypeCompoundIndex(final Attribute.TypeCompound anno);


    @Override
    protected Map> sift(Iterable typeCompounds) {
        final Map> targetClassToAnnos = super.sift(typeCompounds);

        final List targeted = targetClassToAnnos.get(TargetClass.TARGETED);
        final List valid    = targetClassToAnnos.get(TargetClass.VALID);

        final int paramIndex = getElementIndex();

        // filter out annotations in targeted that don't have the correct parameter index. (i.e the one's that
        // are on the same method but don't pertain to the parameter element being processed, see class comments ).
        // Place these annotations into the valid list.
        int i = 0;
        while (i < targeted.size()) {
            if ( getTypeCompoundIndex(targeted.get(i)) != paramIndex) {
                valid.add(targeted.remove(i));
            } else {
                ++i;
            }
        }

        return targetClassToAnnos;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy