com.google.errorprone.refaster.UAnyOf Maven / Gradle / Ivy
/*
* Copyright 2013 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.errorprone.refaster;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.Tree;
import com.sun.source.tree.TreeVisitor;
import com.sun.tools.javac.tree.JCTree.JCExpression;
/**
* {@code UExpression} allowing a match against any of a list of expressions.
*
* @author [email protected] (Louis Wasserman)
*/
@AutoValue
public abstract class UAnyOf extends UExpression {
public static UAnyOf create(UExpression... expressions) {
return create(ImmutableList.copyOf(expressions));
}
public static UAnyOf create(Iterable expressions) {
return new AutoValue_UAnyOf(ImmutableList.copyOf(expressions));
}
abstract ImmutableList expressions();
@Override
public UExpression negate() {
ImmutableList.Builder negations = ImmutableList.builder();
for (UExpression expression : expressions()) {
negations.add(expression.negate());
}
return create(negations.build());
}
@Override
protected Choice defaultAction(final Tree tree, final Unifier unifier) {
return Choice.from(expressions())
.thenChoose(
(UExpression expression) ->
expression.unify(ASTHelpers.stripParentheses(tree), unifier.fork()));
}
@Override
public JCExpression inline(Inliner inliner) throws CouldNotResolveImportException {
throw new UnsupportedOperationException("anyOf should not appear in an @AfterTemplate");
}
@Override
public R accept(TreeVisitor visitor, D data) {
return expressions().get(0).accept(visitor, data);
}
@Override
public Kind getKind() {
return Kind.OTHER;
}
}