Java Class New Instance newInstance(Class type, Object... params)

Here you can find the source of newInstance(Class type, Object... params)

Description

new Instance

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <T> T newInstance(Class<T> type, Object... params) 

Method Source Code

//package com.java2s;
/**/*from   w ww.  j a  va 2s.c  om*/
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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;
import java.lang.reflect.InvocationTargetException;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T newInstance(Class<T> type, Object... params) {
        return instantiate(type.getName(), findConstructor(type, params), params);
    }

    private static <T> T instantiate(final String className, Constructor<T> ctor, Object[] ctorArgs) {
        try {
            return ctor.newInstance(ctorArgs);
        } catch (IllegalAccessException e) {
            throw new UnsupportedOperationException("Unable to access specified class " + className, e);
        } catch (InstantiationException e) {
            throw new UnsupportedOperationException("Unable to instantiate specified class " + className, e);
        } catch (InvocationTargetException e) {
            throw new UnsupportedOperationException("Constructor threw an exception for " + className, e);
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> Constructor<T> findConstructor(Class<T> type, Object... paramTypes) {
        Constructor<T>[] constructors = (Constructor<T>[]) type.getConstructors();
        for (Constructor<T> ctor : constructors) {
            Class<?>[] ctorParamTypes = ctor.getParameterTypes();
            if (ctorParamTypes.length != paramTypes.length) {
                continue;
            }

            boolean match = true;
            for (int i = 0; i < ctorParamTypes.length && match; ++i) {
                Class<?> paramType = paramTypes[i].getClass();
                match = (!ctorParamTypes[i].isPrimitive()) ? ctorParamTypes[i].isAssignableFrom(paramType)
                        : ((int.class.equals(ctorParamTypes[i]) && Integer.class.equals(paramType))
                                || (long.class.equals(ctorParamTypes[i]) && Long.class.equals(paramType))
                                || (char.class.equals(ctorParamTypes[i]) && Character.class.equals(paramType))
                                || (short.class.equals(ctorParamTypes[i]) && Short.class.equals(paramType))
                                || (boolean.class.equals(ctorParamTypes[i]) && Boolean.class.equals(paramType))
                                || (byte.class.equals(ctorParamTypes[i]) && Byte.class.equals(paramType)));
            }

            if (match) {
                return ctor;
            }
        }
        throw new UnsupportedOperationException("Unable to find suitable constructor for class " + type.getName());
    }
}

Related

  1. newInstance(Class type)
  2. newInstance(Class type)
  3. newInstance(Class type, Class declarator, Annotation annotation)
  4. newInstance(Class type, Class[] parameterTypes, Object[] args)
  5. newInstance(Class type, Object... args)
  6. newInstance(Constructor c, List parameters)
  7. newInstance(Constructor constructor, Object... args)
  8. newInstance(Constructor constructor, Object... arguments)
  9. newInstance(Constructor ctor, Object... params)

  10. HOME | Copyright © www.java2s.com 2016