Search in sources :

Example 61 with TypeElement

use of javax.lang.model.element.TypeElement in project bazel by bazelbuild.

the class ElementUtils method getAllMethodsIn.

/**
     * Return all methods declared in the given type or any superclass/interface.
     * Note that no constructors will be returned.
     * TODO: should this use javax.lang.model.util.Elements.getAllMembers(TypeElement)
     * instead of our own getSuperTypes?
     */
public static List<ExecutableElement> getAllMethodsIn(TypeElement type) {
    List<ExecutableElement> meths = new ArrayList<ExecutableElement>();
    meths.addAll(ElementFilter.methodsIn(type.getEnclosedElements()));
    List<TypeElement> alltypes = getSuperTypes(type);
    for (TypeElement atype : alltypes) {
        meths.addAll(ElementFilter.methodsIn(atype.getEnclosedElements()));
    }
    return Collections.<ExecutableElement>unmodifiableList(meths);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList)

Example 62 with TypeElement

use of javax.lang.model.element.TypeElement in project bazel by bazelbuild.

the class ElementUtils method enclosingClass.

/**
     * Returns the innermost type element enclosing the given element
     *
     * @param elem the enclosed element of a class
     * @return  the innermost type element
     */
public static TypeElement enclosingClass(final Element elem) {
    Element result = elem;
    while (result != null && !result.getKind().isClass() && !result.getKind().isInterface()) {
        /*@Nullable*/
        Element encl = result.getEnclosingElement();
        result = encl;
    }
    return (TypeElement) result;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element)

Example 63 with TypeElement

use of javax.lang.model.element.TypeElement in project bazel by bazelbuild.

the class ElementUtils method getAllFieldsIn.

/**
     * Return all fields declared in the given type or any superclass/interface.
     * TODO: should this use javax.lang.model.util.Elements.getAllMembers(TypeElement)
     * instead of our own getSuperTypes?
     */
public static List<VariableElement> getAllFieldsIn(TypeElement type) {
    List<VariableElement> fields = new ArrayList<VariableElement>();
    fields.addAll(ElementFilter.fieldsIn(type.getEnclosedElements()));
    List<TypeElement> alltypes = getSuperTypes(type);
    for (TypeElement atype : alltypes) {
        fields.addAll(ElementFilter.fieldsIn(atype.getEnclosedElements()));
    }
    return Collections.<VariableElement>unmodifiableList(fields);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement)

Example 64 with TypeElement

use of javax.lang.model.element.TypeElement in project bazel by bazelbuild.

the class AnnotationUtils method annotationName.

// **********************************************************************
// Helper methods to handle annotations.  mainly workaround
// AnnotationMirror.equals undesired property
// (I think the undesired property is that it's reference equality.)
// **********************************************************************
/**
     * @return the fully-qualified name of an annotation as a Name
     */
public static final /*@Interned*/
String annotationName(AnnotationMirror annotation) {
    if (annotationMirrorNames.containsKey(annotation))
        return annotationMirrorNames.get(annotation);
    final DeclaredType annoType = annotation.getAnnotationType();
    final TypeElement elm = (TypeElement) annoType.asElement();
    /*@Interned*/
    String name = elm.getQualifiedName().toString().intern();
    annotationMirrorNames.put(annotation, name);
    return name;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 65 with TypeElement

use of javax.lang.model.element.TypeElement in project bazel by bazelbuild.

the class TypesUtils method isThrowable.

/** @return type represents a Throwable type (e.g. Exception, Error) **/
public static boolean isThrowable(TypeMirror type) {
    while (type != null && type.getKind() == TypeKind.DECLARED) {
        DeclaredType dt = (DeclaredType) type;
        TypeElement elem = (TypeElement) dt.asElement();
        Name name = elem.getQualifiedName();
        if ("java.lang.Throwable".contentEquals(name))
            return true;
        type = elem.getSuperclass();
    }
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) Name(javax.lang.model.element.Name)

Aggregations

TypeElement (javax.lang.model.element.TypeElement)1562 ExecutableElement (javax.lang.model.element.ExecutableElement)517 Element (javax.lang.model.element.Element)502 TypeMirror (javax.lang.model.type.TypeMirror)420 VariableElement (javax.lang.model.element.VariableElement)333 DeclaredType (javax.lang.model.type.DeclaredType)201 ArrayList (java.util.ArrayList)177 PackageElement (javax.lang.model.element.PackageElement)159 AnnotationMirror (javax.lang.model.element.AnnotationMirror)151 Test (org.junit.Test)136 HashMap (java.util.HashMap)118 HashSet (java.util.HashSet)109 Elements (javax.lang.model.util.Elements)101 Map (java.util.Map)98 IOException (java.io.IOException)97 List (java.util.List)97 ClassName (com.squareup.javapoet.ClassName)88 Test (org.junit.jupiter.api.Test)80 MethodSpec (com.squareup.javapoet.MethodSpec)72 TypeSpec (com.squareup.javapoet.TypeSpec)63