Java 8 Lambdas in Structural Search
I am having some issues using Structural Search in IntelliJ 13.1. I am attempting to refactor some code which makes use of lambdas but I can't seem to get the pattern to recognize the lambda unless I treat it as a single opaque argument. However, I need to refactor both the method called which accepts the lambda as well as the body of the lambda itself.
Here is an example of the code I am trying to refactor:
getAuthenticatedContext(response)
.then(context -> {
// could be any number of statements here
service.doSomething(context, arg1, arg2, ...)
.then(response::resume)
.fail(response::resume);
})
.fail(response::resume);
I am trying to refactor it to the following:
withAuthenticatedContext(response, context -> {
// could be any number of statements here
return service.doSomething(context, arg1, arg2, ...);
});
The pattern that I have tried so far is as follows:
getAuthenticatedContext($response$) .then($context$ -> { $Statements$; $service$.$method$($args$) .then($response$::resume) .fail($response$::resume); }) .fail($response$::resume);
Where $Statements$ and $args$ are both set to 0...unlimited instances.
I've tried simplified patterns as well just to try to match on the lambda in any form but the only variation that seems to find anything is as follows:
getAuthenticatedContext($response$) .then($Lambda$) .fail($response$::resume);
I assume that I'm formatting the lambda capture incorrectly and that the syntax must be more explicit than Java 8 permits? I know that this tripped me up with the diamond operator. Even at it's most explicit I can't manage a match:
getAuthenticatedContext($response$)
.then(($CallbackType$<$Type$>) ($Type $context$) -> {
$Statements$;
})
Does anyone know what syntax IntelliJ expects for structural search/replace with lambdas?
Thanks
Please sign in to leave a comment.
You are using IntelliJ IDEA 13.1? Lambda matching is only available in IntelliJ IDEA 14, I'm afraid.
Bas
Was this feature removed from the released product? It was explicitly mentioned as a feature available in the IDEA 13.1 EAP 134.1160 build.
If this is the case that is a shame. I am currently waiting on corporate to update our licenses to IntelliJ 14 but you know how that can be, but it would be worth it due to all of the other bugs 13.1 has with lambdas, particularly the false positive inspections.
I did finally work through my problem by tackling it in stages, replacing the bits within the lambda first and then replacing the method call around the lambda and treating the lambda as an opaque argument. The Structural Search/Replace feature is quite awesome but also very finicky and the online documentation and tutorials do leave something to be desired. It is kind of silly that you can't even paste a real code snippet from your project and expect for it to be found.
Sorry, you are right, I am wrong, the functionality for finding lambda's is/should be included in IntelliJ IDEA 13.1. Unfortunately, you seem to have hit a bug. I will try and see if the problem is reproducable on a recent build also. (Later: I have tried a code snippet similar to yours and had no problems finding and replacing using a recent IntelliJ 14.1 EAP build)
Additionally a syntactically valid code snippet copy-pasted from your project should definitely be found by Structural Search. so if you have any other concrete examples of code not found, please submit a bug or post a comment.
Bas
By the way: Structural Search is included in the Community Edition since version 14, so you can try it out if you want.