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

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

Description

Gets the constructor of a class that takes the specified set of arguments if any matches.

License

Apache License

Parameter

Parameter Description
cls The class to get a constructor from.
args The arguments to match.
T The class type.

Return

The constructor.

Declaration

public static <T> Constructor<T> getConstructor(Class<T> cls, Class[] args) 

Method Source Code


//package com.java2s;
/*//from ww  w .  jav a 2s .c  om
 * Copyright The Sett Ltd, 2005 to 2014.
 *
 * 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.
 */

import java.lang.reflect.Constructor;

public class Main {
    /**
     * Gets the constructor of a class that takes the specified set of arguments if any matches. If no matching
     * constructor is found then a runtime exception is raised.
     *
     * @param  cls  The class to get a constructor from.
     * @param  args The arguments to match.
     * @param  <T>  The class type.
     *
     * @return The constructor.
     */
    public static <T> Constructor<T> getConstructor(Class<T> cls, Class[] args) {
        try {
            return cls.getConstructor(args);
        } catch (NoSuchMethodException e) {
            throw new IllegalStateException(e);
        }
    }
}

Related

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