001package de.monochromata.anaphors.ast.relatedexp.strategy;
002
003import static java.util.Collections.emptyList;
004
005import java.util.List;
006
007import org.apache.commons.lang3.tuple.Pair;
008
009import de.monochromata.anaphors.ast.relatedexp.RelatedExpression;
010import de.monochromata.anaphors.ast.spi.RelatedExpressionsSpi;
011import de.monochromata.anaphors.perspectivation.Perspectivation;
012import de.monochromata.anaphors.preferences.Preferences;
013
014/**
015 * Local variable declarations functioning as related expression.
016 *
017 * @param <N>  The node type in the AST
018 * @param <E>  The expression type
019 * @param <T>  The type type
020 * @param <B>  The binding type
021 * @param <MB> The method binding type
022 * @param <TB> The type binding type
023 * @param <S>  The scope type (optional)
024 * @param <I>  The type used to represent identifiers
025 * @param <QI> The type used to represent qualified identifiers
026 * @param <EV> The type of the event contained in the condition that is
027 *             evaluated to check when the perspectivations shall be applied.
028 * @param <PP> The type used for positions that carry perspectivations
029 * @param <R>  The sub-type of related expression to use
030 */
031public class LocalVariableDeclarationStrategy<N, E, T, B, MB extends B, TB extends B, S, I, QI, EV, PP, R extends RelatedExpression<N, T, B, TB, S, QI, R>>
032        extends AbstractRelatedExpressionStrategy<N, E, T, B, MB, TB, S, I, QI, EV, PP, R> {
033
034    public static final String LVD_KIND = "LVD";
035
036    /**
037     * Used in contract testing.
038     */
039    @SuppressWarnings("unused")
040    protected LocalVariableDeclarationStrategy() {
041    }
042
043    public LocalVariableDeclarationStrategy(
044            final RelatedExpressionsSpi<N, E, T, B, MB, TB, S, I, QI, EV, PP, R> relatedExpressionsSpi,
045            final Preferences preferences) {
046        super(relatedExpressionsSpi, preferences);
047    }
048
049    @Override
050    public String getKind() {
051        return LVD_KIND;
052    }
053
054    @Override
055    public List<Perspectivation> underspecifyRelatedExpression(final R relatedExpression,
056            final List<Pair<LocalTempVariableContents, String>> variableContentsAndAnaphors, final S scope) {
057        // No perspectivation necessary
058        return emptyList();
059    }
060
061    protected boolean refersToSameAstNode(final R thisInstance, final R otherInstance) {
062        return thisInstance.equals(otherInstance)
063                && thisInstance.getRelatedExpression() == otherInstance.getRelatedExpression();
064    }
065
066    protected boolean isOnlyFragment(final R thisInstance, final R otherInstance,
067            final RelatedExpressionsSpi<N, E, T, B, MB, TB, S, I, QI, EV, PP, R> relatedExpressionsSpi) {
068        return relatedExpressionsSpi.isOnlyFragmentOfMultiVariable(thisInstance.getRelatedExpression(),
069                otherInstance.getRelatedExpression());
070    }
071
072    protected boolean isInitializer(final R thisInstance, final R otherInstance,
073            final RelatedExpressionsSpi<N, E, T, B, MB, TB, S, I, QI, EV, PP, R> relatedExpressionsSpi) {
074        return otherInstance.getStrategy() instanceof ClassInstanceCreationStrategy && relatedExpressionsSpi
075                .hasInitializer(thisInstance.getRelatedExpression(), otherInstance.getRelatedExpression());
076    }
077}