Best approach for reference creation



I have project containing xml files which contains references into java code. In plugin I'm using static initialization of all references found in java codes but this approach sometimes fail with ProcessCancelledException when project is opened (initialization takes too much time).

I don't know what approach should I use. It must be something which I can do after project is opened. I was thinking about indexing java code and creation FileBasedIndex but for that I'd need serialization and my object contains PsiElement (used as reference).

Now, my static initialization uses search & psi facade look like:

JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
clazz = facade.findClass("some class", GlobalSearchScope.allScope(project));
PsiMethod method = clazz.findMethodsByName("some method", false)[0];
Collection<PsiReference> methodRefs =;

and is initialized when @Referencing annotation is found during dom.extender phase.

Comment actions Permalink

Sorry, I don't understand what exactly you initialize statically and what you need. But MethodReferencesSearch is slow.

The usual approach is to have a PsiReferenceContributor which registers PsiReferenceProvider-s that return some references based on the place specification (via ElementPattern).

Comment actions Permalink

The crux of the problem is that creating one reference (from xml -> java) takes couple of seconds because I need to use string xml attribute value and search 20-30 java classes (out of 10 000 classes), each of those 20-30 classes contains tens-hundreds of fields and I need to parse those fields (PsiField) and extract their data into my X objects. Such a field object I need for 1) reference target 2) documentation target (my custom JavaDocumentationProvider).

Because single opened xml file in editor contains tens of such possible references, it's quite slow and therefore I made a static initialization where I do that java search only once and store my X objects and subsequent reference resolving and documentation providing is fast.

But this approach fails sometime with ProcessCancelledException. Could I tell Idea e.g. that my initialization is not cancelable somehow?


Comment actions Permalink

From the description it seems that you need all this heavy info for reference.resolve(), not reference creation. The creation should be fast, it's the resolve that can be quite slow (and it's expected).

You can't 100% guarantee the absence of ProcessCanceledException whenever you query index (and you do). You can decrease its probability, but it's also not a great idea since that might freeze the UI for substantial time.


Please sign in to leave a comment.