Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions src/main/java/org/codehaus/groovy/control/StaticVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.codehaus.groovy.control;

import org.apache.groovy.ast.tools.ClassNodeUtils;
import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.DynamicVariable;
Expand All @@ -27,9 +28,8 @@
import org.codehaus.groovy.ast.expr.ClosureExpression;
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.transform.trait.Traits;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -88,34 +88,40 @@ public void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
@Override
public void visitVariableExpression(VariableExpression ve) {
if (ve.getAccessedVariable() instanceof DynamicVariable && (ve.isInStaticContext() || inSpecialConstructorCall) && !inClosure) {
String variableName = ve.getName();
// GROOVY-5687: interface constants not visible to implementing subclass in static context
if (methodNode != null && methodNode.isStatic()) {
FieldNode fieldNode = getDeclaredOrInheritedField(methodNode.getDeclaringClass(), ve.getName());
ClassNode classNode = methodNode.getDeclaringClass();
FieldNode fieldNode = getDeclaredOrInheritedField(classNode, variableName);
if (fieldNode != null && fieldNode.isStatic()) {
return;
}
}
addError("Apparent variable '" + ve.getName() + "' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:\n" +
addError("Apparent variable '" + variableName + "' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:\n" +
"You attempted to reference a variable in the binding or an instance variable from a static context.\n" +
"You misspelled a classname or statically imported field. Please check the spelling.\n" +
"You attempted to use a method '" + ve.getName() + "' but left out brackets in a place not allowed by the grammar.", ve);
"You attempted to use a method '" + variableName + "' but left out brackets in a place not allowed by the grammar.",
ve);
}
}

