Java Class New Instance newInstance(Type type)

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

Description

Creates a new instance of the class represented by this Type object.

License

Open Source License

Parameter

Parameter Description
type the Type object whose its representing Class object will be instantiated.

Exception

Parameter Description
ClassNotFoundException if the class represented by this Type object cannotbe located.
InstantiationException if this Type represents an abstract class, aninterface, an array class, a primitive type, or void; or ifthe class has no nullary constructor; or if the instantiationfails for some other reason.
IllegalAccessException if the class or its nullary constructor is not accessible.

Return

a newly allocated instance of the class represented by the invoked Type object.

Declaration

public static Object newInstance(Type type)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Type;

public class Main {
    /**//from  w w  w.j a  v  a2  s  .  co  m
     * When {@code Type} initialized with a value of an object, its fully
     * qualified class name will be prefixed with this.
     * 
     * @see {@link ReflectionUtil#getClassName(Type)}
     */
    private static final String TYPE_CLASS_NAME_PREFIX = "class ";
    private static final String TYPE_INTERFACE_NAME_PREFIX = "interface ";

    /**
     * Creates a new instance of the class represented by this {@code Type}
     * object.
     * 
     * @param type
     *            the {@code Type} object whose its representing {@code Class}
     *            object will be instantiated.
     * @return a newly allocated instance of the class represented by the
     *         invoked {@code Type} object.
     * 
     * @throws ClassNotFoundException
     *             if the class represented by this {@code Type} object cannot
     *             be located.
     * @throws InstantiationException
     *             if this {@code Type} represents an abstract class, an
     *             interface, an array class, a primitive type, or void; or if
     *             the class has no nullary constructor; or if the instantiation
     *             fails for some other reason.
     * @throws IllegalAccessException
     *             if the class or its nullary constructor is not accessible.
     * 
     * @see {@link Class#newInstance()}
     */
    public static Object newInstance(Type type)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<?> clazz = getClass(type);
        if (clazz == null) {
            return null;
        }
        return clazz.newInstance();
    }

    /**
     * Returns the {@code Class} object associated with the given {@link Type}
     * depending on its fully qualified name.
     * 
     * @param type
     *            the {@code Type} whose {@code Class} is needed.
     * @return the {@code Class} object for the class with the specified name.
     * 
     * @throws ClassNotFoundException
     *             if the class cannot be located.
     * 
     * @see {@link ReflectionUtil#getClassName(Type)}
     */
    public static Class<?> getClass(Type type) throws ClassNotFoundException {
        String className = getClassName(type);
        if (className == null || className.isEmpty()) {
            return null;
        }
        return Class.forName(className);
    }

    /**
     * {@link Type#toString()} value is the fully qualified class name prefixed
     * with {@link ReflectionUtil#TYPE_NAME_PREFIX}. This method will substring
     * it, for it to be eligible for {@link Class#forName(String)}.
     * 
     * @param type
     *            the {@code Type} value whose class name is needed.
     * @return {@code String} class name of the invoked {@code type}.
     * 
     * @see {@link ReflectionUtil#getClass()}
     */
    public static String getClassName(Type type) {
        if (type == null) {
            return "";
        }
        String className = type.toString();
        if (className.startsWith(TYPE_CLASS_NAME_PREFIX)) {
            className = className.substring(TYPE_CLASS_NAME_PREFIX.length());
        } else if (className.startsWith(TYPE_INTERFACE_NAME_PREFIX)) {
            className = className.substring(TYPE_INTERFACE_NAME_PREFIX.length());
        }
        return className;
    }
}

Related

  1. newInstance(String type)
  2. newInstance(String type, Class cast)
  3. newInstance(T obj)
  4. newInstance(T obj, Class argType, Object arg)
  5. newInstance(Type type)
  6. newInstance(Type type)
  7. newInstanceByName(String className)
  8. newInstanceByName(String className)
  9. newInstanceForClass(Class type)