Skip to content

Commit daa6cbf

Browse files
committed
SONARJAVA-1418 fix computation of lub to not fail semantic analysis
1 parent a1905bd commit daa6cbf

2 files changed

Lines changed: 26 additions & 45 deletions

File tree

java-squid/src/main/java/org/sonar/java/resolve/Types.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.sonar.java.resolve.JavaType.ParametrizedTypeJavaType;
2525
import org.sonar.plugins.java.api.semantic.Type;
2626

27+
import java.util.Collections;
2728
import java.util.LinkedHashSet;
2829
import java.util.LinkedList;
2930
import java.util.List;
@@ -131,27 +132,34 @@ private static Type best(List<Type> minimalCandidates) {
131132
}
132133

133134
private static List<Set<Type>> supertypes(Iterable<Type> types) {
134-
List<Set<Type>> results = new LinkedList<>();
135-
for (Type type : types) {
136-
checkParametrizedType(type);
137-
Set<Type> supertypes = new LinkedHashSet<>();
138-
supertypes.add(type);
139-
for (Type supertype : ((JavaType) type).symbol.superTypes()) {
140-
checkParametrizedType(supertype);
141-
supertypes.add(supertype);
135+
try {
136+
List<Set<Type>> results = new LinkedList<>();
137+
for (Type type : types) {
138+
checkParametrizedType(type);
139+
Set<Type> supertypes = new LinkedHashSet<>();
140+
supertypes.add(type);
141+
for (Type supertype : ((JavaType) type).symbol.superTypes()) {
142+
checkParametrizedType(supertype);
143+
supertypes.add(supertype);
144+
}
145+
results.add(supertypes);
142146
}
143-
results.add(supertypes);
147+
return results;
148+
} catch (UnsupportedOperationException e) {
149+
return Collections.emptyList();
144150
}
145-
return results;
146151
}
147152

148153
private static void checkParametrizedType(Type type) {
149154
if (type instanceof ParametrizedTypeJavaType) {
150-
throw new IllegalArgumentException("Generics are not handled");
155+
throw new UnsupportedOperationException("Generics are not handled");
151156
}
152157
}
153158

154159
private static List<Type> intersection(List<Set<Type>> supertypes) {
160+
if (supertypes.isEmpty()) {
161+
return Collections.emptyList();
162+
}
155163
List<Type> results = new LinkedList<>(supertypes.get(0));
156164
for (int i = 1; i < supertypes.size(); i++) {
157165
results.retainAll(supertypes.get(i));

java-squid/src/test/java/org/sonar/java/resolve/TypesTest.java

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import com.google.common.base.Charsets;
2323
import com.google.common.collect.Lists;
24-
import org.apache.commons.collections.CollectionUtils;
25-
import org.apache.commons.collections.Predicate;
2624
import org.fest.assertions.Fail;
2725
import org.junit.Test;
2826
import org.sonar.java.ast.parser.JavaParser;
@@ -31,7 +29,6 @@
3129
import org.sonar.plugins.java.api.tree.ClassTree;
3230
import org.sonar.plugins.java.api.tree.CompilationUnitTree;
3331
import org.sonar.plugins.java.api.tree.Tree;
34-
import org.sonar.plugins.java.api.tree.VariableTree;
3532

3633
import java.io.File;
3734
import java.util.Arrays;
@@ -249,21 +246,15 @@ public void lub_with_unknown_inheritance() {
249246

250247
@Test
251248
public void lub_ignores_generics() {
252-
List<Type> typesFromInput = typesOfVariables(
253-
"import java.util.List;",
254-
"class A {",
255-
" List<String> a;",
256-
" List<String> b;",
257-
"}");
249+
List<Type> typesFromInput = declaredTypes(
250+
"class A extends Exception {}",
251+
"class B extends Exception implements I1<Exception> {}",
252+
"interface I1<T> {}");
258253
Type a = typesFromInput.get(0);
259254
Type b = typesFromInput.get(1);
260-
try {
261-
types.leastUpperBound(Lists.newArrayList(a, b));
262-
Fail.fail("should have failed");
263-
} catch (Exception e) {
264-
assertThat(e).isInstanceOf(IllegalArgumentException.class);
265-
assertThat(e.getMessage()).isEqualTo("Generics are not handled");
266-
}
255+
256+
Type lub = types.leastUpperBound(Lists.newArrayList(a, b));
257+
assertThat(lub.isUnknown()).isTrue();
267258
}
268259

269260
private static List<Type> declaredTypes(String... lines) {
@@ -276,24 +267,6 @@ private static List<Type> declaredTypes(String... lines) {
276267
return results;
277268
}
278269

279-
private static List<Type> typesOfVariables(String... lines) {
280-
CompilationUnitTree tree = treeOf(lines);
281-
List<Tree> members = ((ClassTree) tree.types().get(0)).members();
282-
CollectionUtils.filter(members, new Predicate() {
283-
@Override
284-
public boolean evaluate(Object object) {
285-
Tree tree = (Tree) object;
286-
return tree.is(Tree.Kind.VARIABLE);
287-
}
288-
});
289-
List<Type> results = Lists.newLinkedList();
290-
for (Tree member : members) {
291-
Type type = ((VariableTree) member).type().symbolType();
292-
results.add(type);
293-
}
294-
return results;
295-
}
296-
297270
private static CompilationUnitTree treeOf(String... lines) {
298271
StringBuilder builder = new StringBuilder();
299272
for (String line : lines) {

0 commit comments

Comments
 (0)