If STR ==null else setText? I think it is well written. Thinking more and doing more in the development process will bring unexpected results. In the comments below, everyone also said their own methods and opinions. The author later solved some problems in the article.
Return xx==null?” “:xx; I think this is a very simple method, but I don’t know how to configure it, so I created a new bean object in my as tool, held Alt + INSERT and added the get/set method, and found the solution. The diagram below:
IntelliJ Default is the Default generated template. Click on the three buttons at the back to have a look at the following:
This is set, the get method is similar, probably can understand, think about whether can be directly template, found that cannot be modified, and later found + number in the upper left corner, is you can create a new template, and then choose when generating your own template, such as we are get in the template to create a new template:
We know we just need to change the code at the end where the get method returns, which is here:
I’m not sure, but let’s try it with a # after it:
${name}() {
return $field.name+"#";
}
Copy the code
Select our custom template at build time:
Discover the resulting code:
public String getAge() {
return age + "#";
}
public void setAge(String age) {
this.age = age;
}
Copy the code
If this proves to be the case, then we can proceed to the real judgment: let’s copy the default get method code into MyGetter. Let’s modify the template code to suit our needs, but keep everything else unchanged: We want to implement the following: return xx==null?” “:xx,Ok, so I think it looks like this. Note that we only judge a String field.
${name}() {
return $field.name == null ? "" : $field.name;
}
${name}() {
#if($field.String)
return $field.name == null ? "" : $field.name;
#else
return $field.name;
#end
}
Copy the code
Let’s compare a way to write default:
${name}() {
return $field.name;
}
Copy the code
Ok, let’s go ahead and generate:
public String getAge() {
return age == null ? "" : age;
}
public void setAge(String age) {
this.age = age;
}
Copy the code
Good, achieve the effect, yeah, incredibly can also play so, can modify according to their own needs, thank you.