I'd like to inspect and transform a specific statement into a try-with-ressources form. But I have the feeling the syntax of the declarative hints format eludes me for this.
I tried:
<!description="Stmt into try-with-resources">
try {
$before$;
someMethod($arg1, $arg2, $arg3);
$after$;
} catch $catches$
=>
try (Resource res = acquire($arg1, $arg2, $arg3)) {
$before$;
res.use();
$after$;
} catch $catches$
But applied on my code the pattern never matches. Here is some example code section which I expected to match:
public boolean step(String input) {
String id = getId(ID);
try {
SomethingBefore();
someMethod(10, "label", name);
return true;
} catch (Exception ex) {
log.error("problem", ex);
return true;
}
}
Any idea why this does not match? Esp. because I think the example from the documentation matches mine, except for the finally
:
try {
$statements$;
} catch $catches$
finally {
$finally$;
}
Update: It seems that the Jackpot-Rules use the same syntax, probably because it uses the same code-base.
This refactoring is very cumbersome and poorly documented. You must change your pattern according to this example
<!description="Stmt into try-with-resources">
try {
$before$;
$name($arg1, $arg2, $arg3);
$after$;
} catch $catches$
=>
try (Resource res = acquire($arg1, $arg2, $arg3)) {
$before$;
res.use();
$after$;
} catch $catches$
However, I don't know how to handle this if you have other methods calling 3 arguments as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With