[ 
https://issues.apache.org/jira/browse/GROOVY-12555?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18145966#comment-18145966
 ] 

ASF GitHub Bot commented on GROOVY-12555:
-----------------------------------------

jdaugherty commented on PR #3084:
URL: https://github.com/apache/groovy/pull/3084#issuecomment-6931817145

   Thanks, Paul. I took the proposal as written. For item 2 I kept one default 
everywhere and documented GSE instead. The branch is rebased on master, and the 
new commits are `cafadeda54` (option, lookup and tests) and `3b35ce66af` 
(notes). The 6.0.x port branch has the same change.
   
   **Loader history.** Agreed: it applies with or without sources, so I've 
dropped it as an argument. The notes now say that a reused `GroovyShell` or 
GSE, groovysh and the console can resolve a name to a class that the loader 
compiled earlier. With `defaultPackageSources=false` this goes away as well, 
since the class-file lookup doesn't use `loadClass`. 
`testClassCompiledEarlierByTheClassLoader` covers it.
   
   **1. `defaultPackageSources`.** 
`CompilerConfiguration.DEFAULT_PACKAGE_SOURCES` (`defaultPackageSources`, 
system property `groovy.resolve.default.package.sources`) is on unless set to 
`false`, and ignored when `defaultPackageFirst` is `false`. When it is `false`, 
`resolveFromDefaultPackage` calls a new package-private 
`ClassNodeResolver.resolveClassFile`. That method reuses the existing 
`readClassFile`: `loader.getResource(name + ".class")`, then `new 
DecompiledClassNode(AsmDecompiler.parseClass(url), new 
AsmReferenceResolver(this, compilationUnit))`, with the same name-mismatch 
check.
   - A hit goes through `cacheClass`.
   - A miss is not cached as `NO_CLASS`. It goes into a separate set in the 
resolver, so a name that occurs many times in one unit doesn't call 
`getResource` each time, and the later `resolveToOuter` still finds a sibling 
source after the default imports 
(`testDefaultPackageSourceFoundWithoutSources`).
   - It doesn't go through `findClassNode`, so GSE's override and its roots are 
not involved.
   
   I counted `ResourceConnector` calls for a GSE script that uses 12 
default-import types: 76 with the sources on, 52 with them off, and 52 with 
`defaultPackageFirst=false`. That matches your two extra lookups per type, and 
none with the sources off.
   
   **2. GSE default.** I kept one default everywhere. Having GSE resolve the 
same script differently from `groovyc` seemed worse than documenting the 
behaviour. A GSE-only default would also have to copy the caller's 
`CompilerConfiguration` in `ScriptClassLoader` for every compile, to avoid 
changing a configuration it doesn't own. `COMPATIBILITY.md` describes the mixed 
results, the stale results until the engine is recreated, and the two extra 
root lookups per name, with `defaultPackageSources=false` as the remedy. 
`testScriptEngineRootSource` checks both: by default the root's `Date`, with 
the sources off `java.util.Date`.
   
   **3. Gradle.** Agreed. The Groovy 7 entry now says an incremental Gradle 
build gives the same result either way, and gives the route 
`groovyOptions.optimizationOptions['defaultPackageSources']`.
   
   **4. Tests.** With the sources off, `Groovy12555` now covers:
   - a compiled class, which still comes before the default import;
   - a sibling source, which now comes after it;
   - a sibling source whose name isn't a default import, which is still found;
   - a nested class.
   
   It also adds the GSE case and the `GroovyShell` loader case, and 
`CompilerConfigurationTest` covers the new system property. The tests for the 
sibling source, GSE and the loader fail without the change. Core `:test` passes 
locally on both branches.
   
   **5. Notes.** The Groovy 7 and 6.0.1 entries in `COMPATIBILITY.md` and the 
note in `core-program-structure.adoc` now cover the option and the cases above. 
In the 6.0.x port both options are off unless set to `true`, so 
`defaultPackageFirst=true` alone gives the class-file-only lookup there. That 
is enough for the Gradle case.
   
   **6. Housekeeping.** Rebased on current master. In both conflicts, 
`COMPATIBILITY.md` and the system-property table, I kept both sides. The 6.0.x 
port is rebased on `GROOVY_6_0_X` as well.
   
   @daniellansun, could you take another look? Since your approval there's the 
