Java Class New Instance newInstance(String className, Class context)

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

Description

Instantiates an object using its default constructor if the className is found in the classpath and loaded.

License

Apache License

Declaration

@SuppressWarnings("unchecked")
public static <T> T newInstance(String className, Class<?> context) throws Exception 

Method Source Code

//package com.java2s;
//Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**//from  ww  w.ja  v a2 s .  c o  m
     * Instantiates an object using its default constructor if the {@code className} is 
     * found in the classpath and loaded.
     */
    @SuppressWarnings("unchecked")
    public static <T> T newInstance(String className, Class<?> context) throws Exception {
        Class<T> clazz = (Class<T>) loadClass(className, context, false);
        if (clazz == null)
            throw new Exception(className + " not found in the classpath.");

        return clazz.newInstance();
    }

    /**
     * Instantiates an object using its default constructor if the {@code className} is 
     * found in the classpath and loaded.
     */
    @SuppressWarnings("unchecked")
    public static <T> T newInstance(String className, Class<?> context, boolean checkParent) throws Exception {
        Class<T> clazz = (Class<T>) loadClass(className, context, checkParent);
        if (clazz == null)
            throw new Exception(className + " not found in the classpath.");

        return clazz.newInstance();
    }

    /**
     * Loads a class from the classloader; 
     * If not found, the classloader of the {@code context} class specified will be used.
     */
    public static Class<?> loadClass(String className, Class<?> context) {
        return loadClass(className, context, false);
    }

    /**
     * Loads a class from the classloader; 
     * If not found, the classloader of the {@code context} class specified will be used.
     * If the flag {@code checkParent} is true, the classloader's parent is included in 
     * the lookup.
     */
    public static Class<?> loadClass(String className, Class<?> context, boolean checkParent) {
        Class<?> clazz = null;
        try {
            clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
        } catch (ClassNotFoundException e) {
            if (context != null) {
                ClassLoader loader = context.getClassLoader();
                while (loader != null) {
                    try {
                        clazz = loader.loadClass(className);
                        return clazz;
                    } catch (ClassNotFoundException e1) {
                        loader = checkParent ? loader.getParent() : null;
                    }
                }
            }
        }
        return clazz;
    }
}

Related

  1. newInstance(String className)
  2. newInstance(String className)
  3. newInstance(String className)
  4. newInstance(String className, Class instanceClazz)
  5. newInstance(String className, Class cls)
  6. newInstance(String className, Class castTo)
  7. newInstance(String className, Class[] parmsCls, Object[] parms)
  8. newInstance(String className, Object arg1)
  9. newInstance(String className, Object... args)