Structural search and replace question...

Is there a trick in structural search and replace to make assertions about inheritence relationships? For example, can I find all classes that implement a particular interface? I don't mean directly via "implements" but via inheritence from a parent that implements the interface or via a superinterface of an implemented interface, etc.? Are those kinds of things possible?

It seems like this would take the form of ANDing and ORing conditions together... or something like that. But I don't see anything like that.

Just checking...


thanks,
Pat

11 comments

Hello Patrick,

Is there a trick in structural search and replace to make assertions
about inheritence relationships? For example, can I find all classes
that implement a particular interface? I don't mean directly via
"implements" but via inheritence from a parent that implements the
interface or via a superinterface of an implemented interface, etc.?
Are those kinds of things possible?


You can use the built-in template "implementors of interface":
class $Class$ implements $Interface$ {}

Variables / options:
Class: target of search
Interface: "java.util.List", Apply constraint within type hierarchy
(Note that the text/regular expression textfield supports ClassName completion)


0

Unless I am mistaken, this does not work. I did try the template, but it only matches classes that literally use the implement keyword. As far as I can see it will not match a class that implements the interface by way of it's parent class or a more distant relationship.

class Base implements MyInterface { }
class Child extends Base { }

I'd like to find Base and Child.

I guess this makes sense as this is a structural search that is looking at the ASTs and I shouldn't expect it to be doing dynamic inferences. But the "search within hierarhcy" seemed like it was hinting that things like this could be done.

For example, what I really want to do is to move a bunch of annotations from interface methods to their implementing class' methods. To do this I'd have to match both the interface method and one or more class impl methods.... I thought maybe I could declare both structures in one search and they would be AND'd together... I think this would be technically possible. However perhaps a better approach would be to provide a scripting language that lets you operate over the ASTs and apply these kind of tests.


thanks,
Pat

0

Hello Patrick,

Unless I am mistaken, this does not work. I did try the template, but
it only matches classes that literally use the implement keyword. As
far as I can see it will not match a class that implements the
interface by way of it's parent class or a more distant relationship.

class Base implements MyInterface { }
class Child extends Base { }
I'd like to find Base and Child.

I guess this makes sense as this is a structural search that is
looking at the ASTs and I shouldn't expect it to be doing dynamic
inferences. But the "search within hierarhcy" seemed like it was
hinting that things like this could be done.


You are not mistaken, this is indeed how things should work.
For instance, looking for classes implementing java.util.List, I will find:

public abstract class AbstractSequentialList extends AbstractList {...} The above match does not contain an "implements List]]>" clause, but its
superclass does.
What version of Idea are you using, and what options do you set when performing
the structural search?

-tt


0

Thanks for your reply -

I have tried this on 5.0.2 and 5.1.2 (the two builds I happen to be using).

I copy the "classes implementing interfaces" template.
I set the $Class$ as unlimited matches and target of the search.
I set the $Interface$ to List and check search in hierarchy...
I select Project files and Libraries...

The only results I get are those that directly implement List using the implements keyword.

I have also tried hitting "recursive matching" and giving $Class$ a match name of ".*" with "match within hierarchy". Same result. I have also tried setting that $Class$ pattern to unlimited matches... same result.

Can you tell me exactly how you got implementors of List?

thanks,
Pat Niemeyer

0

When I try it on IDEA 6.0 it gave me an out of memory error and corrupted the module file when I quit :(

I repeated it with the same result.


Pat

0

Hello Patrick,

I have tried this on 5.0.2 and 5.1.2 (the two builds I happen to be
using).

I copy the "classes implementing interfaces" template.
I set the $Class$ as unlimited matches and target of the search.
I set the $Interface$ to List and check search in hierarchy...
I select Project files and Libraries...
The only results I get are those that directly implement List using
the implements keyword.

I have also tried hitting "recursive matching" and giving $Class$ a
match name of ".*" with "match within hierarchy". Same result. I
have also tried setting that $Class$ pattern to unlimited matches...
same result.

Can you tell me exactly how you got implementors of List?


I did try this on 5.1.2 as well, with success.
I have a random project loaded that references JDK 1.5.

Structural search template is:
class $Class$ implements $Interface$ {}

Class: is target of search
Interface: List (Ctrl-Alt-Space completed to java.util.List (!!)),
"Apply constraint within type hierarchy"

I use a custom scope for this search, that resolves to the following pattern:
lib:java..* || lib:javax..*

Running this search returns 23 usages, one of which is AbstractSequentialList.
Another one is Stack extends Vector]]>.

Please double check that you completed the "List" pattern to "java.util.List",
using Classname completion in the dialog textfield.

You could also use the "Class Hierarchy" scope, and select java.util.List
as root, but AFAIK there's no way to use that and limit the results to, say,
"lib:java..* || lib:javax..*"

Hope this helps,
-tt


0

Hello Patrick,

When I try it on IDEA 6.0 it gave me an out of memory error and
corrupted the module file when I quit :(

I repeated it with the same result.


Structural search seems quite memory hungry indeed, even in 5.1.2
I've filed the following JIRA issue: http://www.jetbrains.net/jira/browse/IDEA-9981

-tt


0

Fixed. Enjoy.

Taras Tielkes wrote:

Hello Patrick,

>> When I try it on IDEA 6.0 it gave me an out of memory error and
>> corrupted the module file when I quit :(
>>
>> I repeated it with the same result.


Structural search seems quite memory hungry indeed, even in 5.1.2
I've filed the following JIRA issue:
http://www.jetbrains.net/jira/browse/IDEA-9981

-tt



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

Much, much better. (using #6068)

0

I was being stupid and didn't fully qualify my class names.
I don't know how I could have missed that.

Perhaps that entry box should act like a class selection list and force you to choose a legit class?

More generally, it would be neat to be able to write the whole expression in text rather than have to edit the variables via the GUI... e.g. if you could come up with a syntax to let you write this search fully in the structure editor...



thanks,
Pat

0

Hello Patrick,

I was being stupid and didn't fully qualify my class names. I don't
know how I could have missed that.


That is what I suspected from the start ;)
Ctrl-Alt-Space works as well (in the textfield).
As for missing it...yes it's not that obvious.

btw, there's an article about SSR in PFD format on the jetbrains site.


Perhaps that entry box should act like a class selection list and
force you to choose a legit class?


The field can also contain other type of data, not only class references.


More generally, it would be neat to be able to write the whole
expression in text rather than have to edit the variables via the
GUI... e.g. if you could come up with a syntax to let you write this
search fully in the structure editor...


This is an important remark, and something that I've been thinking about
as well.
I've been playing with Jackpot a bit, and having all the stuff in text is
nice.

Maxim, if you're reading this, I'm quite curious about your opinion on jackpot
as well.
Note that I'm not talking about it as an IDEA feature, just what do you think
of actual query and transformation languages used etc?

-tt


0

Please sign in to leave a comment.