Search in sources :

Example 1 with JavaElement

use of org.revapi.java.spi.JavaElement in project revapi by revapi.

the class AbstractIncludeExcludeFilter method decide.

@SuppressWarnings("ConstantConditions")
private boolean decide(@Nullable Object element) {
    // we don't exclude anything that we don't handle...
    if (doNothing || !(element instanceof JavaElement)) {
        return true;
    }
    InclusionState ret = elementResults.get(element);
    if (ret != null) {
        return ret.toBoolean();
    }
    JavaElement el = (JavaElement) element;
    // exploit the fact that parent elements are always filtered before the children
    Element parent = el.getParent();
    InclusionState parentInclusionState = parent == null ? InclusionState.UNDECIDED : elementResults.get(parent);
    // if we have no record of the parent inclusion, then this is a top-level class. Assume it wants to be included.
    if (parentInclusionState == null) {
        parentInclusionState = InclusionState.UNDECIDED;
    }
    // this is a java element, but not a model-based element - i.e. this is an annotation.
    if (!(element instanceof JavaModelElement)) {
        return decideAnnotation((JavaAnnotationElement) element, parentInclusionState);
    }
    JavaModelElement javaElement = (JavaModelElement) element;
    Stream<String> tested = getTestedElementRepresentations(javaElement);
    // let's first assume we're going to inherit the parent's inclusion state
    ret = parentInclusionState;
    // now see if we need to change that assumption
    switch(parentInclusionState) {
        case INCLUDED:
            // on this element should be excluded
            if (excludeTest != null) {
                if (tested.anyMatch(s -> excludeTest.test(s))) {
                    ret = InclusionState.EXCLUDED;
                }
            }
            break;
        case EXCLUDED:
            if (!canBeReIncluded(javaElement)) {
                break;
            }
        // i.e. this fall-through is intentional.
        case UNDECIDED:
            // ok, the parent is undecided. This means we have to do the full checks on this element.
            List<String> testedList = null;
            if (includeTest != null && excludeTest != null) {
                testedList = tested.collect(toList());
                tested = testedList.stream();
            }
            if (includeTest != null) {
                // ok, there is an include test but the parent is undecided. This means that the parent actually
                // didn't match the include test. Let's check with this element.
                ret = tested.anyMatch(s -> includeTest.test(s)) ? InclusionState.INCLUDED : InclusionState.EXCLUDED;
            }
            if (excludeTest != null) {
                if (testedList != null) {
                    tested = testedList.stream();
                }
                if (tested.anyMatch(s -> excludeTest.test(s))) {
                    ret = InclusionState.EXCLUDED;
                }
            }
            break;
    }
    elementResults.put(element, ret);
    return ret.toBoolean();
}
Also used : JavaElement(org.revapi.java.spi.JavaElement) JavaModelElement(org.revapi.java.spi.JavaModelElement) JavaModelElement(org.revapi.java.spi.JavaModelElement) Element(org.revapi.Element) JavaElement(org.revapi.java.spi.JavaElement) JavaAnnotationElement(org.revapi.java.spi.JavaAnnotationElement)

Aggregations

Element (org.revapi.Element)1 JavaAnnotationElement (org.revapi.java.spi.JavaAnnotationElement)1 JavaElement (org.revapi.java.spi.JavaElement)1 JavaModelElement (org.revapi.java.spi.JavaModelElement)1