Skip to content

Commit

Permalink
Fix for #876: check property's field for package-private visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 27, 2019
1 parent 3284db5 commit 7bd1ee9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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
* https://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,
Expand Down Expand Up @@ -129,8 +129,10 @@ protected MethodBinding[] augmentMethodBindings(MethodBinding[] methodBindings)
// create property accessors without resolving the types
if (referenceContext instanceof GroovyTypeDeclaration) {
for (PropertyNode property : ((GroovyTypeDeclaration) referenceContext).getClassNode().getProperties()) {
String name = property.getName(), capitalizedName = MetaClassHelper.capitalize(name);
int modifiers = getModifiers(property);
if (Flags.isPackageDefault(modifiers)) continue;

String name = property.getName(), capitalizedName = MetaClassHelper.capitalize(name);

createGetterMethod(name, "get" + capitalizedName, modifiers, methodBindings)
.ifPresent(groovyMethods::add);
Expand Down Expand Up @@ -195,11 +197,23 @@ protected MethodBinding[] augmentMethodBindings(MethodBinding[] methodBindings)

private int getModifiers(PropertyNode property) {
int modifiers = (property.getModifiers() & 0xF);
if (property.getField().getAnnotations().stream()
.map(anno -> anno.getClassNode().getName())

// if @PackageScope was detected by GCUD, field's modifiers will show it
char[] nameChars = property.getName().toCharArray();
for (FieldDeclaration field : referenceContext.fields) {
if (CharOperation.equals(field.name, nameChars)) {
if (Flags.isPackageDefault(field.modifiers)) {
modifiers &= ~Flags.AccPublic;
}
break;
}
}

if (property.getField().getAnnotations().stream().map(anno -> anno.getClassNode().getName())
.anyMatch(name -> name.equals("Deprecated") || name.equals("java.lang.Deprecated"))) {
modifiers |= Flags.AccDeprecated;
}

return modifiers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite {
new HighlightedTypedPosition(contents.indexOf('four'), 'four'.length(), FIELD))
}

@Test // https://github.com/groovy/groovy-eclipse/issues/876
void testFields2() {
addGroovySource '''\
import groovy.transform.PackageScope
class Pogo {
@PackageScope String string
}
'''

String contents = '''\
class X extends Pogo {{
string
getString()
setString('value')
}}
'''.stripIndent()

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('string'), 'string'.length(), FIELD),
new HighlightedTypedPosition(contents.indexOf('getString'), 'getString'.length(), UNKNOWN),
new HighlightedTypedPosition(contents.indexOf('setString'), 'setString'.length(), UNKNOWN))
}

@Test
void testScriptFields() {
String contents = '''\
Expand Down

0 comments on commit 7bd1ee9

Please sign in to comment.