Java Class New Instance newInstance(String className)

Here you can find the source of newInstance(String className)

Description

new Instance

License

LGPL

Declaration

public static Object newInstance(String className)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.Serializable;

import java.lang.reflect.InvocationTargetException;

public class Main {
    public static Object newInstance(String className)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Object result = null;//from   w w w. j  ava 2  s .com
        Class<?> type = loadClass(className);
        result = (type != null ? type.newInstance() : null);
        return result;
    }

    public static Object newInstance(Class<?> type)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Object result = null;
        result = (type != null ? type.newInstance() : null);
        return result;
    }

    public static Object newInstance(Class<?> type, Long param)
            throws InstantiationException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException, NoSuchMethodException, SecurityException {
        Object result = null;
        result = (type != null ? type.getConstructor(Long.class).newInstance(param) : null);
        return result;
    }

    public static Object newInstance(String className, Long param)
            throws InstantiationException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
        Object result = null;
        Class<?> type = loadClass(className);
        result = newInstance(type, param);
        return result;
    }

    public static Object newInstance(Class<?> type, Serializable param)
            throws InstantiationException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException, NoSuchMethodException, SecurityException {
        Object result = null;
        result = (type != null ? type.getConstructor(Serializable.class).newInstance(param) : null);
        return result;
    }

    public static Object newInstance(String className, Serializable param)
            throws InstantiationException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
        Object result = null;
        Class<?> type = loadClass(className);
        result = newInstance(type, param);
        return result;
    }

    public static Class<?> loadClass(String className) throws ClassNotFoundException {
        Class<?> result = null;
        if (className != null) {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            result = (cl != null ? cl.loadClass(className) : null);
        }
        return result;
    }
}

Related

  1. newInstance(String className)
  2. newInstance(String className)
  3. newInstance(String className)
  4. newInstance(String className)
  5. newInstance(String className)
  6. newInstance(String className, Class instanceClazz)
  7. newInstance(String className, Class cls)
  8. newInstance(String className, Class context)
  9. newInstance(String className, Class castTo)