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);
}
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);
}
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;
}
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);
}
}
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);
}
Aggregations