Package anvil.core.reflect

Source Code of anvil.core.reflect.MethodParameterNode

/*
* $Id: MethodParameterNode.java,v 1.3 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.reflect;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Member;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.math.BigDecimal;
import anvil.core.Any;
import anvil.core.ObjectPool;
import anvil.core.AnyBinary;
import anvil.codec.Code;
import anvil.codec.ConstantPool;
import anvil.script.Context;

public class MethodParameterNode extends ParameterNode implements ParameterTypes
{

  private Method _method = null;
  private boolean _isstatic = false;


  public MethodParameterNode()
  {
    super();
  }
 

  public MethodParameterNode(Class type)
  {
    super(type);
  }


  public ParameterNode createNode(Class type)
  {
    return new MethodParameterNode(type);
  }


  public Member getMember()
  {
    return _method;
  }

  public void insert(Member member, Class[] paramtypes, int index)
  {
    int length = paramtypes.length;
    if (index >= length) {
      if (_method == null) {
        _isstatic = Modifier.isStatic(member.getModifiers());
        _method = (Method)member;
      }
      return;
    }
    super.insert(member, paramtypes, index);
  }

 
  public Any invoke(Context context, Object instance, Object target[], Any[] parameters, int index)
  {
    int length = parameters.length;

    if (_type != null) {
      if (index < length) {
        target[index] = super.extend(parameters[index]);
        index++;
      }
    }
   
    if (index == length) {
      if (instance == null && !_isstatic) {
        throw context.NoInstance(_method.toString());
      }
      try {
        return Any.create(_method.invoke(instance, target));
      } catch (InvocationTargetException e) {
        throw context.exception(e.getTargetException());
      } catch (Throwable t) {
        throw context.exception(t);
      }
    }
       
    Any param = parameters[index];
   
    ParameterNode[] childs = _childs;
    int n = childs.length;
    if (n>1) {
      ParameterNode best = null;
      ParameterNode child;
      int bestlevel = 0;
      int level;
      for(int i=0; i<n; i++) {
        child = childs[i];
        level = child.matching(param);
        if (level > bestlevel) {
          bestlevel = level;
          best = child;
          if (level == BEST) {
            break;
          }
        }
      }
      if (best != null) {
        return best.invoke(context, instance, target, parameters, index);
      }

    } else if (n == 1) {
      return childs[0].invoke(context, instance, target, parameters, index);
     
    }

    throw context.TypeError("Couldn't create parameter match for '"+_method+"'");
  }


}
TOP

Related Classes of anvil.core.reflect.MethodParameterNode

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.