Java Reflection Constructor Get getConstructor(Class type, Class... parameterTypes)

Here you can find the source of getConstructor(Class type, Class... parameterTypes)

Description

Gets declared constructor for given Class and given parameters

License

Open Source License

Parameter

Parameter Description
type a parameter
parameterTypes a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static <T> Constructor<T> getConstructor(Class<T> type, Class<?>... parameterTypes) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  w  w .  j av  a  2s  .co m
 * Lightmare, Lightweight embedded EJB container (works for stateless session beans) with JPA / Hibernate support
 *
 * Copyright (c) 2013, Levan Tsinadze, or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */

import java.io.IOException;

import java.lang.reflect.Constructor;

public class Main {
    /**
     * Gets declared constructor for given {@link Class} and given parameters
     * 
     * @param type
     * @param parameterTypes
     * @return {@link Constructor}
     * @throws IOException
     */
    public static <T> Constructor<T> getConstructor(Class<T> type, Class<?>... parameterTypes) throws IOException {

        Constructor<T> constructor;

        try {
            constructor = type.getDeclaredConstructor(parameterTypes);
        } catch (NoSuchMethodException ex) {
            throw new IOException(ex);
        } catch (SecurityException ex) {
            throw new IOException(ex);
        }

        return constructor;
    }
}

Related

  1. getConstructor(Class cls, Class... parameterClses)
  2. getConstructor(Class cls, Class[] args)
  3. getConstructor(Class cls, Object... args)
  4. getConstructor(Class instanceType)
  5. getConstructor(Class targetClass, Class... parameterTypes)
  6. getConstructor(Class type, Class[] parameterTypes)
  7. getConstructor(final Class theClass, final Class... parameterTypes)
  8. getConstructor(final Class clazz, final Class... parameterTypes)
  9. getConstructor(final Class clazz, final Class[] paramTypes)