Java Reflection Constructor Get getConstructor(Class type, Object[] args)

Here you can find the source of getConstructor(Class type, Object[] args)

Description

get Constructor

License

Apache License

Declaration

public static Constructor<?> getConstructor(Class<?> type, Object[] args) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.*;

public class Main {
    public static Constructor<?> getConstructor(Class<?> type, Object[] args) {
        for (Constructor<?> c : type.getConstructors()) {
            if (instanceOfs(c.getParameterTypes(), args))
                return c;
        }//from w ww. j  ava 2  s .com

        return null;
    }

    public static boolean instanceOfs(Class<?>[] classes, Object[] objs) {
        if (objs.length != classes.length)
            return false;

        for (int i = 0; i < objs.length; i++) {
            if (objs[i] != null && !asBoxedClass(classes[i]).isInstance(objs[i]))
                return false;
        }

        return true;
    }

    public static Class<?> asBoxedClass(Class<?> c) {
        if (!c.isPrimitive())
            return c;
        else if (c == Boolean.TYPE)
            return Boolean.class;
        else if (c == Byte.TYPE)
            return Byte.class;
        else if (c == Character.TYPE)
            return Character.class;
        else if (c == Short.TYPE)
            return Short.class;
        else if (c == Integer.TYPE)
            return Integer.class;
        else if (c == Long.TYPE)
            return Long.class;
        else if (c == Float.TYPE)
            return Float.class;
        else if (c == Double.TYPE)
            return Double.class;
        else
            throw new RuntimeException(new ClassNotFoundException());
    }
}

Related

  1. getConstructor(Class cls, Object... parameters)
  2. getConstructor(Class klass, Class... parameterTypes)
  3. getConstructor(Class sourceClass, boolean isFindDeclaredConstructor, boolean isUpwardFind, Class... constructorParameterTypes)
  4. getConstructor(Class theClass, Class[] paramTypes)
  5. getConstructor(Class type, Constructor c)
  6. getConstructor(Class type, Class[] givenTypes)
  7. getConstructor(Class aClass)
  8. getConstructor(Class cl, Class... params)
  9. getConstructor(Class clazz)