@Test public void testInterface () { User user = EntityFactory.createEntity(User.class); assertNotNull(user); assertNull(user.getCode()); user.setCode("john"); assertNotNull(user.getCode()); assertEquals("john", user.getCode()); }
public static <T extends Base> T createEntity(Class<T> clazz) throws UserInterfaceDalErrorException { Assert.notNull(clazz, "The class of the entity interface is null."); try { if (clazz.isInterface()) { clazz = EntityFactory.getEntityClass(clazz); } return clazz.newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) { if (logger.isErrorEnabled()) { logger.error(String.format("Create entity[%s] fail.", clazz.getName()), ex); } throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL); } }
EntityFactory { public static <T extends Base> T createEntity(Class<T> clazz) throws UserInterfaceDalErrorException { Assert.notNull(clazz, "The class of the entity interface is null."); try { if (clazz.isInterface()) { clazz = EntityFactory.getEntityClass(clazz); } return clazz.newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) { if (logger.isErrorEnabled()) { logger.error(String.format("Create entity[%s] fail.", clazz.getName()), ex); } throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL); } } }
EntityFactory { public static <T extends Base> T createEntity(Class<T> clazz) throws UserInterfaceDalErrorException { Assert.notNull(clazz, "The class of the entity interface is null."); try { if (clazz.isInterface()) { clazz = EntityFactory.getEntityClass(clazz); } return clazz.newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) { if (logger.isErrorEnabled()) { logger.error(String.format("Create entity[%s] fail.", clazz.getName()), ex); } throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL); } }  }
EntityFactory { public static <T extends Base> T createEntity(Class<T> clazz) throws UserInterfaceDalErrorException { Assert.notNull(clazz, "The class of the entity interface is null."); try { if (clazz.isInterface()) { clazz = EntityFactory.getEntityClass(clazz); } return clazz.newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) { if (logger.isErrorEnabled()) { logger.error(String.format("Create entity[%s] fail.", clazz.getName()), ex); } throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL); } }  @SuppressWarnings("unchecked") static Class<T> getEntityClass(Class<T> clazz); static T createEntity(Class<T> clazz); }
EntityFactory { public static <T extends Base> T createEntity(Class<T> clazz) throws UserInterfaceDalErrorException { Assert.notNull(clazz, "The class of the entity interface is null."); try { if (clazz.isInterface()) { clazz = EntityFactory.getEntityClass(clazz); } return clazz.newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) { if (logger.isErrorEnabled()) { logger.error(String.format("Create entity[%s] fail.", clazz.getName()), ex); } throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL); } }  @SuppressWarnings("unchecked") static Class<T> getEntityClass(Class<T> clazz); static T createEntity(Class<T> clazz);  }