Skip to content

Commit

Permalink
Fix for #1048: add support for Groovy 3's Type::new and Type.&new
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Mar 13, 2020
1 parent 37de7c6 commit 4044bbe
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
package org.eclipse.jdt.core.groovy.tests.search;

import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isAtLeastGroovy;
import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isParrotParser;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import java.util.Set;

Expand Down Expand Up @@ -621,6 +624,19 @@ public void testClosure4() {
assertType(contents, 4, 6, "groovy.lang.Closure<java.lang.Class<?>>");
}

@Test
public void testClosure5() {
String contents = "def fn = String[].&new";
assertType(contents, 4, 6, isAtLeastGroovy(30) ? "groovy.lang.Closure<java.lang.String[]>" : "groovy.lang.Closure");
}

@Test
public void testClosure6() {
assumeTrue(isParrotParser());
String contents = "def fn = String[]::new";
assertType(contents, 4, 6, "groovy.lang.Closure<java.lang.String[]>");
}

@Test
public void testArrayDGM() {
String contents = "String[] strings = ['1', '2']; strings.iterator()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ protected TypeLookupResult findType(final Expression node, final ClassNode decla
// "super.@value" prefers fields but supports general Groovy property access; see AsmClassGenerator#visitAttributeExpression
}

if ("new".equals(node.getText()) && isStaticObjectExpression && isStaticReferenceToInstanceMethod(scope)) {
return new TypeLookupResult(declaringType.getGenericsTypes()[0].getType(), null, node, confidence, scope);
}

boolean isLhsExpression = (scope.getWormhole().remove("lhs") == node);
return findTypeForNameWithKnownObjectExpression(node.getText(), nodeType, declaringType, scope, isLhsExpression, isStaticObjectExpression);
}
Expand Down Expand Up @@ -905,6 +909,8 @@ protected static ClassNode getTypeFromDeclaration(final ASTNode declaration) {
if (field.isDynamicTyped() && field.hasInitialExpression()) {
type = field.getInitialExpression().getType();
}
} else if (decl instanceof ConstructorNode) {
type = ((ConstructorNode) decl).getDeclaringClass();
} else if (decl instanceof MethodNode) {
type = ((MethodNode) decl).getReturnType();
} else if (decl instanceof Expression) {
Expand Down Expand Up @@ -1022,7 +1028,7 @@ protected static boolean isNotThisOrOuterClass(final ClassNode thisType, final C
* implicit in some sense).
*/
protected static boolean isSynthetic(final MethodNode method) {
// TODO: What about 'method.getDeclaringClass().equals(ClassHelper.GROOVY_OBJECT_TYPE)'?
// TODO: What about 'method.getDeclaringClass().equals(VariableScope.GROOVY_OBJECT_CLASS_NODE)'?
return method.isSynthetic() || method.getDeclaringClass().equals(VariableScope.CLOSURE_CLASS_NODE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,7 @@ public void updateVariable(String name, ClassNode type, ClassNode declaringType)
@Override
public void visitListExpression(final ListExpression node) {
scopes.getLast().setCurrentNode(node);

if (isDependentExpression(node)) {
primaryTypeStack.removeLast();
}
Expand All @@ -1290,12 +1291,14 @@ public void visitListExpression(final ListExpression node) {
}
ClassNode exprType = createParameterizedList(itemType);
handleCompleteExpression(node, exprType, null);

scopes.getLast().forgetCurrentNode();
}

@Override
public void visitMapEntryExpression(final MapEntryExpression node) {
scopes.getLast().setCurrentNode(node);

if (isDependentExpression(node)) {
primaryTypeStack.removeLast();
}
Expand All @@ -1312,6 +1315,7 @@ public void visitMapEntryExpression(final MapEntryExpression node) {
}
ClassNode exprType = createParameterizedMap(keyType, valType);
handleCompleteExpression(node, exprType, null);

scopes.getLast().forgetCurrentNode();
}

Expand Down Expand Up @@ -1492,7 +1496,7 @@ public void visitMethodPointerExpression(final MethodPointerExpression node) {
}

Tuple t = dependentDeclarationStack.removeLast();
if (!(t.declaration instanceof MethodNode)) {
if (t.declaration == null) {
dependentTypeStack.removeLast(); // unused type
handleCompleteExpression(node, node.getType(), null);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2886,7 +2886,31 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite {
}

@Test
void testMethodReference() {
void testMethodPointer4() {
String contents = 'String[].&new'

if (isAtLeastGroovy(30)) {
assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('String'), 6, CLASS))
} else {
assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('String'), 6, CLASS),
new HighlightedTypedPosition(contents.indexOf('new'), 'new'.length(), UNKNOWN))
}
}

@Test
void testMethodReference1() {
assumeTrue(isParrotParser())

String contents = 'String[]::new'

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('String'), 6, CLASS))
}

@Test
void testMethodReference2() {
assumeTrue(isParrotParser())

String contents = '''\
Expand Down

0 comments on commit 4044bbe

Please sign in to comment.