Search in sources :

Example 61 with AnnotationMirror

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);
}
Also used : Messager(javax.annotation.processing.Messager) HashMap(java.util.HashMap) FieldDefinition(org.kie.workbench.common.forms.adf.definitions.annotations.metaModel.FieldDefinition) MirroredTypeException(javax.lang.model.type.MirroredTypeException) GenerationException(org.uberfire.annotations.processors.exceptions.GenerationException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) AnnotationMirror(javax.lang.model.element.AnnotationMirror) FieldValue(org.kie.workbench.common.forms.adf.definitions.annotations.metaModel.FieldValue)

Example 62 with AnnotationMirror

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;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ArrayList(java.util.ArrayList)

Example 63 with AnnotationMirror

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());
                }
            }
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) QualifierHierarchy(org.checkerframework.framework.type.QualifierHierarchy) ClassName(org.checkerframework.dataflow.analysis.FlowExpressions.ClassName) FieldAccess(org.checkerframework.dataflow.analysis.FlowExpressions.FieldAccess) ThisReference(org.checkerframework.dataflow.analysis.FlowExpressions.ThisReference)

Example 64 with AnnotationMirror

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);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) ClassName(org.checkerframework.dataflow.analysis.FlowExpressions.ClassName) CFAbstractValue(org.checkerframework.framework.flow.CFAbstractValue) LocalVariable(org.checkerframework.dataflow.analysis.FlowExpressions.LocalVariable) CFAbstractStore(org.checkerframework.framework.flow.CFAbstractStore) FieldAccess(org.checkerframework.dataflow.analysis.FlowExpressions.FieldAccess) ThisReference(org.checkerframework.dataflow.analysis.FlowExpressions.ThisReference) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 65 with AnnotationMirror

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);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)665 TypeElement (javax.lang.model.element.TypeElement)159 ExecutableElement (javax.lang.model.element.ExecutableElement)112 TypeMirror (javax.lang.model.type.TypeMirror)99 ArrayList (java.util.ArrayList)92 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)90 Element (javax.lang.model.element.Element)87 VariableElement (javax.lang.model.element.VariableElement)79 AnnotationValue (javax.lang.model.element.AnnotationValue)73 DeclaredType (javax.lang.model.type.DeclaredType)58 List (java.util.List)52 CFValue (org.checkerframework.framework.flow.CFValue)52 Map (java.util.Map)46 HashSet (java.util.HashSet)40 JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)39 CFStore (org.checkerframework.framework.flow.CFStore)39 HashMap (java.util.HashMap)37 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)36 ExpressionTree (com.sun.source.tree.ExpressionTree)30 MethodTree (com.sun.source.tree.MethodTree)29