@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; }
public static CompilationUnit getCompilationUnit( final SourceFile sourceFile, final CodeModel internalCodeModel) { for (AbstractCodeElement c : internalCodeModel.getCodeElement()) { if (c instanceof CompilationUnit) { for (SourceRef ref : c.getSource()) { for (SourceRegion r : ref.getRegion()) { if (r.getFile() == sourceFile) { return (CompilationUnit) c; } } } } } throw new IllegalStateException(); }
/** * 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); }