Skip to content

Commit 09c9824

Browse files
committed
added JavaDSLOOWithinTypeBasicSymbolsResolver
1 parent f96a165 commit 09c9824

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package de.monticore.java.javadsl.types3;
2+
3+
import com.google.common.base.Preconditions;
4+
import de.monticore.symbols.basicsymbols._symboltable.FunctionSymbol;
5+
import de.monticore.symbols.oosymbols._symboltable.OOTypeSymbol;
6+
import de.monticore.symboltable.IScopeSpanningSymbol;
7+
import de.monticore.symboltable.modifiers.AccessModifier;
8+
import de.monticore.types.check.SymTypeExpression;
9+
import de.monticore.types.check.SymTypeOfFunction;
10+
import de.monticore.types3.util.OOWithinTypeBasicSymbolsResolver;
11+
import de.monticore.types3.util.WithinTypeBasicSymbolsResolver;
12+
import de.se_rwth.commons.logging.Log;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.Optional;
17+
import java.util.function.Predicate;
18+
import java.util.stream.Collectors;
19+
20+
public class JavaDSLOOWithinTypeBasicSymbolsResolver extends OOWithinTypeBasicSymbolsResolver {
21+
22+
@Override
23+
protected List<SymTypeOfFunction> resolvedFunctionsInSuperTypes(SymTypeExpression thisType,
24+
String name, AccessModifier accessModifier, Predicate<FunctionSymbol> predicate) {
25+
List<SymTypeOfFunction> resolvedFuncsInSuperTypes =
26+
super.resolvedFunctionsInSuperTypes(thisType, name, accessModifier, predicate);
27+
List<SymTypeOfFunction> filteredFuncs =
28+
filterMultipleInterfaceImplementation(resolvedFuncsInSuperTypes);
29+
30+
return filteredFuncs;
31+
}
32+
33+
protected List<SymTypeOfFunction> filterMultipleInterfaceImplementation(
34+
List<SymTypeOfFunction> funcs) {
35+
if (funcs.size() < 2) {
36+
return funcs;
37+
}
38+
List<SymTypeOfFunction> functionInheritedFromClasses = funcs.stream()
39+
.filter(x -> getEnclosingType(x).isPresent() && getEnclosingType(x).get().isIsClass())
40+
.collect(Collectors.toList());
41+
if (functionInheritedFromClasses.isEmpty()) {
42+
return funcs;
43+
}
44+
else if (functionInheritedFromClasses.size() > 1) {
45+
Log.error("0x7A017: Can not filter inherited functions with multiple superclasses present.");
46+
return funcs;
47+
}
48+
else {
49+
SymTypeOfFunction classFunc = functionInheritedFromClasses.get(0);
50+
Optional<OOTypeSymbol> classFuncSymbol = getEnclosingType(classFunc);
51+
if (classFuncSymbol.isEmpty()) {
52+
Log.error("0x7A018: Enclosing type of classFunc is empty!");
53+
return funcs;
54+
}
55+
List<SymTypeExpression> classFuncSuperTypes = classFuncSymbol.get().getSuperTypesList();
56+
List<OOTypeSymbol> classFuncSuperTypeSymbols =
57+
getOOTypeSymbolsIfAvailable(classFuncSuperTypes);
58+
List<SymTypeOfFunction> functionInheritedFromInterfaces = funcs.stream()
59+
.filter(x -> getEnclosingType(x).isPresent() && getEnclosingType(x).get().isIsInterface())
60+
.collect(Collectors.toList());
61+
List<SymTypeOfFunction> filteredFuncs = new ArrayList<>();
62+
filteredFuncs.add(classFunc);
63+
for (SymTypeOfFunction func : functionInheritedFromInterfaces) {
64+
Optional<OOTypeSymbol> funcInterface = getEnclosingType(func);
65+
if (funcInterface.isPresent()) {
66+
if (!isInterfaceContainedInSuperClass(classFuncSuperTypeSymbols, funcInterface.get())) {
67+
filteredFuncs.add(func);
68+
}
69+
}
70+
}
71+
return filteredFuncs;
72+
}
73+
}
74+
75+
protected List<OOTypeSymbol> getOOTypeSymbolsIfAvailable(List<SymTypeExpression> exprs) {
76+
// TODO: Replace with TypeDispatcher when fixed
77+
return exprs.stream().filter(SymTypeExpression::hasTypeInfo).map(SymTypeExpression::getTypeInfo)
78+
.filter(x -> x instanceof OOTypeSymbol).map(x -> (OOTypeSymbol) x)
79+
.collect(Collectors.toList());
80+
}
81+
82+
protected boolean isInterfaceContainedInSuperClass(List<OOTypeSymbol> parents,
83+
OOTypeSymbol testingType) {
84+
if (parents.stream().anyMatch(x -> x.equals(testingType))) {
85+
return true;
86+
}
87+
else {
88+
for (OOTypeSymbol parent : parents) {
89+
List<OOTypeSymbol> newParents = getOOTypeSymbolsIfAvailable(parent.getSuperTypesList());
90+
if (isInterfaceContainedInSuperClass(newParents, testingType)) {
91+
return true;
92+
}
93+
}
94+
return false;
95+
}
96+
}
97+
98+
protected Optional<OOTypeSymbol> getEnclosingType(SymTypeOfFunction func) {
99+
if (func.hasSymbol()) {
100+
IScopeSpanningSymbol spanningSymbol =
101+
func.getSymbol().getEnclosingScope().getSpanningSymbol();
102+
if (spanningSymbol instanceof OOTypeSymbol) {
103+
return Optional.of((OOTypeSymbol) spanningSymbol);
104+
}
105+
}
106+
return Optional.empty();
107+
}
108+
109+
// static delegate
110+
111+
public static void init() {
112+
Log.trace("init JavaDSLOOWithinTypeBasicSymbolsResolver", "TypeCheck setup");
113+
setDelegate(new JavaDSLOOWithinTypeBasicSymbolsResolver());
114+
}
115+
116+
public static void reset() {
117+
OOWithinTypeBasicSymbolsResolver.delegate = null;
118+
WithinTypeBasicSymbolsResolver.reset();
119+
}
120+
121+
protected static void setDelegate(JavaDSLOOWithinTypeBasicSymbolsResolver newDelegate) {
122+
OOWithinTypeBasicSymbolsResolver.delegate = Preconditions.checkNotNull(newDelegate);
123+
WithinTypeBasicSymbolsResolver.setDelegate(newDelegate);
124+
}
125+
126+
protected static OOWithinTypeBasicSymbolsResolver getDelegate() {
127+
if (OOWithinTypeBasicSymbolsResolver.delegate == null) {
128+
init();
129+
}
130+
return OOWithinTypeBasicSymbolsResolver.delegate;
131+
}
132+
}

0 commit comments

Comments
 (0)