package org.apache.velocity.tools;

/*
 * 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.
 */

import java.lang.reflect.Method;
import java.util.Map;
import org.apache.velocity.tools.ToolContext;

/**
 * Manages old tools which still use deprecated init() and configure()
 * methods.
 *
 * @author Nathan Bubna
 * @version $Id: OldToolInfo.java 511959 2007-02-26 19:24:39Z nbubna $
 */
public class OldToolInfo extends ToolInfo
{
    private static final long serialVersionUID = -4062162635847288761L;
    public static final String INIT_METHOD_NAME = "init";

    private Method init;

    /**
     * Creates a new instance using the minimum required info
     * necessary for a tool.
     */
    public OldToolInfo(String key, Class clazz)
    {
        super(key, clazz);
    }

    /**
     * 
     *
     * @param clazz the java.lang.Class of the tool
     */
    @Override
    public void setClass(Class clazz)
    {
        super.setClass(clazz);

        try
        {
            // try to get an init(Object) method
            this.init = clazz.getMethod("init", new Class[]{ Object.class });
        }
        catch (NoSuchMethodException nsme)
        {
            // ignore
        }
    }


    @Override
    protected void configure(Object tool, Map<String,Object> configuration)
    {
        // have specific setters and configure(Map) called first
        super.configure(tool, configuration);

        if (this.init != null)
        {
            // ctx should, in all cases where a tool has such a method,
            // actually be a View(Tool)Context, but we don't want to link
            // to that class here, so as not to pollute the generic jar
            Object ctx = configuration.get(ToolContext.CONTEXT_KEY);
            if (ctx != null)
            {
                invoke(this.init, tool, ctx);
            }
        }
    }

}

@@ -1316,16 +1316,26 @@
 private 
+transient 
 Method i
@@ -1543,177 +1543,86 @@
     
-/**%0A     * %0A     *%0A     * @param clazz the java.lang.Class of the tool
+protected Method getInit()
 %0A    
- */
+%7B
 %0A    
-@Override%0A    public void setClass(Class clazz)%0A    %7B%0A        super.setClass(clazz);%0A%0A
+    if (this.init == null)%0A        %7B%0A    
     
@@ -1629,34 +1629,41 @@
     try%0A        
-%7B%0A
+    %7B%0A   
             // t
@@ -1654,24 +1654,25 @@
             
+ 
 // try to ge
@@ -1696,16 +1696,20 @@
  method%0A
+    
         
@@ -1778,34 +1778,42 @@
 ass %7D);%0A        
+  
-%7D%0A
+  %7D%0A    
         catch (N
@@ -1839,34 +1839,42 @@
 n nsme)%0A        
+ 
-%7B%0A
+   %7B%0A    
             // i
@@ -1879,33 +1879,321 @@
  ignore%0A        
-%7D
+    %7D%0A        %7D%0A        return this.init;%0A    %7D%0A%0A    /**%0A     * %0A     *%0A     * @param clazz the java.lang.Class of the tool%0A     */%0A    @Override%0A    public void setClass(Class clazz)%0A    %7B%0A        super.setClass(clazz);%0A%0A        // clear any existing init method%0A        this.init = null;
 %0A    %7D%0A%0A%0A    @Ov
@@ -2397,33 +2397,61 @@
 );%0A%0A        
-if (this.
+Method init = getInit();%0A        if (
 init != null
@@ -2815,21 +2815,16 @@
  invoke(
-this.
 init, to

make init transient for proper serialization