All Downloads are FREE. Search and download functionalities are using the official Maven repository.

checker.src.org.checkerframework.checker.regex.RegexAnnotationConverter Maven / Gradle / Ivy

Go to download

The Checker Framework enhances Java’s type system to make it more powerful and useful. This lets software developers detect and prevent errors in their Java programs. The Checker Framework includes compiler plug-ins ("checkers") that find bugs or verify their absence. It also permits you to write your own compiler plug-ins.

There is a newer version: 3.42.0
Show newest version
package org.checkerframework.checker.regex;

import org.checkerframework.checker.experimental.regex_qual.Regex;
import org.checkerframework.checker.experimental.regex_qual.RegexQualifierHierarchy;
import org.checkerframework.checker.regex.qual.ClassRegexParam;
import org.checkerframework.checker.regex.qual.MethodRegexParam;
import org.checkerframework.checker.regex.qual.MultiRegex;
import org.checkerframework.checker.regex.qual.PolyRegex;
import org.checkerframework.checker.regex.qual.Var;
import org.checkerframework.checker.regex.qual.Wild;
import org.checkerframework.javacutil.AnnotationUtils;
import org.checkerframework.javacutil.ErrorReporter;
import org.checkerframework.qualframework.poly.AnnotationConverterConfiguration;
import org.checkerframework.qualframework.poly.CombiningOperation.Lub;
import org.checkerframework.qualframework.poly.PolyQual.GroundQual;
import org.checkerframework.qualframework.poly.PolyQual.QualVar;
import org.checkerframework.qualframework.poly.QualParams;
import org.checkerframework.qualframework.poly.SimpleQualifierParameterAnnotationConverter;
import org.checkerframework.qualframework.util.ExtendedTypeMirror;

import javax.lang.model.element.AnnotationMirror;
import java.util.Arrays;
import java.util.HashSet;

/**
 * Convert {@link org.checkerframework.checker.regex.qual.Regex}
 * annotations into a {@link Regex} qualifier with support for
 * PolyRegex, Var, Wild annotations.
 *
 */
public class RegexAnnotationConverter extends SimpleQualifierParameterAnnotationConverter {

    public RegexAnnotationConverter() {
        super(new AnnotationConverterConfiguration<>(new Lub<>(new RegexQualifierHierarchy()),
                new Lub<>(new RegexQualifierHierarchy()),
                MultiRegex.class.getPackage().getName() + ".Multi",
                new HashSet<>(Arrays.asList(org.checkerframework.checker.regex.qual.Regex.class.getName())),
                null,
                ClassRegexParam.class,
                MethodRegexParam.class,
                PolyRegex.class,
                Var.class,
                Wild.class,
                Regex.TOP,
                Regex.BOTTOM,
                Regex.TOP));
    }

    /**
     * Convert @Regex(value) into a Regex qualifier.
     */
    @Override
    public Regex getQualifier(AnnotationMirror anno) {
        if (AnnotationUtils.annotationName(anno).equals(
                org.checkerframework.checker.regex.qual.Regex.class.getName())) {

            Integer value = AnnotationUtils.getElementValue(anno, "value", Integer.class, true);
            return new Regex.RegexVal(value);
        }
        return null;
    }

    /**
     * Process annotations from the old namespace.
     */
    @Override
    protected QualParams specialCaseHandle(AnnotationMirror anno) {

        if (AnnotationUtils.annotationName(anno).equals(
                org.checkerframework.checker.regex.qual.Regex.class.getName())) {

            Integer value = AnnotationUtils.getElementValue(anno, "value", Integer.class, true);
            return new QualParams<>(new GroundQual(new Regex.RegexVal(value)));

        } else if (AnnotationUtils.annotationName(anno).equals(
                org.checkerframework.checker.regex.qual.PolyRegex.class.getName())) {

            return new QualParams<>(new QualVar<>(POLY_NAME, BOTTOM, TOP));
        }

        ErrorReporter.errorAbort("Unexpected AnnotationMirror found in special case handling: " + anno);
        return null;
    }

    /**
     * This override sets up a polymorphic qualifier when the old PolyRegex annotation is used.
     */
    @Override
    protected boolean hasPolyAnnotationCheck(ExtendedTypeMirror type) {
        if (type == null) {
            return false;
        }

        for (AnnotationMirror anno : type.getAnnotationMirrors()) {
            if (AnnotationUtils.annotationName(anno).equals(org.checkerframework.checker.regex.qual.PolyRegex.class.getName())) {
                return true;
            }
        }
        return super.hasPolyAnnotationCheck(type);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy