groovy-eclipse icon indicating copy to clipboard operation
groovy-eclipse copied to clipboard

Import not added when referencing type in different package from subclass

Open mauromol opened this issue 3 years ago • 8 comments

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 in MyEnum.FOO
  • no compilation error is produced by Greclipse
  • if you hit F3 on MyEnum in MyEnum.FOO, you'll be directed to Base.myEnum rather than to MyEnum 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.").

mauromol avatar Dec 30 '21 10:12 mauromol

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.

eric-milles avatar Dec 30 '21 14:12 eric-milles

What version of Groovy are you compiling with from Gradle?

eric-milles avatar Dec 30 '21 14:12 eric-milles

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.

mauromol avatar Dec 30 '21 14:12 mauromol

Please note that, if I manually add the import, all starts to work fine.

mauromol avatar Dec 30 '21 14:12 mauromol

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.

eric-milles avatar Dec 30 '21 14:12 eric-milles

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.

eric-milles avatar Jan 24 '22 18:01 eric-milles

https://issues.apache.org/jira/browse/GROOVY-6483 https://issues.apache.org/jira/browse/GROOVY-8449

eric-milles avatar Feb 05 '22 17:02 eric-milles

Hi, is no one working on this issue? If not, I would like to take this up and I would appreciate some guidance too.

TheMarvelFan avatar Sep 10 '23 17:09 TheMarvelFan