首页 > 解决方案 > 在intellij模板中区分int和double

问题描述

我在 IntelliJ 中定义了一个自定义 toString 模板。IntelliJ 提供了一个基于 Velocity 脚本语言的模板系统。好东西 !

但我现在正面临一个特殊的问题。我需要区分int字段和double字段。本质上是生成类似(简化)的东西:

public String toString() {
  String output = "";
  output += "myIntField:" + Util.intDescription(myIntField);
  output += "myDblField:" + Util.doubleDescription(myDblField);
  return output;
}

对于Integer它工作正常的类型,但我似乎无法让它为 original 工作int

#if ($member.typeQualifiedName == "java.lang.Integer")
...
#end
#if ($member.typeName == "Integer")
...
#end

我可以使用类似$member.primitiveand的方法构建结构,$member.numeric但它们都没有区分doubleand int

IntelliJ的文档给我的印象是我陷入了死胡同。

PS:当然可以解决,通过改变java代码/api,可以简化toString()方法所需的格式。但这对我来说真的是最后的手段。

标签: intellij-ideavelocity

解决方案


#elseif ( $member.type == 'double' )
output += "myDoubleField:" + Util.doubleDescription(myDoubleField);
#elseif ( $member.type == 'int' )
output += "myIntField:" + Util.intDescription(myIntField);

无论出于何种原因,我都无法访问 $member.isDouble 属性。它没有列在IntelliJ 的文档VTL 参考VTL 用户指南中,.type 也没有。

我首先打印出 $member,它列出了这个对象的所有属性。我不确定为什么它列出了 isDouble。


推荐阅读