Here you can find the source of getConstructor(Class
Parameter | Description |
---|---|
cls | The class to get a constructor from. |
args | The arguments to match. |
T | The class type. |
public static <T> Constructor<T> getConstructor(Class<T> cls, Class[] args)
//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); } } }