private static FieldNode getDeclaredOrInheritedField(ClassNode cn, String fieldName) {
ClassNode node = cn;
while (node != null) {
FieldNode fn = node.getDeclaredField(fieldName);
if (fn != null) return fn;
List<ClassNode> interfacesToCheck = new ArrayList<>(Arrays.asList(node.getInterfaces()));
while (!interfacesToCheck.isEmpty()) {
ClassNode nextInterface = interfacesToCheck.remove(0);
fn = nextInterface.getDeclaredField(fieldName);
if (fn != null) return fn;
interfacesToCheck.addAll(Arrays.asList(nextInterface.getInterfaces()));
private static FieldNode getDeclaredOrInheritedField(ClassNode classNode, String fieldName) {
FieldNode fieldNode = ClassNodeUtils.getField(classNode, fieldName);
if (fieldNode == null && fieldName.contains("__")) { // GROOVY-11663
List<ClassNode> traits = Traits.findTraits(classNode);
traits.remove(classNode); // included if it is a trait
for (ClassNode cn : traits) {
cn = Traits.findFieldHelper(cn);
if (cn != null) {
for (FieldNode fn : cn.getFields()) {
if (fn.getName().endsWith(fieldName)) { // prefix for modifiers
fieldNode = fn;
break;
}
}
}
}
node = node.getSuperClass();
}
return null;
return fieldNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -763,28 +763,39 @@ private boolean tryVariableExpressionAsProperty(final VariableExpression vexp, f

@Override
public void visitPropertyExpression(final PropertyExpression expression) {
if (existsProperty(expression, !typeCheckingContext.isTargetOfEnclosingAssignment(expression))) return;

if (!extension.handleUnresolvedProperty(expression)) {
var objectExpression = expression.getObjectExpression();
var objectExpressionType = (objectExpression instanceof ClassExpression
? objectExpression.getType() : wrapTypeIfNecessary(getType(objectExpression)));
objectExpressionType = findCurrentInstanceOfClass(objectExpression, objectExpressionType);
addStaticTypeError("No such property: " + expression.getPropertyAsString() + " for class: " + prettyPrintTypeName(objectExpressionType), expression);
boolean readOnly = !typeCheckingContext.isTargetOfEnclosingAssignment(expression);
if (existsProperty(expression, readOnly)
|| extension.handleUnresolvedProperty(expression)) {
return; // resolved or excused
}
recordMissingProperty(expression);
}

@Override
public void visitAttributeExpression(final AttributeExpression expression) {
if (existsProperty(expression, true)) return;
boolean readOnly = true;
if (existsProperty(expression, readOnly)
|| extension.handleUnresolvedAttribute(expression)) {
return; // resolved or excused
}
recordMissingProperty(expression);
}

if (!extension.handleUnresolvedAttribute(expression)) {
var objectExpression = expression.getObjectExpression();
var objectExpressionType = (objectExpression instanceof ClassExpression
? objectExpression.getType() : wrapTypeIfNecessary(getType(objectExpression)));
objectExpressionType = findCurrentInstanceOfClass(objectExpression, objectExpressionType);
addStaticTypeError("No such attribute: " + expression.getPropertyAsString() + " for class: " + prettyPrintTypeName(objectExpressionType), expression);
private void recordMissingProperty(final PropertyExpression expression) {
var objectExpression = expression.getObjectExpression();
var objectExpressionType = findCurrentInstanceOfClass(objectExpression, getType(objectExpression));

String pattern;
if (!isClassClassNodeWrappingConcreteType(objectExpressionType)) {
pattern = "No such {0,choice,1#attribute|2#property}: {1} for class: {2}";
} else {
objectExpressionType = objectExpressionType.getGenericsTypes()[0].getType();
pattern = "No such {0,choice,1#attribute|2#property}: {1} for Class or static {0,choice,1#field|2#property} for class: {2}";
}

String error = java.text.MessageFormat.format(pattern, expression instanceof AttributeExpression ? 1 : 2, expression.getPropertyAsString(), prettyPrintTypeName(wrapTypeIfNecessary(objectExpressionType)));
ASTNode node = expression.getLineNumber() > 0 ? expression : expression.getProperty(); // GROOVY-11663
addStaticTypeError(error, node);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
import org.codehaus.groovy.ast.expr.MethodCall;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.expr.PropertyExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.transform.trait.TraitASTTransformation;
import org.codehaus.groovy.transform.trait.Traits;

Expand Down Expand Up @@ -93,4 +95,34 @@ public List<MethodNode> handleMissingMethod(final ClassNode receiver, final Stri

return Collections.emptyList();
}

@Override
public boolean handleUnresolvedProperty(final PropertyExpression pexp) {
var objectExpression = pexp.getObjectExpression();
if (objectExpression instanceof VariableExpression
&& objectExpression.getText().endsWith(Traits.THIS_OBJECT)) {
String propertyName = pexp.getPropertyAsString();
if (propertyName != null) {
ClassNode objectExpressionType = getType(objectExpression);
if (isClassClassNodeWrappingConcreteType(objectExpressionType)) {
objectExpressionType = objectExpressionType.getGenericsTypes()[0].getType();
}
for (ClassNode trait : Traits.findTraits(objectExpressionType)) {
if (propertyName.startsWith(trait.getName().replace('.', '_') + "__")) {
ClassNode staticFieldHelper = Traits.findStaticFieldHelper(trait);
if (staticFieldHelper != null) {
MethodNode getter = staticFieldHelper.getDeclaredMethod(propertyName + "$get", Parameter.EMPTY_ARRAY);
if (getter != null) { // GROOVY-11663: resolve "$self.pack_Type__name" to a static field access method
ClassNode returnType = typeCheckingVisitor.inferReturnTypeGenerics(objectExpressionType, getter, ArgumentListExpression.EMPTY_ARGUMENTS);
pexp.putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, returnType);
storeType(pexp, returnType);
return true;
}
}
}
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,6 @@ final class DifferentPackageTest {
'''
)
}
assert err =~ /No such property: answer for class: p.One/ // TODO: Cannot access p.One#getAnswer?
assert err =~ /No such property: answer for Class or static property for class: p.One/ // TODO: Cannot access p.One#getAnswer?
}
}
Loading