Home » Language IDEs » Java Development Tools (JDT) » How to find specific .java files
How to find specific .java files [message #258723] |
Tue, 17 February 2009 07:52  |
Eclipse User |
|
|
|
Hi there,
I was wondering if anyone could tell me how I can find .java files that
matches specific criteria.
So far I've been able to find the IClasspathEntries where the files are
located but how can I use these entries to reach my goal ?
|
|
| |
Re: How to find java files containing specific annotations [message #258755 is a reply to message #258752] |
Wed, 18 February 2009 05:09   |
Eclipse User |
|
|
|
Julien M. wrote:
> Ok I managed to find some of the files that match my criteria but when I
> try to find the ones with annotations using SearchPattern I don't get
> the results I expect.
>
> Say I'm looking for files that contain the annotation @SomeType what
> should my createPattern call look like ?
>
If you're looking for the declaration of SomeType, then your pattern
should look like:
SearchPattern pattern = SearchPattern.createPattern("SomeType",
IJavaSearchConstants.ANNOTATION_TYPE,
IJavaSearchConstants.DECLARATIONS,
SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
Note that using SearchEngine avoid you to first search for the java
files, it will do this for you using its own index files...
So, the following snippet would bring you the expected matches:
SearchRequestor requestor = new SearchRequestor() {
public void acceptSearchMatch(SearchMatch match) throws CoreException {
// do something each time a match is got...
}
};
new SearchEngine().search(pattern,
new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
SearchEngine.createWorkspaceScope(),
requestor,
null);
To better understand the Search Engine behavior and how use it,
first have a look to:
Help Contents -> JDT Plug-in Developer Guide:
-> Programmer's Guide
-> JDT/Core
-> Using the Java Search Engine
then, read the SearchPattern and SearchEngine API.
HTH
|
|
|
Re: How to find java files containing specific annotations [message #258758 is a reply to message #258755] |
Wed, 18 February 2009 06:07   |
Eclipse User |
|
|
|
Thanks for your tip Frederic, but I still don't get the expected results
so let me give you more details about my search : I'm looking for files
that contain the following :
package mockapplication.componenta;
import mockapplication.SomeType;
@SomeType
public interface ComponentA
{
}
The workspace scope is properly initialized.
About the search, I tried to change the first and the second parameter of
createPattern respectively to "@SomeType" (no results at all) and to
ALL_OCCURRENCES (2 results per file)
> If you're looking for the declaration of SomeType, then your pattern
> should look like:
> SearchPattern pattern = SearchPattern.createPattern("SomeType",
> IJavaSearchConstants.ANNOTATION_TYPE,
> IJavaSearchConstants.DECLARATIONS,
> SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
> Note that using SearchEngine avoid you to first search for the java
> files, it will do this for you using its own index files...
> So, the following snippet would bring you the expected matches:
> SearchRequestor requestor = new SearchRequestor() {
> public void acceptSearchMatch(SearchMatch match) throws CoreException {
> // do something each time a match is got...
> }
> };
> new SearchEngine().search(pattern,
> new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
> SearchEngine.createWorkspaceScope(),
> requestor,
> null);
> To better understand the Search Engine behavior and how use it,
> first have a look to:
> Help Contents -> JDT Plug-in Developer Guide:
> -> Programmer's Guide
> -> JDT/Core
> -> Using the Java Search Engine
> then, read the SearchPattern and SearchEngine API.
> HTH
|
|
|
Re: How to find java files containing specific annotations [message #258762 is a reply to message #258758] |
Wed, 18 February 2009 07:04   |
Eclipse User |
|
|
|
Julien Manankiandrianana wrote:
> Thanks for your tip Frederic, but I still don't get the expected results
> so let me give you more details about my search : I'm looking for files
> that contain the following :
>
> package mockapplication.componenta;
>
> import mockapplication.SomeType;
>
> @SomeType
> public interface ComponentA
> {
> }
>
> The workspace scope is properly initialized.
>
> About the search, I tried to change the first and the second parameter
> of createPattern respectively to "@SomeType" (no results at all) and to
> ALL_OCCURRENCES (2 results per file)
I wrongly assumed that you were looking for declarations although
it was for references...
Hence, the limitTo argument of the createPattern only needs to be:
IJavaSearchConstants.REFERENCES,
instead of:
IJavaSearchConstants.DECLARATIONS,
ALL_OCCURENCES means searching both for DECLARATIONS and REFERENCES and that
does not seem to be necessary in your case. However, using it is OK to find
references and as you find 2 results, I guess that you got what you're expecting,
didn't you?
>> If you're looking for the declaration of SomeType, then your pattern
>> should look like:
>> SearchPattern pattern = SearchPattern.createPattern("SomeType",
>> IJavaSearchConstants.ANNOTATION_TYPE,
>> IJavaSearchConstants.DECLARATIONS,
>> SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
>
>> Note that using SearchEngine avoid you to first search for the java
>> files, it will do this for you using its own index files...
>
>> So, the following snippet would bring you the expected matches:
>
>> SearchRequestor requestor = new SearchRequestor() {
>> public void acceptSearchMatch(SearchMatch match) throws
>> CoreException {
>> // do something each time a match is got...
>> }
>> };
>> new SearchEngine().search(pattern,
>> new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
>> SearchEngine.createWorkspaceScope(),
>> requestor,
>> null);
>
>> To better understand the Search Engine behavior and how use it,
>> first have a look to:
>> Help Contents -> JDT Plug-in Developer Guide:
>> -> Programmer's Guide
>> -> JDT/Core
>> -> Using the Java Search Engine
>
>> then, read the SearchPattern and SearchEngine API.
>
>> HTH
>
>
|
|
|
Re: How to find java files containing specific annotations [message #258766 is a reply to message #258762] |
Wed, 18 February 2009 08:46   |
Eclipse User |
|
|
|
Actually unfortunately, no I didn't. I still get 2 results per file and
there is something else I should've said before : files like following :
package mockapplication.componenta;
import components.SomeType;
public interface ComponentA
extends SomeType
{
}
are also considered a match, even 2 actually.
I tried to use TYPE for the 1st argument and I got exactly the same
results as when using ANNOTATION_TYPE. It doesn't seem that
ANNOTATION_TYPE is actually only looking for annotation which led me to
modify the import of one file so that the import doesn't contain a
"SomeType" string wich made the search find only one result for the
modified file.
Any idea what I should do to get only one result per file that actually
contain the right annotation ?
> I wrongly assumed that you were looking for declarations although
> it was for references...
> Hence, the limitTo argument of the createPattern only needs to be:
> IJavaSearchConstants.REFERENCES,
> instead of:
> IJavaSearchConstants.DECLARATIONS,
> ALL_OCCURENCES means searching both for DECLARATIONS and REFERENCES and that
> does not seem to be necessary in your case. However, using it is OK to find
> references and as you find 2 results, I guess that you got what you're
expecting,
> didn't you?
>>> If you're looking for the declaration of SomeType, then your pattern
>>> should look like:
>>> SearchPattern pattern = SearchPattern.createPattern("SomeType",
>>> IJavaSearchConstants.ANNOTATION_TYPE,
>>> IJavaSearchConstants.DECLARATIONS,
>>> SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
>>
>>> Note that using SearchEngine avoid you to first search for the java
>>> files, it will do this for you using its own index files...
>>
>>> So, the following snippet would bring you the expected matches:
>>
>>> SearchRequestor requestor = new SearchRequestor() {
>>> public void acceptSearchMatch(SearchMatch match) throws
>>> CoreException {
>>> // do something each time a match is got...
>>> }
>>> };
>>> new SearchEngine().search(pattern,
>>> new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
>>> SearchEngine.createWorkspaceScope(),
>>> requestor,
>>> null);
>>
>>> To better understand the Search Engine behavior and how use it,
>>> first have a look to:
>>> Help Contents -> JDT Plug-in Developer Guide:
>>> -> Programmer's Guide
>>> -> JDT/Core
>>> -> Using the Java Search Engine
>>
>>> then, read the SearchPattern and SearchEngine API.
>>
>>> HTH
>>
>>
|
|
|
Re: How to find java files containing specific annotations [message #258770 is a reply to message #258766] |
Wed, 18 February 2009 09:48   |
Eclipse User |
|
|
|
To limit the type references to this kind of reference, then you should
use the fine grain flag IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE
to the limitTo argument:
SearchPattern pattern = SearchPattern.createPattern("SomeType",
IJavaSearchConstants.ANNOTATION_TYPE,
IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE,
SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
I guess that with this pattern you will get what you're expecting for...
Julien Manankiandrianana wrote:
> Actually unfortunately, no I didn't. I still get 2 results per file and
> there is something else I should've said before : files like following :
> package mockapplication.componenta;
>
> import components.SomeType;
>
> public interface ComponentA extends SomeType
> {
> }
>
> are also considered a match, even 2 actually.
>
> I tried to use TYPE for the 1st argument and I got exactly the same
> results as when using ANNOTATION_TYPE. It doesn't seem that
> ANNOTATION_TYPE is actually only looking for annotation which led me to
> modify the import of one file so that the import doesn't contain a
> "SomeType" string wich made the search find only one result for the
> modified file.
>
> Any idea what I should do to get only one result per file that actually
> contain the right annotation ?
>
>> I wrongly assumed that you were looking for declarations although
>> it was for references...
>
>> Hence, the limitTo argument of the createPattern only needs to be:
>> IJavaSearchConstants.REFERENCES,
>> instead of:
>> IJavaSearchConstants.DECLARATIONS,
>
>> ALL_OCCURENCES means searching both for DECLARATIONS and REFERENCES
>> and that
>> does not seem to be necessary in your case. However, using it is OK to
>> find
>> references and as you find 2 results, I guess that you got what you're
> expecting,
>> didn't you?
>
>>>> If you're looking for the declaration of SomeType, then your pattern
>>>> should look like:
>>>> SearchPattern pattern = SearchPattern.createPattern("SomeType",
>>>> IJavaSearchConstants.ANNOTATION_TYPE,
>>>> IJavaSearchConstants.DECLARATIONS,
>>>> SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
>>>
>>>> Note that using SearchEngine avoid you to first search for the java
>>>> files, it will do this for you using its own index files...
>>>
>>>> So, the following snippet would bring you the expected matches:
>>>
>>>> SearchRequestor requestor = new SearchRequestor() {
>>>> public void acceptSearchMatch(SearchMatch match) throws
>>>> CoreException {
>>>> // do something each time a match is got...
>>>> }
>>>> };
>>>> new SearchEngine().search(pattern,
>>>> new SearchParticipant[]
>>>> {SearchEngine.getDefaultSearchParticipant()},
>>>> SearchEngine.createWorkspaceScope(),
>>>> requestor,
>>>> null);
>>>
>>>> To better understand the Search Engine behavior and how use it,
>>>> first have a look to:
>>>> Help Contents -> JDT Plug-in Developer Guide:
>>>> -> Programmer's Guide
>>>> -> JDT/Core
>>>> -> Using the Java Search Engine
>>>
>>>> then, read the SearchPattern and SearchEngine API.
>>>
>>>> HTH
>>>
>>>
>
>
|
|
| |
Re: How to find java files containing specific annotations [message #258804 is a reply to message #258758] |
Thu, 19 February 2009 08:56   |
Eclipse User |
|
|
|
Here's a snippet which could solve your problem:
final String annotationName = "SomeType";
SearchRequestor requestor = new SearchRequestor() {
public void acceptSearchMatch(SearchMatch match) throws CoreException {
Object element = match.getElement();
if (element instanceof IType) {
IType type = (IType) element;
if (type.getAnnotation(annotationName).exists()) {
System.out.println(type.getElementName()+" is annotated by "+annotationName);
}
}
}
};
SearchPattern pattern = SearchPattern.createPattern(annotationName,
IJavaSearchConstants.ANNOTATION_TYPE,
IJavaSearchConstants.REFERENCES,
SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
try {
new SearchEngine().search(pattern,
new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
SearchEngine.createWorkspaceScope(),
requestor,
null);
participants, scope, requestor, null);
} catch (Exception e) {
e.printStackTrace();
}
Be careful using this snippet as the exist() method called in the acceptMatch(...)
method will open the type, hence populates the Java Model which may be problematic
if you're expecting to get tons of matches...
HTH
Julien Manankiandrianana wrote:
> Thanks for your tip Frederic, but I still don't get the expected results
> so let me give you more details about my search : I'm looking for files
> that contain the following :
>
> package mockapplication.componenta;
>
> import mockapplication.SomeType;
>
> @SomeType
> public interface ComponentA
> {
> }
>
> The workspace scope is properly initialized.
>
> About the search, I tried to change the first and the second parameter
> of createPattern respectively to "@SomeType" (no results at all) and to
> ALL_OCCURRENCES (2 results per file)
>
>> If you're looking for the declaration of SomeType, then your pattern
>> should look like:
>> SearchPattern pattern = SearchPattern.createPattern("SomeType",
>> IJavaSearchConstants.ANNOTATION_TYPE,
>> IJavaSearchConstants.DECLARATIONS,
>> SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
>
>> Note that using SearchEngine avoid you to first search for the java
>> files, it will do this for you using its own index files...
>
>> So, the following snippet would bring you the expected matches:
>
>> SearchRequestor requestor = new SearchRequestor() {
>> public void acceptSearchMatch(SearchMatch match) throws
>> CoreException {
>> // do something each time a match is got...
>> }
>> };
>> new SearchEngine().search(pattern,
>> new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
>> SearchEngine.createWorkspaceScope(),
>> requestor,
>> null);
>
>> To better understand the Search Engine behavior and how use it,
>> first have a look to:
>> Help Contents -> JDT Plug-in Developer Guide:
>> -> Programmer's Guide
>> -> JDT/Core
>> -> Using the Java Search Engine
>
>> then, read the SearchPattern and SearchEngine API.
>
>> HTH
>
>
|
|
| | |
Re: How to find java files containing specific annotations [message #258819 is a reply to message #258812] |
Fri, 20 February 2009 09:13   |
Eclipse User |
|
|
|
Julien Manankiandrianana wrote:
> It's alright I Found a way around my problem, thanks a lot for all your
> tips and snippets,
>
> Julien
>
You meant that the snippet I provided didn't work for you?
Frederic Fusier wrote:
> Here's a snippet which could solve your problem:
>
> final String annotationName = "SomeType";
> SearchRequestor requestor = new SearchRequestor() {
> public void acceptSearchMatch(SearchMatch match) throws CoreException {
> Object element = match.getElement();
> if (element instanceof IType) {
> IType type = (IType) element;
> if (type.getAnnotation(annotationName).exists()) {
> System.out.println(type.getElementName()+" is annotated by
> "+annotationName);
> }
> }
> }
> };
> SearchPattern pattern = SearchPattern.createPattern(annotationName,
> IJavaSearchConstants.ANNOTATION_TYPE,
> IJavaSearchConstants.REFERENCES,
> SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
> try {
> new SearchEngine().search(pattern,
> new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
> SearchEngine.createWorkspaceScope(),
> requestor,
> null);
> participants, scope, requestor, null);
> } catch (Exception e) {
> e.printStackTrace();
> }
>
> Be careful using this snippet as the exist() method called in the
> acceptMatch(...)
> method will open the type, hence populates the Java Model which may be
> problematic
> if you're expecting to get tons of matches...
>
> HTH
>
|
|
|
Re: How to find java files containing specific annotations [message #258834 is a reply to message #258819] |
Mon, 23 February 2009 03:37  |
Eclipse User |
|
|
|
The getAnnotation() function of IType was undefined in my version of JDT
(3.3). So I had to filter myself the search matches to keep what I was
interested in (ResolvedSourceField, etc.), but the snippet really helped.
> Julien Manankiandrianana wrote:
>> It's alright I Found a way around my problem, thanks a lot for all your
>> tips and snippets,
>>
>> Julien
>>
> You meant that the snippet I provided didn't work for you?
> Frederic Fusier wrote:
> > Here's a snippet which could solve your problem:
> >
> > final String annotationName = "SomeType";
> > SearchRequestor requestor = new SearchRequestor() {
> > public void acceptSearchMatch(SearchMatch match) throws CoreException {
> > Object element = match.getElement();
> > if (element instanceof IType) {
> > IType type = (IType) element;
> > if (type.getAnnotation(annotationName).exists()) {
> > System.out.println(type.getElementName()+" is annotated by
> > "+annotationName);
> > }
> > }
> > }
> > };
> > SearchPattern pattern = SearchPattern.createPattern(annotationName,
> > IJavaSearchConstants.ANNOTATION_TYPE,
> > IJavaSearchConstants.REFERENCES,
> > SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
> > try {
> > new SearchEngine().search(pattern,
> > new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
> > SearchEngine.createWorkspaceScope(),
> > requestor,
> > null);
> > participants, scope, requestor, null);
> > } catch (Exception e) {
> > e.printStackTrace();
> > }
> >
> > Be careful using this snippet as the exist() method called in the
> > acceptMatch(...)
> > method will open the type, hence populates the Java Model which may be
> > problematic
> > if you're expecting to get tons of matches...
> >
> > HTH
> >
|
|
|
Goto Forum:
Current Time: Wed May 07 14:54:32 EDT 2025
Powered by FUDForum. Page generated in 0.07428 seconds
|