Package de.odysseus.calyxo.base.conf.impl

Source Code of de.odysseus.calyxo.base.conf.impl.MethodConfigImpl

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed 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.
*/
package de.odysseus.calyxo.base.conf.impl;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.base.conf.MethodConfig;

/**
* Method configuration implementation.
*
* @author Christoph Beck
*/
public class MethodConfigImpl extends CallableConfigImpl implements MethodConfig {
  private String name;

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getElementName()
   */
  protected String _getElementName() {
    return "method";
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_putAttributes(de.odysseus.calyxo.base.conf.impl.ConfigImpl.Attributes)
   */
  protected void _putAttributes(Attributes map) {
    super._putAttributes(map);
    map.put("name", name);
  }
 
  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.MethodConfig#invoke(java.lang.Class, java.lang.Object)
   */
  public Object invoke(Class clazz, Object object, ClassLoader loader) throws ConfigException {
    if (clazz == null) {
      if (object == null) {
        throw new ConfigException("Cannot invoke method on null in " + toInlineString());
      }
      clazz = object.getClass();
    } else if (object != null && !clazz.isInstance(object)) {
      throw new ConfigException("Specified object is not an instance of " + clazz.getName() + " in " + toInlineString());
    }
    Object[] rawArgs = evalArgs(loader);
    Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
      if (methods[i].getName().equals(name)) {
        if (object == null && !Modifier.isStatic(methods[i].getModifiers())) {
          continue;
        }
        Class[] types = methods[i].getParameterTypes();
        Object[] coercedArgs = coerce(rawArgs, types);
        if (coercedArgs != null) {
          try {
            return methods[i].invoke(object, coercedArgs);
          } catch (Exception e) {
            throw new ConfigException(
              "Could not invoke method " + name + " in " + toInlineString(), e);
          }
        }
      }
    }
    throw new ConfigException(
      "Could not find method " + name + " in " + toInlineString());
  }

  /**
   * Get name property
   */
  public String getName() {
    return name;
  }

  /**
   * Set name property
   */
  public void setName(String name) {
    this.name = name;
  }
}
TOP

Related Classes of de.odysseus.calyxo.base.conf.impl.MethodConfigImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.