New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GROOVY-9282: Allow protected override of package scoped methods #1038
Conversation
Relates to GROOVY-8651 Signed-off-by: Tobias Gesellchen <tobias@gesellix.de>
package-private methods can only be accessed by classes in the same package, so the improvement violates the rule of JLS. |
Would that be another check in another verifier? Otherwise I would extend the check in the current method we're looking at. Do you have any preference? |
It seems that no more checks are required. Here is the rule Java obeys, groovy should obey too: If a method is declared with default access (that is, not private, protected, nor public), it can only be overridden by methods in the same package. If a method of the same signature is defined in a subclass in a different package, it is a completely separate method and no overriding occurs. |
Could you add more test cases to cover the following rule: If a method of the same signature is defined in a subclass in a different package, it is a completely separate method and no overriding occurs. |
Due to Groovy's handling (some would say "ignorance" :) ) of private visibility at fields and methods I'm unsure how to provide such test cases. See https://issues.apache.org/jira/browse/GROOVY-3010 for a collection of tasks regarding that topic. Would you mind if we consider that scenario unrelated to the current pull request or maybe can you provide a rough idea how such a test case should look like? |
Here is the test case in Java to show the Java rule, i.e. "If a method of the same signature is defined in a subclass in a different package, it is a completely separate method and no overriding occurs", but it is hard to test in Groovy as Groovy will invoke methods on the instance of specific type(not the declaring type). @paulk-asert any thoughts on the PR?
package groovy9282;
import groovy9282.subpkg.DerivedDiffPkg;
public class Base {
String x() {
return "Base";
}
public static void main(String[] args) {
Base dsp = new DerivedSamePkg();
if (!"DerivedSamePkg".equals(dsp.x())) throw new AssertionError();
Base ddp = new DerivedDiffPkg();
if (!"Base".equals(ddp.x())) throw new AssertionError(); // the key part
}
} package groovy9282;
public class DerivedSamePkg extends Base {
@Override
protected String x() {
return "DerivedSamePkg";
}
} package groovy9282.subpkg;
import groovy9282.Base;
public class DerivedDiffPkg extends Base {
// @Override
protected String x() {
return "DerivedDiffPkg";
}
} |
@blackdrag Could you review the PR too, thanks in advance :-) |
Thanks a lot! |
See GROOVY-9282 for the Jira issue.
When overriding a method as
protected
with a super method being package-private (no modifier in Java or@PackageScope
in Groovy), compilation fails with an error similar to this:This edge case seems to be missed at GROOVY-8651.
Groovy 2.5.7 and 2.5.8 are affected. I didn't check Groovy 3.x.
Signed-off-by: Tobias Gesellchen tobias@gesellix.de