private static TypeElement getEnclosingType(Element element) {
    Element enclosingTypeElement = element;
    while (enclosingTypeElement != null && !isType(enclosingTypeElement)) {
      enclosingTypeElement = enclosingTypeElement.getEnclosingElement();
    }

    if (enclosingTypeElement == null) {
      throw new IllegalArgumentException(element + " is not enclosed in any TypeElement.");
    }
    return asType(enclosingTypeElement);
  }
Returns the nearest enclosing {@link TypeElement} to the current element, throwing an {@link
IllegalArgumentException} if the provided {@link Element} is not enclosed by a type.
common/src/main/java/com/google/auto/common/BasicAnnotationProcessor.java