I want to include one template nested into others cont1, cont2, cont3.
And nested template should be hide one specific control for cont1 only.
Before inclusion into cont1 I would like to assign value to some flag variable $hideMyControl.
And inside nested template I would like to check if $hideMyControl is assigned value.
How to perform such check?
#if($hideMyControl)
// your code
#end
If $hideMyControl is defined, your code will execute
You can do this using
#if($!{$articleLeader})
// Perform your operation or the template part you want to show.
#end
For more info, see the 'formal reference' section of the Apache Velocity Reference Manual.
#if($!{hideMyControl} != "")
## do something if $hideMyControl is defined
#end
This works for me in AWS API Gateway Body Mapping Templates. Please refer to Quiet Reference Notation in Velocity User Guide for more information.
I was using
#if ($hideMyControl)
//do something
#end
since a few months ago,
however today its not working anymore.
I came here to find help, and noticed a new way of writing it :
#if($!{$hideMyControl})
// do something
#end
this code works!
According to the docs for Strict Reference Mode it is possible to several constructions to check if variable is defined.
#if ($foo)#end ## False
#if ( ! $foo)#end ## True
#if ($foo && $foo.bar)#end ## False and $foo.bar will not be evaluated
#if ($foo && $foo == "bar")#end ## False and $foo == "bar" wil not be evaluated
#if ($foo1 || $foo2)#end ## False $foo1 and $foo2 are not defined
So this code works in my case.
#if( !$value )
// Perform your operation or the template part you want to show.
#end
To check if $hideMyControl is in Velocity context and IS NOT boolean 'true' value (or 'false' as well):
#if ($hideMyControl && $hideMyControl != true)
##do stuff
#end
Sure, if you really use your $hideMyControl variable as boolean type, you don't need second part of condition.
Related
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.
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.
I have defined a macro value in Constant.h (#define OK "OK")
And I imported in a First.m file and redefined it (#undef OK, #define OK "Hi")
Then I include Constant.h in Second.m and when I access the "OK" the value is still "OK" not "Hi"
I noticed that the value only changed in First.m.
Just wondering how to change the OK value globally.
Since many .m file are acessing the OK and OK needs to be changed often according to different event
Thanks
#define OK(str) ISVALIDBOOL(str) ? #"HI" : #"OK"
#define ISVALIDBOOL(str) (str == NO) // Import in above header
BOOL str=YES;
NSLog(#"Hi:%#",OK(str));
str=NO;
NSLog(#"Ok:%#",OK(str));
No other way to change the macro at runtime
Refer that
You'll need to turn the OK macro from a simple string definition into some conditional statement that tests this special event you talk about. You can only change a macro in the implementation file being compiled; changes are not seen in other compilation units. So the change must be made to the macro in the header file itself.
For example, if the two strings are based on the success of an operation you could do:
#define OK(condition) ((condition) ? #"OK" : #"Failed")
and use it like this:
BOOL success = [self doThing];
NSLog(#"doThing %#", OK(success));
I often define a similar macro to turn BOOLs into NSStrings:
#define STRBOOL(b) ((b) ? #"YES" : #"NO"))
Is there a way to get IntelliJ to be smarter about the getters/setters it generates? In particular, if a field name has underscores in it, can the getter strip them out and convert to CamelCase?
For instance, I would expect the below getter to be getCarneAsada():
public class Taco {
private String carne_asada;
public String getCarne_asada() {
return carne_asada;
}
}
I found the Code Style->Java->Code Generation area, but none of the options seem appropriate...it's not a prefix or a suffix I want to exclude. It's inner underscores.
You can use Live Templates to accomplish this. It will not be accessible from Code Generation but it will still be quite easy to use.
I have created a Main class with the property carne_asada:
Now open up Live Templates in the settings and create a new template called getu under other:
Press the little link called Define at the bottom to define when this template is valid. Then choose Java | Declaration:
Press the button Edit Variables to open a window where each of the live template variables can be defined. Now create the following variables:
VAR : suggestFirstVariableName("Object")
TYPE : typeOfVariable(VAR)
CAP_CAM_VAR : capitalize(underscoresToCamelCase(VAR))
The order is quite important so the VAR must come first. In my example CAP_CAM_VAR stands for Capitalize and CamelCase the VAR variable. Set the Skip if defined according to the image:
Now press OK and OK again to get back to the editor.
Try the new getu live template by typing getu and then press Tab:
Press Enter and the getter generation has finished:
Now if you had had some more variables with underscores you will get a list to choose from so here is an example:
And the result is just beautiful:
You can easily create the same setu live template and maybe some sgetu that creates them both at the same time.
Hope this helps a little bit!
Here are templates for generating getter/setter working under IntelliJ IDEA 2016 and Android Studio 2.2.
Camelized Getter:
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
#if ($StringUtil.startsWithIgnoreCase($name, 'is'))
#set($name = $StringUtil.decapitalize($name))
#else
is##
#end
#else
get##
#end
#set($words = $StringUtil.split($name, "_"))
#set($name = "")
#foreach($word in $words)
#set($name = $name + $StringUtil.capitalize($word))
#end
${name}() {
return $field.name;
}
Camelized Setter:
#set($paramName = $helper.getParamName($field, $project))
#if($field.modifierStatic)
static ##
#end
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#set($words = $StringUtil.split($name, "_"))
#set($name = "")
#foreach($word in $words)
#set($name = $name + $StringUtil.capitalize($word))
#end
void set$name($field.type $StringUtil.decapitalize($name)) {
#if ($field.name == $paramName)
#if (!$field.modifierStatic)
this.##
#else
$classname.##
#end
#end
$field.name = $paramName;
}
If you want fluent getter/setter with above, please refer my gists.
https://gist.github.com/k24/72d0be3d76d4eb987c6d4eb1d8f42db2
I have NSZombieEnabled to NO in my arguments.
I am checking to see if it is enabled:
if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled"))
{
NSLog(#"NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled enabled!");
}
My debugger says it is still enabled. Why?
try to uncheck it. If it has no checkmark in front it shouldn't be passed to the application.
It should be off when you set the value to NO, but getenv("NSZombieEnabled") will return "NO". Which is not a boolean NO but a cstring "NO". So the if condition will be true anyway.
I know this question is old, but for people’s reference, you can use this technique for many debug flags:
extern BOOL NSZombieEnabled;
if (NSZombieEnabled)
...
If it links, it will work.
Here's a suggestion which checks for both the existence of the env variable and the correct value.
char* szZombie = getenv("NSZombieEnabled");
if (szZombie && 0 == strcasecmp(szZombie, "YES"))
{
NSLog(#"NSZombieEnabled enabled!");
}