Skip to content

Commit

Permalink
Fix for #993: search hierarchy (not just interfaces) for static call exp
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 24, 2019
1 parent a0e5110 commit 9c9d32b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,57 @@ public void testStaticMethod9() throws Exception {
assertType(contents, "meth(compile('abc'))", "java.util.regex.Pattern");
}

@Test
public void testStaticMethod10() throws Exception {
createUnit("foo", "Bar", "package foo\n" +
"import java.util.regex.*\n" +
"abstract class Bar {\n" +
" static Object meth(Object o) { return o;}\n" +
" static Pattern meth(Pattern p) { return p;}\n" +
" static Collection meth(Collection c) { return c;}\n" +
"}");

String contents =
"import static foo.Bar.*\n" +
"import static java.util.regex.Pattern.*\n" +
"meth(compile('abc'))";
assertType(contents, "meth", "java.util.regex.Pattern");
}

@Test
public void testStaticMethod11() throws Exception {
String contents =
"import static java.util.regex.Pattern.*\n" +
"import java.util.regex.*\n" +
"abstract class Bar {\n" +
" static Object meth(Object o) { return o;}\n" +
" static Pattern meth(Pattern p) { return p;}\n" +
" static Collection meth(Collection c) { return c;}\n" +
" static main(args) {\n" +
" meth(compile('abc'))\n" +
" }\n" +
"}";
assertType(contents, "meth", "java.util.regex.Pattern");
}

@Test
public void testStaticMethod12() throws Exception {
String contents =
"import static java.util.regex.Pattern.*\n" +
"import java.util.regex.*\n" +
"class Foo {\n" +
" static Object meth(Object o) { return o;}\n" +
" static Pattern meth(Pattern p) { return p;}\n" +
" static Collection meth(Collection c) { return c;}\n" +
"}\n" +
"abstract class Bar extends Foo {\n" +
" static main(args) {\n" +
" meth(compile('abc'))\n" +
" }\n" +
"}";
assertType(contents, "meth", "java.util.regex.Pattern");
}

@Test
public void testStaticThisAndSuper1() {
String contents =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,11 @@ protected TypeLookupResult findType(final Expression node, final ClassNode decla
} else if (node instanceof StaticMethodCallExpression) {
List<MethodNode> candidates = new LinkedList<>();
java.util.function.BiConsumer<ClassNode, String> collector = (classNode, methodName) -> {
// concrete types (without mixins/traits) return all methods from getMethods(String)
if (classNode.isAbstract() || classNode.isInterface() || implementsTrait(classNode)) {
LinkedHashSet<ClassNode> abstractTypes = new LinkedHashSet<>();
VariableScope.findAllInterfaces(classNode, abstractTypes, false);
for (ClassNode abstractType : abstractTypes) {
candidates.addAll(abstractType.getMethods(methodName));
}
LinkedHashSet<ClassNode> hierarchy = new LinkedHashSet<>();
VariableScope.createTypeHierarchy(classNode, hierarchy, false);
hierarchy.forEach(type -> candidates.addAll(type.getMethods(methodName)));
} else {
candidates.addAll(classNode.getMethods(methodName));
}
Expand Down

0 comments on commit 9c9d32b

Please sign in to comment.