Repository navigation
GROOVY-11916: do not resolve inaccessible types from star imports - #3227
daniellansun wants to merge 8 commits into
Conversation
Codecov Reportβ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3227 +/- ##
==================================================
+ Coverage 74.6267% 74.6377% +0.0110%
- Complexity 45464 45523 +59
==================================================
Files 1708 1708
Lines 153429 153531 +102
Branches 29526 29566 +40
==================================================
+ Hits 114499 114592 +93
- Misses 28408 28414 +6
- Partials 10522 10525 +3
π New features to boost your workflow:
|
| ClassNode outer = importNode.getType(); | ||
| if (outer != null) { | ||
| ClassNode tmp = new ConstructedNestedClass(outer, name); | ||
| if (resolve(tmp, false, false, true) && Modifier.isStatic(tmp.getModifiers()) |
There was a problem hiding this comment.
it is unclear why here only a static class is valid. It may be an issue of context, but I think it needs some explanation.
There was a problem hiding this comment.
outer != null covers both import static Type.* and import Type.* when that name resolves to a class (visitClass then stores the class on the import). Both forms look up a member type, and only a static member type is in scope, the same rule as a single static import. A non-static inner class is not part of that star. A member type of an interface is implicitly static, so Map.Entry still matches.
This comment has been minimized.
This comment has been minimized.
| positioned.sort((a, b) -> { | ||
| int byLine = Integer.compare(a.line, b.line); | ||
| if (byLine != 0) return byLine; | ||
| return Integer.compare(a.column, b.column); |
There was a problem hiding this comment.
This still gives me a headache, I think we should not depend on the line information for this. But I do not think it is worth blocking the issue.
There was a problem hiding this comment.
Hi Jochen,
Agreed β line numbers are only a stand-in. Source on-demand imports live in three lists, and import module stamps the statement position only on the last expanded package, so the current walk sorts by that position and treats line <= 0 as a customizer import. A proper fix would record declaration order and origin when the import is added, rather than recovering them from the AST coordinates. I would rather not hold this change for that, as you said, and can follow up separately if you would like that cleaned up.
Star imports, module imports, and the implicit default imports now bind only types accessible from the importing class. A package-private type such as java.util.TimSort is no longer resolved by import java.util.*, so a type use fails at compile time. Same-package package-private types, protected member types used from a subclass, and private member types used from the enclosing nest remain visible. Explicit single-type imports and fully-qualified names are unchanged, as is Class.forName. Static field imports have required accessibility since Groovy 5.
Type-import-on-demand and static-import-on-demand, including the implicit java.lang import, bind a simple name only when exactly one accessible type is imported. Two or more accessible types are a compile-time error. An inaccessible type is not imported. Two programs that depended on the first accessible star winning now name the type they meant: the Guice example qualifies Module, and NamedVariantTransformTest single-type-imports groovy.transform.ASTTest.
On-demand simple names follow the Groovy specification. A source import shadows every default import, including java.lang and star imports added by ImportCustomizer. Two source on-demand imports bind the first accessible type in source order, and a module import is that same kind of star import placed at its statement. An inaccessible type is not a candidate. An explicit single-type import, a fully qualified name, and Class.forName can still name a type that is not public.
Explain why a type star binds only a static member type, and why on-demand order is two lists rather than one sort of the raw positions. Drop the sequence key, since List.sort is stable, and do not cache the lists on the module. Read @PackageScope while resolve still sees a synthetic public class. Say in the tests what the modifier checks, the TimSort script, and the three source-order matrices are checking.
ConstructedNestedClass does not override getOuterClass(): resolve redirects the node first, and returning the construction-time outer would change later callers that still hold that node. The fixture comments now separate the flag lock from the accessibility policy, and state why an explicit import of a non-public type still resolves.
An earlier source on-demand import that finds a type but cannot access it no longer lets a later source import bind a different type. The name falls through to customizer imports and the language defaults. Two accessible source stars still bind the earlier type. Membership of a class read from bytecode comes from the InnerClasses attribute, or from Class.getDeclaringClass when the class was loaded. A top-level class whose name contains '$' is not treated as nested.
Move the DecompiledClassNode import next to the other ast imports. Customizer stars are tried when no source statement bound the name, including when one claimed an inaccessible type. Document that getMemberDeclaringClassName reads the last InnerClasses entry. Describe one module import as a single statement. Rename the source-star matrix test and close its temporary class loader.
3c6c81f to
b6bb2c2
Compare
https://issues.apache.org/jira/browse/GROOVY-11916
Star imports,
import module, and the implicit default imports resolve only types accessible from the importing class.import java.util.*no longer binds package-privatejava.util.TimSort; a type use of that simple name fails at compile time.Same-package package-private types, protected member types used from a subclass, and private member types used from the enclosing nest stay visible. An explicit single-type import and a fully-qualified name still resolve, and
Class.forNameis unchanged. Static field imports have required accessibility since Groovy 5 and are not changed here.Testing
bugs.Groovy11916