I'm trying to use IntelliJ Structural Search and Replace to overload a suite of setter methods with Optional
variants. This needs to honour that the target value may be primitive (e.g. boolean
to Optional<Boolean>
). Here's the core part of the conversion:
Search:
public static void set($BType$ $BName$, $VType$ $VName$) {
$body$; // 0 - infinity
}
Replacement:
public static void set($BType$ $BName$, java.util.Optional<$VTypeMightBeBoxed$> $VName$) {
$VName$.ifPresent(v -> set($BName$, v));
}
public static void set($BType$ $BName$, $VType$ $VName$) {
$body$;
}
I'm a bit stuck on the PSI Groovy script required to generate $vTypeMightBeBoxed
PS. This is mostly caused by the total lack of editor-assistance when adding these scripts! Not what is expected from Jetbrains!
A script for $VTypeMightBeBoxed$
like this should work:
import com.intellij.psi.*
def x = VType.getType()
if (x instanceof PsiPrimitiveType) {
return x.getBoxedTypeName();
} else {
return VType.getText();
}
But watch out, your replacement will generate red code when there are multiple of these set methods with the same name in a class: the methods will have the same erasure.