Search in sources :

Example 31 with AnnotationBuilder

use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.

the class InitializationAnnotatedTypeFactory method createFreeAnnotation.

/**
 * Returns a {@link UnderInitialization} annotation with a given type frame.
 */
public AnnotationMirror createFreeAnnotation(TypeMirror typeFrame) {
    assert typeFrame != null;
    assert useFbc : "The rawness type system does not have a @UnderInitialization annotation.";
    AnnotationBuilder builder = new AnnotationBuilder(processingEnv, UnderInitialization.class);
    builder.setValue("value", typeFrame);
    return builder.build();
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Example 32 with AnnotationBuilder

use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.

the class LockAnnotatedTypeFactory method createGuardedByAnnotationMirror.

/**
 * @param values a list of lock expressions
 * @return an AnnotationMirror corresponding to @GuardedBy(values)
 */
private AnnotationMirror createGuardedByAnnotationMirror(List<String> values) {
    AnnotationBuilder builder = new AnnotationBuilder(getProcessingEnv(), GuardedBy.class);
    builder.setValue("value", values.toArray());
    // Return the resulting AnnotationMirror
    return builder.build();
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Example 33 with AnnotationBuilder

use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.

the class FormatterTreeUtil method categoriesToFormatAnnotation.

/**
 * Takes a list of ConversionCategory elements, and returns a syntax tree element that
 * represents a {@link Format} annotation with the list as value.
 */
public AnnotationMirror categoriesToFormatAnnotation(ConversionCategory[] args) {
    AnnotationBuilder builder = new AnnotationBuilder(processingEnv, Format.class.getCanonicalName());
    builder.setValue("value", args);
    return builder.build();
}
Also used : InvalidFormat(org.checkerframework.checker.formatter.qual.InvalidFormat) Format(org.checkerframework.checker.formatter.qual.Format) ReturnsFormat(org.checkerframework.checker.formatter.qual.ReturnsFormat) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder)

Example 34 with AnnotationBuilder

use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.

the class I18nFormatterTransfer method visitMethodInvocation.

@Override
public TransferResult<CFValue, CFStore> visitMethodInvocation(MethodInvocationNode node, TransferInput<CFValue, CFStore> in) {
    I18nFormatterAnnotatedTypeFactory atypeFactory = (I18nFormatterAnnotatedTypeFactory) analysis.getTypeFactory();
    TransferResult<CFValue, CFStore> result = super.visitMethodInvocation(node, in);
    I18nFormatterTreeUtil tu = atypeFactory.treeUtil;
    // If hasFormat is called, make sure that the format string is annotated correctly
    if (tu.isHasFormatCall(node, atypeFactory)) {
        CFStore thenStore = result.getRegularStore();
        CFStore elseStore = thenStore.copy();
        ConditionalTransferResult<CFValue, CFStore> newResult = new ConditionalTransferResult<>(result.getResultValue(), thenStore, elseStore);
        Result<I18nConversionCategory[]> cats = tu.getHasFormatCallCategories(node);
        if (cats.value() == null) {
            tu.failure(cats, "i18nformat.indirect.arguments");
        } else {
            Receiver firstParam = FlowExpressions.internalReprOf(atypeFactory, node.getArgument(0));
            AnnotationMirror anno = atypeFactory.treeUtil.categoriesToFormatAnnotation(cats.value());
            thenStore.insertValue(firstParam, anno);
        }
        return newResult;
    }
    // If isFormat is called, annotate the format string with I18nInvalidFormat
    if (tu.isIsFormatCall(node, atypeFactory)) {
        CFStore thenStore = result.getRegularStore();
        CFStore elseStore = thenStore.copy();
        ConditionalTransferResult<CFValue, CFStore> newResult = new ConditionalTransferResult<>(result.getResultValue(), thenStore, elseStore);
        Receiver firstParam = FlowExpressions.internalReprOf(atypeFactory, node.getArgument(0));
        AnnotationBuilder builder = new AnnotationBuilder(tu.processingEnv, I18nInvalidFormat.class.getCanonicalName());
        // No need to set a value of @I18nInvalidFormat
        builder.setValue("value", "");
        elseStore.insertValue(firstParam, builder.build());
        return newResult;
    }
    // corresponding key's value
    if (tu.isMakeFormatCall(node, atypeFactory)) {
        Result<I18nConversionCategory[]> cats = tu.makeFormatCallCategories(node, atypeFactory);
        if (cats.value() == null) {
            tu.failure(cats, "i18nformat.key.not.found");
        } else {
            AnnotationMirror anno = atypeFactory.treeUtil.categoriesToFormatAnnotation(cats.value());
            CFValue newResultValue = analysis.createSingleAnnotationValue(anno, result.getResultValue().getUnderlyingType());
            return new RegularTransferResult<>(newResultValue, result.getRegularStore());
        }
    }
    return result;
}
Also used : CFStore(org.checkerframework.framework.flow.CFStore) ConditionalTransferResult(org.checkerframework.dataflow.analysis.ConditionalTransferResult) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) CFValue(org.checkerframework.framework.flow.CFValue) AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) I18nInvalidFormat(org.checkerframework.checker.i18nformatter.qual.I18nInvalidFormat) RegularTransferResult(org.checkerframework.dataflow.analysis.RegularTransferResult)

Example 35 with AnnotationBuilder

use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.

the class I18nFormatterTreeUtil method stringToInvalidFormatAnnotation.

/**
 * Takes an invalid formatter string and returns a syntax trees element that represents a {@link
 * I18nInvalidFormat} annotation with the invalid formatter string as value.
 */
// package-private
AnnotationMirror stringToInvalidFormatAnnotation(String invalidFormatString) {
    AnnotationBuilder builder = new AnnotationBuilder(processingEnv, I18nInvalidFormat.class.getCanonicalName());
    builder.setValue("value", invalidFormatString);
    return builder.build();
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) I18nInvalidFormat(org.checkerframework.checker.i18nformatter.qual.I18nInvalidFormat)

Aggregations

AnnotationBuilder (org.checkerframework.javacutil.AnnotationBuilder)63 Test (org.junit.Test)30 AnnotationMirror (javax.lang.model.element.AnnotationMirror)11 ArrayList (java.util.ArrayList)3 InvalidFormat (org.checkerframework.checker.formatter.qual.InvalidFormat)3 I18nInvalidFormat (org.checkerframework.checker.i18nformatter.qual.I18nInvalidFormat)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Elements (javax.lang.model.util.Elements)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)2 NodeList (com.github.javaparser.ast.NodeList)1 Expression (com.github.javaparser.ast.expr.Expression)1 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)1 MemberValuePair (com.github.javaparser.ast.expr.MemberValuePair)1 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)1 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)1 Annotation (java.lang.annotation.Annotation)1 List (java.util.List)1 TypeElement (javax.lang.model.element.TypeElement)1