differentiate int and double in intellij template - intellij-idea

I am defining a custom toString template in IntelliJ. IntelliJ offers a template system based on the Velocity scripting language. Great stuff !
But I am facing a particular issue with it right now. I need to tell the difference between an int field and a double field. Essentially to generate something like (simplified):
public String toString() {
String output = "";
output += "myIntField:" + Util.intDescription(myIntField);
output += "myDblField:" + Util.doubleDescription(myDblField);
return output;
}
For the Integer type it works fine, but I can't seem to get it to work for primitive int.
#if ($member.typeQualifiedName == "java.lang.Integer")
...
#end
#if ($member.typeName == "Integer")
...
#end
I can make constructs with methods like $member.primitive and $member.numeric but none of them makes a distinction between double and int.
The documentation of IntelliJ gives me the impression that I'm at a dead end.
PS: Sure it could be solved, by changing the java code/api, which can simplify the required format for the toString() method. But that's really a last resort for me.

#elseif ( $member.type == 'double' )
output += "myDoubleField:" + Util.doubleDescription(myDoubleField);
#elseif ( $member.type == 'int' )
output += "myIntField:" + Util.intDescription(myIntField);
For whatever reason the $member.isDouble property isn't accessible for me. It's not listed in IntelliJ's docs, the VTL Reference or the VTL User Guide, nor is .type.
I first printed out $member and it listed all the properties on this object. I'm not sure why it listed isDouble though.

Related

PhpStorm typed variable alignment

I haven't found any good resources for this, so here's the question.
I have the following code:
public function myFunction(
string $name,
int $age
) {}
When I format the code, PhpStorm will align the variables like so:
public function myFunction(
string $name,
int $age
) {}
I want to STOP this behaviour and have it format the code like the first block. Running build #PS-213.7172.28 in case that matters.
If I'm getting this correctly: it is introduced by WI-59669 in PhpStorm 2021.2.3.
Here is a WI-61085 ticket that asks to have such double alignment (by both type and parameter name) optional -- watch it (star/vote/comment) to get notified with any progress.

wxDataViewListCtrl and wxVariant

I have a simple setup, a wxDataViewListCtrl, the first column uses wxDataViewCustomRenderer and the second column is just text.
class MyCustomRenderer : public wxDataViewCustomRenderer
I add a line to the wxDataViewListCtrl like this:
wxVector<wxVariant> item;
item.push_back(wxVariant(/*a raw pointer of MyClass goes here*/));
item.push_back(wxVariant("some string goes here"));
m_data_view_list_ctrl->AppendItem(item);
item.clear();
And this is MyClass:
class MyClass final : public wxObject
And this is how my SetValue method looks like:
bool MyCustomRenderer::SetValue(const wxVariant& value)
{
MyClass* temp = static_cast<MyClass*>(value.GetWxObjectPtr());
/*Do stuff with temp here...*/
return true;
}
It worked, now it does not. It fails with the following error:
https://www.dropbox.com/s/acxbzthp3ltadny/wxwidgets.png?dl=0
The only thing I changed is that I updated my static libs of wxWidgets from 3.0.4 to 3.1.2.
Why has it stopped working? What am I missing here?
Please help me :-)
Update
Thank you all for answering. The problem was solved here. In short I needed to change this line like this:
MyCustomRenderer::MyCustomRenderer() : wxDataViewCustomRenderer("void*", wxDATAVIEW_CELL_INERT, wxALIGN_CENTER)
And this one like this:
item.push_back(wxVariant(static_cast<void*>(/*Raw pointer to an instance of MyClass*/)));
I'm not sure which change exactly is responsible for this, but the value, returned by your model for the cell being drawn, is null, so your renderer can't just use it blindly and should check if ( !value.IsNull() ) before doing it (and maybe just return in this case or do whatever is appropriate to show the absence of a value in your case).

IntelliJ Velocity template $Character.isUpperCase return false on uppercase char

