Skip to content

Commit

Permalink
GROOVY-9510, GROOVY-10192 (pt.2)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Aug 12, 2023
1 parent 306c496 commit 67588cd
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,18 @@ private void visitMethodInternal(final MethodNode node) {
ASTNode enclosingDeclaration0 = enclosingDeclarationNode;
enclosingDeclarationNode = node;
try {
visitAnnotations(node); // annotations are in class scope
scopes.add(new VariableScope(scopes.getLast(), node, node.isStatic()));
VariableScope classScope = scopes.getLast();
if (isNotEmpty(node.getAnnotations())) {
// assert(!node.isScriptBody());
classScope.setCurrentNode(node);
try {
visitAnnotations(node); // annotations are in class scope
} finally {
classScope.forgetCurrentNode();
}
}

scopes.add(new VariableScope(classScope, node, node.isStatic()));
try {
visitConstructorOrMethod(node, node instanceof ConstructorNode || node.getName().equals("<clinit>"));
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,18 @@ public FieldNode getEnclosingFieldDeclaration() {
}

public MethodNode getEnclosingMethodDeclaration() {
if (scopeNode instanceof MethodNode) {
return (MethodNode) scopeNode;
} else if (parent != null) {
return parent.getEnclosingMethodDeclaration();
} else {
return null;
for (Iterator<ASTNode> it = shared.nodeStack.descendingIterator(); it.hasNext();) {
ASTNode node = it.next();
if (node instanceof MethodNode) {
return (MethodNode) node;
}
}
for (VariableScope scope = this; scope != null; scope = scope.parent) {
if (scope.scopeNode instanceof MethodNode) {
return (MethodNode) scope.scopeNode;
}
}
return null;
}

public ClosureExpression getEnclosingClosure() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2021 the original author or authors.
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1066,6 +1066,24 @@ final class DSLContentAssistTests extends CompletionTestSuite {
proposalExists(proposals, 'HashMap', 0)
}

@Test
void testNewifyTransform1a() {
String contents = '''\
|class Foo {
| @Newify bar() {
| List list = ArrayList.n
| Map map = HashM
| }
|}
|'''.stripMargin()

ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, '.n'))
proposalExists(proposals, 'new', 3) // one for each constructor in ArrayList

proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'HashM'))
proposalExists(proposals, 'HashMap', 0)
}

@Test
void testNewifyTransform2() {
String contents = '''\
Expand All @@ -1082,6 +1100,24 @@ final class DSLContentAssistTests extends CompletionTestSuite {
proposalExists(proposals, 'HashMap', 4) // one for each constructor in HashMap
}

@Test
void testNewifyTransform2a() {
String contents = '''\
|class Foo {
| @Newify(HashMap) bar() {
| List list = ArrayList.n
| Map map = HashM
| }
|}
|'''.stripMargin()

ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, '.n'))
proposalExists(proposals, 'new', 3) // one for each constructor in ArrayList

proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'HashM'))
proposalExists(proposals, 'HashMap', 4) // one for each constructor in HashMap
}

@Test
void testNewifyTransform3() {
String contents = '''\
Expand All @@ -1099,23 +1135,25 @@ final class DSLContentAssistTests extends CompletionTestSuite {
}

@Test
void testNewifyTransform4() {
void testNewifyTransform3a() {
String contents = '''\
|@Newify
|List list = ArrayList.n
|@Newify(HashMap)
|Map map = HashM
|class Foo {
| @Newify(auto=false, value=HashMap) bar() {
| List list = ArrayList.n
| Map map = HashM
| }
|}
|'''.stripMargin()

ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, '.n'))
proposalExists(proposals, 'new', 3) // one for each constructor in ArrayList
proposalExists(proposals, 'new', 0)

proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'HashM'))
proposalExists(proposals, 'HashMap', 4) // one for each constructor in HashMap
}

