Skip to content

Commit

Permalink
Fix for issue #267: add pointcut for variable assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jul 31, 2017
1 parent d05fb6b commit b43f93c
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 315 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ contribute(currentType(mixins: annotatedBy(Mixin))) {
}

//@groovy.lang.Newify
contribute(enclosingClass(annos: annotatedBy(Newify)) | enclosingField(annos: annotatedBy(Newify)) | enclosingMethod(annos: annotatedBy(Newify))) {
contribute(enclosingClass(annos: annotatedBy(Newify)) | enclosingField(annos: annotatedBy(Newify)) | enclosingMethod(annos: annotatedBy(Newify)) | assignedVariable(annos: annotatedBy(Newify))) {
provider = 'Newify AST transform'

def addNewifyMethods = { ClassNode type, String name = type.nameWithoutPackage ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ contribute(currentType(mixins: annotatedBy(Mixin))) {
}

//@groovy.lang.Newify
contribute(enclosingClass(annos: annotatedBy(Newify)) | enclosingField(annos: annotatedBy(Newify)) | enclosingMethod(annos: annotatedBy(Newify))) { //TODO: local variable declaration
contribute(enclosingClass(annos: annotatedBy(Newify)) | enclosingField(annos: annotatedBy(Newify)) | enclosingMethod(annos: annotatedBy(Newify)) | assignedVariable(annos: annotatedBy(Newify))) {
provider = '{@link groovy.lang.Newify Newify} AST transform'

def addNewifyMethods = { ClassNode type, String name = type.nameWithoutPackage ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,7 @@ private boolean handleSimpleExpression(Expression node) {
scope.setMethodCallGenericsTypes(getMethodCallGenericsTypes(completeExpressionStack.getLast()));
}
scope.setPrimaryNode(primaryType == null);
scope.getWormhole().put("enclosingAssignment", enclosingAssignment);

TypeLookupResult result = lookupExpressionType(node, primaryType, isStatic, scope);
return handleRequestor(node, primaryType, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,11 @@ public Map<String, Object> getWormhole() {
}

public ASTNode getEnclosingNode() {
if (shared.nodeStack.size() > 1) {
ASTNode current = shared.nodeStack.removeLast();
ASTNode enclosing = shared.nodeStack.getLast();
shared.nodeStack.add(current);
return enclosing;
} else {
return null;
int n = shared.nodeStack.size();
if (n > 1) {
return shared.nodeStack.get(n - 2);
}
return null;
}

public void setPrimaryNode(boolean isPrimaryNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,23 @@ final class DSLContentAssistTests extends CompletionTestSuite {
proposalExists(proposals, 'HashMap', 4) // one for each constructor in HashMap
}

@Test
void testNewifyTransform4() {
String contents = '''\
@Newify
List list = ArrayList.n
@Newify(HashMap)
Map map = HashM
'''.stripIndent()

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

proposals = orderByRelevance(createProposalsAtOffset(contents, getIndexOf(contents, 'HashM')))
//proposalExists(proposals, 'HashMap', 4) // one for each constructor in HashMap
// TODO: waiting for https://issues.apache.org/jira/browse/GROOVY-8249
}

@Test
void testSelfTypeTransform1() {
assumeTrue(isAtLeastGroovy(24)) // SelfType was added in Groovy 2.4
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2009-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.eclipse.dsl.pointcuts.impl;

import java.util.Collection;
import java.util.Collections;

import org.codehaus.groovy.ast.Variable;
import org.codehaus.groovy.ast.expr.BinaryExpression;
import org.codehaus.groovy.eclipse.dsl.pointcuts.AbstractPointcut;
import org.codehaus.groovy.eclipse.dsl.pointcuts.GroovyDSLDContext;
import org.codehaus.groovy.eclipse.dsl.pointcuts.IPointcut;
import org.codehaus.groovy.eclipse.dsl.pointcuts.PointcutVerificationException;
import org.eclipse.core.resources.IStorage;

/**
* Matches when current context is enclosed by a variable assignment that
* satisfies the given name (string) or constraints (pointcut).
*/
public class AssignedVariablePointcut extends AbstractPointcut {

public AssignedVariablePointcut(IStorage containerIdentifier, String pointcutName) {
super(containerIdentifier, pointcutName);
}

@Override
public Collection<?> matches(GroovyDSLDContext pattern, Object toMatch) {
BinaryExpression enclosing = (BinaryExpression) pattern.getCurrentScope().getWormhole().get("enclosingAssignment");
if (enclosing != null && enclosing.getLeftExpression() instanceof Variable) {
Object argument = getFirstArgument();
if (argument instanceof String) {
if (argument.equals(((Variable) enclosing.getLeftExpression()).getName())) {
return Collections.singleton(enclosing);
}
} else {
return matchOnPointcutArgument((IPointcut) argument, pattern, Collections.singleton(enclosing));
}
}
return null;
}

@Override
public void verify() throws PointcutVerificationException {
String failure = oneStringOrOnePointcutArg();
if (failure != null) {
throw new PointcutVerificationException(failure, this);
}
super.verify();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 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 All @@ -26,11 +26,8 @@
import org.eclipse.core.resources.IStorage;

/**
* Tests that the type being analyzed matches. The match can
* either be a string match (ie - the type name),
* or it can pass the current type to a containing pointcut
* @author andrew
* @created Feb 10, 2011
* Matches when current context is enclosed by a field declaration that satisfies
* the given name (string) or constraints (pointcut).
*/
public class EnclosingFieldPointcut extends AbstractPointcut {

Expand All @@ -41,32 +38,25 @@ public EnclosingFieldPointcut(IStorage containerIdentifier, String pointcutName)
@Override
public Collection<?> matches(GroovyDSLDContext pattern, Object toMatch) {
FieldNode enclosing = pattern.getCurrentScope().getEnclosingFieldDeclaration();
if (enclosing == null) {
return null;
}

Object firstArgument = getFirstArgument();
Collection<FieldNode> enclosingCollection = Collections.singleton(enclosing);
if (firstArgument instanceof String) {
if (enclosing.getName().equals(firstArgument)) {
return enclosingCollection;
if (enclosing != null) {
Object argument = getFirstArgument();
if (argument instanceof String) {
if (argument.equals(enclosing.getName())) {
return Collections.singleton(enclosing);
}
} else {
return null;
return matchOnPointcutArgument((IPointcut) argument, pattern, Collections.singleton(enclosing));
}
} else {
return matchOnPointcutArgument((IPointcut) firstArgument, pattern, enclosingCollection);
}
return null;
}

/**
* expecting one arg that is either a string or a pointcut or a class
*/
@Override
public void verify() throws PointcutVerificationException {
String oneStringOrOnePointcutArg = oneStringOrOnePointcutArg();
if (oneStringOrOnePointcutArg != null) {
throw new PointcutVerificationException(oneStringOrOnePointcutArg, this);
String failure = oneStringOrOnePointcutArg();
if (failure != null) {
throw new PointcutVerificationException(failure, this);
}
super.verify();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 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 All @@ -26,11 +26,8 @@
import org.eclipse.core.resources.IStorage;

/**
* Tests that the type being analyzed matches. The match can
* either be a string match (ie - the type name),
* or it can pass the current type to a containing pointcut
* @author andrew
* @created Feb 10, 2011
* Matches when current context is enclosed by a method declaration that satisfies
* the given name (string) or constraints (pointcut).
*/
public class EnclosingMethodPointcut extends AbstractPointcut {

Expand All @@ -41,32 +38,25 @@ public EnclosingMethodPointcut(IStorage containerIdentifier, String pointcutName
@Override
public Collection<?> matches(GroovyDSLDContext pattern, Object toMatch) {
MethodNode enclosing = pattern.getCurrentScope().getEnclosingMethodDeclaration();
if (enclosing == null) {
return null;
}

Object firstArgument = getFirstArgument();
Collection<MethodNode> enclosingCollection = Collections.singleton(enclosing);
if (firstArgument instanceof String) {
if (enclosing.getName().equals(firstArgument)) {
return enclosingCollection;
if (enclosing != null) {
Object argument = getFirstArgument();
if (argument instanceof String) {
if (argument.equals(enclosing.getName())) {
return Collections.singleton(enclosing);
}
} else {
return null;
return matchOnPointcutArgument((IPointcut) argument, pattern, Collections.singleton(enclosing));
}
} else {
return matchOnPointcutArgument((IPointcut) firstArgument, pattern, enclosingCollection);
}
return null;
}

/**
* expecting one arg that is either a string or a pointcut or a class
*/
@Override
public void verify() throws PointcutVerificationException {
String oneStringOrOnePointcutArg = oneStringOrOnePointcutArg();
if (oneStringOrOnePointcutArg != null) {
throw new PointcutVerificationException(oneStringOrOnePointcutArg, this);
String failure = oneStringOrOnePointcutArg();
if (failure != null) {
throw new PointcutVerificationException(failure, this);
}
super.verify();
}
}
}
Loading

0 comments on commit b43f93c

Please sign in to comment.