Search in sources :

Example 1 with SideEffectFree

use of org.checkerframework.dataflow.qual.SideEffectFree in project checker-framework by typetools.

the class RegexUtil method regexError.

/**
 * Returns null if the argument is a syntactically valid regular expression with at least the
 * given number of groups. Otherwise returns a string describing why the argument is not a regex.
 *
 * @param s string to check for being a regular expression
 * @param groups number of groups expected
 * @return null, or a string describing why the argument is not a regex
 */
// RegexUtil;
@SuppressWarnings({ "regex", "not.sef" })
@SideEffectFree
@Nullable
public static String regexError(String s, int groups) {
    try {
        Pattern p = Pattern.compile(s);
        int actualGroups = getGroupCount(p);
        if (actualGroups < groups) {
            return regexErrorMessage(s, groups, actualGroups);
        }
    } catch (PatternSyntaxException e) {
        return e.getMessage();
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) PatternSyntaxException(java.util.regex.PatternSyntaxException) SideEffectFree(org.checkerframework.dataflow.qual.SideEffectFree) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with SideEffectFree

use of org.checkerframework.dataflow.qual.SideEffectFree in project checker-framework by typetools.

the class RegexUtil method asRegex.

/**
 * Returns the argument as a {@code @Regex(groups) String} if it is a regex with at least the
 * given number of groups, otherwise throws an error.
 *
 * <p>The purpose of this method is to suppress Regex Checker warnings. It should be very rarely
 * needed.
 *
 * @param s string to check for being a regular expression
 * @param groups number of groups expected
 * @return its argument
 * @throws Error if argument is not a regex
 */
// RegexUtil
@SuppressWarnings("regex")
@SideEffectFree
// RegexAnnotatedTypeFactory.
@Regex
public static String asRegex(String s, int groups) {
    try {
        Pattern p = Pattern.compile(s);
        int actualGroups = getGroupCount(p);
        if (actualGroups < groups) {
            throw new Error(regexErrorMessage(s, groups, actualGroups));
        }
        return s;
    } catch (PatternSyntaxException e) {
        throw new Error(e);
    }
}
Also used : Pattern(java.util.regex.Pattern) PatternSyntaxException(java.util.regex.PatternSyntaxException) SideEffectFree(org.checkerframework.dataflow.qual.SideEffectFree) Regex(org.checkerframework.checker.regex.qual.Regex)

Example 3 with SideEffectFree

use of org.checkerframework.dataflow.qual.SideEffectFree in project checker-framework by typetools.

the class RegexUtil method regexException.

/**
 * Returns null if the argument is a syntactically valid regular expression with at least the
 * given number of groups. Otherwise returns a PatternSyntaxException describing why the argument
 * is not a regex.
 *
 * @param s string to check for being a regular expression
 * @param groups number of groups expected
 * @return null, or a PatternSyntaxException describing why the argument is not a regex
 */
// RegexUtil
@SuppressWarnings("regex")
@SideEffectFree
@Nullable
public static PatternSyntaxException regexException(String s, int groups) {
    try {
        Pattern p = Pattern.compile(s);
        int actualGroups = getGroupCount(p);
        if (actualGroups < groups) {
            return new PatternSyntaxException(regexErrorMessage(s, groups, actualGroups), s, -1);
        }
    } catch (PatternSyntaxException pse) {
        return pse;
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) PatternSyntaxException(java.util.regex.PatternSyntaxException) SideEffectFree(org.checkerframework.dataflow.qual.SideEffectFree) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 4 with SideEffectFree

use of org.checkerframework.dataflow.qual.SideEffectFree in project checker-framework by typetools.

the class AnnotationUtils method toStringSimple.

/**
 * Returns a string representation of the annotation mirrors, using simple (not fully-qualified)
 * names.
 *
 * @param annos annotations to format
 * @return the string representation, using simple (not fully-qualified) names
 */
@SideEffectFree
public static String toStringSimple(Set<AnnotationMirror> annos) {
    DefaultAnnotationFormatter defaultAnnotationFormatter = new DefaultAnnotationFormatter();
    StringJoiner result = new StringJoiner(" ");
    for (AnnotationMirror am : annos) {
        result.add(defaultAnnotationFormatter.formatAnnotationMirror(am));
    }
    return result.toString();
}
Also used : CheckerFrameworkAnnotationMirror(org.checkerframework.javacutil.AnnotationBuilder.CheckerFrameworkAnnotationMirror) AnnotationMirror(javax.lang.model.element.AnnotationMirror) DefaultAnnotationFormatter(org.checkerframework.framework.util.DefaultAnnotationFormatter) StringJoiner(java.util.StringJoiner) SideEffectFree(org.checkerframework.dataflow.qual.SideEffectFree)

Example 5 with SideEffectFree

use of org.checkerframework.dataflow.qual.SideEffectFree in project checker-framework by typetools.

the class DefaultAnnotationFormatter method formatAnnotationString.

/**
 * Creates a String of each annotation in annos separated by a single space character and
 * terminated by a space character, obeying the printInvisible parameter.
 *
 * @param annos a collection of annotations to print
 * @param printInvisible whether or not to print "invisible" annotation mirrors
 * @return the list of annotations converted to a String
 */
@Override
@SideEffectFree
public String formatAnnotationString(Collection<? extends AnnotationMirror> annos, boolean printInvisible) {
    StringBuilder sb = new StringBuilder();
    for (AnnotationMirror obj : annos) {
        if (obj == null) {
            throw new BugInCF("AnnotatedTypeMirror.formatAnnotationString: found null AnnotationMirror");
        }
        if (isInvisibleQualified(obj) && !printInvisible) {
            continue;
        }
        formatAnnotationMirror(obj, sb);
        sb.append(" ");
    }
    return sb.toString();
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) BugInCF(org.checkerframework.javacutil.BugInCF) SideEffectFree(org.checkerframework.dataflow.qual.SideEffectFree)

Aggregations

SideEffectFree (org.checkerframework.dataflow.qual.SideEffectFree)5 Pattern (java.util.regex.Pattern)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 StringJoiner (java.util.StringJoiner)1 Regex (org.checkerframework.checker.regex.qual.Regex)1 DefaultAnnotationFormatter (org.checkerframework.framework.util.DefaultAnnotationFormatter)1 CheckerFrameworkAnnotationMirror (org.checkerframework.javacutil.AnnotationBuilder.CheckerFrameworkAnnotationMirror)1 BugInCF (org.checkerframework.javacutil.BugInCF)1