@Test
void testNewifyTransform5() {
void testNewifyTransform4() {
String contents = '''\
|@Newify(auto=false, pattern=/(Linked)?Hash.*/) class Foo {
| List list = ArrayList.n
Expand All @@ -1131,7 +1169,25 @@ final class DSLContentAssistTests extends CompletionTestSuite {
}

@Test
void testNewifyTransform5a() {
void testNewifyTransform4a() {
String contents = '''\
|class Foo {
| @Newify(auto=false, pattern=/(Linked)?Hash.*/) bar() {
| List list = ArrayList.n
| Map map = HashM
| }
|}
|'''.stripMargin()

ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, '.n'))
proposalExists(proposals, 'new', 0)

proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'HashM'))
proposalExists(proposals, 'HashMap', 4) // one for each constructor in HashMap
}

@Test
void testNewifyTransform5() {
String contents = '''\
|@Newify(auto=false, pattern=/(Linked)?Hash.*/) class Foo {
| Map map = LinkedH
Expand All @@ -1143,7 +1199,21 @@ final class DSLContentAssistTests extends CompletionTestSuite {
}

@Test
void testNewifyTransform5b() {
void testNewifyTransform5a() {
String contents = '''\
|class Foo {
| @Newify(auto=false, pattern=/(Linked)?Hash.*/) bar() {
| Map map = LinkedH
| }
|}
|'''.stripMargin()

ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'LinkedH'))
proposalExists(proposals, 'LinkedHashMap', 5) // one for each constructor in LinkedHashMap
}

@Test
void testNewifyTransform6() {
String contents = '''\
|@Newify(auto=false, pattern=/(Linked)?Hash.*/) class Foo {
| Map map = LinkedHashMap()
Expand All @@ -1154,6 +1224,54 @@ final class DSLContentAssistTests extends CompletionTestSuite {
proposalExists(proposals, 'LinkedHashMap', 5) // one for each constructor in LinkedHashMap
}

@Test
void testNewifyTransform6a() {
String contents = '''\
|class Foo {
| @Newify(auto=false, pattern=/(Linked)?Hash.*/) bar() {
| Map map = LinkedHashMap()
| }
|}
|'''.stripMargin()

ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'LinkedHashMap'))
proposalExists(proposals, 'LinkedHashMap', 5) // one for each constructor in LinkedHashMap
}

@Test // field/property annotations
void testNewifyTransform7() {
String contents = '''\
|class Foo {
| @Newify
| private List list = ArrayList.n
| @Newify(HashMap)
| Map map = HashM
|}
|'''.stripMargin()

ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, '.n'))
proposalExists(proposals, 'new', 3) // one for each constructor in ArrayList

proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'HashM'))
proposalExists(proposals, 'HashMap', 4) // one for each constructor in HashMap
}

@Test // local variable annotations
void testNewifyTransform8() {
String contents = '''\
|@Newify
|List list = ArrayList.n
|@Newify(HashMap)
|Map map = HashM
|'''.stripMargin()

ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, '.n'))
proposalExists(proposals, 'new', 3) // one for each constructor in ArrayList

proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'HashM'))
proposalExists(proposals, 'HashMap', 4) // one for each constructor in HashMap
}

@Test
void testSelfTypeTransform1() {
String contents = '''\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.codehaus.groovy.eclipse.dsl.tests

import groovy.test.NotYetImplemented
import groovy.transform.ToString
import groovy.transform.TupleConstructor

Expand Down Expand Up @@ -90,21 +91,13 @@ final class PointcutEvaluationTests extends GroovyEclipseTestSuite {
final String bindingToString
}

private void doTestOfLastBindingSet(String cuContents, String pointcutText, BindingResult... results) {
doTestOfLastBindingSet('p', cuContents, pointcutText, results)
}

private void doTestOfLastBindingSet(String pkg, String cuContents, String pointcutText, BindingResult... results) {
private void doTestOfLastBindingSet(String pkg = 'p', String cuContents, String pointcutText, BindingResult... results) {
GroovyCompilationUnit unit = addGroovySource(cuContents, nextUnitName(), pkg)
BindingSet bindings = evaluateForBindings(unit, pointcutText)
assertAllBindings(bindings, results)
}

private void doTestOfLastMatch(String cuContents, String pointcutText, String name) {
doTestOfLastMatch('p', cuContents, pointcutText, name)
}

private void doTestOfLastMatch(String pkg, String cuContents, String pointcutText, String name) {
private void doTestOfLastMatch(String pkg = 'p', String cuContents, String pointcutText, String name) {
GroovyCompilationUnit unit = addGroovySource(cuContents, nextUnitName(), pkg)
Collection<?> match = evaluateForMatch(unit, pointcutText)
assertSingleBinding(match, name)
Expand Down Expand Up @@ -398,12 +391,12 @@ final class PointcutEvaluationTests extends GroovyEclipseTestSuite {

@Test
void testPackageFolder1() {
doTestOfLastMatch('p', '2', 'packageFolder("p")', 'p')
doTestOfLastMatch('2', 'packageFolder("p")', 'p')
}

@Test
void testPackageFolder2() {
doTestOfLastMatch('p', '2', 'packageFolder("invalid")', null)
doTestOfLastMatch('2', 'packageFolder("invalid")', null)
}

@Test
Expand All @@ -421,37 +414,32 @@ final class PointcutEvaluationTests extends GroovyEclipseTestSuite {

@Test
void testNamedBinding3() {
doTestOfLastBindingSet('2', 'bind( b : nature("org.eclipse.jdt.groovy.core.groovyNature") ) | ' +
'bind( c : fileExtension("groovy") )',
doTestOfLastBindingSet('2', 'bind( b : nature("org.eclipse.jdt.groovy.core.groovyNature") ) | bind( c : fileExtension("groovy") )',
new BindingResult('b', 'org.eclipse.jdt.groovy.core.groovyNature'),
new BindingResult('c', 'src/p/TestUnit_[0-9a-f]{32}.groovy'))
}

@Test
void testNamedBinding4() {
doTestOfLastBindingSet('2', 'bind( b : nature("org.eclipse.jdt.groovy.core.groovyNature") ) & ' +
'bind( c : fileExtension("groovy") )',
doTestOfLastBindingSet('2', 'bind( b : nature("org.eclipse.jdt.groovy.core.groovyNature") ) & bind( c : fileExtension("groovy") )',
new BindingResult('b', 'org.eclipse.jdt.groovy.core.groovyNature'),
new BindingResult('c', 'src/p/TestUnit_[0-9a-f]{32}.groovy'))
}

@Test
void testNamedBinding5() {
doTestOfLastBindingSet('2', 'bind( b : nature("org.eclipse.jdt.groovy.core.groovyNature") ) | ' +
'bind( c : fileExtension("invalid") )',
doTestOfLastBindingSet('2', 'bind( b : nature("org.eclipse.jdt.groovy.core.groovyNature") ) | bind( c : fileExtension("invalid") )',
new BindingResult('b', 'org.eclipse.jdt.groovy.core.groovyNature'))
}

@Test
void testNamedBinding6() {
doTestOfLastBindingSet('2', 'bind( b : nature("invalid") ) & ' +
'bind( c : fileExtension("groovy") )')
doTestOfLastBindingSet('2', 'bind( b : nature("invalid") ) & bind( c : fileExtension("groovy") )')
}

@Test
void testNamedBinding6a() {
doTestOfLastBindingSet('2', 'bind( b : nature("invalid") ) | ' +
'bind( c : fileExtension("groovy") )',
void testNamedBinding7() {
doTestOfLastBindingSet('2', 'bind( b : nature("invalid") ) | bind( c : fileExtension("groovy") )',
new BindingResult('c', 'src/p/TestUnit_[0-9a-f]{32}.groovy'))
}

Expand All @@ -463,16 +451,13 @@ final class PointcutEvaluationTests extends GroovyEclipseTestSuite {

@Test
void testTypesNamedBinding2() {
doTestOfLastBindingSet('2', 'bind( b : currentType("java.lang.Integer") ) | ' +
'bind( c : fileExtension("invalid") )',
doTestOfLastBindingSet('2', 'bind( b : currentType("java.lang.Integer") ) | bind( c : fileExtension("invalid") )',
new BindingResult('b', 'java.lang.Integer'))
}

@Test
void testTypesNamedBinding3() {
doTestOfLastBindingSet('2',
'bind( b : currentType("java.lang.Integer") ) | ' +
'bind( c : fileExtension("groovy") )',
doTestOfLastBindingSet('2', 'bind( b : currentType("java.lang.Integer") ) | bind( c : fileExtension("groovy") )',
new BindingResult('b', 'java.lang.Integer'),
new BindingResult('c', 'src/p/TestUnit_[0-9a-f]{32}.groovy'))
}
Expand Down Expand Up @@ -653,7 +638,7 @@ final class PointcutEvaluationTests extends GroovyEclipseTestSuite {
}

@Test
void testAnnotatedByBinding7Fail() {
void testAnnotatedByBinding7() {
addGroovySource('@Deprecated\nclass Foo { \n def f }', 'Foo', 'p')
doTestOfLastBindingSet('Foo', 'currentType( fields("g") & bind( b : annotatedBy("java.lang.Deprecated") ) )')
}
Expand Down Expand Up @@ -684,6 +669,34 @@ final class PointcutEvaluationTests extends GroovyEclipseTestSuite {
new BindingResult('b', '@java.lang.Deprecated, @java.lang.Deprecated'))
}

@NotYetImplemented @Test
void testEnclosingFieldBinding() {
addGroovySource('@interface Tag {\n Class<? extends Closure> value()\n}', 'Tag', 'p')

doTestOfLastBindingSet('''\
|class C {
| @Tag({ x })
| protected f
|}
|'''.stripMargin(),
'inClosure() & bind(f: enclosingField(annotatedBy("p.Tag")))',
new BindingResult('f', 'p.C.f'))
}

@Test
void testEnclosingMethodBinding() {
addGroovySource('@interface Tag {\n Class<? extends Closure> value()\n}', 'Tag', 'p')

doTestOfLastBindingSet('''\
|class C {
| @Tag({ x })
| void m() {}
|}
|'''.stripMargin(),
'inClosure() & bind(m: enclosingMethod(annotatedBy("p.Tag")))',
new BindingResult('m', 'p.C.m'))
}

@Test
void testNestedCalls1() {
doTestOfLastBindingSet('''\
Expand Down

0 comments on commit 67588cd

Please sign in to comment.