Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.7.5
-
None
Description
Inside the StaticImportVisitor, I would have expected that the PropertyExpressions generated by the visitor contain proper source locations. There are several places where an existing ConstantExpression or VariableExpression is found to be a reference to a statically imported field. In this case, a synthetic PropertyExpression is created. The source location for the synthetic PropertyExpression is set to be the source location for the original Constant/Variable Expression, but the expression returned by PropertyExpression.getExpression() no longer has an sloc.
For example the script:
import static javax.swing.text.html.HTML.* NULL_ATTRIBUTE_VALUE
after the StaticImportVisitor does its magic, the NULL_ATTRIBUTE_VALUE constant expression is converted into HTML.NULL_ATTRIBUTE_VALUE PropertyExpression. The new PropertyExpression has the same sloc as the original NULL_ATTRIBUTE_VALUE expression, but the contained expressions do not have any slocs.
Note that this may be a problem for StaticMethodCalls as well, but so far I cannot reproduce it.
Proposed solution:
In StaticImportVisitor.java, create method:
private void setSourcePosition(Expression toSet, Expression origNode) { toSet.setSourcePosition(origNode); if (toSet instanceof PropertyExpression) { ((PropertyExpression) toSet).getProperty().setSourcePosition(origNode); } }
And in this file replace all calls to ASTNode.setSourcePosition(ASTNode) with a call to the new setSourcePosition method. If the ASTNode toSet is not a property expression, then no change in behavior occurs, but if it is, then slocs are set properly.
I have made this change to Groovy-Eclipse and everything is working fine. It would be nice to see this contributed back to groovy core.