Search in sources :

Example 1 with Iterables.getOnlyElement

use of com.google.common.collect.Iterables.getOnlyElement in project error-prone by google.

the class FormatStringValidation method validate.

@Nullable
public static ValidationResult validate(Collection<? extends ExpressionTree> arguments, final VisitorState state) {
    Deque<ExpressionTree> args = new ArrayDeque<>(arguments);
    Stream<String> formatStrings = constValues(args.removeFirst());
    if (formatStrings == null) {
        return null;
    }
    // Bail out, since we don't know what the actual argument types are.
    if (args.size() == 1) {
        Type type = ASTHelpers.getType(Iterables.getOnlyElement(args));
        if (type instanceof Type.ArrayType && ASTHelpers.isSameType(((Type.ArrayType) type).elemtype, state.getSymtab().objectType, state)) {
            return null;
        }
    }
    Iterable<Object> instances = Iterables.transform(args, new Function<ExpressionTree, Object>() {

        @Override
        public Object apply(ExpressionTree input) {
            try {
                return getInstance(input, state);
            } catch (Throwable t) {
                // ignore symbol completion failures
                return null;
            }
        }
    });
    return formatStrings.map(formatString -> validate(formatString, instances)).filter(x -> x != null).findFirst().orElse(null);
}
Also used : Formatter(edu.umd.cs.findbugs.formatStringChecker.Formatter) Iterables(com.google.common.collect.Iterables) Date(java.util.Date) LocalDateTime(java.time.LocalDateTime) FormatFlagsConversionMismatchException(java.util.FormatFlagsConversionMismatchException) Deque(java.util.Deque) MissingFormatArgumentException(java.util.MissingFormatArgumentException) TemporalAccessor(java.time.temporal.TemporalAccessor) VisitorState(com.google.errorprone.VisitorState) BigDecimal(java.math.BigDecimal) IllegalFormatPrecisionException(java.util.IllegalFormatPrecisionException) Calendar(java.util.Calendar) IllegalFormatWidthException(java.util.IllegalFormatWidthException) BigInteger(java.math.BigInteger) Tree(com.sun.source.tree.Tree) Nullable(javax.annotation.Nullable) DuplicateFormatFlagsException(java.util.DuplicateFormatFlagsException) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) Function(com.google.common.base.Function) GregorianCalendar(java.util.GregorianCalendar) UnknownFormatFlagsException(java.util.UnknownFormatFlagsException) ExpressionTree(com.sun.source.tree.ExpressionTree) IllegalFormatFlagsException(java.util.IllegalFormatFlagsException) MissingFormatWidthException(java.util.MissingFormatWidthException) Collection(java.util.Collection) UnknownFormatConversionException(java.util.UnknownFormatConversionException) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) IllegalFormatConversionException(java.util.IllegalFormatConversionException) Instant(java.time.Instant) ZoneId(java.time.ZoneId) TypeKind(javax.lang.model.type.TypeKind) ExtraFormatArgumentsException(edu.umd.cs.findbugs.formatStringChecker.ExtraFormatArgumentsException) Types(com.sun.tools.javac.code.Types) Stream(java.util.stream.Stream) IllegalFormatCodePointException(java.util.IllegalFormatCodePointException) AutoValue(com.google.auto.value.AutoValue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) ASTHelpers(com.google.errorprone.util.ASTHelpers) Type(com.sun.tools.javac.code.Type) ArrayDeque(java.util.ArrayDeque) Type(com.sun.tools.javac.code.Type) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) Nullable(javax.annotation.Nullable)

Example 2 with Iterables.getOnlyElement

use of com.google.common.collect.Iterables.getOnlyElement in project auto by google.

the class MoreTypesIsTypeOfTest method extractReturnTypeFromHolder.

// Utility methods for this test.
private TypeMirror extractReturnTypeFromHolder(TypeElement typeElement) {
    Element element = Iterables.getOnlyElement(typeElement.getEnclosedElements());
    TypeMirror arrayType = MoreElements.asExecutable(element).getReturnType();
    return arrayType;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement)

Aggregations

Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)2 AutoValue (com.google.auto.value.AutoValue)1 Function (com.google.common.base.Function)1 Iterables (com.google.common.collect.Iterables)1 VisitorState (com.google.errorprone.VisitorState)1 ASTHelpers (com.google.errorprone.util.ASTHelpers)1 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 Tree (com.sun.source.tree.Tree)1 Type (com.sun.tools.javac.code.Type)1 Types (com.sun.tools.javac.code.Types)1 ExtraFormatArgumentsException (edu.umd.cs.findbugs.formatStringChecker.ExtraFormatArgumentsException)1 Formatter (edu.umd.cs.findbugs.formatStringChecker.Formatter)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Instant (java.time.Instant)1 LocalDateTime (java.time.LocalDateTime)1 ZoneId (java.time.ZoneId)1 TemporalAccessor (java.time.temporal.TemporalAccessor)1 ArrayDeque (java.util.ArrayDeque)1