Skip to content

Commit

Permalink
Fix for #769: limit annotation member value proposals to static fields
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 25, 2018
1 parent 4566617 commit 7741295
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ final class AnnotationCompletionTests extends CompletionTestSuite {
addJavaSource '''\
package p;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Target(ElementType.TYPE)
public @interface A {
boolean one();
String two();
Expand All @@ -403,6 +403,32 @@ final class AnnotationCompletionTests extends CompletionTestSuite {
assertThat(proposals).excludes('one', 'Three').includes('two')
}

@Test // https://github.com/groovy/groovy-eclipse/issues/769
void testAnnoAttr13() {
addJavaSource '''\
package p;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
public @interface A {
boolean one();
}
''', 'A', 'p'

String contents = '''\
import p.A
class Something {
@A(one=false)
def meth() {}
boolean someFalseCheck() {}
private boolean someFalseFlag
public static final boolean SOME_FALSE_CONST = false
}
'''.stripIndent()
def proposals = getProposals(contents, 'false')

assertThat(proposals).excludes('someFalseCheck', 'someFalseFlag').includes('SOME_FALSE_CONST')
}

@Test
void testAnnoAttrPacks() {
String contents = '''\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.AnnotatedNode;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ConstructorNode;
import org.codehaus.groovy.ast.ImportNode;
Expand Down Expand Up @@ -348,7 +349,7 @@ private List<IProposalCreator> chooseProposalCreators() {
String fullCompletionExpression = context.fullCompletionExpression;
if (fullCompletionExpression == null) fullCompletionExpression = "";

if (FIELD_ACCESS_COMPLETION.matcher(fullCompletionExpression).matches()) {
if (FIELD_ACCESS_COMPLETION.matcher(fullCompletionExpression).matches() || context.containingCodeBlock instanceof AnnotationNode) {
return Collections.singletonList(new FieldProposalCreator());
}
if (METHOD_POINTER_COMPLETION.matcher(fullCompletionExpression).matches()) {
Expand Down Expand Up @@ -448,6 +449,7 @@ public VisitStatus acceptASTNode(ASTNode node, TypeLookupResult result, IJavaEle
setResultingType(result, derefList);
categories = result.scope.getCategoryNames();
isStatic = (node instanceof StaticMethodCallExpression ||
getContext().containingCodeBlock instanceof AnnotationNode ||
// if we are completing on '.class' then never static context
(node instanceof ClassExpression && !VariableScope.CLASS_CLASS_NODE.equals(resultingType)));
return VisitStatus.STOP_VISIT;
Expand Down Expand Up @@ -538,6 +540,13 @@ private void maybeRememberTypeOfLHS(TypeLookupResult result) {
}
}
}

ASTNode enclosingBlock = getContext().containingCodeBlock;
if (enclosingBlock instanceof AnnotationNode && lhsNode instanceof Variable) {
ClassNode annotation = ((AnnotationNode) enclosingBlock).getClassNode();
MethodNode attribute = annotation.getMethod(((Variable) lhsNode).getName(), Parameter.EMPTY_ARRAY);
lhsType = attribute.getReturnType();
}
}
if (VariableScope.OBJECT_CLASS_NODE.equals(lhsType)) {
lhsType = null;
Expand Down

0 comments on commit 7741295

Please sign in to comment.