Skip to content

Commit

Permalink
Fix for #1113: open declaration (F3) on accessor should go to property
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed May 16, 2020
1 parent 4051446 commit 6e9855a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,21 +289,31 @@ private void initializeMembers() {
if (ClassHelper.boolean_TYPE.equals(pNode.getType())) {
MethodNode mNode = addMethod("is" + capitalizedName, mMods, pNode.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
if (!(mNode instanceof JDTNode)) {
mNode.setNameStart(pNode.getField().getNameStart());
mNode.setNameEnd(pNode.getField().getNameEnd());
mNode.setSynthetic(true);
mNode = addMethod("get" + capitalizedName, mMods, pNode.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
if (!(mNode instanceof JDTNode)) {
mNode.setSynthetic(true);
}

// GROOVY-9382: include "getter" if "isser" was not declared
mNode = addMethod("get" + capitalizedName, mMods, pNode.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
if (!(mNode instanceof JDTNode)) {
mNode.setNameStart(pNode.getField().getNameStart());
mNode.setNameEnd(pNode.getField().getNameEnd());
mNode.setSynthetic(true);
}
}
} else {
MethodNode mNode = addMethod("get" + capitalizedName, mMods, pNode.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
if (!(mNode instanceof JDTNode)) {
mNode.setNameStart(pNode.getField().getNameStart());
mNode.setNameEnd(pNode.getField().getNameEnd());
mNode.setSynthetic(true);
}
}
if (!Flags.isFinal(pNode.getModifiers())) {
MethodNode mNode = addMethod("set" + capitalizedName, mMods, ClassHelper.VOID_TYPE, new Parameter[] {new Parameter(pNode.getType(), pName)}, ClassNode.EMPTY_ARRAY, null);
if (!(mNode instanceof JDTNode)) {
mNode.setNameStart(pNode.getField().getNameStart());
mNode.setNameEnd(pNode.getField().getNameEnd());
mNode.setSynthetic(true);
}
}
Expand Down Expand Up @@ -575,11 +585,13 @@ public List<PropertyNode> getProperties() {
if (synth) {
field = new FieldNode(node.getName(), Flags.AccPrivate | (node.getModifiers() & Flags.AccStatic), resolver.resolve(node.getType().getName()), this, null);
field.setDeclaringClass(this);
field.setSourcePosition(node.getField());
field.setSynthetic(true);
}

PropertyNode clone = new PropertyNode(field, node.getModifiers(), null, null);
clone.setDeclaringClass(this);
clone.setSourcePosition(node);
clone.setSynthetic(synth);

nodes.add(clone);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ final class CodeSelectFieldsTests extends BrowsingTestSuite {
| }
|}
|'''.stripMargin()], 'T__f', 'f')
assert elem.inferredElement.declaringClass.nameWithoutPackage == 'T'
assert elem.declaringType.fullyQualifiedName == 'T'
}

@Test // https://github.com/groovy/groovy-eclipse/issues/756
Expand All @@ -203,7 +203,7 @@ final class CodeSelectFieldsTests extends BrowsingTestSuite {
| }
|}
|'''.stripMargin()], 'T__f', 'f')
assert elem.inferredElement.declaringClass.nameWithoutPackage == 'T'
assert elem.declaringType.fullyQualifiedName == 'T'
}

@Test // https://github.com/groovy/groovy-eclipse/issues/756
Expand All @@ -218,6 +218,24 @@ final class CodeSelectFieldsTests extends BrowsingTestSuite {
| }
|}
|'''.stripMargin()], 'T__f', 'f')
assert elem.inferredElement.declaringClass.nameWithoutPackage == 'T'
assert elem.declaringType.fullyQualifiedName == 'T'
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1113
void testCodeSelectFieldFromTrait4() {
addGroovySource('''\
|trait T {
| String f
|}
|'''.stripMargin())
def elem = assertCodeSelect(['''\
|class C implements T {
| def m() {
| f
| }
|}
|'''.stripMargin()], 'f')
assert elem.declaringType.fullyQualifiedName == 'T'
assert elem.elementInfo.nameSourceStart == 19
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ final class CodeSelectMethodsTests extends BrowsingTestSuite {
|'''.stripMargin()

IJavaElement elem = assertCodeSelect([contents1, contents2], 'redirect')
assert elem.inferredElement.declaringClass.nameWithoutPackage == 'PlantController'
assert elem.declaringType.fullyQualifiedName == 'PlantController'
}

