Intellij live template to convert variable to constant - intellij-idea

I'm trying to make a live template in intellij to convert the following examples into constants, given that I select the input line.
Examples Inputs:
String houseName = "The big house"
int houseNumber = 1
Intended results: (and ideally at the top of the class)
public static final String HOUSE_NAME = "The big house"
public static final int HOUSE_NUMBER = 1
Any ideas?

You are probably looking for the 'Extract Constant' refactoring dialog (Ctrl+Alt+C while on variable declaration).
It gives you options to rename your variable and moves the declaration to the top of the class automatically.

Related

How to set intellij so it forces the static final variable in the format of VAR_NAME

For the following code, how to get warning in intellij ?
private static final int varInt = 0;
What i want to see is
private static final int VAR_INT = 0;
Thanks !
Editor
Inspections
Java
Naming Conventions
Field naming conventions
From the right menu you may decide pattern, min and max length. After that, the naming which doesn't match the convention will be show as "warning"(You may edit it too on the severity tab)
After the changes you may cmd + enter to select from available names.

Spring Shell 2: Refactoring-safe Dynamic Command Availability

I'm just trying out Spring Shell 2. The section Dynamic Command Availability of the reference documentation shows three ways to indicate availability. However, all of them rely on a naming scheme or string parameter in an annotation. This will break (at runtime) if one uses the refactor functionality of an IDE. So, is there a possibility to use the Dynamic Command Availability feature in a refactoring-safe way?
Update 1:
Considering the answer below, I think this snippet demonstrates the solution:
#ShellComponent
public class MyCommands {
private final static String ADD_NAME = "add";
#ShellMethod(key=ADD_NAME, value = "Add two integers together.")
public int addTwoInts(int a, int b) {
return a+b;
}
#ShellMethodAvailability(ADD_NAME)
public Availability checkAddAvailability() {
return Availability.available();
}
}
Note that the string parameter in the annotation is the command name, so if you specify it both on the availability method and on the command method, this will survive refactoring.
Bonus points if you extract the command name in a constant.

Creating F# discrimated union type in C#

I'm trying to add F# project to my C# solution. I created a F# project and wrote some F# code, now I'm trying to use it from my C# projects.
I successfully referenced F# project and can access it's types, but having issues with discriminated unions. For example I have following types defined in F#:
namespace Sample
type NotificationReceiverUser = NotificationReceiverUser of string
type NotificationReceiverGroup = NotificationReceiverGroup of string
type NotificationReceiver = NotificationReceiverUser | NotificatonReceiverGroup
I can create NotificationReceiverUser object directly as follows:
var receiver = NotificationReceiverUser.NewNotificationReceiverUser("abc");
,but I need NotificationReceiver object, and I'm not getting NotificationReceiver.NewNotificationReceiverUser or NotificationReceiver.NewNotificationReceiverGroup static methods. Looking at some other SO questions it looks like these methods should be available by default. Would appreciate any pointers on why they are missing for me.
What you're trying to do is not a "discriminated union". It's an indiscrimnated union. First you created two types, and then you're trying to say: "values of this third type may be either this or that". Some languages have indiscriminated unions (e.g. TypeScript), but F# does not.
In F#, you can't just say "either this or that, go figure it out". You need to give each case of the union a "tag". Something by which to recognize it. That's why it's called a "discriminated" union - because you can discriminate between the cases.
For example:
type T = A of string | B of int
Values of type T may be either string or int, and the way to know which one is to look at the "tags" assigned to them - A or B respectively.
The following, on the other hand, is illegal in F#:
type T = string | int
Coming back to your code, in order to "fix" it the mechanical way, all you need to do is add case discriminators:
type NotificationReceiverUser = NotificationReceiverUser of string
type NotificationReceiverGroup = NotificationReceiverGroup of string
type NotificationReceiver = A of NotificationReceiverUser | B of NotificatonReceiverGroup
But my intuition tells me that what you actually meant to do was this:
type NotificationReceiver =
| NotificationReceiverUser of string
| NotificatonReceiverGroup of string
Two cases of the same type (weird, but legal), still distinguished by tags.
With such definition, you would access it from C# thusly:
var receiver = NotificationReceiver.NewNotificationReceiverUser("abc");

Auto generate random long in IntelliJ Live Template

How do I auto generate an arbitrary number (Long) for use in a Live Template in IntelliJ?
Example:
public static final long uid = $randomLong$;
where randomLong is replaced with a random long value. I have tried adding the following as an expression for the variable on the live template definition but nothing is generated when the template outputs.
new Random().nextLong()
What I am trying to achieve is very similar to what the IDEA code inspector generates for the Serialization version UID field but with a live template.
Please try adding groovyScript("new Random().nextLong()") as the variable expression instead.

How to convert string into integer in the Velocity template?

I have a Velocity template file which has the data from XML. I want to convert the string into integer type.
How can I do that?
Aha! Been there.
#set($intString = "9")
#set($Integer = 0)
$Integer.parseInt($intString)
Doing this uses the java underlying velocity. The $Integer variable is nothing more that a java Integer object which you can then use to access .parseInt
Edit: The above code is for demonstration. Of course there are ways to optimize it.
If you have some control over the velocity context, here's an alternative that alleviates the need to set a variable in the Velocity template.
Context velocityContext = new Context();
velocityContext.put(Integer.class.getSimpleName(), Integer.class);
This allows you to call the static methods of java.lang.Integer in your template using $Integer.parseInt($value) and doesn't rely upon the #set having been called prior to performing the type conversion in the template.
The problem with parseInt is that it throws an exception in case the string is not parseable.
In case you have the NumberTool loaded into your context a better solution than parseInt is the following:
#set($intString = "009")
#set($Integer=$numberTool.toNumber($intString).intValue())
#if($Integer)
## ok
#else
## nok
#end
Sometimes the NumberTool is also loaded as $number.
However, a little drawback is, that the NumberTool simply parses the first number it finds and ignores the rest, so "123a" => 123.
Nice and easy:
#set( $stringToCast = "0" )
$number.toNumber($stringToCast)
$number is the default key name for the NumberTool, but it can be override by specifying a different name in the configuration (for example $numberTool). You have to check what name for NumberTool is used in your Velocity environment.
toNumber method returns:
the object as a Number or null if no conversion is possible
If you want to have explicite an int variable, not a Number object, you can use the intValue method on the result. So the above code will looks like this:
#set( $stringToCast = "0" )
$number.toNumber($stringToCast).intValue()
Of course, you can assign the result to another variable (for example $intVal).
So the full code can look like this:
#set( $stringToCast = "0" )
#set( $intVal = $number.toNumber($stringToCast).intValue() )