Skip to content

Commit

Permalink
Fix for #705: allow trait bridge methods to be overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 15, 2018
1 parent 881d7b5 commit 8f8c2b6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,42 @@ final class MethodCompletionTests extends CompletionTestSuite {
proposalExists(proposals, 'equals(Object obj) : boolean - Override method in \'Comparator\'', 1)
}

@Test
void testOverride6() {
addGroovySource '''\
trait T {
String getFoo() { 'foo' }
}
'''.stripIndent()

String contents = '''\
class A implements T {
x
}
'''.stripIndent()
ICompletionProposal[] proposals = createProposalsAtOffset(contents.replace('x', ''), contents.indexOf('x'))
proposalExists(proposals, 'getFoo() : String - Override method in \'T\'', 1)
}

@Test // https://github.com/groovy/groovy-eclipse/issues/705
void testOverride6a() {
addGroovySource '''\
trait T {
String getFoo() { 'foo' }
}
'''.stripIndent()

buildProject()

String contents = '''\
class A implements T {
x
}
'''.stripIndent()
ICompletionProposal[] proposals = createProposalsAtOffset(contents.replace('x', ''), contents.indexOf('x'))
proposalExists(proposals, 'getFoo() : String - Override method in \'T\'', 1)
}

@Test // GRECLIPSE-1752
void testStatic1() {
String contents = '''\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
import org.codehaus.groovy.eclipse.codeassist.ProposalUtils;
import org.codehaus.groovy.eclipse.codeassist.relevance.Relevance;
import org.codehaus.groovy.eclipse.codeassist.requestor.ContentAssistContext;
import org.codehaus.groovy.transform.trait.Traits.TraitBridge;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.CompletionProposal;
import org.eclipse.jdt.core.CompletionRequestor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.groovy.core.util.GroovyUtils;
import org.eclipse.jdt.groovy.search.GenericsMapper;
import org.eclipse.jdt.groovy.search.VariableScope;
import org.eclipse.jdt.internal.codeassist.CompletionEngine;
Expand Down Expand Up @@ -145,16 +147,16 @@ private static List<MethodNode> collectUnimplementedMethods(String completionExp

for (MethodNode meth : type.getMethods()) {
String name = meth.getName();
if (name.startsWith(completionExpression) &&
!name.contains("$") && !name.contains("<")) {
if (name.startsWith(completionExpression) && name.indexOf('$') < 0 && name.indexOf('<') < 0 && !isTraitBridge(meth)) {
methods.putIfAbsent(meth.getTypeDescriptor(), meth);
}
}
} while (!types.isEmpty());

// remove implemented or re-declared methods
for (MethodNode meth : declaringType.getMethods()) {
methods.remove(meth.getTypeDescriptor());
if (!isTraitBridge(meth))
methods.remove(meth.getTypeDescriptor());
}

// restrict to methods that can be overridden
Expand Down Expand Up @@ -233,4 +235,8 @@ private static ClassNode findResolvedType(ClassNode target, ClassNode toResolve)
}
return null;
}

private static boolean isTraitBridge(MethodNode method) {
return GroovyUtils.getAnnotations(method, TraitBridge.class.getName()).anyMatch(x -> true);
}
}

0 comments on commit 8f8c2b6

Please sign in to comment.