| 1 | /* |
| 2 | * This file is part of JON. |
| 3 | * |
| 4 | * JON is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * JON is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * Copyright 2007 Michael Doberenz |
| 18 | */ |
| 19 | package org.fuwjax.jon.type; |
| 20 | |
| 21 | import org.fuwjax.jon.ObjectAccessException; |
| 22 | |
| 23 | /** |
| 24 | * Instantiates objects using Class.newInstance(). |
| 25 | * @author michaeldoberenz |
| 26 | */ |
| 27 | public final class NaiveInstantiator{ |
| 28 | private NaiveInstantiator(){ |
| 29 | // for utility |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Creates a new instance of <code>type</code>. |
| 34 | * @param type the class of the new instance |
| 35 | * @return the new instance |
| 36 | * @throws ObjectAccessException if the object cannot be created |
| 37 | */ |
| 38 | public static Object create(final Class<?> type) throws ObjectAccessException{ |
| 39 | try{ |
| 40 | return type.newInstance(); |
| 41 | }catch(InstantiationException e){ |
| 42 | throw ObjectAccessException.Message.NaiveCreationFailed.exception(e); |
| 43 | }catch(IllegalAccessException e){ |
| 44 | throw ObjectAccessException.Message.ConstructorInaccessible.exception(e); |
| 45 | } |
| 46 | } |
| 47 | } |