Skip to content

Commit f74988d

Browse files
committed
SONARJAVA-1589 fix validation of substitution
1 parent d5b3a84 commit f74988d

5 files changed

Lines changed: 46 additions & 4 deletions

File tree

its/ruling/src/test/resources/java-squid/squid-S2698.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,9 @@
16471647
57,
16481648
58,
16491649
59,
1650+
64,
1651+
65,
1652+
66,
16501653
],
16511654
'org.sonarsource.java:java-squid:src/test/java/org/sonar/java/resolve/ImportResolutionTest.java':[
16521655
48,

java-checks/src/test/files/checks/EnumSetCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public void doSomething(Set<COLOR> param) { // compliant, we ignore parameters.
3131
Set<Integer> ports2 = new HashSet<>();
3232
Set<COLOR> ports = new HashSet<>(); // Noncompliant [[sc=24;ec=39]] {{Convert this Set to an EnumSet.}}
3333
SetColor ports3 = new HashSet<>();
34-
Set<COLOR> ports4 = Sets.immutableEnumSet(COLOR.RED); // compliant because of bad type substitution : immutable set of E is not detected as a set of enum.
34+
Set<COLOR> ports4 = Sets.immutableEnumSet(COLOR.RED); // Noncompliant
3535
}
3636
}

java-frontend/src/main/java/org/sonar/java/resolve/TypeSubstitutionSolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private void completeSubstitution(TypeSubstitution currentSubstitution, JavaType
267267
}
268268
}
269269

270-
private static boolean isValidSubtitution(TypeSubstitution substitutions, JavaType site) {
270+
private boolean isValidSubtitution(TypeSubstitution substitutions, JavaType site) {
271271
for (Map.Entry<JavaType.TypeVariableJavaType, JavaType> substitution : substitutions.substitutionEntries()) {
272272
if (!isValidSubstitution(substitutions, substitution.getKey(), substitution.getValue(), site)) {
273273
return false;
@@ -276,9 +276,9 @@ private static boolean isValidSubtitution(TypeSubstitution substitutions, JavaTy
276276
return true;
277277
}
278278

279-
private static boolean isValidSubstitution(TypeSubstitution candidate, JavaType.TypeVariableJavaType typeVar, JavaType typeParam, JavaType site) {
279+
private boolean isValidSubstitution(TypeSubstitution candidate, JavaType.TypeVariableJavaType typeVar, JavaType typeParam, JavaType site) {
280280
for (JavaType bound : typeVar.bounds) {
281-
JavaType currentBound = bound;
281+
JavaType currentBound = applySubstitution(bound, candidate);
282282
while (currentBound.isTagged(JavaType.TYPEVAR)) {
283283
JavaType newBound = candidate.substitutedType(currentBound);
284284
if (newBound == null && isParametrizedType(site)) {

java-frontend/src/test/files/sym/references/MethodCall.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,34 @@ void test() {
238238
varargs6(null);
239239
varargs6("");
240240
}
241+
}
242+
243+
class HidingOfStaticMethod {
244+
static class A<T> {
245+
static <E> void of(E e1, E e2) { }
246+
static void by(Object o1, Object o2) { }
247+
}
248+
249+
static class B<T> extends A<T> {
250+
static <E extends Comparable<? super E>> void of(E e1, E e2) { }
251+
static void by(Object o1, Object o2) { }
252+
253+
void tstFromB() {
254+
B.by("hello", "world"); // call to B.by(), as A.by() is hidden
255+
256+
B.of("hello", "world"); // explicit call to B.of()
257+
B.of(new C(), new C()); // call to inherited method A.of() through B
258+
}
259+
}
260+
261+
static class C {}
262+
263+
void tstFromOutsideHierarchy() {
264+
A.by("hello", "world"); // explicit call to A.of()
265+
B.by("hello", "world"); // explicit call to B.of()
266+
267+
A.of("hello", "world"); // explicit call to A.of()
268+
B.of("hello", "world"); // explicit call to B.of()
269+
B.of(new C(), new C()); // call to inherited method A.of() through B
270+
}
241271
}

java-frontend/src/test/java/org/sonar/java/resolve/SymbolTableTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.sonar.java.resolve;
2121

2222
import com.google.common.collect.Iterables;
23+
2324
import org.junit.Rule;
2425
import org.junit.Test;
2526
import org.junit.rules.ExpectedException;
@@ -641,7 +642,15 @@ public void MethodCall() {
641642
assertThat(result.reference(238, 5)).isSameAs(result.symbol("varargs6", 233));
642643
assertThat(result.reference(239, 5)).isSameAs(result.symbol("varargs6", 233));
643644

645+
assertThat(result.reference(254, 9)).isSameAs(result.symbol("by", 251));
646+
assertThat(result.reference(264, 7)).isSameAs(result.symbol("by", 246));
647+
assertThat(result.reference(265, 7)).isSameAs(result.symbol("by", 251));
644648

649+
assertThat(result.reference(256, 9)).isSameAs(result.symbol("of", 250));
650+
assertThat(result.reference(257, 9)).isSameAs(result.symbol("of", 245));
651+
assertThat(result.reference(267, 7)).isSameAs(result.symbol("of", 245));
652+
assertThat(result.reference(268, 7)).isSameAs(result.symbol("of", 250));
653+
assertThat(result.reference(269, 7)).isSameAs(result.symbol("of", 245));
645654
}
646655

647656
@Test

0 commit comments

Comments
 (0)