Search in sources :

Example 1 with CtModifiable

use of spoon.reflect.declaration.CtModifiable in project spoon by INRIA.

the class PotentialVariableDeclarationFunction method apply.

@Override
public void apply(CtElement input, CtConsumer<Object> outputConsumer) {
    isTypeOnTheWay = false;
    isInStaticScope = false;
    // Search previous siblings for element which may represents the declaration of this local variable
    CtQuery siblingsQuery = input.getFactory().createQuery().map(new SiblingsFunction().mode(SiblingsFunction.Mode.PREVIOUS)).select(new TypeFilter<>(CtVariable.class));
    if (variableName != null) {
        // variable name is defined so we have to search only for variables with that name
        siblingsQuery = siblingsQuery.select(new NamedElementFilter<>(CtNamedElement.class, variableName));
    }
    CtElement scopeElement = input;
    // Search input and then all parents until first CtPackage for element which may represents the declaration of this local variable
    while (scopeElement != null && !(scopeElement instanceof CtPackage) && scopeElement.isParentInitialized()) {
        CtElement parent = scopeElement.getParent();
        if (parent instanceof CtType<?>) {
            isTypeOnTheWay = true;
            // visit each CtField of `parent` CtType
            CtQuery q = parent.map(new AllTypeMembersFunction(CtField.class));
            q.forEach((CtField<?> field) -> {
                if (isInStaticScope && field.hasModifier(ModifierKind.STATIC) == false) {
                    /*
						 * the variable reference is used in static scope,
						 * but the field is not static - ignore it
						 */
                    return;
                }
                // else send field as potential variable declaration
                if (sendToOutput(field, outputConsumer)) {
                    // and terminate the internal query q if outer query is already terminated
                    q.terminate();
                }
            });
            if (query.isTerminated()) {
                return;
            }
        } else if (parent instanceof CtBodyHolder || parent instanceof CtStatementList) {
            // visit all previous CtVariable siblings of scopeElement element in parent BodyHolder or Statement list
            siblingsQuery.setInput(scopeElement).forEach(outputConsumer);
            if (query.isTerminated()) {
                return;
            }
            // visit parameters of CtCatch and CtExecutable (method, lambda)
            if (parent instanceof CtCatch) {
                CtCatch ctCatch = (CtCatch) parent;
                if (sendToOutput(ctCatch.getParameter(), outputConsumer)) {
                    return;
                }
            } else if (parent instanceof CtExecutable) {
                CtExecutable<?> exec = (CtExecutable<?>) parent;
                for (CtParameter<?> param : exec.getParameters()) {
                    if (sendToOutput(param, outputConsumer)) {
                        return;
                    }
                }
            }
        }
        if (parent instanceof CtModifiable) {
            isInStaticScope = isInStaticScope || ((CtModifiable) parent).hasModifier(ModifierKind.STATIC);
        }
        scopeElement = parent;
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtQuery(spoon.reflect.visitor.chain.CtQuery) CtExecutable(spoon.reflect.declaration.CtExecutable) CtBodyHolder(spoon.reflect.code.CtBodyHolder) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) CtVariable(spoon.reflect.declaration.CtVariable) CtPackage(spoon.reflect.declaration.CtPackage) CtStatementList(spoon.reflect.code.CtStatementList) CtCatch(spoon.reflect.code.CtCatch) CtModifiable(spoon.reflect.declaration.CtModifiable)

Example 2 with CtModifiable

use of spoon.reflect.declaration.CtModifiable in project spoon by INRIA.

the class EqualsChecker method scanCtModifiable.

@Override
public void scanCtModifiable(CtModifiable m) {
    final CtModifiable peek = (CtModifiable) this.other;
    if (m.getVisibility() == null) {
        if (peek.getVisibility() != null) {
            isNotEqual = true;
            return;
        }
    } else if (peek.getVisibility() == null) {
        isNotEqual = true;
        return;
    } else if (!m.getVisibility().equals(peek.getVisibility())) {
        isNotEqual = true;
        return;
    }
    if (m.getModifiers().size() != peek.getModifiers().size()) {
        isNotEqual = true;
        return;
    }
    if (!m.getModifiers().containsAll(peek.getModifiers())) {
        isNotEqual = true;
        return;
    }
    super.scanCtModifiable(m);
}
Also used : CtModifiable(spoon.reflect.declaration.CtModifiable)

Aggregations

CtModifiable (spoon.reflect.declaration.CtModifiable)2 CtBodyHolder (spoon.reflect.code.CtBodyHolder)1 CtCatch (spoon.reflect.code.CtCatch)1 CtStatementList (spoon.reflect.code.CtStatementList)1 CtElement (spoon.reflect.declaration.CtElement)1 CtExecutable (spoon.reflect.declaration.CtExecutable)1 CtField (spoon.reflect.declaration.CtField)1 CtPackage (spoon.reflect.declaration.CtPackage)1 CtType (spoon.reflect.declaration.CtType)1 CtVariable (spoon.reflect.declaration.CtVariable)1 CtQuery (spoon.reflect.visitor.chain.CtQuery)1