find Constructor - Java Reflection

Java examples for Reflection:Constructor

Description

find Constructor

Demo Code



//package com.java2s;
import java.lang.reflect.Constructor;
import java.util.Arrays;

public class Main {

  public static <T> Constructor<T> findConstructor(Class<T> beanClass, boolean allowNonPublic,
      Class<?>... argumentTypes) {
    Constructor<T>[] candidates = (Constructor<T>[]) (allowNonPublic ? beanClass.getDeclaredConstructors()
        : beanClass.getConstructors());//from  ww  w .  j  a va 2  s .  c om
    sortConstructors(candidates);
    for (Constructor<T> candidate : candidates) {
      if (matchParameterTypes(candidate.getParameterTypes(), argumentTypes)) {
        return candidate;
      }
    }
    return null;
  }

  private static <T> void sortConstructors(Constructor<T>[] constructors) {
    Arrays.sort(constructors, (constructor1, constructor2) -> compareParameterTypes(constructor1.getParameterTypes(),
        constructor2.getParameterTypes()));
  }

  private static boolean matchParameterTypes(Class<?>[] parameterTypes, Class<?>[] argumentTypes) {
    if (parameterTypes.length == argumentTypes.length) {
      boolean match = true;
      for (int i = 0; i < parameterTypes.length; i++) {
        if (!parameterTypes[i].isAssignableFrom(argumentTypes[i])) {
          match = false;
          break;
        }
      }
      return match;
    } else {
      return false;
    }
  }

  private static int compareParameterTypes(Class<?>[] parameterTypes1, Class<?>[] parameterTypes2) {
    if (parameterTypes1.length != parameterTypes2.length) {
      return 0;
    } else {
      int result = 0;
      for (int i = 0; result == 0 && i < parameterTypes1.length; i++) {
        Class<?> parameterType1 = parameterTypes1[i];
        Class<?> parameterType2 = parameterTypes2[i];
        if (!parameterType1.equals(parameterType2)) {
          if (parameterType1.isAssignableFrom(parameterType2)) {
            result = 1;
          } else if (parameterType2.isAssignableFrom(parameterType1)) {
            result = -1;
          }
        }
      }
      return result;
    }
  }
}

Related Tutorials