Is there a fast way to find elements annotated with a specific annotation?

What I currently try to do is:

PsiClass nameAnnotation = psiManager.findClass("org.jboss.seam.annotations.Name");
Query]]> query = ReferencesSearch.search(nameAnnotation);

This aborts after a couple of minutes with OutOutMemoryError.

7 comments

It throws an OutOfMemoryError without even calling query.findAll() ?
That looks like a bug to me.

Bas

Stephen Kelvin wrote:

What I currently try to do is:

PsiClass nameAnnotation = psiManager.findClass("org.jboss.seam.annotations.Name");
Query<PsiReference> query = ReferencesSearch.search(nameAnnotation);

This aborts after a couple of minutes with OutOutMemoryError.

0

:)
Of course, findAll() is exactly what I try do to after that, just missed
the line in copy/paste.
So: Do you know a faster way to do it?
(If I restrict the search to production scope it finishes successfully,
but still is way too slow - and isn't really what I want.)

Being able to quickly find elements (classes and fields) annotated with
certain annotations would be a central issue in a plugin that helps
using JBoss Seam in Idea.

Bas Leijdekkers wrote:

It throws an OutOfMemoryError without even calling query.findAll() ?
That looks like a bug to me.

Bas

Stephen Kelvin wrote:

>> What I currently try to do is:
>>
>> PsiClass nameAnnotation =
>> psiManager.findClass("org.jboss.seam.annotations.Name");
>> Query query = ReferencesSearch.search(nameAnnotation); >> >>]]> This aborts after a couple of minutes with OutOutMemoryError.

0

If possible try to use query.forEach(processor). It doesn't build up the
entire search result in memory but calls the process() method of the
specified processor for each element as it finds it. There you could for
example decide that you don't need certain result and discard them,
storing only the results you do need.

Bas

Stephen Kelvin wrote:

:)
Of course, findAll() is exactly what I try do to after that, just missed
the line in copy/paste.
So: Do you know a faster way to do it?
(If I restrict the search to production scope it finishes successfully,
but still is way too slow - and isn't really what I want.)

Being able to quickly find elements (classes and fields) annotated with
certain annotations would be a central issue in a plugin that helps
using JBoss Seam in Idea.

Bas Leijdekkers wrote:

>> It throws an OutOfMemoryError without even calling query.findAll() ?
>> That looks like a bug to me.
>>
>> Bas
>>
>> Stephen Kelvin wrote:
>>> What I currently try to do is:
>>>
>>> PsiClass nameAnnotation =
>>> psiManager.findClass("org.jboss.seam.annotations.Name");
>>> Query query = ReferencesSearch.search(nameAnnotation); >>> >>>]]> This aborts after a couple of minutes with OutOutMemoryError.

0

Hello Stephen,

There's another search, much faster one: AnnotatedMembersSearch

-


Maxim Shafirov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

What I currently try to do is:

PsiClass nameAnnotation =
psiManager.findClass("org.jboss.seam.annotations.Name");
Query<PsiReference> query =
ReferencesSearch.search(nameAnnotation);
This aborts after a couple of minutes with OutOutMemoryError.



0

Hooray, that made my day! I already thought I had to dig into
the new global inspection api.
Now I'll test again to see if it is fast enough to run my
inspections on the fly.

If I may shamelessly ask an unrelated question:
I am currently stuck with a quick fix that should convert this
annotation
@Interceptors(String.class)
to
@Interceptors({String.class, SeamInterceptor.class})

Basically this involves converting a PsiClassObjectAccessExpression
to a PsiArrayInitializerMemberValue.
The best I have managed so far is creating a PsiArrayInitializerExpression.
No exception, correct text in editor, but the code gets a red error until
I make a dummy change.

Any advice?

Maxim Shafirov (JetBrains) wrote:

Hello Stephen,

There's another search, much faster one: AnnotatedMembersSearch

------------------
Maxim Shafirov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>> What I currently try to do is:
>>
>> PsiClass nameAnnotation =
>> psiManager.findClass("org.jboss.seam.annotations.Name");
>> Query query = >> ReferencesSearch.search(nameAnnotation); >> This aborts after a couple of minutes with OutOutMemoryError. >>]]>


0

Red code until a dummy change usually happens when the psi tree is incorrectly modified. The easiest way for these kinds of things most of the time is to create a new annotation (or any other element) from a string:
PsiElementFactory.createAnnotationFromText()
And replace the old annotation with the new one.

Bas

0

Maxim, this is fantastic: about 2 milliseonds for a global search (including jars).

Bas, thanks for the tip - should have thought of that.
To avoid removing any comments that might be embedded (well, ok, unlikely) I first
replace the class reference with the array initializer, and after that get the
annotation text and re-create the annotation.

Bas Leijdekkers wrote:

Red code until a dummy change usually happens when the psi tree is incorrectly modified. The easiest way for these kinds of things most of the time is to create a new annotation (or any other element) from a string:
PsiElementFactory.createAnnotationFromText()
And replace the old annotation with the new one.

Bas

0

Please sign in to leave a comment.