001package de.monochromata.anaphors.ast.spi; 002 003import static java.util.Collections.singletonList; 004 005import java.util.List; 006import java.util.Set; 007import java.util.function.Predicate; 008 009import org.apache.commons.lang3.tuple.ImmutablePair; 010import org.apache.commons.lang3.tuple.Pair; 011 012import de.monochromata.anaphors.ast.feature.Feature; 013import de.monochromata.anaphors.ast.relatedexp.RelatedExpression; 014import de.monochromata.anaphors.ast.relatedexp.strategy.LocalTempVariableContents; 015import de.monochromata.anaphors.perspectivation.Perspectivation; 016 017/** 018 * A special service provider interface to perform actions related to finding 019 * and processing related expressions. 020 * 021 * @param <N> The node type in the AST 022 * @param <E> The expression type The class instance creation expression node 023 * type 024 * @param <T> The type type 025 * @param <B> The binding type 026 * @param <MB> The method binding type 027 * @param <TB> The type binding type 028 * @param <S> The scope type (optional) 029 * @param <I> The type used to represent identifiers 030 * @param <QI> The type used to represent qualified identifiers 031 * @param <EV> The type of the event contained in the condition that is 032 * evaluated to check when the perspectivations shall be applied. 033 * @param <PP> The type used for positions that carry perspectivations 034 * @param <R> The sub-type of related expression to use 035 */ 036public interface RelatedExpressionsSpi<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>> { 037 038 /** 039 * @return {@literal true} if the {@code node1} is able to declare more than one 040 * variable but {@code node2} is the only variable declaration that 041 * {@code node1} actually contains. 042 */ 043 boolean isOnlyFragmentOfMultiVariable(final N node1, final N node2); 044 045 boolean hasInitializer(N node, N potentialInitializer); 046 047 T getReservedTypeVar(S scope); 048 049 // TODO: Array creation 050 051 Set<Feature<QI>> getFeatures(N relatedExpression); 052 053 /** 054 * Compares all strings occurring in the two AST nodes. 055 * 056 * @throws IllegalArgumentException if the given nodes do not have the same 057 * type. 058 */ 059 boolean compare(N node1, N node2); 060 061 String getDescription(N node); 062 063 int getLine(N node); 064 065 int getColumn(N node); 066 067 I getIdentifier(final TB typeBinding); 068 069 int getLengthOfSimpleNameOfType(final T type); 070 071 int getLength(final I identifier); 072 073 default I guessTempName(final R relatedExpression, final String anaphor, 074 final LocalTempVariableContents localTempVariableContents, final S scope) { 075 return guessTempName(relatedExpression, singletonList(new ImmutablePair<>(localTempVariableContents, anaphor)), 076 scope); 077 } 078 079 I guessTempName(final R relatedExpression, 080 final List<Pair<LocalTempVariableContents, String>> indirectionsAndAnaphors, final S scope); 081 082 QI toQualifiedIdentifier(final I identifier); 083 084 MB resolveMethodInvocationBinding(N relatedExpression, S scope); 085 086 String identifierToString(final I identifier); 087 088 String qualifiedIdentifierToString(final QI qualifiedIdentifier); 089 090 default boolean supportsLocalVariableTypeInference(final S scope) { 091 return false; 092 } 093 094 PP createPositionForNode(final N node, final Predicate<EV> condition, final List<Perspectivation> perspectivations); 095}