Java Class New Instance newInstanceWithParameterTypes(Constructor constructor)

Here you can find the source of newInstanceWithParameterTypes(Constructor constructor)

Description

new Instance With Parameter Types

License

Open Source License

Declaration

private static <T> T newInstanceWithParameterTypes(Constructor<T> constructor) 

Method Source Code


//package com.java2s;
/*/*w w  w. j  av  a2  s .c  om*/
 * JBoss, Home of Professional Open Source
 * Copyright 2014 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;

public class Main {
    private static <T> T newInstanceWithParameterTypes(Constructor<T> constructor) {
        Constructor paramConstructor = getConstructorWithNoParams(constructor.getParameterTypes()[0]);
        if (paramConstructor == null)
            return null;
        setAccessible(constructor);
        try {
            return constructor.newInstance(paramConstructor.newInstance());
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    private static Constructor getConstructorWithNoParams(Class clazz) {
        for (Constructor constructor : clazz.getConstructors()) {
            if (constructor.getParameterTypes().length == 0) {
                setAccessible(constructor);
                return constructor;
            }
        }
        return null;
    }

    private static <T> void setAccessible(Constructor<T> constructor) {
        if (Modifier.isPrivate(constructor.getModifiers()) || Modifier.isProtected(constructor.getModifiers())) {
            constructor.setAccessible(true);
        }
    }

    public static <T> T newInstance(final Class<T> clazz) {
        if (clazz.isAnonymousClass() || clazz.isInterface() || clazz.isAnnotation()) {
            throw new RuntimeException("Can not build new instance of an " + clazz.getName());
        }

        T instance = null;
        for (Constructor<?> constructor : clazz.getConstructors()) {
            instance = (T) instantiateWithConstructor(constructor);
            if (isValidInstance(instance))
                return instance;
        }

        for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
            instance = (T) instantiateWithConstructor(constructor);
            if (isValidInstance(instance))
                return instance;
        }

        throw new RuntimeException("Could not instantiate class: " + clazz + ", no access to constructors.");
    }

    private static <T> T instantiateWithConstructor(Constructor<T> constructor) {
        T instance = null;
        if (constructor.getParameterTypes().length == 0) {
            instance = newInstanceWithoutParameterTypes(constructor);
        }

        if (constructor.getParameterTypes().length == 1) {
            instance = newInstanceWithParameterTypes(constructor);
        }

        return isValidInstance(instance) ? instance : null;
    }

    private static <T> boolean isValidInstance(T instance) {
        if (instance != null) {
            return true;
        }
        return false;
    }

    private static <T> T newInstanceWithoutParameterTypes(Constructor<T> constructor) {
        try {
            setAccessible(constructor);
            return constructor.newInstance();
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

Related

  1. newInstancesViaMetaAnnotation(Class declarator, Class metaAnnotationClass, Class expected)
  2. newInstanceViaAnnotation(Class declarator, Annotation annotation, Class expected, Annotation parameter)
  3. newInstanceWithDefaults(Class annotationType)
  4. newInstanceWithFill(Class componentType, int length, Object filledValue)
  5. newInstanceWithoutInit(Class clazz)