All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.bonita.util.ReflectUtilDescriptor Maven / Gradle / Ivy

package org.ow2.bonita.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;

import org.ow2.bonita.pvm.internal.wire.descriptor.ArgDescriptor;

public abstract class ReflectUtilDescriptor {

  private static Log log = Log.getLog(ReflectUtilDescriptor.class.getName());

  public static Method findMethod(Class clazz, String methodName,
      List argDescriptors, Object[] args) {
    log.trace("searching for method " + methodName + " in " + clazz.getName());
    Method[] candidates = clazz.getDeclaredMethods();
    for (int i = 0; i < candidates.length; i++) {
      Method candidate = candidates[i];
      if ((candidate.getName().equals(methodName))
          && (isArgumentMatch(candidate.getParameterTypes(), argDescriptors,
              args))) {

        if (log.isTraceEnabled()) {
          log.trace("found matching method " + clazz.getName() + "."
              + methodName);
        }

        return candidate;
      }
    }
    if (clazz.getSuperclass() != null) {
      return findMethod(clazz.getSuperclass(), methodName, argDescriptors, args);
    }
    return null;
  }

  public static Constructor findConstructor(Class clazz,
      List argDescriptors, Object[] args) {
    Constructor[] constructors = clazz.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++) {
      if (isArgumentMatch(constructors[i].getParameterTypes(), argDescriptors,
          args)) {
        return constructors[i];
      }
    }
    return null;
  }

  public static boolean isArgumentMatch(Class[] parameterTypes,
      List argDescriptors, Object[] args) {
    int nbrOfArgs = 0;
    if (args != null)
      nbrOfArgs = args.length;

    int nbrOfParameterTypes = 0;
    if (parameterTypes != null)
      nbrOfParameterTypes = parameterTypes.length;

    if ((nbrOfArgs == 0) && (nbrOfParameterTypes == 0)) {
      return true;
    }

    if (nbrOfArgs != nbrOfParameterTypes) {
      return false;
    }

    for (int i = 0; (i < parameterTypes.length); i++) {
      Class parameterType = parameterTypes[i];
      String argTypeName = (argDescriptors != null ? argDescriptors.get(i)
          .getTypeName() : null);
      if (argTypeName != null) {
        if (!argTypeName.equals(parameterType.getName())) {
          return false;
        }
      } else if ((args[i] != null)
          && (!parameterType.isAssignableFrom(args[i].getClass()))) {
        return false;
      }
    }
    return true;
  }

  public static String getSignature(String methodName,
      List argDescriptors, Object[] args) {
    String signature = methodName + "(";
    if (args != null) {
      for (int i = 0; i < args.length; i++) {
        String argType = null;
        if (argDescriptors != null) {
          ArgDescriptor argDescriptor = argDescriptors.get(i);
          if ((argDescriptor != null) && (argDescriptor.getTypeName() != null)) {
            argType = argDescriptor.getTypeName();
          }
        }
        if ((argType == null) && (args[i] != null)) {
          argType = args[i].getClass().getName();
        }
        signature += argType;
        if (i < (args.length - 1)) {
          signature += ", ";
        }
      }
    }
    signature += ")";
    return signature;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy