groovy-eclipse
groovy-eclipse copied to clipboard
Import not added when referencing type in different package from subclass
Using Greclipse 4.4.0.v202112282355-e2009.
Consider the following enum:
package test69b;
public enum MyEnum {
FOO, BAR;
}
And the following classes:
package test69
import test69b.MyEnum
class Base {
MyEnum myEnum
}
package test69
import groovy.transform.CompileStatic
@CompileStatic
class Ext extends Base {
void foo() {
if(myEnum == MyEnum.FOO)
println 'bla'
}
}
Please note that MyEnum
is in a different package.
Observed behaviour in Ext
:
- Greclipse does not automatically insert an import for
test69b.MyEnum
- Greclipse does nothing with Ctrl+Shift+O (organize imports) or Ctrl+Shift+M (add import) on
MyEnum
inMyEnum.FOO
- no compilation error is produced by Greclipse
- if you hit F3 on
MyEnum
inMyEnum.FOO
, you'll be directed toBase.myEnum
rather than toMyEnum
type - syntax highlighting reflects the fact that
MyEnum
is not recognized as a type
If you however try to compile this with groovyc (I tried it through Gradle), compilation correctly fails because MyEnum
is not recognized ("[Static type checking] - The variable [MyEnum] is undeclared.").
I'm checking to see what can be done here. There are a couple places in the compiler where case is ignored for property accessor lookup. Because you have "MyEnum" for the type and "myEnum" for the property, case comes into play. If you change the name of the property, "MyEnum" will not be recognized as "getMyEnum()" from Base.
What version of Groovy are you compiling with from Gradle?
Yes, I guessed the problem lies in the fact that myEnum
is named like MyEnum
. However, in this specific case I don't think the lookup is ambiguous.
I'm compiling with Groovy 2.5.15-indy.
Please note that, if I manually add the import, all starts to work fine.
I get it. I would not expect it to work this way either. The recognition is in Groovy, not Greclipse. I'm just looking at 2.5 now to see if a patch has been applied to backport Groovy 3 or 4 behavior.
I'm not sure what can be done here. STC determines that the property satisfies "MyEnum" and this is not resolved as an undeclared variable or unresolved type until a later compiler phase of SC. The editor does not reach this compiler phase, so it only has the STC metadata.
https://issues.apache.org/jira/browse/GROOVY-6483 https://issues.apache.org/jira/browse/GROOVY-8449
Hi, is no one working on this issue? If not, I would like to take this up and I would appreciate some guidance too.