Home » Language IDEs » Java Development Tools (JDT) » How to search for subclasses of an arbitrary type
How to search for subclasses of an arbitrary type [message #254278] |
Thu, 26 June 2008 14:00  |
Eclipse User |
|
|
|
Originally posted by: myawn.ebay.com
I've done some stuff with the SearchEngine before, but it's always been
searching within projects loaded in the workspace. I now have a need to
search not only my workspace but any jars that are in the user's classpath.
I need to do a type hierarchy search for subtypes of a particular
interface; that interface is not part of the classpath of the plugin.
It looks like the SearchEngine wants an IType, but I'm not sure how to
get one given only a fully-qualified class name.
So question one is, given a fully-qualified class name, how can I create
an IType?
Question two is, if I use SearchEngine.createHierarchyScope() of that
IType, and then create a search pattern using
IJavaSearchConstants.IMPLEMENTORS, will that find implementors in any
jars as well?
(createOrSearchPattern is deprecated, but it seems that what I want is
to OR together my hierarchy scope with an
IJavaSearchScope.APPLICATION_LIBRARIES -- is that on the right track?)
Thanks,
Mike
|
|
|
SearchEngine not finding matches in application libraries [message #254459 is a reply to message #254278] |
Tue, 01 July 2008 12:21   |
Eclipse User |
|
|
|
Originally posted by: myawn.ebay.com
I must be doing something wrong here, but I can't figure out what.
I want to find classes that extend a particular abstract base class.
The base class is in a jar file; the classes that extend it may be in
the same jar file, in a different jar file, or in the current or
referenced projects.
When I do the search, I am finding a match within my current project
(good!), but not finding qualifying matches in the jar files. The local
match is a source file, while everything in the jar file is classes,
which might be a factor.
Here's the code I'm using to perform the search: Can anyone spot
something that is set up wrong?
Thanks,
Mike
private void findImplementors(final IJavaProject project)
throws CoreException {
int includeMask = IJavaSearchScope.SOURCES |
IJavaSearchScope.APPLICATION_LIBRARIES |
//IJavaSearchScope.SYSTEM_LIBRARIES |
IJavaSearchScope.REFERENCED_PROJECTS;
IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(
new IJavaElement[] { project }, includeMask);
// DEBUG: what does our scope encompass?
for (IPath path : searchScope.enclosingProjectsAndJars())
System.out.println("will search " + path);
// jar containing classes we want to match is seen,
// but results within that jar are not being returned.
SearchEngine se = new SearchEngine();
String tpFQName = "AbstractProvider"; // tried both simple and
// fully-qualified variants here
SearchPattern pattern =
SearchPattern.createPattern(tpFQName,
IJavaSearchConstants.CLASS,
IJavaSearchConstants.IMPLEMENTORS,
SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match)
throws CoreException {
System.err.println("found match " +
match.getElement().getClass() + ":" +
match.getElement());
m_results.add((IType)match.getElement());
}
};
long start = System.currentTimeMillis();
se.search(pattern, new SearchParticipant[] {
SearchEngine.getDefaultSearchParticipant() } ,
searchScope, requestor, monitor);
System.err.println("done in " +
(System.currentTimeMillis() - start) + " ms");
}
Mike Yawn wrote:
> I've done some stuff with the SearchEngine before, but it's always been
> searching within projects loaded in the workspace. I now have a need to
> search not only my workspace but any jars that are in the user's classpath.
>
> I need to do a type hierarchy search for subtypes of a particular
> interface; that interface is not part of the classpath of the plugin.
>
> It looks like the SearchEngine wants an IType, but I'm not sure how to
> get one given only a fully-qualified class name.
>
> So question one is, given a fully-qualified class name, how can I create
> an IType?
>
> Question two is, if I use SearchEngine.createHierarchyScope() of that
> IType, and then create a search pattern using
> IJavaSearchConstants.IMPLEMENTORS, will that find implementors in any
> jars as well?
>
> (createOrSearchPattern is deprecated, but it seems that what I want is
> to OR together my hierarchy scope with an
> IJavaSearchScope.APPLICATION_LIBRARIES -- is that on the right track?)
>
> Thanks,
> Mike
|
|
|
Re: How to search for subclasses of an arbitrary type [message #254499 is a reply to message #254278] |
Wed, 02 July 2008 06:12   |
Eclipse User |
|
|
|
Mike Yawn wrote:
> I've done some stuff with the SearchEngine before, but it's always been
> searching within projects loaded in the workspace. I now have a need to
> search not only my workspace but any jars that are in the user's classpath.
>
SearchEngine uses the given scope to limit the area where its looks for.
Typically, if you create the scope using a project, by default the scope
will be the project's classpath including libraries, referenced jar files
and projects it depends on...
> I need to do a type hierarchy search for subtypes of a particular
> interface; that interface is not part of the classpath of the plugin.
>
Do you mean that subtypes are in jar files that are on the project's
classpath but not the interface they implement?
> It looks like the SearchEngine wants an IType, but I'm not sure how to
> get one given only a fully-qualified class name.
>
In fact SearchEngine wants a SearchPattern which can be created either
using an IJavaElement (an IType in your case):
SearchPattern.createPattern(IJavaElement element, int limitTo)
or a String (then you need to precise what you're searching for):
SearchPattern.createPattern(String stringPattern, int searchFor,
int limitTo, int matchRule)
> So question one is, given a fully-qualified class name, how can I create
> an IType?
>
I do not think it's necessary in your case, but to do this you can
use IJavaProject.findType(String) or other findType(*) methods
> Question two is, if I use SearchEngine.createHierarchyScope() of that
> IType, and then create a search pattern using
> IJavaSearchConstants.IMPLEMENTORS, will that find implementors in any
> jars as well?
>
Yes, but if the interface is not in the project's classpath (as it seems
to be the case), then the SearchEngine will spot the matches as 'potential'
ones...
> (createOrSearchPattern is deprecated, but it seems that what I want is
> to OR together my hierarchy scope with an
> IJavaSearchScope.APPLICATION_LIBRARIES -- is that on the right track?)
>
As I said, the SearchEngine needs a pattern. To create an OR pattern
you need to use SearchPattern.createOrPattern(SearchPattern leftPattern,
SearchPattern rightPattern) method. That's why createOrSearchPattern
is deprecated.
BTW you should not need this kind of pattern here. The HierarchyScope
should be enough to find the expected matches.
> Thanks,
> Mike
|
|
|
Re: SearchEngine not finding matches in application libraries [message #254530 is a reply to message #254459] |
Wed, 02 July 2008 12:09  |
Eclipse User |
|
|
|
I do not see anything wrong in your snippet. Using it, I can find
interface implementors both in source and in a referenced jar file.
Perhaps may you try to create a small project allowing us to reproduce
your problem and open a bug against JDT/Core component with that
project zipped and attached to the bug?
Mike Yawn wrote:
>
> I must be doing something wrong here, but I can't figure out what.
>
> I want to find classes that extend a particular abstract base class. The
> base class is in a jar file; the classes that extend it may be in
> the same jar file, in a different jar file, or in the current or
> referenced projects.
>
> When I do the search, I am finding a match within my current project
> (good!), but not finding qualifying matches in the jar files. The local
> match is a source file, while everything in the jar file is classes,
> which might be a factor.
>
> Here's the code I'm using to perform the search: Can anyone spot
> something that is set up wrong?
>
> Thanks,
> Mike
>
> private void findImplementors(final IJavaProject project)
> throws CoreException {
>
> int includeMask = IJavaSearchScope.SOURCES |
> IJavaSearchScope.APPLICATION_LIBRARIES |
> //IJavaSearchScope.SYSTEM_LIBRARIES |
> IJavaSearchScope.REFERENCED_PROJECTS;
>
> IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(
> new IJavaElement[] { project }, includeMask);
>
> // DEBUG: what does our scope encompass?
> for (IPath path : searchScope.enclosingProjectsAndJars())
> System.out.println("will search " + path);
> // jar containing classes we want to match is seen,
> // but results within that jar are not being returned.
>
> SearchEngine se = new SearchEngine();
> String tpFQName = "AbstractProvider"; // tried both simple and
> // fully-qualified variants here
> SearchPattern pattern =
> SearchPattern.createPattern(tpFQName,
> IJavaSearchConstants.CLASS,
> IJavaSearchConstants.IMPLEMENTORS,
> SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
>
> SearchRequestor requestor = new SearchRequestor() {
> @Override
> public void acceptSearchMatch(SearchMatch match)
> throws CoreException {
> System.err.println("found match " +
> match.getElement().getClass() + ":" +
> match.getElement());
>
> m_results.add((IType)match.getElement());
> }
> };
> long start = System.currentTimeMillis();
> se.search(pattern, new SearchParticipant[] {
> SearchEngine.getDefaultSearchParticipant() } ,
> searchScope, requestor, monitor);
> System.err.println("done in " +
> (System.currentTimeMillis() - start) + " ms");
> }
>
>
|
|
|
Goto Forum:
Current Time: Sat May 31 06:17:03 EDT 2025
Powered by FUDForum. Page generated in 0.03587 seconds
|