Skip to content
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

Import not added when referencing type in different package from subclass #1326

Open
mauromol opened this issue Dec 30, 2021 · 8 comments
Open
Assignees

Comments

@mauromol
Copy link

mauromol commented Dec 30, 2021

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.").

@eric-milles
Copy link
Member

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 eric-milles self-assigned this Dec 30, 2021
@eric-milles
Copy link
Member

What version of Groovy are you compiling with from Gradle?

@mauromol
Copy link
Author

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
Copy link
Author

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

@eric-milles
Copy link
Member

eric-milles commented Dec 30, 2021

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
Copy link
Member

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
Copy link
Member

eric-milles commented Feb 5, 2022

@eric-milles eric-milles changed the title Import not added when referencing enum type in different package from subclass Import not added when referencing type in different package from subclass Nov 14, 2022
@TheMarvelFan
Copy link

TheMarvelFan commented Sep 10, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants