Apache Velocity 模板文件中出现意外错误
Unexpected error in Apache Velocity template file
我有一个 xml 包含我想用速度生成的 类 的描述,形式如下:
<Class name="name">
<Attribute name="name" type="typeName"/>
...
<Method name="name" type="typeName"/>
</Class>
模板代码为:
public class $class.Name implements NodeIface {
#foreach($att in $class.Attributes)
private $att.Type $att.name;
public $att.Type get$utility.firstToUpperCase($att.Name)() {
return this.$att.Name;
}
public void set$utility.firstToUpperCase($att.Name)($att.Type $att.Name) {
this.$att.Name = $att.Name;
}
#end
#foreach($mtd in $class.Methods)
if($class.Name == "name") {
public $mtd.Name() {
this.$att.Name = new $att.Type(Constants.SERVER_PORT, classNameHandler.class); //here lies the error
}
@Override
public $mtd.Type $mtd.Name() throws TException {
TSocket $att.Name = new TSocket("localhost", Constants.SERVER_PORT);
TBinaryProtocol $att.Name = new TBinaryProtocol($att.Name);
managementClient = new ManagementService.Client($att.Name);
this.setDispatcher(new OperationDispatcher($att.Name));
arithmeticServerTransport.open();
$att.Name.start();
$att.Name = new $att.Type($att.Name);
PortNegotiator negotiator = new PortNegotiator($att.Name);
negotiator.negotiate($att.Type, $att.Name);
}
@Override
public $mtd.Type $mtd.Name() {
$att.Name.stop();
}
}
else if ($class.Name == "GreetingsNode") {
public $mtd.Name();
this.$att.Name = new $att.Type((Constants.SERVER_PORT, GreetingsServiceHandler.class));
}
#Override
public $mtd.Type $mtd.Name() throws TException {
TSocket $att.Name = new TSocekt("localhost", Constants.SERVER_PORT);
TBinaryProtocol $att.Name = new TBinaryProtocol($att.Name);
$att.Name = new ManagementService.Client($att.Name);
this.SetUser(new User($att.Name);
this.setMessenger(new MessageDispatcher($att.Name));
$att.Name.open()
$att.Name = new $att.Type($att.Name);
PortNegotiator negotiator = new PortNegotiator($att.Name);
negotiator.negotiate($att.Type, $att.Name);
}
@Override
public $mtd.Type $mtd.Name() {
$att.Name.stop();
}
#End
Eclipse 的速度文本编辑器告诉我此消息有错误:"Encountered ");\r\n\t\t}\r\n\t\t\r\n\t\t@Override\r\n\t\t public“在Class.vm[第 23 行,第 86 列]
期待其中之一:
“[” ...
”、“……
“)” ...
..."
这是什么意思?
在这种情况下,您需要使用 formal reference notation:
this.$att.Name = new ${att.Type}(Constants.SERVER_PORT, classNameHandler.class);
否则 Velocity 解析器会认为您正在尝试调用 $att.Type
对象上的方法。
我有一个 xml 包含我想用速度生成的 类 的描述,形式如下:
<Class name="name">
<Attribute name="name" type="typeName"/>
...
<Method name="name" type="typeName"/>
</Class>
模板代码为:
public class $class.Name implements NodeIface {
#foreach($att in $class.Attributes)
private $att.Type $att.name;
public $att.Type get$utility.firstToUpperCase($att.Name)() {
return this.$att.Name;
}
public void set$utility.firstToUpperCase($att.Name)($att.Type $att.Name) {
this.$att.Name = $att.Name;
}
#end
#foreach($mtd in $class.Methods)
if($class.Name == "name") {
public $mtd.Name() {
this.$att.Name = new $att.Type(Constants.SERVER_PORT, classNameHandler.class); //here lies the error
}
@Override
public $mtd.Type $mtd.Name() throws TException {
TSocket $att.Name = new TSocket("localhost", Constants.SERVER_PORT);
TBinaryProtocol $att.Name = new TBinaryProtocol($att.Name);
managementClient = new ManagementService.Client($att.Name);
this.setDispatcher(new OperationDispatcher($att.Name));
arithmeticServerTransport.open();
$att.Name.start();
$att.Name = new $att.Type($att.Name);
PortNegotiator negotiator = new PortNegotiator($att.Name);
negotiator.negotiate($att.Type, $att.Name);
}
@Override
public $mtd.Type $mtd.Name() {
$att.Name.stop();
}
}
else if ($class.Name == "GreetingsNode") {
public $mtd.Name();
this.$att.Name = new $att.Type((Constants.SERVER_PORT, GreetingsServiceHandler.class));
}
#Override
public $mtd.Type $mtd.Name() throws TException {
TSocket $att.Name = new TSocekt("localhost", Constants.SERVER_PORT);
TBinaryProtocol $att.Name = new TBinaryProtocol($att.Name);
$att.Name = new ManagementService.Client($att.Name);
this.SetUser(new User($att.Name);
this.setMessenger(new MessageDispatcher($att.Name));
$att.Name.open()
$att.Name = new $att.Type($att.Name);
PortNegotiator negotiator = new PortNegotiator($att.Name);
negotiator.negotiate($att.Type, $att.Name);
}
@Override
public $mtd.Type $mtd.Name() {
$att.Name.stop();
}
#End
Eclipse 的速度文本编辑器告诉我此消息有错误:"Encountered ");\r\n\t\t}\r\n\t\t\r\n\t\t@Override\r\n\t\t public“在Class.vm[第 23 行,第 86 列] 期待其中之一: “[” ... ”、“…… “)” ... ..."
这是什么意思?
在这种情况下,您需要使用 formal reference notation:
this.$att.Name = new ${att.Type}(Constants.SERVER_PORT, classNameHandler.class);
否则 Velocity 解析器会认为您正在尝试调用 $att.Type
对象上的方法。