##
## The Velocity code generation template file containing various method macro utilities
##
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements.  See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License.  You may obtain a copy of the License at
## 
##     http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
## $Header:$
##
## The following context variables are used by this template:
##      $bean - a ControlBean instance that defines the attributes of the bean
##      $intf - a ControlInterface instance that defines the attributes of the public interface
##
## The actual class template apears at the end of this file, and is preceded by a number of
## supporting macros that define elements of the template.
##
## SUPPORTING MACROS
##
## A simple helper macro that converts a primitive type to the equivalent object
##
#macro (toObject $type)#if ($type == "int")Integer#elseif ($type == "long")Long#elseif ($type == "boolean")Boolean#elseif ($type == "byte")Byte#elseif ($type == "short")Short#elseif ($type == "char")Character#elseif ($type == "float")Float#elseif ($type == "double")Double#else${type}#end#end
##
## A simple helper macro that converts a object type to the equivalent primitive
##
## This macro provides the template for declaring the static final Method fields that
## are associated with all declared operations
##
#macro (declareMethodStatics)
    #foreach ($operation in $intf.operations)
        static final Method $operation.methodField;
    #end
    #foreach ($eventSet in $intf.eventSets)
        #foreach ($event in $eventSet.events)
            static final Method $event.methodField;
        #end
    #end

    //
    // This HashMap will map from a Method to the array of names for parameters of the
    // method.  This is necessary because parameter name data isn't carried along in the
    // class file, but if available can enable ease of use by referencing parameters by
    // the declared name (vs. by index).
    //
    // This map should be read-only after its initialization in the static block, hence
    // using a plain HashMap is thread-safe.
    //
    static HashMap<Method, String []> _methodParamMap = new HashMap<Method, String []>();
#end
##
## This macros provides the template for initializing the static final Method fields that
## are associated with declared operations
##
#macro (initMethodStatics)

  ## First verify that if an eventSet was defined it is not empty.
  #set($eCount=0)
  #foreach($eventSet in $intf.eventSets)
      #foreach($event in $eventSet.events)
          #set($eCount=$eCount+1)
      #end
  #end

  #if ($intf.operations.size() > 0 || $eCount > 0)
    try
    {
        #foreach ($operation in $intf.operations)
            $operation.methodField = ${intf.className}.class.getMethod("${operation.name}", new Class [] {$operation.argTypes});
            _methodParamMap.put($operation.methodField, new String [] { $operation.getArgList(true) });
        #end
        #foreach ($eventSet in $intf.eventSets)
            #foreach ($event in $eventSet.events)
                $event.methodField = ${eventSet.className}.class.getMethod("${event.name}", new Class [] {$event.argTypes});
                _methodParamMap.put($event.methodField, new String [] { $event.getArgList(true) });
            #end
        #end
    }
    catch (NoSuchMethodException __bc_nsme)
    {
        throw new ExceptionInInitializerError(__bc_nsme);
    }
  #end
#end
