Skip to content

Commit

Permalink
Fix quality flaw: increase coverage and remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohops committed Apr 8, 2016
1 parent 0eacb9a commit 24c6c21
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
8 changes: 3 additions & 5 deletions java-frontend/src/main/java/org/sonar/java/resolve/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.java.resolve;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;

import org.sonar.plugins.java.api.semantic.Symbol;
Expand Down Expand Up @@ -115,7 +116,6 @@ public static Type leastUpperBound(Set<Type> types) {
}
}


List<Set<Type>> supertypes = supertypes(types);

List<Type> candidates = intersection(supertypes);
Expand All @@ -127,7 +127,8 @@ public static Type leastUpperBound(Set<Type> types) {
return best(minimalCandidates);
}

private static Type best(List<Type> minimalCandidates) {
@VisibleForTesting
static Type best(List<Type> minimalCandidates) {
Collections.sort(minimalCandidates, new Comparator<Type>() {
// Sort minimal candidates by name with classes before interfaces, to guarantee always the same type is returned when approximated.
@Override
Expand Down Expand Up @@ -162,9 +163,6 @@ private static List<Set<Type>> supertypes(Iterable<Type> types) {
}

private static List<Type> intersection(List<Set<Type>> supertypes) {
if (supertypes.isEmpty()) {
return Collections.emptyList();
}
List<Type> results = new LinkedList<>(supertypes.get(0));
for (int i = 1; i < supertypes.size(); i++) {
results.retainAll(supertypes.get(i));
Expand Down
19 changes: 19 additions & 0 deletions java-frontend/src/test/java/org/sonar/java/resolve/TypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,25 @@ public void lub_approximation_with_complexe_inheritance() {
assertThat(lub.is("java.lang.Exception")).isTrue();
}

@Test
public void lub_select_best_return_first_classes_then_interfaces_ordered_alphabetically() {
List<Type> typesFromInput = declaredTypes(
"class A {}",
"class B {}",
"interface I1 {}",
"interface I2 {}");
Type a = typesFromInput.get(0);
Type b = typesFromInput.get(1);
Type i1 = typesFromInput.get(2);
Type i2 = typesFromInput.get(3);

Type best = Types.best(Lists.newArrayList(i1, a, b, i2));
assertThat(best.is("A")).isTrue();

best = Types.best(Lists.newArrayList(i2, i1));
assertThat(best.is("I1")).isTrue();
}

@Test
public void lub_with_unknown_inheritance() {
List<Type> typesFromInput = declaredTypes(
Expand Down

0 comments on commit 24c6c21

Please sign in to comment.