Java Class New Instance newInstanceForClass(Class type)

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

Description

new Instance For Class

License

Apache License

Declaration

public static <T> T newInstanceForClass(Class<T> type) 

Method Source Code

//package com.java2s;
/**/*from w  w w  .  ja  va 2s  . c  o  m*/
 * Copyright (C) 2012 Red Hat, Inc. and/or its affiliates.
 *
 * 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.Array;

public class Main {
    public static <T> T newInstanceForClass(Class<T> type) {
        Object object = null;
        if (type.isArray()) {
            Class componentClass = type.getComponentType();
            if (componentClass.isPrimitive()) {
                if (componentClass.equals(int.class))
                    object = new int[0];
                else if (componentClass.equals(boolean.class))
                    object = new boolean[0];
                else if (componentClass.equals(long.class))
                    object = new long[0];
                else if (componentClass.equals(char.class))
                    object = new char[0];
                else if (componentClass.equals(double.class))
                    object = new double[0];
                else if (componentClass.equals(float.class))
                    object = new float[0];
                else if (componentClass.equals(byte.class))
                    object = new byte[0];
                else if (componentClass.equals(short.class))
                    object = new short[0];
            } else if (String.class.equals(componentClass))
                object = new String[0];
            else if (componentClass.equals(Integer.class))
                object = new Integer[0];
            else if (componentClass.equals(Boolean.class))
                object = new Boolean[0];
            else if (componentClass.equals(Long.class))
                object = new Long[0];
            else if (componentClass.equals(Character.class))
                object = new Character[0];
            else if (componentClass.equals(Double.class))
                object = new Double[0];
            else if (componentClass.equals(Float.class))
                object = new Float[0];
            else if (componentClass.equals(Byte.class))
                object = new Byte[0];
            else if (componentClass.equals(Short.class))
                object = new Short[0];
            else
                object = Array.newInstance(componentClass, 0);
        } else {
            if (String.class.equals(type))
                object = "";
            else if (type.equals(int.class))
                object = new Integer(0);
            else if (type.equals(boolean.class))
                object = Boolean.FALSE;
            else if (type.equals(long.class))
                object = new Long(0);
            else if (type.equals(char.class))
                object = new Character((char) 0);
            else if (type.equals(double.class))
                object = new Double(0);
            else if (type.equals(float.class))
                object = new Float(0);
            else if (type.equals(byte.class))
                object = new Byte((byte) 0);
            else if (type.equals(short.class))
                object = new Short((short) 0);
            else if (type.equals(Integer.class))
                object = new Integer(0);
            else if (type.equals(Boolean.class))
                object = Boolean.FALSE;
            else if (type.equals(Long.class))
                object = new Long(0);
            else if (type.equals(Character.class))
                object = new Character((char) 0);
            else if (type.equals(Double.class))
                object = new Double(0);
            else if (type.equals(Float.class))
                object = new Float(0);
            else if (type.equals(Byte.class))
                object = new Byte((byte) 0);
            else if (type.equals(Short.class))
                object = new Short((short) 0);
            else
                object = null;
        }
        return type.cast(object);
    }
}

Related

  1. newInstance(Type type)
  2. newInstance(Type type)
  3. newInstance(Type type)
  4. newInstanceByName(String className)
  5. newInstanceByName(String className)
  6. newInstanceForName(String fullClassName, Object... args)
  7. newInstanceFromClass(final Class clazz)
  8. newInstanceFromClassName(String className, Class classType)
  9. newInstanceFromUnknownArgumentTypes(Class cls, Object[] args)