IntelliJ中Groovy编译未正确导入自定义OrderBy类问题
I’ve run into this exact kind of import ambiguity before—super frustrating when the compiler picks the wrong class even though you intended to use your custom one. Here’s how to fix it without ditching your wildcard import:
1. Add an Explicit Import for Your Custom OrderBy
The simplest and cleanest solution is to add a specific import statement for your br.com.petrobras.sddi.domain.OrderBy after the wildcard import. In Groovy (and Java), explicit imports take precedence over wildcard imports, so this will force the compiler to prioritize your custom class over the Groovy utility one.
Your imports would look like this:
import br.com.petrobras.sddi.domain.* import br.com.petrobras.sddi.domain.OrderBy // This overrides the wildcard's ambiguous match
When you recompile, the BaseJPARepository.class file will correctly reference your custom OrderBy instead of groovy.util.OrderBy.
2. Use Fully Qualified Class Name Where Needed (Alternative)
If you’d rather avoid adding an extra import line, you can reference your custom OrderBy using its full class name wherever you use it in the code. For example:
def customOrderBy = new br.com.petrobras.sddi.domain.OrderBy(/* your parameters */)
This works, but it’s less clean if you use the class multiple times throughout the file.
Why This Happens
Groovy’s compiler resolves wildcard imports by scanning the classpath, and since groovy.util.OrderBy is part of the core Groovy libraries, it might be getting picked up before your project’s custom class. The explicit import acts as a clear tiebreaker, telling the compiler exactly which OrderBy you mean.
内容的提问来源于stack exchange,提问作者Rodrigo Bittencourt

