All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.mmm.util.pojo.base.SimplePojoFactory Maven / Gradle / Ivy

The newest version!
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.pojo.base;

import java.lang.reflect.Array;

import net.sf.mmm.util.component.base.AbstractComponent;
import net.sf.mmm.util.pojo.api.PojoFactory;
import net.sf.mmm.util.reflect.api.InstantiationFailedException;

/**
 * This is the simplest implementation of the {@link PojoFactory} interface. 
* It uses {@link Class#newInstance()} to create {@link #newInstance(Class) new instances}. * * @author Joerg Hohwiller (hohwille at users.sourceforge.net) * @since 1.1.0 */ public class SimplePojoFactory extends AbstractComponent implements PojoFactory { @Override public POJO newInstance(Class pojoType) throws InstantiationFailedException { try { if (pojoType.isInterface()) { POJO result = newInstanceForInterface(pojoType); if (result == null) { throw new InstantiationFailedException(pojoType); } return result; } else if (pojoType.isArray()) { return pojoType.cast(Array.newInstance(pojoType.getComponentType(), 0)); } else { return newInstanceForClass(pojoType); } } catch (Exception e) { throw new InstantiationFailedException(e, pojoType); } } /** * Implementation of {@link #newInstance(Class)} for regular class. * * @param is the generic type of the {@link net.sf.mmm.util.pojo.api.Pojo} to create. * @param pojoType is the {@link Class} reflecting the {@link net.sf.mmm.util.pojo.api.Pojo} to create. * @return the new instance of the given {@code pojoType}. * @throws InstantiationFailedException if the instantiation failed. */ protected POJO newInstanceForClass(Class pojoType) throws InstantiationFailedException { try { return pojoType.newInstance(); } catch (Exception e) { throw new InstantiationFailedException(e, pojoType); } } /** * This method is invoked from {@link #newInstance(Class)} if the given {@link Class} is an {@link Class#isInterface() * interface}. * * @param is the generic type of the {@link net.sf.mmm.util.pojo.api.Pojo} to create. * @param pojoInterface is the interface reflecting the {@link net.sf.mmm.util.pojo.api.Pojo} to create. * @return the new instance of the given {@code pojoType} or {@code null} if no implementation could be found. * @throws InstantiationFailedException if the instantiation failed. */ protected POJO newInstanceForInterface(Class pojoInterface) throws InstantiationFailedException { return null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy