Skip to content

Commit

Permalink
Fix for #1142: check static import for inner class and update field expr
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jul 19, 2020
1 parent 71dbb51 commit 70655e8
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,15 +463,10 @@ public void testStaticImport10() {
assertKnown(contents, "dump", "p.Other", "java.lang.Void");
}

@Test // GRECLIPSE-1371
@Test
public void testStaticImport11() {
String contents =
"import static Boolean.TRUE\n" +
"class StaticImportStaticField {\n" +
" static boolean FLAG = TRUE\n" +
"}\n";
int offset = contents.lastIndexOf("TRUE");
assertType(contents, offset, offset + 4, "java.lang.Boolean");
String contents = "import static java.util.Map.Entry\n";
assertType(contents, "Entry", "java.lang.Class<java.util.Map$Entry>");
}

@Test // GRECLIPSE-1363
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.codehaus.groovy.ast;

import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.Expression;
import groovyjarjarasm.asm.Opcodes;

/**
Expand Down Expand Up @@ -147,22 +147,22 @@ public void visit(GroovyCodeVisitor visitor) {
}

// GRECLIPSE add
private ConstantExpression aliasExpr;
private ConstantExpression fieldNameExpr;
private Expression aliasExpr;
private Expression fieldNameExpr;

public ConstantExpression getAliasExpr() {
public Expression getAliasExpr() {
return aliasExpr;
}

public void setAliasExpr(ConstantExpression aliasExpr) {
public void setAliasExpr(Expression aliasExpr) {
this.aliasExpr = aliasExpr;
}

public ConstantExpression getFieldNameExpr() {
public Expression getFieldNameExpr() {
return fieldNameExpr;
}

public void setFieldNameExpr(ConstantExpression fieldNameExpr) {
public void setFieldNameExpr(Expression fieldNameExpr) {
this.fieldNameExpr = fieldNameExpr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1561,13 +1561,24 @@ public void visitClass(ClassNode node) {
// GRECLIPSE end
for (ImportNode importNode : module.getStaticImports().values()) {
ClassNode type = importNode.getType();
if (resolve(type, true, true, true)) continue;
addError("unable to resolve class " + type.getName(), type);
if (!resolve(type, false, false, true))
addError("unable to resolve class " + type.getName(), type);
// GRECLIPSE add
else {
type = ClassHelper.makeWithoutCaching(
type.getName() + "$" + importNode.getFieldName());
if (resolve(type, false, false, true)) {
Expression nameExpr = importNode.getFieldNameExpr();
importNode.setFieldNameExpr(new ClassExpression(type));
importNode.getFieldNameExpr().setSourcePosition(nameExpr);
}
}
// GRECLIPSE end
}
for (ImportNode importNode : module.getStaticStarImports().values()) {
ClassNode type = importNode.getType();
if (resolve(type, true, true, true)) continue;
addError("unable to resolve class " + type.getName(), type);
if (!resolve(type, false, false, true))
addError("unable to resolve class " + type.getName(), type);
}
module.setImportsResolved(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.codehaus.groovy.ast;

import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.Expression;
import groovyjarjarasm.asm.Opcodes;

/**
Expand Down Expand Up @@ -147,22 +147,22 @@ public void visit(GroovyCodeVisitor visitor) {
}

// GRECLIPSE add
private ConstantExpression aliasExpr;
private ConstantExpression fieldNameExpr;
private Expression aliasExpr;
private Expression fieldNameExpr;

public ConstantExpression getAliasExpr() {
public Expression getAliasExpr() {
return aliasExpr;
}

public void setAliasExpr(ConstantExpression aliasExpr) {
public void setAliasExpr(Expression aliasExpr) {
this.aliasExpr = aliasExpr;
}

public ConstantExpression getFieldNameExpr() {
public Expression getFieldNameExpr() {
return fieldNameExpr;
}

public void setFieldNameExpr(ConstantExpression fieldNameExpr) {
public void setFieldNameExpr(Expression fieldNameExpr) {
this.fieldNameExpr = fieldNameExpr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1568,13 +1568,24 @@ public void visitClass(ClassNode node) {
// GRECLIPSE end
for (ImportNode importNode : module.getStaticImports().values()) {
ClassNode type = importNode.getType();
if (resolve(type, true, true, true)) continue;
addError("unable to resolve class " + type.getName(), type);
if (!resolve(type, false, false, true))
addError("unable to resolve class " + type.getName(), type);
// GRECLIPSE add
else {
type = ClassHelper.makeWithoutCaching(
type.getName() + "$" + importNode.getFieldName());
if (resolve(type, false, false, true)) {
Expression nameExpr = importNode.getFieldNameExpr();
importNode.setFieldNameExpr(new ClassExpression(type));
importNode.getFieldNameExpr().setSourcePosition(nameExpr);
}
}
// GRECLIPSE end
}
for (ImportNode importNode : module.getStaticStarImports().values()) {
ClassNode type = importNode.getType();
if (resolve(type, true, true, true)) continue;
addError("unable to resolve class " + type.getName(), type);
if (!resolve(type, false, false, true))
addError("unable to resolve class " + type.getName(), type);
}
module.setImportsResolved(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.codehaus.groovy.ast;

import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.Expression;
import groovyjarjarasm.asm.Opcodes;

/**
Expand Down Expand Up @@ -147,22 +147,22 @@ public void visit(GroovyCodeVisitor visitor) {
}

// GRECLIPSE add
private ConstantExpression aliasExpr;
private ConstantExpression fieldNameExpr;
private Expression aliasExpr;
private Expression fieldNameExpr;

public ConstantExpression getAliasExpr() {
public Expression getAliasExpr() {
return aliasExpr;
}

public void setAliasExpr(ConstantExpression aliasExpr) {
public void setAliasExpr(Expression aliasExpr) {
this.aliasExpr = aliasExpr;
}

public ConstantExpression getFieldNameExpr() {
public Expression getFieldNameExpr() {
return fieldNameExpr;
}

public void setFieldNameExpr(ConstantExpression fieldNameExpr) {
public void setFieldNameExpr(Expression fieldNameExpr) {
this.fieldNameExpr = fieldNameExpr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ private boolean setRedirect(final ClassNode type, final ClassNode classToCheck)
return true;
}
// GRECLIPSE add
resolutionFailedCache.add(maybeNested.getName());
}
resolutionFailedCache.add(maybeNested.getName());
// GRECLIPSE end
}
return false;
Expand Down Expand Up @@ -581,7 +581,7 @@ protected boolean resolveFromDefaultImports(final ClassNode type, final String[]
// inner class in DEFAULT_IMPORTS
ConstructedClassWithPackage tmp = new ConstructedClassWithPackage(packagePrefix, typeName);
// GRECLIPSE add
if (resolutionFailedCache.contains(tmp.getName())) continue;
if (!resolutionFailedCache.contains(tmp.getName())) {
// GRECLIPSE end
if (resolve(tmp, false, false, false)) {
type.setRedirect(tmp.redirect());
Expand All @@ -594,7 +594,8 @@ protected boolean resolveFromDefaultImports(final ClassNode type, final String[]
return true;
}
// GRECLIPSE add
resolutionFailedCache.add(tmp.getName());
resolutionFailedCache.add(tmp.getName());
}
// GRECLIPSE end
}

Expand Down Expand Up @@ -747,7 +748,7 @@ protected boolean resolveFromModule(final ClassNode type, final boolean testModu
return true;
}
// GRECLIPSE add
resolutionFailedCache.add(tmp.getName());
resolutionFailedCache.add(tmp.getName());
}
// GRECLIPSE end
}
Expand All @@ -757,7 +758,7 @@ protected boolean resolveFromModule(final ClassNode type, final boolean testModu
if (importNode.getFieldName().equals(name)) {
ClassNode tmp = new ConstructedNestedClass(importNode.getType(), name);
// GRECLIPSE add
if (resolutionFailedCache.contains(tmp.getName())) continue;
if (!resolutionFailedCache.contains(tmp.getName())) {
// GRECLIPSE end
if (resolve(tmp, false, false, true)) {
if ((tmp.getModifiers() & Opcodes.ACC_STATIC) != 0) {
Expand All @@ -766,7 +767,8 @@ protected boolean resolveFromModule(final ClassNode type, final boolean testModu
}
}
// GRECLIPSE add
resolutionFailedCache.add(tmp.getName());
resolutionFailedCache.add(tmp.getName());
}
// GRECLIPSE end
}
}
Expand All @@ -780,23 +782,24 @@ protected boolean resolveFromModule(final ClassNode type, final boolean testModu
// packagePrefix is really a class is handled elsewhere.
ConstructedClassWithPackage tmp = new ConstructedClassWithPackage(packagePrefix, name);
// GRECLIPSE add
if (resolutionFailedCache.contains(tmp.getName())) continue;
if (!resolutionFailedCache.contains(tmp.getName())) {
// GRECLIPSE end
if (resolve(tmp, false, false, true)) {
ambiguousClass(type, tmp, name);
type.setRedirect(tmp.redirect());
return true;
}
// GRECLIPSE add
resolutionFailedCache.add(tmp.getName());
resolutionFailedCache.add(tmp.getName());
}
// GRECLIPSE end
}

// check for star imports (import static pkg.Outer.*) matching static inner classes
for (ImportNode importNode : module.getStaticStarImports().values()) {
ClassNode tmp = new ConstructedNestedClass(importNode.getType(), name);
// GRECLIPSE add
if (resolutionFailedCache.contains(tmp.getName())) continue;
if (!resolutionFailedCache.contains(tmp.getName())) {
// GRECLIPSE end
if (resolve(tmp, false, false, true)) {
if ((tmp.getModifiers() & Opcodes.ACC_STATIC) != 0) {
Expand All @@ -806,7 +809,8 @@ protected boolean resolveFromModule(final ClassNode type, final boolean testModu
}
}
// GRECLIPSE add
resolutionFailedCache.add(tmp.getName());
resolutionFailedCache.add(tmp.getName());
}
// GRECLIPSE end
}
}
Expand Down Expand Up @@ -1468,13 +1472,24 @@ public void visitClass(final ClassNode node) {
// GRECLIPSE end
for (ImportNode importNode : module.getStaticImports().values()) {
ClassNode type = importNode.getType();
if (resolve(type, true, true, true)) continue;
addError("unable to resolve class " + type.getName(), type);
if (!resolve(type, false, false, true))
addError("unable to resolve class " + type.getName(), type);
// GRECLIPSE add
else {
type = ClassHelper.makeWithoutCaching(
type.getName() + "$" + importNode.getFieldName());
if (resolve(type, false, false, true)) {
Expression nameExpr = importNode.getFieldNameExpr();
importNode.setFieldNameExpr(new ClassExpression(type));
importNode.getFieldNameExpr().setSourcePosition(nameExpr);
}
}
// GRECLIPSE end
}
for (ImportNode importNode : module.getStaticStarImports().values()) {
ClassNode type = importNode.getType();
if (resolve(type, true, true, true)) continue;
addError("unable to resolve class " + type.getName(), type);
if (!resolve(type, false, false, true))
addError("unable to resolve class " + type.getName(), type);
}
module.setImportsResolved(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public void visitImports(final ModuleNode node) {
try {
scope.setCurrentNode(imp);
primaryTypeStack.add(type);
visitConstantExpression(imp.getFieldNameExpr());
imp.getFieldNameExpr().visit(this);

scope.forgetCurrentNode();
dependentTypeStack.removeLast();
Expand Down

0 comments on commit 70655e8

Please sign in to comment.