Java Class New Instance newInstance(Class c, Class[] argTypes, Object[] args)

Here you can find the source of newInstance(Class c, Class[] argTypes, Object[] args)

Description

new Instance

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <T> T newInstance(Class<T> c, Class<?>[] argTypes,
            Object[] args) throws IllegalArgumentException,
            SecurityException, InstantiationException,
            IllegalAccessException, InvocationTargetException,
            NoSuchMethodException 

Method Source Code

//package com.java2s;
/*//from   w  w w .j a va2s. co  m
 Copyright 2008 Olivier Chafik

 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.

 This file comes from the Jeneral project (Java Reifiable Generics & Class Templates)

 http://jeneral.googlecode.com/.
 */

import java.lang.reflect.InvocationTargetException;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T newInstance(Class<T> c, Class<?>[] argTypes,
            Object[] args) throws IllegalArgumentException,
            SecurityException, InstantiationException,
            IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {
        if (!c.isPrimitive())
            return (T) c.getConstructor(argTypes).newInstance(args);

        if (argTypes.length != args.length)
            throw new IllegalArgumentException();

        if (argTypes.length > 1)
            throw new NoSuchMethodException(
                    "Primitive cannot be constructed with more than one value");

        if (argTypes.length == 0) {
            return getNeutralValue(c);
        }
        Class<?> cc = argTypes[0];
        if (!c.isAssignableFrom(cc))
            throw new NoSuchMethodException(c
                    + " cannot be constructed from a " + cc);

        Object a = args[0];
        if (c == Integer.TYPE)
            return (T) (Integer) a;
        else if (c == Long.TYPE)
            return (T) (Long) a;
        else if (c == Double.TYPE)
            return (T) (Double) a;
        else if (c == Short.TYPE)
            return (T) (Short) a;
        else if (c == Byte.TYPE)
            return (T) (Byte) a;
        else if (c == Character.TYPE)
            return (T) (Character) a;
        else if (c == Float.TYPE)
            return (T) (Float) a;

        throw new UnsupportedOperationException();

    }

    @SuppressWarnings("unchecked")
    public static <T> T getNeutralValue(Class<T> c) {
        if (!c.isPrimitive())
            return null;
        else if (c == Integer.TYPE)
            return (T) new Integer(0);
        else if (c == Long.TYPE)
            return (T) new Long(0);
        else if (c == Double.TYPE)
            return (T) new Double(0);
        else if (c == Short.TYPE)
            return (T) new Short((short) 0);
        else if (c == Byte.TYPE)
            return (T) new Byte((byte) 0);
        else if (c == Character.TYPE)
            return (T) new Character((char) 0);
        else if (c == Float.TYPE)
            return (T) new Float(0);

        throw new UnsupportedOperationException();
    }
}

Related

  1. newInstance(Class aClass, Object... args)
  2. newInstance(Class arrayComponentClass, T value)
  3. newInstance(Class beanClass)
  4. newInstance(Class c)
  5. newInstance(Class c)
  6. newInstance(Class cl)
  7. newInstance(Class classOf)
  8. newInstance(Class clazz)
  9. newInstance(Class clazz)