Skip to content

Commit

Permalink
GROOVY-11104: same-unit static import of generated property method
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jun 18, 2023
1 parent 0f9af3d commit ceb167a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,16 @@ public static void addDeclaredMethodsFromAllInterfaces(final ClassNode cNode, fi
* @param name the name of the method of interest
* @param arguments the arguments to match against
* @param trySpread whether to try to account for SpreadExpressions within the arguments
* @return true if a matching method was found
* @return {@code true} if a matching method was found.
*/
public static boolean hasPossibleStaticMethod(final ClassNode cNode, final String name, final Expression arguments, final boolean trySpread) {
int count = 0;
boolean foundSpread = false;

int count = 0; boolean foundSpread = false;
if (arguments instanceof TupleExpression) {
TupleExpression tuple = (TupleExpression) arguments;
for (Expression arg : tuple.getExpressions()) {
for (Expression arg : (TupleExpression) arguments) {
if (arg instanceof SpreadExpression) {
foundSpread = true;
} else {
count++;
count += 1;
}
}
} else if (arguments instanceof MapExpression) {
Expand All @@ -270,16 +267,29 @@ public static boolean hasPossibleStaticMethod(final ClassNode cNode, final Strin
int nonDefaultParameters = 0;
for (Parameter parameter : parameters) {
if (!parameter.hasInitialExpression()) {
nonDefaultParameters++;
nonDefaultParameters += 1;
}
}

if (count < parameters.length && nonDefaultParameters <= count) {
return true;
}
// TODO handle spread with nonDefaultParams?
// TODO: Handle spread with nonDefaultParams?
}
}

// GROOVY-11104: generated method
if (cNode.isPrimaryClassNode()) {
for (PropertyNode pNode : cNode.getProperties()) {
if (pNode.getGetterNameOrDefault().equals(name)) {
return pNode.isStatic();
}
if (pNode.getSetterNameOrDefault().equals(name)) {
return pNode.isStatic() && !pNode.isFinal();
}
}
}

return false;
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/groovy/StaticImportTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,20 @@ final class StaticImportTest extends groovy.test.GroovyTestCase {
assert z == 'baz_set_get'
"""
}

assertScript '''
import groovy.transform.CompileStatic
import static Pogo.setP as store
class Pogo {
static p
}
assert Pogo.p == null
@CompileStatic test() {
store('')
assert Pogo.p == ''
}
test()
'''
}

// GROOVY-9382, GROOVY-10133
Expand Down

0 comments on commit ceb167a

Please sign in to comment.