ElementParentsIterable implements Iterable<Element> { public static Stream<Element> stream(Element element) { return StreamSupport.stream(new ElementParentsIterable(element).spliterator(), false); } ElementParentsIterable(Element start); @Override Iterator<Element> iterator(); static Stream<Element> stream(Element element);  }
@Test public void shouldGetParents() throws Exception { TypeElement twiceNested = compilation.getElements() .getTypeElement(Outer.Nested.TwiceNested.class.getCanonicalName()); List<? extends Element> members = compilation.getElements().getAllMembers(twiceNested); Element field = members.stream().filter(ofKind(ElementKind.FIELD)).findFirst() .orElseThrow(IllegalStateException::new); List<String> parents = stream(field) .map(element -> element.getSimpleName().toString()) .collect(Collectors.toList()); assertThat(parents, hasItems("field", "TwiceNested", "Nested", "Outer")); }
@Test public void shouldReturnNothingForPackage() throws Exception { PackageElement pkg = compilation.getElements() .getPackageElement(Outer.class.getPackage().getName()); assertThat(stream(pkg).count(), is(0L)); }