Java Class New Instance newInstance(T obj, Class argType, Object arg)

Here you can find the source of newInstance(T obj, Class argType, Object arg)

Description

new Instance

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <T> T newInstance(T obj, Class<?> argType, Object arg)
            throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException,
            InvocationTargetException, NoSuchMethodException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2012 Google, Inc.// w w  w. j  av a 2s  . c o  m
 *  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:
 *  Google, Inc. - initial API and implementation
 *******************************************************************************/

import java.lang.reflect.InvocationTargetException;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T newInstance(T obj) throws IllegalArgumentException, SecurityException,
            InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Object newObj = obj.getClass().getConstructor().newInstance();
        return (T) newObj;
    }

    @SuppressWarnings("unchecked")
    public static <T> T newInstance(T obj, Class<?> argType, Object arg)
            throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {
        Object newObj = obj.getClass().getConstructor(argType).newInstance(arg);
        return (T) newObj;
    }

    @SuppressWarnings("unchecked")
    public static <T> T newInstance(Class<?> instanceClass, Class<?> argType, Object arg)
            throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {
        Object newObj = instanceClass.getConstructor(argType).newInstance(arg);
        return (T) newObj;
    }
}

Related

  1. newInstance(String name, ClassLoader loader)
  2. newInstance(String s)
  3. newInstance(String type)
  4. newInstance(String type, Class cast)
  5. newInstance(T obj)
  6. newInstance(Type type)
  7. newInstance(Type type)
  8. newInstance(Type type)
  9. newInstanceByName(String className)