Skip to content

Commit

Permalink
Fix for #1306: recognize constructor and static metaclass properties
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Oct 8, 2021
1 parent 4dd0ccc commit e563159
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,17 @@ protected ASTNode findDeclaration(final String name, final ClassNode declaringTy
}
}

if (!isMethodCall && isOrImplements(declaringType, ClassHelper.METACLASS_TYPE)) {
try {
if ("constructor".equals(name)) {
return createDynamicProperty(name, ClassHelper.make(Class.forName("groovy.lang.ExpandoMetaClass$ExpandoMetaConstructor")), declaringType, false);
} else if ("static".equals(name)) {
return createDynamicProperty(name, ClassHelper.make(Class.forName("groovy.lang.ExpandoMetaClass$ExpandoMetaProperty")), declaringType, false);
}
} catch (Exception ignore) {
}
}

if (methodCallArgumentTypes == null || methodCallArgumentTypes == UNKNOWN_TYPES) {
// reference may be in static import or method pointer; look for method as last resort
return findMethodDeclaration(name, declaringType, methodCallArgumentTypes, isStaticExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,29 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite {
new HighlightedTypedPosition(contents.indexOf('println'), 7, GROOVY_CALL))
}

@Test
void testMetaClassProperty1() {
String contents = 'String.metaClass.constructor << { p1, p2 -> "foo" }'

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('String'), 6, CLASS),
new HighlightedTypedPosition(contents.indexOf('metaClass'), 9, GROOVY_CALL),
new HighlightedTypedPosition(contents.indexOf('constructor'), 11, MAP_KEY),
new HighlightedTypedPosition(contents.indexOf('p1'), 2, PARAMETER),
new HighlightedTypedPosition(contents.indexOf('p2'), 2, PARAMETER))
}

@Test
void testMetaClassProperty2() {
String contents = 'String.metaClass.static.hello = { -> "world" }'

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('String'), 6, CLASS),
new HighlightedTypedPosition(contents.indexOf('metaClass'), 9, GROOVY_CALL),
new HighlightedTypedPosition(contents.indexOf('static'), 6, MAP_KEY),
new HighlightedTypedPosition(contents.indexOf('hello'), 5, MAP_KEY))
}

@Test
void testNotCategoryMethod1() {
String contents = 'def x = "equals"' // equals is a DGM and had been improperly identified by CategoryTypeLookup
Expand Down

0 comments on commit e563159

Please sign in to comment.