Java Reflection Constructor Get getConstructor(final Class instantiableClass, final Class... classes)

Here you can find the source of getConstructor(final Class instantiableClass, final Class... classes)

Description

Get a constructor for a class and put the result in cache, to increase speed for the next call.

License

Apache License

Parameter

Parameter Description
instantiableClass The class to instanciate
classes The parameter classes to search
T The type of class and constructor

Return

The list of constructors

Declaration

@SuppressWarnings("unchecked")
protected static <T> Constructor<T> getConstructor(final Class<T> instantiableClass,
        final Class<?>... classes) 

Method Source Code


//package com.java2s;
/*//  w  w w.j  ava 2s . co m
 * #%L
 * utils-commons
 * %%
 * Copyright (C) 2016 - 2018 Gilles Landel
 * %%
 * 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.
 * #L%
 */

import java.lang.reflect.Constructor;

import java.util.Arrays;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class Main {
    /**
     * Simple cache to avoid reflect calls
     */
    private static ConcurrentMap<Class<?>, Constructor<?>[]> constructorsCache = new ConcurrentHashMap<>();
    /**
     * Simple cache to avoid re-analyzing
     */
    private static ConcurrentMap<Integer, Constructor<?>> constructorCache = new ConcurrentHashMap<>();

    /**
     * Get a constructor for a class and put the result in cache, to increase speed
     * for the next call.
     * 
     * @param instantiableClass
     *            The class to instanciate
     * @param classes
     *            The parameter classes to search
     * @param <T>
     *            The type of class and constructor
     * @return The list of constructors
     */
    @SuppressWarnings("unchecked")
    protected static <T> Constructor<T> getConstructor(final Class<T> instantiableClass,
            final Class<?>... classes) {
        final int hashCode = instantiableClass.hashCode() ^ Arrays.hashCode(classes);

        if (constructorCache.containsKey(hashCode)) {
            return (Constructor<T>) constructorCache.get(hashCode);
        } else {
            if (!constructorsCache.containsKey(instantiableClass)) {
                constructorsCache.put(instantiableClass, instantiableClass.getDeclaredConstructors());
            }

            boolean mismatch;

            for (Constructor<?> constructor : constructorsCache.get(instantiableClass)) {
                Class<?>[] parameterTypes = constructor.getParameterTypes();
                if (parameterTypes.length == classes.length) {
                    mismatch = false;
                    // only take the first matching constructor
                    for (int i = 0; i < parameterTypes.length; i++) {
                        if (classes[i] != null && !parameterTypes[i].isAssignableFrom(classes[i])) {
                            mismatch = true;
                            break;
                        }
                    }
                    if (!mismatch) {
                        constructorCache.put(hashCode, constructor);
                        return (Constructor<T>) constructor;
                    }
                }
            }
        }

        return null;
    }
}

Related

  1. getConstructor(final Class valueClass, final Class parameter)
  2. getConstructor(final Class clazz, final Class... parametertypes)
  3. getConstructor(final Class clazz, final Class... parameterTypes)
  4. getConstructor(final Class clazz, final Object... constructorArgs)
  5. getConstructor(final Class clazz, final Object... objs)
  6. getConstructor(String className, Class... argClasses)
  7. getConstructor(String cls_name, Class[] param_cls)
  8. getConstructor(String string, Class... types)
  9. getConstructor(String type, Class[] paramTypes)