// Assumes targetKinds and E are sensible. private static <E extends Element> List<E> listFilter( Iterable<? extends Element> elements, Set<ElementKind> targetKinds, Class<E> clazz) { List<E> list = new ArrayList<E>(); for (Element e : elements) { if (targetKinds.contains(e.getKind())) list.add(clazz.cast(e)); } return list; }
// Assumes targetKinds and E are sensible. private static <E extends Element> Set<E> setFilter( Set<? extends Element> elements, Set<ElementKind> targetKinds, Class<E> clazz) { // Return set preserving iteration order of input set. Set<E> set = new LinkedHashSet<E>(); for (Element e : elements) { if (targetKinds.contains(e.getKind())) set.add(clazz.cast(e)); } return set; }
private boolean isInstanceOf(TypeMirror typeMirror, Class type) { if (!(typeMirror instanceof DeclaredType)) { return false; } if (typeMirror.toString().startsWith(type.getName())) { return true; } DeclaredType declaredType = (DeclaredType) typeMirror; TypeElement typeElement = (TypeElement) declaredType.asElement(); for (TypeMirror iface : typeElement.getInterfaces()) { if (isInstanceOf(iface, type)) { return true; } } return isInstanceOf(typeElement.getSuperclass(), type); }
/** * Returns an object cast to the specified type. * * @throws NullPointerException if the object is {@code null} * @throws IllegalArgumentException if the object is of the wrong type */ private static <T> T cast(Class<T> clazz, Object o) { if (!clazz.isInstance(o)) throw new IllegalArgumentException(o.toString()); return clazz.cast(o); }