@SuppressWarnings("unchecked") public static <T> T getChildItemByType( final List<? extends AbstractCodeElement> elements, final Class<T> clazz) { for (AbstractCodeElement ace : elements) { if (clazz.isAssignableFrom(ace.getClass())) return (T) ace; } return null; }
/** * Also searches in children of a child * * @param elements * @param name * @param clazz * @return */ @SuppressWarnings("unchecked") public static <T> T getAbstractCodeElementByName( final List<? extends AbstractCodeElement> elements, final String name, final Class<T> clazz) { for (AbstractCodeElement el : elements) { if (clazz.isAssignableFrom(el.getClass()) && name.equals(el.getName())) return (T) el; if (el instanceof ClassUnit) { try { return getAbstractCodeElementByName(((ClassUnit) el).getCodeElement(), name, clazz); } catch (IllegalStateException e) { // do nothing; proceed } } else if (el instanceof TemplateUnit) { try { return getAbstractCodeElementByName(((TemplateUnit) el).getCodeElement(), name, clazz); } catch (IllegalStateException e) { // do nothing; proceed } } } throw new IllegalStateException("Could not find " + name); }