lundi 30 juillet 2018

How to avoid using break

I think break is here not the best solution to stop this method;

How can I stop the method, when I once set the object and entitiy manager; instead of using break;

/**
 * Convenience method for setting the given entity manager to the given
 * object via reflection.
 *
 * @param object the object whose entity manager should be set
 * @param em     the entity manager that should be set
 */
protected void setEntityManager(Object object, EntityManager em) {
    Field[] fields = object.getClass().getDeclaredFields();
    for (Field f : fields) {
        if (f.getType().isAssignableFrom(EntityManager.class)) {
            f.setAccessible(true);
            try {
                f.set(object, em);
                break;
            } catch (IllegalArgumentException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire