TypeElements { @Nonnull public static TypeElement toElement(@Nonnull TypeMirror type) { if (type.getKind() != TypeKind.DECLARED) { throw new IllegalArgumentException("Kind of type(" + type + ") must be " + TypeKind.DECLARED); } final DeclaredType declaredType = (DeclaredType) type; final Element element = declaredType.asElement(); if (!(element instanceof TypeElement)) { throw new IllegalArgumentException("Provided element(" + element + ") is not instance of TypeElement"); } return (TypeElement) element; } private  TypeElements(); @Nonnull static TypeElement toElement(@Nonnull TypeMirror type); @Nonnull static List<TypeName> findGenericTypes(@Nonnull TypeElement element, @Nonnull String className);  }
@Test public void toElementShouldReturnElement() throws Exception { final TypeElement element = elements.getTypeElement(String.class.getCanonicalName()); final TypeMirror mirror = element.asType(); final TypeElement actual = TypeElements.toElement(mirror); assertThat(actual) .isEqualTo(element); }
@Test public void toElementShouldThrowExceptionWhenTypeIsNotDeclared() throws Exception { final Element element = elements.getPackageElement("java.util"); final TypeMirror mirror = element.asType(); assertThatThrownBy(() -> { TypeElements.toElement(mirror); }).isInstanceOf(IllegalArgumentException.class); }