Java Class New Instance newInstance(String name)

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

Description

new Instance

License

Apache License

Declaration

public static Object newInstance(String name) 

Method Source Code

//package com.java2s;
/*//from www  .ja va2  s  . c om
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

public class Main {
    public static Object newInstance(String name) {
        try {
            return forName(name).newInstance();
        } catch (InstantiationException e) {
            throw new IllegalStateException(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    public static Class<?> forName(String[] packages, String className) {
        try {
            return _forName(className);
        } catch (ClassNotFoundException e) {
            if (packages != null && packages.length > 0) {
                for (String pkg : packages) {
                    try {
                        return _forName(pkg + "." + className);
                    } catch (ClassNotFoundException e2) {
                    }
                }
            }
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    public static Class<?> forName(String className) {
        try {
            return _forName(className);
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    public static Class<?> _forName(String className) throws ClassNotFoundException {
        switch (className) {
        case "boolean":
            return boolean.class;
        case "byte":
            return byte.class;
        case "char":
            return char.class;
        case "short":
            return short.class;
        case "int":
            return int.class;
        case "long":
            return long.class;
        case "float":
            return float.class;
        case "double":
            return double.class;
        case "boolean[]":
            return boolean[].class;
        case "byte[]":
            return byte[].class;
        case "char[]":
            return char[].class;
        case "short[]":
            return short[].class;
        case "int[]":
            return int[].class;
        case "long[]":
            return long[].class;
        case "float[]":
            return float[].class;
        case "double[]":
            return double[].class;
        }
        try {
            return arrayForName(className);
        } catch (ClassNotFoundException e) {
            // try to load from java.lang package
            if (className.indexOf('.') == -1) {
                try {
                    return arrayForName("java.lang." + className);
                } catch (ClassNotFoundException e2) {
                    // ignore, let the original exception be thrown
                }
            }
            throw e;
        }
    }

    private static Class<?> arrayForName(String className) throws ClassNotFoundException {
        return Class.forName(
                className.endsWith("[]") ? "[L" + className.substring(0, className.length() - 2) + ";" : className,
                true, Thread.currentThread().getContextClassLoader());
    }
}

Related

  1. newInstance(String classname, Properties properties)
  2. newInstance(String clazz)
  3. newInstance(String clazzStr)
  4. newInstance(String cls, Class interfaceType)
  5. newInstance(String klass)
  6. newInstance(String name, ClassLoader loader)
  7. newInstance(String s)
  8. newInstance(String type)
  9. newInstance(String type, Class cast)