Skip to content

Commit

Permalink
Fix for #1159: provide outer class link for inner type built from Class
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 13, 2020
1 parent 424e510 commit 242ef15
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,26 @@ public class ClassHelper {
public static final String OBJECT = "java.lang.Object";

public static ClassNode makeCached(Class c) {
final SoftReference<ClassNode> classNodeSoftReference = ClassHelperCache.classCache.get(c);
ClassNode classNode;
final SoftReference<ClassNode> classNodeSoftReference = ClassHelperCache.classCache.get(c);
if (classNodeSoftReference == null || (classNode = classNodeSoftReference.get()) == null) {
// GRECLIPSE edit
classNode = new /*ClassNode*/ImmutableClassNode(c);
/* GRECLIPSE edit
classNode = new ClassNode(c);
*/
if (!c.isMemberClass()) {
classNode = new ImmutableClassNode(c);
} else {
classNode = new ImmutableClassNode(c) {
@Override
public ClassNode getOuterClass() {
return makeCached(clazz.getEnclosingClass());
}
};
}
// GRECLIPSE end
ClassHelperCache.classCache.put(c, new SoftReference<ClassNode>(classNode));

VMPluginFactory.getPlugin().setAdditionalClassInformation(classNode);
}

return classNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1685,8 +1685,16 @@ public int getNameStart2() {
return nameStart > 0 ? nameStart : getStart();
}

public void setNameStart2(int offset) {
public void setNameStart2(final int offset) {
nameStart = offset;
}

@Override
public void setSourcePosition(final ASTNode node) {
super.setSourcePosition(node);
if (node instanceof ClassNode) {
setNameStart2(((ClassNode) node).getNameStart2());
}
}
// GRECLIPSE end
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public String getText() {
}

// GRECLIPSE add
public void setSourcePosition(ASTNode node) {
public void setSourcePosition(final ASTNode node) {
super.setSourcePosition(node);
// propagate source position
if (getType().getEnd() <= 0)
getType().setSourcePosition(this);
getType().setSourcePosition(node);
}
// GRECLIPSE end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private Expression transformPropertyExpression(final PropertyExpression exp) {
? thisPropX(false, "class") : varX("this")
)
);
methodCall.getObjectExpression().setSourcePosition(classExpression);
methodCall.getObjectExpression().setSourcePosition(traitType);
methodCall.getMethod().setSourcePosition(exp.getProperty());
methodCall.setMethodTarget(methodNode);
methodCall.setImplicitThis(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,26 @@ public class ClassHelper {
public static final String OBJECT = "java.lang.Object";

public static ClassNode makeCached(Class c) {
final SoftReference<ClassNode> classNodeSoftReference = ClassHelperCache.classCache.get(c);
ClassNode classNode;
final SoftReference<ClassNode> classNodeSoftReference = ClassHelperCache.classCache.get(c);
if (classNodeSoftReference == null || (classNode = classNodeSoftReference.get()) == null) {
// GRECLIPSE edit
classNode = new ImmutableClassNode(c);
/* GRECLIPSE edit
classNode = new ClassNode(c);
*/
if (!c.isMemberClass()) {
classNode = new ImmutableClassNode(c);
} else {
classNode = new ImmutableClassNode(c) {
@Override
public ClassNode getOuterClass() {
return makeCached(clazz.getEnclosingClass());
}
};
}
// GRECLIPSE end
ClassHelperCache.classCache.put(c, new SoftReference<ClassNode>(classNode));

VMPluginFactory.getPlugin().setAdditionalClassInformation(classNode);
}

return classNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1675,8 +1675,16 @@ public int getNameStart2() {
return nameStart > 0 ? nameStart : getStart();
}

public void setNameStart2(int offset) {
public void setNameStart2(final int offset) {
nameStart = offset;
}

@Override
public void setSourcePosition(final ASTNode node) {
super.setSourcePosition(node);
if (node instanceof ClassNode) {
setNameStart2(((ClassNode) node).getNameStart2());
}
}
// GRECLIPSE end
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public String getText() {
}

// GRECLIPSE add
public void setSourcePosition(ASTNode node) {
public void setSourcePosition(final ASTNode node) {
super.setSourcePosition(node);
// propagate source position
if (getType().getEnd() <= 0)
getType().setSourcePosition(this);
getType().setSourcePosition(node);
}
// GRECLIPSE end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private Expression transformPropertyExpression(final PropertyExpression exp) {
? thisPropX(false, "class") : varX("this")
)
);
methodCall.getObjectExpression().setSourcePosition(classExpression);
methodCall.getObjectExpression().setSourcePosition(traitType);
methodCall.getMethod().setSourcePosition(exp.getProperty());
methodCall.setMethodTarget(methodNode);
methodCall.setImplicitThis(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,22 @@ public class ClassHelper {
public static final String OBJECT = "java.lang.Object";

public static ClassNode makeCached(Class c) {
final SoftReference<ClassNode> classNodeSoftReference = ClassHelperCache.classCache.get(c);
ClassNode classNode;
final SoftReference<ClassNode> classNodeSoftReference = ClassHelperCache.classCache.get(c);
if (classNodeSoftReference == null || (classNode = classNodeSoftReference.get()) == null) {
// GRECLIPSE edit
classNode = new ImmutableClassNode(c);
/* GRECLIPSE edit
classNode = new ClassNode(c);
*/
if (!c.isMemberClass()) {
classNode = new ImmutableClassNode(c);
} else {
classNode = new ImmutableClassNode(c) {
@Override
public ClassNode getOuterClass() {
return makeCached(clazz.getEnclosingClass());
}
};
}
// GRECLIPSE end
ClassHelperCache.classCache.put(c, new SoftReference<ClassNode>(classNode));
VMPluginFactory.getPlugin().setAdditionalClassInformation(classNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1621,8 +1621,16 @@ public int getNameStart2() {
return nameStart > 0 ? nameStart : getStart();
}

public void setNameStart2(int offset) {
public void setNameStart2(final int offset) {
nameStart = offset;
}

@Override
public void setSourcePosition(final ASTNode node) {
super.setSourcePosition(node);
if (node instanceof ClassNode) {
setNameStart2(((ClassNode) node).getNameStart2());
}
}
// GRECLIPSE end
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public String getText() {
}

// GRECLIPSE add
public void setSourcePosition(ASTNode node) {
public void setSourcePosition(final ASTNode node) {
super.setSourcePosition(node);
// propagate source position
if (getType().getEnd() <= 0)
getType().setSourcePosition(this);
getType().setSourcePosition(node);
}
// GRECLIPSE end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ private Expression transformBinaryExpression(final BinaryExpression exp) {
bin.getRightExpression()
)
);
setterCall.getObjectExpression().setSourcePosition(leftExpression.getObjectExpression());
setterCall.getMethod().setSourcePosition(leftExpression.getProperty());
setterCall.getObjectExpression().setSourcePosition(traitType);
setterCall.setSpreadSafe(leftExpression.isSpreadSafe());
setterCall.setImplicitThis(false);
return setterCall;
Expand Down Expand Up @@ -150,7 +150,7 @@ private Expression transformPropertyExpression(final PropertyExpression exp) {
? thisPropX(false, "class") : varX("this")
)
);
methodCall.getObjectExpression().setSourcePosition(((PropertyExpression) exp.getObjectExpression()).getObjectExpression());
methodCall.getObjectExpression().setSourcePosition(traitType);
methodCall.getMethod().setSourcePosition(exp.getProperty());
methodCall.setSpreadSafe(exp.isSpreadSafe());
methodCall.setMethodTarget(methodNode);
Expand Down Expand Up @@ -207,7 +207,7 @@ private Expression transformMethodCallExpression(final MethodCallExpression exp)
transform(exp.getMethod()),
newArgs
);
newCall.getObjectExpression().setSourcePosition(((PropertyExpression) exp.getObjectExpression()).getObjectExpression());
newCall.getObjectExpression().setSourcePosition(traitType);
newCall.setSpreadSafe(exp.isSpreadSafe());
newCall.setImplicitThis(false);
return newCall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3911,6 +3911,32 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite {
new HighlightedTypedPosition(contents.lastIndexOf('toString'), 8, UNKNOWN))
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1159
void testTraits12() {
addGroovySource '''\
|package p
|trait T {
| Number getFoo() { 'foo' }
|}
|'''.stripMargin(), 'T', 'p'
buildProject()

String contents = '''\
|class C implements p.T {
| void test() {
| p.T.super.getFoo()
| }
|}
|'''.stripMargin()

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('C'), 1, CLASS),
new HighlightedTypedPosition(contents.indexOf('T'), 1, TRAIT),
new HighlightedTypedPosition(contents.indexOf('test'), 4, METHOD),
new HighlightedTypedPosition(contents.lastIndexOf('T'), 1, TRAIT),
new HighlightedTypedPosition(contents.lastIndexOf('getFoo'), 6, STATIC_CALL))
}

//
private int counter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public VisitStatus acceptASTNode(ASTNode node, TypeLookupResult result, IJavaEle
} else if (node instanceof ClassNode) {
// visit "Map" of "Map.Entry" separately
if (((ClassNode) node).getNameEnd() < 1) {
checkOuterClass((ClassNode) node, outer -> {
checkOuterClass(result.type, outer -> {
acceptASTNode(outer, new TypeLookupResult(outer, outer, outer, TypeLookupResult.TypeConfidence.EXACT, result.scope), enclosingElement);
});
}
Expand Down

0 comments on commit 242ef15

Please sign in to comment.