Search in sources :

Example 36 with ElementKind

use of javax.lang.model.element.ElementKind in project checker-framework by typetools.

the class LockVisitor method isTreeSymbolEffectivelyFinalOrUnmodifiable.

/**
 * Returns true if the symbol for the given tree is final or effectively final. Package, class and
 * method symbols are unmodifiable and therefore considered final.
 */
private boolean isTreeSymbolEffectivelyFinalOrUnmodifiable(Tree tree) {
    Element elem = TreeUtils.elementFromTree(tree);
    ElementKind ek = elem.getKind();
    return ek == ElementKind.PACKAGE || ek == ElementKind.CLASS || ek == ElementKind.METHOD || ElementUtils.isEffectivelyFinal(elem);
}
Also used : ElementKind(javax.lang.model.element.ElementKind) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 37 with ElementKind

use of javax.lang.model.element.ElementKind in project checker-framework by typetools.

the class OptionalVisitor method visitVariable.

/**
 * Rule #6 (partial).
 *
 * <p>Don't use Optional in fields and method parameters.
 */
@Override
public Void visitVariable(VariableTree node, Void p) {
    VariableElement ve = TreeUtils.elementFromDeclaration(node);
    TypeMirror tm = ve.asType();
    if (isOptionalType(tm)) {
        ElementKind ekind = TreeUtils.elementFromDeclaration(node).getKind();
        if (ekind.isField()) {
            checker.reportWarning(node, "optional.field");
        } else if (ekind == ElementKind.PARAMETER) {
            checker.reportWarning(node, "optional.parameter");
        }
    }
    return super.visitVariable(node, p);
}
Also used : ElementKind(javax.lang.model.element.ElementKind) TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement)

Example 38 with ElementKind

use of javax.lang.model.element.ElementKind in project checker-framework by typetools.

the class InitializationAnnotatedTypeFactory method getUnderInitializationAnnotationOfSuperType.

/**
 * Returns an {@link UnderInitialization} annotation that has the superclass of {@code type} as
 * type frame.
 *
 * @param type a type
 * @return true an {@link UnderInitialization} for the supertype of {@code type}
 */
protected AnnotationMirror getUnderInitializationAnnotationOfSuperType(TypeMirror type) {
    // Find supertype if possible.
    AnnotationMirror annotation;
    List<? extends TypeMirror> superTypes = types.directSupertypes(type);
    TypeMirror superClass = null;
    for (TypeMirror superType : superTypes) {
        ElementKind kind = types.asElement(superType).getKind();
        if (kind == ElementKind.CLASS) {
            superClass = superType;
            break;
        }
    }
    // Create annotation.
    if (superClass != null) {
        annotation = createUnderInitializationAnnotation(superClass);
    } else {
        // Use Object as a valid super-class.
        annotation = createUnderInitializationAnnotation(Object.class);
    }
    return annotation;
}
Also used : ElementKind(javax.lang.model.element.ElementKind) AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror)

Example 39 with ElementKind

use of javax.lang.model.element.ElementKind in project auto by google.

the class AutoValueishProcessor method validateType.

/**
 * Validations common to all the subclasses. An {@code @AutoFoo} type must be a class, or possibly
 * an interface for {@code @AutoBuilder}. If it is a class then it must have a non-private no-arg
 * constructor. And, since we'll be generating a subclass, it can't be final.
 */
private void validateType(TypeElement type) {
    ElementKind kind = type.getKind();
    boolean kindOk = kind.equals(ElementKind.CLASS) || (appliesToInterfaces && kind.equals(ElementKind.INTERFACE));
    if (!kindOk) {
        String appliesTo = appliesToInterfaces ? "classes and interfaces" : "classes";
        errorReporter.abortWithError(type, "[%sWrongType] @%s only applies to %s", simpleAnnotationName, simpleAnnotationName, appliesTo);
    }
    checkModifiersIfNested(type);
    if (!hasVisibleNoArgConstructor(type)) {
        errorReporter.reportError(type, "[%sConstructor] @%s class must have a non-private no-arg constructor", simpleAnnotationName, simpleAnnotationName);
    }
    if (type.getModifiers().contains(Modifier.FINAL)) {
        errorReporter.abortWithError(type, "[%sFinal] @%s class must not be final", simpleAnnotationName, simpleAnnotationName);
    }
}
Also used : ElementKind(javax.lang.model.element.ElementKind)

Example 40 with ElementKind

use of javax.lang.model.element.ElementKind in project auto by google.

the class AutoBuilderProcessor method getOfClass.

private TypeElement getOfClass(TypeElement autoBuilderType, AnnotationMirror autoBuilderAnnotation) {
    TypeElement ofClassValue = findOfClassValue(autoBuilderAnnotation);
    boolean isDefault = typeUtils().isSameType(ofClassValue.asType(), javaLangVoid);
    if (!isDefault) {
        return ofClassValue;
    }
    Element enclosing = autoBuilderType.getEnclosingElement();
    ElementKind enclosingKind = enclosing.getKind();
    if (enclosing.getKind() != ElementKind.CLASS && enclosingKind != ELEMENT_KIND_RECORD) {
        errorReporter().abortWithError(autoBuilderType, "[AutoBuilderEnclosing] @AutoBuilder must specify ofClass=Something.class or it" + " must be nested inside the class to be built; actually nested inside %s %s.", Ascii.toLowerCase(enclosingKind.name()), enclosing);
    }
    return MoreElements.asType(enclosing);
}
Also used : ElementKind(javax.lang.model.element.ElementKind) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Aggregations

ElementKind (javax.lang.model.element.ElementKind)50 TypeElement (javax.lang.model.element.TypeElement)27 Element (javax.lang.model.element.Element)25 ExecutableElement (javax.lang.model.element.ExecutableElement)23 VariableElement (javax.lang.model.element.VariableElement)13 TypeMirror (javax.lang.model.type.TypeMirror)13 PackageElement (javax.lang.model.element.PackageElement)9 ArrayList (java.util.ArrayList)8 AnnotationMirror (javax.lang.model.element.AnnotationMirror)7 LinkedHashSet (java.util.LinkedHashSet)6 ClassName (com.squareup.javapoet.ClassName)5 MethodSpec (com.squareup.javapoet.MethodSpec)5 TypeName (com.squareup.javapoet.TypeName)5 HashSet (java.util.HashSet)5 Modifier (javax.lang.model.element.Modifier)5 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 TypeParameterElement (javax.lang.model.element.TypeParameterElement)4 CodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror)3