Skip to content

Commit

Permalink
Refactor handling of Class method lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 10, 2017
1 parent afd444f commit 0dde05c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ protected TypeLookupResult findType(Expression node, ClassNode declaringType, Va
if (isCompatible((AnnotatedNode) result.declaration, isStaticObjectExpression)) {
return result;
}
if (isStaticObjectExpression) { // might be reference to a method defined on java.lang.Class
return findTypeForVariable(expr, scope, confidence, VariableScope.newClassClassNode(declaringType));
}
}

ClassNode nodeType = node.getType();
Expand All @@ -215,8 +218,7 @@ protected TypeLookupResult findType(Expression node, ClassNode declaringType, Va
return new TypeLookupResult(VariableScope.VOID_CLASS_NODE, clazz, null, TypeConfidence.UNKNOWN, scope);
}
}
}
if (!isPrimaryExpression || scope.isMethodCall()) {

return findTypeForNameWithKnownObjectExpression(node.getText(), nodeType, declaringType, scope, confidence,
isStaticObjectExpression, isPrimaryExpression, /*isLhsExpression:*/(scope.getWormhole().remove("lhs") == node));
}
Expand Down Expand Up @@ -337,23 +339,9 @@ protected TypeLookupResult findTypeForNameWithKnownObjectExpression(String name,
VariableScope scope, TypeConfidence confidence, boolean isStaticObjectExpression, boolean isPrimaryExpression, boolean isLhsExpression) {

TypeConfidence confidence0 = confidence;

boolean isFieldAccessDirect = (isThisObjectExpression(scope) ? scope.isFieldAccessDirect() : false);
ASTNode declaration = findDeclaration(name, declaringType, isLhsExpression, isStaticObjectExpression, isFieldAccessDirect, scope.getMethodCallArgumentTypes());

if (declaration == null && isPrimaryExpression) {
if (!isStaticObjectExpression) {
ClassNode thiz = scope.getThis();
if (thiz != null && !thiz.equals(declaringType)) {
// probably in a closure where the delegate has changed
declaration = findDeclaration(name, thiz, isLhsExpression, isStaticObjectExpression, false, scope.getMethodCallArgumentTypes());
}
} else {
// might be reference to a method/property defined on java.lang.Class
declaration = findDeclaration(name, VariableScope.newClassClassNode(declaringType), isLhsExpression, isStaticObjectExpression, false, scope.getMethodCallArgumentTypes());
}
}

ClassNode realDeclaringType;
VariableInfo variableInfo;
if (declaration != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,22 @@ final class CodeSelectMethodsTests extends BrowsingTestSuite {

@Test
void testCodeSelectStaticMethodInSuperClass() {
String contents = 'class PlantController {\ndef redirect(controller, action) { }\n' +
'static def checkUser() {\nredirect(controller:\"user\",action:\"login\")\n}}\n'
String contents2 =
'class Other extends PlantController {\nstatic def doNothing() {\nredirect(controller:\"user\",action:\"login\")\n}}'

String contents = '''\
class PlantController {
static def redirect(controller, action) {
}
static def checkUser() {
redirect(controller:'user', action:'login')
}
}
'''.stripIndent()
String contents2 = '''\
class Other extends PlantController {
static def doNothing() {
redirect(controller:'user', action:'login')
}
}
'''.stripIndent()
assertCodeSelect([contents, contents2], 'redirect')
}

Expand Down

0 comments on commit 0dde05c

Please sign in to comment.