Java Reflection Constructor Get getConstructorFromSignature(Class clazz, String cstrSig)

Here you can find the source of getConstructorFromSignature(Class clazz, String cstrSig)

Description

Get constructor from signature

License

Apache License

Parameter

Parameter Description
clazz a parameter
cstrSig a parameter

Exception

Parameter Description
ClassNotFoundException an exception
NoSuchMethodException an exception
SecurityException an exception

Declaration

public static Constructor<?> getConstructorFromSignature(Class<?> clazz, String cstrSig)
        throws ClassNotFoundException, SecurityException, NoSuchMethodException 

Method Source Code


//package com.java2s;
/*// w ww. j a  v a2  s  .  c o  m
 * Copyright (C) 2012-2018 Gregory Hedlund <https://www.phon.ca>
 * Copyright (C) 2012 Jason Gedge <http://www.gedge.ca>
 *
 * 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 {
    /**
     * Get constructor from signature
     *
     * @param clazz
     * @param cstrSig
     * @return
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Constructor<?> getConstructorFromSignature(Class<?> clazz, String cstrSig)
            throws ClassNotFoundException, SecurityException, NoSuchMethodException {
        Constructor<?> retVal = null;

        final String methodName = getMethodNameFromSignature(cstrSig);
        if (!methodName.equals("<init>")) {
            throw new IllegalArgumentException("Not a constructor signature");
        }

        final Class<?>[] paramTypes = getParametersFromSignature(cstrSig);

        retVal = clazz.getConstructor(paramTypes);

        return retVal;
    }

    /**
     * Get method name from signature
     *
     * @param sig
     * @return
     */
    public static String getMethodNameFromSignature(String sig) {
        return sig.substring(0, sig.indexOf('('));
    }

    /**
     * Get paramters from signature
     *
     * @param sig
     * @return
     * @throws ClassNotFoundException
     *
     */
    public static Class<?>[] getParametersFromSignature(String sig) throws ClassNotFoundException {
        Class<?> retVal[] = new Class<?>[0];
        final String paramString = sig.substring(sig.indexOf('(') + 1, sig.lastIndexOf(')')).trim();
        if (paramString.length() > 0) {
            final String[] paramClassNames = paramString.split(",");
            retVal = new Class<?>[paramClassNames.length];
            for (int i = 0; i < paramClassNames.length; i++) {
                final String paramClassName = paramClassNames[i];

                if (paramClassName.equals("int")) {
                    retVal[i] = int.class;
                } else if (paramClassName.equals("double")) {
                    retVal[i] = double.class;
                } else if (paramClassName.equals("float")) {
                    retVal[i] = float.class;
                } else {
                    retVal[i] = Class.forName(paramClassName.trim());
                }
            }
        }
        return retVal;
    }
}

Related

  1. getConstructor(String typeString)
  2. getConstructorAccessor(Class enumClass, Class[] additionalParameterTypes)
  3. getConstructorCalls(Class aClass)
  4. getConstructorDescriptor(final Constructor c)
  5. getConstructorForArguments( java.lang.reflect.Constructor[] constructors, Object... arguments)
  6. getConstructorIfAvailable(Class clazz, Class... paramTypes)
  7. getConstructorLabel(java.lang.reflect.Constructor con)
  8. getConstructorOptional(Class cls, Class... argsTypes)
  9. getConstructorOrFail(Class clazz, Class... argTypes)