Java Class New Instance newInstance(Class type, Class declarator, Annotation annotation)

Here you can find the source of newInstance(Class type, Class declarator, Annotation annotation)

Description

new Instance

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <T> T newInstance(Class<T> type, Class<?> declarator, Annotation annotation)
            throws IllegalAccessException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T newInstance(Class<T> type, Class<?> declarator, Annotation annotation)
            throws IllegalAccessException {
        if (declarator != null)
            try {
                Constructor<?> c = type.getDeclaredConstructor(declarator.getClass(), annotation.annotationType());
                c.setAccessible(true);/*from  w ww  .j  a va2  s .c  om*/
                if (c != null)
                    return (T) c.newInstance(declarator, annotation);
            } catch (SecurityException e) {
            } catch (NoSuchMethodException e) {
            } catch (InstantiationException e) {
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        try {
            Constructor<?> c = type.getConstructor(annotation.annotationType());
            if (c != null)
                return (T) c.newInstance(annotation);
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
        } catch (InstantiationException e) {
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        try {
            Constructor<?> c = type.getConstructor();
            if (c != null)
                return (T) c.newInstance();
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
        } catch (InstantiationException e) {
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}

Related

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

    HOME | Copyright © www.java2s.com 2016