use of javax.lang.model.element.AnnotationMirror in project kie-wb-common by kiegroup.
the class FormDefinitionsProcessor method processFieldDefinition.
private void processFieldDefinition(TypeElement fieldDefinitionElement) throws Exception {
final Messager messager = processingEnv.getMessager();
messager.printMessage(Diagnostic.Kind.NOTE, "Discovered FieldDefinition class [" + fieldDefinitionElement.getSimpleName() + "]");
Collection<FieldInfo> fieldInfos = extractFieldInfos(fieldDefinitionElement, null);
String modelClassName = fieldDefinitionElement.getQualifiedName().toString();
String fieldModifierName = fixClassName(modelClassName) + "_FieldStatusModifier";
Map<String, String> fieldDefinition = new HashMap<>();
fieldDefinition.put("className", modelClassName);
fieldDefinition.put("fieldModifierName", fieldModifierName);
Map<String, Object> templateContext = new HashMap<>();
templateContext.put("modelClassName", modelClassName);
templateContext.put("fieldModifierName", fieldModifierName);
FieldDefinition fieldDefinitionAnnotation = fieldDefinitionElement.getAnnotation(FieldDefinition.class);
for (FieldInfo fieldInfo : fieldInfos) {
AnnotationMirror annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldValue.class.getName());
if (annotation != null) {
if (fieldDefinition.containsKey("value")) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldValue");
}
if (fieldInfo.getter == null || fieldInfo.setter == null) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldValue should have setter & getter");
}
fieldDefinition.put("value", fieldInfo.fieldElement.getSimpleName().toString());
} else {
annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldReadOnly.class.getName());
if (annotation != null) {
if (templateContext.containsKey("readOnly")) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldReadOnly");
}
if (!fieldInfo.fieldElement.asType().getKind().equals(TypeKind.BOOLEAN) && !fieldInfo.fieldElement.asType().toString().equals(Boolean.class.getName())) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldReadOnly must be boolean or Boolean");
}
if (fieldInfo.getter == null) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldReadOnly should have getter");
}
templateContext.put("readOnly", fieldInfo.getter);
}
annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldRequired.class.getName());
if (annotation != null) {
if (templateContext.containsKey("required")) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldRequired");
}
if (!fieldInfo.fieldElement.asType().getKind().equals(TypeKind.BOOLEAN) && !fieldInfo.fieldElement.asType().toString().equals(Boolean.class.getName())) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldRequired must be boolean or Boolean");
}
if (fieldInfo.getter == null) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldRequired should have getter");
}
templateContext.put("required", fieldInfo.getter);
}
if (fieldDefinitionAnnotation.i18nMode().equals(I18nMode.OVERRIDE)) {
annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldLabel.class.getName());
if (annotation != null) {
if (templateContext.containsKey("label")) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldLabel");
}
if (!fieldInfo.fieldElement.asType().toString().equals(String.class.getName())) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldLabel must be a String");
}
if (fieldInfo.getter == null) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldLabel should have getter");
}
templateContext.put("label", fieldInfo.getter);
}
annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldHelp.class.getName());
if (annotation != null) {
if (templateContext.containsKey("helpMessage")) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it has more than one field marked as @FieldHelp");
}
if (!fieldInfo.fieldElement.asType().toString().equals(String.class.getName())) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldHelp must be a String");
}
if (fieldInfo.getter == null) {
throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldHelp should have getter");
}
templateContext.put("helpMessage", fieldInfo.getter);
}
}
}
}
StringBuffer source = writeTemplate("templates/FieldDefinitionModifier.ftl", templateContext);
fieldDefinition.put("sourceCode", source.toString());
context.getFieldDefinitions().add(fieldDefinition);
}
use of javax.lang.model.element.AnnotationMirror in project checker-framework by typetools.
the class UpperBoundTransfer method visitLengthAccess.
/**
* Computes a type of a sequence length access. This is case 18.
*
* @param n sequence length access node
*/
private TransferResult<CFValue, CFStore> visitLengthAccess(Node n, TransferInput<CFValue, CFStore> in, Receiver sequenceRec, Tree sequenceTree) {
if (sequenceTree == null) {
return null;
}
// Look up the SameLen type of the sequence.
AnnotationMirror sameLenAnno = atypeFactory.sameLenAnnotationFromTree(sequenceTree);
List<String> sameLenSequences = sameLenAnno == null ? new ArrayList<>() : IndexUtil.getValueOfAnnotationWithStringArgument(sameLenAnno);
if (!sameLenSequences.contains(sequenceRec.toString())) {
sameLenSequences.add(sequenceRec.toString());
}
ArrayList<String> offsets = new ArrayList<>(sameLenSequences.size());
for (String s : sameLenSequences) {
offsets.add("-1");
}
if (CFAbstractStore.canInsertReceiver(sequenceRec)) {
UBQualifier qualifier = UBQualifier.createUBQualifier(sameLenSequences, offsets);
UBQualifier previous = getUBQualifier(n, in);
return createTransferResult(n, in, qualifier.glb(previous));
}
return null;
}
use of javax.lang.model.element.AnnotationMirror in project checker-framework by typetools.
the class InitializationStore method insertValue.
/**
* {@inheritDoc}
*
* <p>If the receiver is a field, and has an invariant annotation, then it can be considered
* initialized.
*/
@Override
public void insertValue(Receiver r, V value) {
if (value == null) {
// top and top is also the default value.
return;
}
InitializationAnnotatedTypeFactory<?, ?, ?, ?> atypeFactory = (InitializationAnnotatedTypeFactory<?, ?, ?, ?>) analysis.getTypeFactory();
QualifierHierarchy qualifierHierarchy = atypeFactory.getQualifierHierarchy();
AnnotationMirror invariantAnno = atypeFactory.getFieldInvariantAnnotation();
// Remember fields that have the 'invariant' annotation in the store.
if (r instanceof FieldAccess) {
FieldAccess fieldAccess = (FieldAccess) r;
if (!fieldValues.containsKey(r)) {
Set<AnnotationMirror> declaredAnnos = atypeFactory.getAnnotatedType(fieldAccess.getField()).getAnnotations();
if (AnnotationUtils.containsSame(declaredAnnos, invariantAnno)) {
if (!invariantFields.containsKey(fieldAccess)) {
invariantFields.put(fieldAccess, analysis.createSingleAnnotationValue(invariantAnno, r.getType()));
}
}
}
}
super.insertValue(r, value);
for (AnnotationMirror a : value.getAnnotations()) {
if (qualifierHierarchy.isSubtype(a, invariantAnno)) {
if (r instanceof FieldAccess) {
FieldAccess fa = (FieldAccess) r;
if (fa.getReceiver() instanceof ThisReference || fa.getReceiver() instanceof ClassName) {
addInitializedField(fa.getField());
}
}
}
}
}
use of javax.lang.model.element.AnnotationMirror in project checker-framework by typetools.
the class InitializationVisitor method checkContract.
@Override
protected boolean checkContract(Receiver expr, AnnotationMirror necessaryAnnotation, AnnotationMirror inferredAnnotation, CFAbstractStore<?, ?> store) {
// also use the information about initialized fields to check contracts
final AnnotationMirror invariantAnno = atypeFactory.getFieldInvariantAnnotation();
if (atypeFactory.getQualifierHierarchy().isSubtype(invariantAnno, necessaryAnnotation)) {
if (expr instanceof FieldAccess) {
FieldAccess fa = (FieldAccess) expr;
if (fa.getReceiver() instanceof ThisReference || fa.getReceiver() instanceof ClassName) {
@SuppressWarnings("unchecked") Store s = (Store) store;
if (s.isFieldInitialized(fa.getField())) {
AnnotatedTypeMirror fieldType = atypeFactory.getAnnotatedType(fa.getField());
// is this an invariant-field?
if (AnnotationUtils.containsSame(fieldType.getAnnotations(), invariantAnno)) {
return true;
}
}
} else {
Set<AnnotationMirror> recvAnnoSet;
@SuppressWarnings("unchecked") Value value = (Value) store.getValue(fa.getReceiver());
if (value != null) {
recvAnnoSet = value.getAnnotations();
} else if (fa.getReceiver() instanceof LocalVariable) {
Element elem = ((LocalVariable) fa.getReceiver()).getElement();
AnnotatedTypeMirror recvType = atypeFactory.getAnnotatedType(elem);
recvAnnoSet = recvType.getAnnotations();
} else {
// Is there anything better we could do?
return false;
}
boolean isRecvCommitted = false;
for (AnnotationMirror anno : recvAnnoSet) {
if (atypeFactory.isCommitted(anno)) {
isRecvCommitted = true;
}
}
AnnotatedTypeMirror fieldType = atypeFactory.getAnnotatedType(fa.getField());
// has the invariant type.
if (isRecvCommitted && AnnotationUtils.containsSame(fieldType.getAnnotations(), invariantAnno)) {
return true;
}
}
}
}
return super.checkContract(expr, necessaryAnnotation, inferredAnnotation, store);
}
use of javax.lang.model.element.AnnotationMirror in project checker-framework by typetools.
the class NullnessAnnotatedTypeFactory method hasFieldInvariantAnnotation.
/**
* {@inheritDoc}
*
* <p>In other words, is the lower bound @NonNull?
*
* @param type of field that might have invariant annotation
* @return whether or not type has the invariant annotation
*/
@Override
protected boolean hasFieldInvariantAnnotation(AnnotatedTypeMirror type) {
AnnotationMirror invariant = getFieldInvariantAnnotation();
Set<AnnotationMirror> lowerBounds = AnnotatedTypes.findEffectiveLowerBoundAnnotations(qualHierarchy, type);
return AnnotationUtils.containsSame(lowerBounds, invariant);
}
Aggregations