nested-name commit (`289dfdeca2` after the rebase, `b96fa00c30` before it) and 
the two commits above.
   




> A default-package class resolves a precompiled default-package class to a 
> default import of the same name
> ---------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12555
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12555
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 4.0.33, 5.1.3
>            Reporter: James Daugherty
>            Priority: Major
>
> A class in the default package that refers to another class of the default 
> package by its simple name finds that class while both are compiled together. 
> When the other class is already compiled, the name resolves to a class of the 
> default imports with the same simple name instead, here 
> {{groovy.util.Factory}}, and the call fails at run time. In Java, a class of 
> the same package, the unnamed one included, takes precedence over on-demand 
> imports such as {{java.lang.*}}.
> {code:groovy}
> // Factory.groovy, default package
> class Factory {
>     static String hello() { 'hello from the Factory of this project' }
> }
> // Caller.groovy, default package
> class Caller {
>     static void main(String[] args) {
>         println Factory.hello() // MissingMethodException for 
> groovy.util.Factory when Factory is already compiled
>     }
> }
> {code}
> Reproduced with 4.0.33, 5.1.3 and master. A reproducer with a groovyc script 
> and an incremental Gradle build: 
> https://github.com/jdaugherty/groovy-default-package-resolution
> *Cause*
> {{ResolveVisitor.resolve}} tries, in order, the classes of the module, of the 
> compilation unit, of the default imports, and then the class path. For a 
> module of a named package, the module step also looks up {{package.Name}} on 
> the class path, so a compiled class of the same package comes first. For the 
> default package there is no such step, so the default imports come before a 
> compiled class of the default package.
> *Proposed fix*
> For a class of the default package, look up the simple name on the class path 
> after the compilation unit and before the default imports, as is done for a 
> named package. The lookup is behind the optimization option 
> {{defaultPackageFirst}} ({{CompilerConfiguration.DEFAULT_PACKAGE_FIRST}}; 
> system property {{groovy.resolve.default.package.first}}; in Gradle, 
> {{groovyOptions.optimizationOptions['defaultPackageFirst']}}, the only one of 
> Gradle's settings that reaches the compiler's optimization options): on by 
> default in Groovy 7, and an opt-in in 6.0.x.
> *Compatibility*
> For a compilation unit in the default package, a simple type name that is not 
> declared or imported in the unit and not in the same {{CompilationUnit}} is 
> looked up on the class path, and on the source path of the 
> {{GroovyClassLoader}}, as a default-package class before the default imports 
> ({{java.lang}}, {{java.util}}, {{java.io}}, {{java.net}}, {{java.time}}, 
> {{groovy.lang}}, {{groovy.util}}, {{BigInteger}}, {{BigDecimal}}). A 
> default-package {{List}} or {{Node}} on the class path therefore shadows 
> {{java.util.List}} or {{groovy.util.Node}} in default-package code, where the 
> default import used to win. This matches JLS ยง6.4.1 (a type declared in the 
> same package, the unnamed package included, shadows a type imported on 
> demand), except that an explicit star import still takes precedence, as 
> elsewhere in Groovy; it is also what Groovy already does when the class is in 
> the same {{CompilationUnit}}, and what {{ResolveVisitor.resolveFromModule}} 
> does for a named package. The change is not limited to incremental builds: 
> the {{groovy}} command uses the current directory as the class path when none 
> is given, so in a directory of scripts a sibling {{Node.groovy}}, 
> {{Timer.groovy}} or {{Math.groovy}} stands for that name in the other 
> scripts, and a sibling source that does not compile becomes a compile error 
> for a script that uses its name; the roots of a {{GroovyScriptEngine}} are 
> source paths of the same kind. Each default-package compilation unit also 
> pays one class-path lookup per default-import type it names, as a 
> named-package unit always has, and a {{GroovyShell}}, {{Eval}} or 
> {{GroovyScriptEngine}} pays it per script, since the lookup cache is per 
> unit. The entries in COMPATIBILITY.md have the details and the remedies.
> Found while making incremental builds of a Grails application produce the 
> same classes as a full build; see also GROOVY-12550 and GROOVY-12544.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to