@Test // GRECLIPSE-1755
Expand Down Expand Up @@ -157,7 +157,7 @@ final class CodeSelectMethodsTests extends BrowsingTestSuite {
|'''.stripMargin()

IJavaElement elem = assertCodeSelect([contents1, contents2, contents3, contents4], 'foo')
assert elem.inferredElement.declaringClass.nameWithoutPackage == 'SuperInterface'
assert elem.declaringType.fullyQualifiedName == 'SuperInterface'
}

@Test
Expand Down Expand Up @@ -276,7 +276,77 @@ final class CodeSelectMethodsTests extends BrowsingTestSuite {
|}
|'''.stripMargin()
IJavaElement elem = assertCodeSelect([contents], 'x')
assert elem.inferredElement.declaringClass.nameWithoutPackage == 'T'
assert elem.declaringType.fullyQualifiedName == 'T'
}

@Test // https://github.com/groovy/groovy-eclipse/issues/960
void testCodeSelectStaticMethodFromTrait4() {
String contents = '''\
|trait T {
| Number number
|}
|class C implements T {
| def m() {
| getNumber()
| }
|}
|'''.stripMargin()
IJavaElement elem = assertCodeSelect([contents], 'getNumber')
assert elem.declaringType.fullyQualifiedName == 'T'
assert elem.elementInfo.nameSourceStart == contents.indexOf('number')
}

@Test // https://github.com/groovy/groovy-eclipse/issues/960
void testCodeSelectStaticMethodFromTrait5() {
String contents = '''\
|trait T {
| Number number
|}
|class C implements T {
| def m() {
| setNumber(42)
| }
|}
|'''.stripMargin()
IJavaElement elem = assertCodeSelect([contents], 'setNumber')
assert elem.declaringType.fullyQualifiedName == 'T'
assert elem.elementInfo.nameSourceStart == contents.indexOf('number')
}

@Test // https://github.com/groovy/groovy-eclipse/issues/960
void testCodeSelectStaticMethodFromTrait6() {
String contents = '''\
|trait T {
| boolean condition
|}
|class C implements T {
| def m() {
| isCondition()
| }
|}
|'''.stripMargin()
IJavaElement elem = assertCodeSelect([contents], 'isCondition')
assert elem.declaringType.fullyQualifiedName == 'T'
assert elem.elementInfo.nameSourceStart == contents.indexOf('condition')
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1113
void testCodeSelectStaticMethodFromTrait7() {
addGroovySource'''\
|trait T {
| String foo
|}
|'''.stripMargin()
String contents = '''\
|class C implements T {
| def m() {
| getFoo()
| }
|}
|'''.stripMargin()
IJavaElement elem = assertCodeSelect([contents], 'getFoo')
assert elem.declaringType.fullyQualifiedName == 'T'
assert elem.elementInfo.nameSourceStart == 19
}

@Test
Expand Down Expand Up @@ -372,7 +442,7 @@ final class CodeSelectMethodsTests extends BrowsingTestSuite {
void testCodeSelectStaticMethod4() {
String contents = 'List<String> empty = Collections.&emptyList'
IJavaElement elem = assertCodeSelect([contents], 'emptyList')
assert elem.inferredElement.returnType.toString(false) == 'java.util.List <T>' // want T to be java.lang.String
assert elem.inferredElement.returnType.toString(false) == 'java.util.List <T>' // TODO: want T to be java.lang.String
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.codehaus.groovy.eclipse.codebrowsing.elements.GroovyResolvedSourceMethod;
import org.codehaus.groovy.eclipse.codebrowsing.elements.GroovyResolvedSourceType;
import org.codehaus.groovy.eclipse.core.GroovyCore;
import org.codehaus.groovy.transform.trait.Traits;
import org.codehaus.jdt.groovy.internal.compiler.ast.JDTFieldNode;
import org.codehaus.jdt.groovy.internal.compiler.ast.JDTMethodNode;
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
Expand Down Expand Up @@ -523,6 +524,9 @@ private boolean existsOnlyInGroovyModel(final FieldNode node, final String name,
return true;
}
// check for @Trait field
if (!node.isFinal() && !node.isStatic() && Traits.isTrait(node.getDeclaringClass())) {
return true;
}
List<FieldNode> traitFields = declaringType.redirect().getNodeMetaData("trait.fields");
if (traitFields != null) {
for (FieldNode traitField : traitFields) {
Expand Down

0 comments on commit 6e9855a

Please sign in to comment.