Problem with running plugin code in IntelliJ for finding the .java file

I have the following piece of code in my pluing:

  public static PsiElement getSourceElement(Project pProject, String pClassName)
 {
   String fqn = pClassName.endsWith(EXTENSION_JAVA) ? pClassName.replaceFirst(EXTENSION_JAVA, "") : pClassName;

   GlobalSearchScope scope = GlobalSearchScope.allScope(pProject);
   PsiClass psiClass = JavaPsiFacade.getInstance(pProject).findClass(fqn, scope);
   //No class found!
   if (psiClass == null) return null;

   return psiClass.getNavigationElement();
 }

Starting from a PsiClass (which has a link to the .java file from my project) that is passed to pClassName the returning PsiElement is a .class file while there exists a .java file in my project. The .class file that is returned comes from an external library that includes a jar containing the same class I am looking for (but of another version). Why does the plugin prefer this ‘older’ .class file above the ‘current’ .java file that is present in my project?

This can be fixed however by doing an invalidate caches and restart, but than it fails again for the same reason when I open a different project (read same project different version). Again this can be fixed by invalidate caches and restart, but then the first project encounters that same issue again..

In de gradle scripts when we add a dependency to such an external lib, we explicitly exclude it to avoid issues

for example (here we excluded module dce where the .class file I don't want to find is found)

    implementation(project(":objt.schedulingserver-api")) {
       exclude(group = "com.objt", module = "dce")

The .class file is found in a .gradle cache folder.

When running in debug we never encounter this issue, probably because the IntelliJ starts ‘clean’.

 

 

 

0

Please sign in to leave a comment.