Search in sources :

Example 1 with UnifierWithUnconsumedStatements

use of com.google.errorprone.refaster.UStatement.UnifierWithUnconsumedStatements in project error-prone by google.

the class BlockTemplate method matchesStartingAtBeginning.

private Choice<List<BlockTemplateMatch>> matchesStartingAtBeginning(final JCBlock block, final int offset, final ImmutableList<? extends StatementTree> statements, final Context context) {
    if (statements.isEmpty()) {
        return Choice.none();
    }
    final JCStatement firstStatement = (JCStatement) statements.get(0);
    Choice<UnifierWithUnconsumedStatements> choice = Choice.of(UnifierWithUnconsumedStatements.create(new Unifier(context), statements));
    for (UStatement templateStatement : templateStatements()) {
        choice = choice.thenChoose(templateStatement);
    }
    return choice.thenChoose(new Function<UnifierWithUnconsumedStatements, Choice<List<BlockTemplateMatch>>>() {

        @Override
        public Choice<List<BlockTemplateMatch>> apply(UnifierWithUnconsumedStatements state) {
            Unifier unifier = state.unifier();
            Inliner inliner = unifier.createInliner();
            try {
                Optional<Unifier> checkedUnifier = typecheck(unifier, inliner, new Warner(firstStatement), expectedTypes(inliner), actualTypes(inliner));
                if (checkedUnifier.isPresent()) {
                    int consumedStatements = statements.size() - state.unconsumedStatements().size();
                    BlockTemplateMatch match = new BlockTemplateMatch(block, checkedUnifier.get(), offset, offset + consumedStatements);
                    return matchesStartingAnywhere(block, offset + consumedStatements, statements.subList(consumedStatements, statements.size()), context).transform(prepend(match));
                }
            } catch (CouldNotResolveImportException e) {
            // fall through
            }
            return Choice.none();
        }
    });
}
Also used : Optional(com.google.common.base.Optional) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) UnifierWithUnconsumedStatements(com.google.errorprone.refaster.UStatement.UnifierWithUnconsumedStatements) Warner(com.sun.tools.javac.util.Warner)

Example 2 with UnifierWithUnconsumedStatements

use of com.google.errorprone.refaster.UStatement.UnifierWithUnconsumedStatements in project error-prone by google.

the class UBlank method apply.

@Override
public Choice<UnifierWithUnconsumedStatements> apply(final UnifierWithUnconsumedStatements state) {
    int goodIndex = 0;
    while (goodIndex < state.unconsumedStatements().size()) {
        StatementTree stmt = state.unconsumedStatements().get(goodIndex);
        if (firstNonNull(FORBIDDEN_REFERENCE_SCANNER.scan(stmt, state.unifier()), false) && ControlFlowVisitor.INSTANCE.visitStatement(stmt) == ControlFlowVisitor.Result.NEVER_EXITS) {
            break;
        } else {
            goodIndex++;
        }
    }
    Collection<Integer> breakPoints = ContiguousSet.create(Range.closed(0, goodIndex), DiscreteDomain.integers());
    return Choice.from(breakPoints).transform(new Function<Integer, UnifierWithUnconsumedStatements>() {

        @Override
        public UnifierWithUnconsumedStatements apply(Integer k) {
            Unifier unifier = state.unifier().fork();
            unifier.putBinding(key(), state.unconsumedStatements().subList(0, k));
            List<? extends StatementTree> remaining = state.unconsumedStatements().subList(k, state.unconsumedStatements().size());
            return UnifierWithUnconsumedStatements.create(unifier, remaining);
        }
    });
}
Also used : StatementTree(com.sun.source.tree.StatementTree) UnifierWithUnconsumedStatements(com.google.errorprone.refaster.UStatement.UnifierWithUnconsumedStatements) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

UnifierWithUnconsumedStatements (com.google.errorprone.refaster.UStatement.UnifierWithUnconsumedStatements)2 Optional (com.google.common.base.Optional)1 ImmutableList (com.google.common.collect.ImmutableList)1 StatementTree (com.sun.source.tree.StatementTree)1 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)1 Warner (com.sun.tools.javac.util.Warner)1 List (java.util.List)1