Here you can find the source of newInstance(String classname, Properties properties)
Parameter | Description |
---|---|
classname | the name of a class. |
properties | properties to feed to the constructor or method if accepted. |
public static Object newInstance(String classname, Properties properties)
//package com.java2s; /*//from ww w . j a v a 2 s.c om Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com) This file is part of the Semantic Discovery Toolkit. The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. The Semantic Discovery Toolkit is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with The Semantic Discovery Toolkit. If not, see <http://www.gnu.org/licenses/>. */ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Properties; public class Main { /** * Get a new instance of an object based on its classpath. * * @param classname the name of a class. * @param properties properties to feed to the constructor or method * if accepted. * * @return an instance of the class created through its default (no-arg) constructor. */ public static Object newInstance(String classname, Properties properties) { Object result = null; Class<?> theClass = null; try { theClass = Class.forName(classname); final Constructor constructor = theClass.getConstructor(Class.forName("java.util.Properties")); result = constructor.newInstance(properties); } catch (NoSuchMethodException e) { // when can't find a constructor that takes properties, use default constructor. result = newInstance(theClass); } catch (InstantiationException e) { throw new IllegalArgumentException("Can't instantiate '" + classname + "' ...", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("Can't access '" + classname + "' ...", e); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Can't find '" + classname + "' ...", e); } catch (InvocationTargetException e) { throw new IllegalArgumentException("Can't invoke '" + classname + ".newInstance(properties)' ...", e); } catch (NullPointerException e) { throw new IllegalArgumentException("Null instance '" + classname + "' ...", e); } return result; } /** * Get a new instance of an object based on its classpath. * * @param classname the name of a class. * * @return an instance of the class created through its default (no-arg) constructor. */ public static Object newInstance(String classname) { Object result = null; try { final Class theClass = Class.forName(classname); result = newInstance(theClass); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Can't find '" + classname + "' ...", e); } catch (NullPointerException e) { throw new IllegalArgumentException("Null instance '" + classname + "' ...", e); } return result; } /** * Get a new instance of an object based on its class. * * @param theClass the class. * * @return an instance of the class created through its default (no-arg) constructor. */ public static Object newInstance(Class theClass) { Object result = null; try { result = theClass.newInstance(); } catch (InstantiationException e) { throw new IllegalArgumentException("Can't instantiate '" + theClass + "' ...", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("Can't access '" + theClass + "' ...", e); } catch (NullPointerException e) { throw new IllegalArgumentException("Null instance '" + theClass + "' ...", e); } return result; } }