Search code examples
javatemplatesvelocity

Unexpected error in Apache Velocity template file


I have an xml containing the description of the classes I want to generate with velocity, in this form:

<Class name="name">
  <Attribute name="name" type="typeName"/>
  ...
  <Method name="name" type="typeName"/>
</Class>

the template's code is:

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's velocity text editor tells me that there is an error with this message: "Encountered ");\r\n\t\t}\r\n\t\t\r\n\t\t@Override\r\n\t\tpublic " at Class.vm[line 23, column 86] Was expecting one of: "[" ... "," ... ")" ... ..."

What does this mean?


Solution

  • It's a case where you need to use the formal reference notation:

          this.$att.Name = new ${att.Type}(Constants.SERVER_PORT, classNameHandler.class);
    

    otherwise the Velocity parser will think that you are trying to call a method on the $att.Type object.