Java Class New Instance newInstance(Class type)

Here you can find the source of newInstance(Class type)

Description

new Instance

License

Open Source License

Declaration

private static <T> T newInstance(Class<T> type) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007, 2013 EclipseSource 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
 *
 * Contributors://w w  w  .  ja v a2 s  . c o  m
 *    EclipseSource - initial API and implementation
 ******************************************************************************/

import java.lang.reflect.*;

public class Main {
    private static <T> T newInstance(Class<T> type) {
        T result = null;
        try {
            Constructor<T> constructor = type.getDeclaredConstructor();
            if (!constructor.isAccessible()) {
                constructor.setAccessible(true);
            }
            result = constructor.newInstance();
        } catch (RuntimeException rte) {
            throw rte;
        } catch (Exception exception) {
            String msg = "Failed to create instance of type: " + type.getName();
            throw new RuntimeException(msg, exception);
        }
        return result;
    }
}

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)
  7. newInstance(Class type, Class declarator, Annotation annotation)
  8. newInstance(Class type, Class[] parameterTypes, Object[] args)
  9. newInstance(Class type, Object... args)