I'm trying to make a setter template which will allow me to use a m prefix for member variables. So when I have a field mTest is should give me a setter: public setTest and not setmTest. I think I have the correct logic, but Character.isUpperCase returns false even if it's a upper case letter. I've added some debugging a bit improvised, since it's kind of weird to test, because IntelliJ check if there is a proper function returned. When generating a setter I get an error dialog where I can see my output of:
#if($Character.isUpperCase($paramName.charAt(1)))
paramIsUppercase: $paramName.charAt(1)
#else
paramIsNotUppercase: $paramName.charAt(1)
#end
Complete code:
#set($paramName = $helper.getParamName($field, $project))
// debugging
#if($Character.isUpperCase($paramName.charAt(1)))
paramIsUppercase: $paramName.charAt(1)
#else
paramIsNotUppercase: $paramName.charAt(1)
#end
#if($StringUtil.startsWith($paramName, 'm') && $Character.isUpperCase($paramName.charAt(1)))
#set($paramName = $paramName.substring(1))
#end
#set($paramName = $StringUtil.decapitalize($paramName))
public ##
#if($field.modifierStatic)
static void ##
#else
$classname ##
#end
set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($paramName))($field.type $paramName) {
#if ($field.name == $paramName)
#if (!$field.modifierStatic)
this.##
#else
$classname.##
#end
#end
$field.name = $paramName;
#if(!$field.modifierStatic)
return this;
#end
}
When using this to create a setter for mTest I get an error for my debugging
paramIsNotUppercase: T
Why is this returning false and is there a fix for this?
It looks like the problem is that $Character is not defined, which means the expression will always return false. There is a way to get around this. It is a horrible hack, but it works for me. Use the following template lines.
## get some object
#set($String='')
## abuse it to obtain the desired jdk class
#set($Character=$String.class.forName('java.lang.Character'))
After that you can use $Character regularly as you desire (i.e. $Character.isUpperCase($paramName.charAt(1))).
However there is no need to create your own setter template if you want to use prefixes for fields. Just go to the settings File | Settings | Editor | Code Style | Java | Code Generation and specify a Name prefix for Field and getters and setters will be generated correctly.

What is this Objective C code doing

I am a developer in C-like languages (Java/JavaScript/C#) and I am attempting to convert some Objective-C code into Java.
For the most part, it is relatively straightforward but I have hit a stumbling block with the following bit of code:
typedef struct {
char *PAGE_AREA_ONE;
char *PAGE_AREA_TWO;
char *PAGE_AREA_THREE;
} CODES;
- (CODES*) getOpCode {
CODES *result = NULL;
result = malloc(sizeof(CODES));
result->PAGE_AREA_ONE = "\x1b\x1b\x1b";
result->PAGE_AREA_TWO = "\x2d\x2d\x2d";
result->PAGE_AREA_THREE = "\x40\x40";
return result;
}
What would the Java equivalent of this be? From what I can tell in other areas of the code, it is being used to store constants. But I am not 100% certain.
Thanks.
The typedef is just creating a structure that contains three string properties. The getOpCode method is apparently trying to create a new structure and assign values to those three properties. C# code would be:
public class Codes
{
public string PageAreaOne;
public string PageAreaTwo;
public string PageAreaThree;
}
public Codes GetCodes()
{
Codes result = new Codes();
result.PageAreaOne = "\x1b\x1b\x1b"; // three ESC characters
result.PageAreaTwo = "---";
result.PageAreaThree = "##";
return result;
}
The code in question is allocating a block of memory that the size of the CODES structure, filling it with some data, and returning a pointer to the new block. The data is apparently some operation codes (that is, instructions) for something, so perhaps the data is being sent to some other device where the instructions will be executed.

A program that will let the user input a string and then output each letter on a new line

import java.util.Scanner;
public class Program3_5
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
String input = new String();
System.out.println("Please enter a string: ");
input=scan.next();
int length;
length = input.length;
input.substring();
System.out.println(charAt(0));
while (length)
{
System.out.println(charAt(0 + 1));
}
}
}
I am getting an error stating that it "cannot find symbol - variable length"
I have tried numerous things yet I am having trouble getting it to work. New to Java! Thanks in advance.
For example if the user were to input: Hello There
The Output would print the letters on separate lines.
String#length() is a method, not a field, of String. You need to call the method. In Java, methods are called (or "invoked") using parentheses. So, change
length = input.length;
// to
length = input.length();
Anticipating the next compile error you see:
while (length)
won't compile in Java because length is an int, but the condition part of a while must be a boolean. I'm guessing you want to continue as long as the string is not empty, so change the while condition to be
while (length > 0)
Other problems you'll need to solve to get your code to compile:
String#substring() requires integer arguments
Also, the code will compile with the String input = new String(); but the assignment is completely unnecessary. In Java, you almost never need to new a string. Instead, use string literals.