How can I use Intellij Structural Search to find all methods that use two named classes? - intellij-idea

Say I have two Java classes, TradeType and InstrumentType. I know that somewhere in my codebase there is a method that maps from TradeType to InstrumentType.
How can I use Structural Search in Intellij to say
"find me all methods that have a single parameter of type TradeType and which return InstrumentType"

Search template:
class $Class$ {
InstrumentType $MethodName$(TradeType $Parameter$);
}
Then click Edit variables..., and set the following for the MethodName variable:
Occurrences count: Unlimited
This variable is target of the search
Also pay attention to what the scope of the search is.

Related

Kotlin extension functions vs member functions?

I am aware that extension functions are used in Kotlin to extend the functionality of a class (for example, one from a library or API).
However, is there any advantage, in terms of code readability/structure, by using extension functions:
class Foo { ... }
fun Foo.bar() {
// Some stuff
}
As opposed to member functions:
class Foo {
...
fun bar() {
// Some stuff
}
}
?
Is there a recommended practice?
When to use member functions
You should use member functions if all of the following apply:
The code is written originally in Kotlin
You can modify the code
The method makes sense to be able to use from any other code
When to use extension functions
You should use extension functions if any of the following apply:
The code was originally written in Java and you want to add methods written in Kotlin
You cannot change the original code
You want a special function that only makes sense for a particular part of the code
Why?
Generally, member functions are easier to find than extension functions, as they are guaranteed to be in the class they are a member of (or a super class/interface).
They also do not need to be imported into all of the code that uses them.
From my point of view, there are two compelling reasons to use extension functions:
To "extend" the behaviour of a class you're not the author of / can't change (and where inheritance doesn't make sense or isn't possible).
To provide a scope for particular functionality. For example, an extension function may be declared as a freestanding function, in which case it's usable everywhere. Or you may choose to declare it as a (private) member function of another class, in which case it's only usable from inside that class.
It sounds like #1 isn't a concern in your case, so it's really more down to #2.
Extension functions are similar to those you create as a utility functions.
A basic example would be something like this:
// Strings.kt
fun String.isEmail() : Boolean {
// check for email pattern and return true/false
}
This code can be written as a utility function in Java like this:
class StringUtils {
public static boolean isEmail(String email) {
// check for email pattern and return true/false
}
}
So what it essentially does is, calling the same function with the object you call on will be passed as the first parameter to the argument. Like the same function I have given example of in Java.
If you want to call the extension function created in kotlin from java, you need to pass the caller as the first argument. Like,
StringsKt.isEmail("example#example.com")
As per the documentation,
Extensions do not actually modify classes they extend. By defining an extension, you do not insert new members into a class, but merely make new functions callable with the dot-notation on variables of this type.
They are simply static functions with the caller as the first argument and other parameters followed by it. It just extends the ability for us to write it that way.
When to create extension functions?
When you don't have access to that class. When that class belongs to some library you have not created.
For primitive types. Int, Float, String, etc.
The another reason for using extension function is, you don't have to extend that class in order to use the methods, as if they belong to that class (but not actually part of that class).
Hope it makes a bit clear for you..
As mentioned in other answers, extension functions are primarily used in code that you can't change - maybe you want to change complex expression around some library object into easier and more readable expression.
My take would be to use extension functions for data classes. My reasoning is purely philosophical, data classes should be used only as data carriers, they shouldn't carry state and by themselves shouldn't do anything. That's why I think you should use extension function in case you need to write a function around data class.

Structural Search to match method call with generic parameter

Let's say that I have a class class Foo : Base and I want to perform a certain method call with signature
public void someStuf(Iterable<? extends Base> param)
For the search template I just take as starting point the pre-existing one
$Instance$.$MethodCall$($Parameter$)
Is it possible to match a parameter of any kind Iterable of a specific Base subclass (Foo in this example)??
List<Foo> fooList = new ArrayList<>();
fooList.add(new Foo("A"));
List<Bar> barList = new ArrayList<>();
barList.add(new Bar(1));
someStuff(fooList); // find this!
someStuff(barList); // don't find this one
someStuff(Collections.singletonList(new Foo("B"))); // also match this one
I've tried several combinations without any luck, is it even possible to do?
This was previously not possible without resorting to a hack. See further below for that. Currently, the code can be found using a Type modifier.
search template
$Instance$.$MethodCall$($Parameter$)
variables
$Instance$ Count=[0,1]
$MethodCall Text=someStuff
$Parameter$ Type=Iterable<Foo>, within type hierarchy
hack
The hack previously needed used a Script modifier and a simple Type modifier on the $Parameter$ variable:
$Parameter$
Script=__context__.type.parameters[0].presentableText.contains("Foo")
Type=Iterable
The related bug report is fixed since IntelliJ IDEA 2017.3.

structural search Intellij IDEA : Howto find occurrences of all classes extending another class

is there a way to search for all occurences of subclasses of a particular class ?
Say I have a class AbstractItem and I have got a lot of implementations of this AbstractItem class. Now I want to know where all subclasses are used.
Can I search for all occurences of all subclasses with a structural search pattern in Intellij IDEA ?
Thanks,
Detlef
The easiest way is use the following:
Search template:
$A$
Set Expression type (regexp) to AbstractItem and check the Apply constraint within type hierarchy checkbox (under Edit Variables...).
This will find all expressions with type or subtype of AbstractItem.

IntelliJ: find method by signature

I wonder if it's possible to find method of object which returns specific type?
For example I want to look in autocompletion all methods which return String:
For this example it's easy to find it just by scrolling down, but for objects with large number of methods it can be painful.
Actually there is no default sorting by return type(yet?), even in class structure view.
I'm using smart completion(ctrl+shift+space) for this purposes in place where only certain type is accepted, for example:
String s = aClass.<smart completion here>
It will show methods from a aClass that return String at the beginning.

IntelliJ IDEA: how to find all instance creation of a class?

abstract class Base {}
class A extends Base
class B extends Base
How do I find all places in the code that create Base? (that is, have either new A() or new B())
UPDATE
To make it clear, the above is just and example. I'm interested in a way of searching for object creation of any class, including 3rd party classes that I don't control.
Using Structural Search (Edit -> Find -> Search Structurally):
Create a template: new $Type$($P$)
Edit Type variable: type Base in the text field, check 'Apply constraint within type hierarchy', check 'This variable is target of the search'
Edit P variable: type .* in the text field, set Minimum count to 0, check Unlimited under Maximum count.
Voila!
IttayD if I have understood correctly your latest update, what I normally do (IntelliJ 9.0.4) if I have a similar need to yours is Right click on class name and do "Find Usages" and this will list results in the form of usage categories,
Variable declaration
New Instance creation, to name a few.
As far as I'm aware I do not think there is a specific option/selection choice to fulfill such a usage search check. Thanks
You can create empty default constructor of Base and press Ctrl+Alt+H (Hierarchy Callers). Then you'll see all creations of A and B in as a tree.