本文整理汇总了Java中com.google.errorprone.matchers.Matcher.matches方法的典型用法代码示例。如果您正苦于以下问题:Java Matcher.matches方法的具体用法?Java Matcher.matches怎么用?Java Matcher.matches使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.errorprone.matchers.Matcher
的用法示例。
在下文中一共展示了Matcher.matches方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getInvokeOfSafeInitMethod
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
/**
* A safe init method is an instance method that is either private or final (so no overriding is
* possible)
*
* @param stmt the statement
* @param enclosingClassSymbol symbol for enclosing constructor / initializer
* @param state visitor state
* @return element of safe init function if stmt invokes that function; null otherwise
*/
@Nullable
private Element getInvokeOfSafeInitMethod(
StatementTree stmt, final Symbol.ClassSymbol enclosingClassSymbol, VisitorState state) {
Matcher<ExpressionTree> invokeMatcher =
(expressionTree, s) -> {
if (!(expressionTree instanceof MethodInvocationTree)) {
return false;
}
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expressionTree;
Symbol.MethodSymbol symbol = ASTHelpers.getSymbol(methodInvocationTree);
Set<Modifier> modifiers = symbol.getModifiers();
if ((symbol.isPrivate() || modifiers.contains(Modifier.FINAL)) && !symbol.isStatic()) {
// check it's the same class (could be an issue with inner classes)
if (ASTHelpers.enclosingClass(symbol).equals(enclosingClassSymbol)) {
// make sure the receiver is 'this'
ExpressionTree receiver = ASTHelpers.getReceiver(expressionTree);
return receiver == null || isThisIdentifier(receiver);
}
}
return false;
};
if (stmt.getKind().equals(EXPRESSION_STATEMENT)) {
ExpressionTree expression = ((ExpressionStatementTree) stmt).getExpression();
if (invokeMatcher.matches(expression, state)) {
return ASTHelpers.getSymbol(expression);
}
}
return null;
}
示例2: anyCatchBlockMatches
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
private boolean anyCatchBlockMatches(TryTree tree, VisitorState state, Matcher<Tree> matcher) {
for (CatchTree catchTree : tree.getCatches()) {
if (matcher.matches(catchTree.getBlock(), state)) {
return true;
}
}
return false;
}
示例3: matchIf
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
@Override
public Description matchIf(IfTree ifTree, VisitorState visitorState) {
ExpressionTree expressionTree = stripParentheses(ifTree.getCondition());
if (expressionTree instanceof InstanceOfTree) {
InstanceOfTree instanceOfTree = (InstanceOfTree) expressionTree;
if (!(instanceOfTree.getExpression() instanceof IdentifierTree)) {
return Description.NO_MATCH;
}
Matcher<Tree> assignmentTreeMatcher =
new AssignmentTreeMatcher(instanceOfTree.getExpression());
Matcher<Tree> containsAssignmentTreeMatcher = contains(assignmentTreeMatcher);
if (containsAssignmentTreeMatcher.matches(ifTree, visitorState)) {
return Description.NO_MATCH;
}
// set expression and type to look for in matcher
Matcher<Tree> nestedInstanceOfMatcher =
new NestedInstanceOfMatcher(instanceOfTree.getExpression(), instanceOfTree.getType());
Matcher<Tree> containsNestedInstanceOfMatcher = contains(nestedInstanceOfMatcher);
if (containsNestedInstanceOfMatcher.matches(ifTree.getThenStatement(), visitorState)) {
return describeMatch(ifTree);
}
}
return Description.NO_MATCH;
}
示例4: matchMethodInvocation
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!SET_DEFAULT.matches(tree, state)) {
return NO_MATCH;
}
Type type = ASTHelpers.getType(tree.getArguments().get(0));
if (type == null) {
return NO_MATCH;
}
Type classType = state.getTypes().asSuper(type, state.getSymtab().classType.asElement());
if (classType == null || classType.getTypeArguments().isEmpty()) {
return NO_MATCH;
}
String defaultTypeName =
getOnlyElement(classType.getTypeArguments()).asElement().getQualifiedName().toString();
if (!DEFAULTS.containsKey(defaultTypeName)) {
return NO_MATCH;
}
Matcher<ExpressionTree> defaultType = DEFAULTS.get(defaultTypeName);
if (!defaultType.matches(tree.getArguments().get(1), state)) {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
ExpressionTree receiver = ASTHelpers.getReceiver(tree);
Tree ancestor = state.getPath().getParentPath().getLeaf();
if (ancestor instanceof ExpressionStatementTree) {
description.addFix(SuggestedFix.delete(ancestor));
} else if (receiver != null) {
description.addFix(
SuggestedFix.replace(state.getEndPosition(receiver), state.getEndPosition(tree), ""));
}
return description.build();
}
示例5: expressionFromUnaryTree
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
/** Extracts the expression from a UnaryTree and applies a matcher to it. */
private static Matcher<UnaryTree> expressionFromUnaryTree(
final Matcher<ExpressionTree> exprMatcher) {
return new Matcher<UnaryTree>() {
@Override
public boolean matches(UnaryTree tree, VisitorState state) {
return exprMatcher.matches(tree.getExpression(), state);
}
};
}
示例6: variableFromCompoundAssignmentTree
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
/** Extracts the variable from a CompoundAssignmentTree and applies a matcher to it. */
private static Matcher<CompoundAssignmentTree> variableFromCompoundAssignmentTree(
final Matcher<ExpressionTree> exprMatcher) {
return new Matcher<CompoundAssignmentTree>() {
@Override
public boolean matches(CompoundAssignmentTree tree, VisitorState state) {
return exprMatcher.matches(tree.getVariable(), state);
}
};
}
示例7: variableFromAssignmentTree
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
/** Extracts the variable from an AssignmentTree and applies a matcher to it. */
private static Matcher<AssignmentTree> variableFromAssignmentTree(
final Matcher<ExpressionTree> exprMatcher) {
return new Matcher<AssignmentTree>() {
@Override
public boolean matches(AssignmentTree tree, VisitorState state) {
return exprMatcher.matches(tree.getVariable(), state);
}
};
}
示例8: getMethod
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
private static MethodTree getMethod(
Matcher<MethodTree> methodMatcher, ClassTree classTree, VisitorState state) {
for (Tree member : classTree.getMembers()) {
if (member instanceof MethodTree) {
MethodTree memberTree = (MethodTree) member;
if (methodMatcher.matches(memberTree, state)) {
return memberTree;
}
}
}
return null;
}
示例9: anyOfIterable
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
private static <T extends Tree> Matcher<T> anyOfIterable(Iterable<Matcher<T>> matchers) {
final ImmutableList<Matcher<T>> copyOfMatchers = ImmutableList.copyOf(matchers);
return new Matcher<T>() {
@Override
public boolean matches(T t, VisitorState state) {
for (Matcher<? super T> matcher : copyOfMatchers) {
if (matcher.matches(t, state)) {
return true;
}
}
return false;
}
};
}
示例10: matcherAsPredicate
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
private static <T extends Tree> Predicate<T> matcherAsPredicate(
final Matcher<? super T> matcher, final VisitorState state) {
return new Predicate<T>() {
@Override
public boolean apply(T t) {
return matcher.matches(t, state);
}
};
}
示例11: matchMethod
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
/**
* Matches if:
* 1) Method's name begins with misspelled variation of "test".
* 2) Method is public, returns void, and has no parameters.
* 3) Enclosing class is JUnit3 test (extends TestCase, has no RunWith annotation,
* and is not abstract).
*/
@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
@SuppressWarnings("unchecked")
Matcher<MethodTree> methodMatcher = allOf(
not(methodNameStartsWith("test")),
Matchers.<MethodTree>hasModifier(Modifier.PUBLIC),
methodReturns(VOID_TYPE),
methodHasParameters(),
enclosingClass(isJUnit3TestClass));
if (!methodMatcher.matches(methodTree, state)) {
return Description.NO_MATCH;
}
String name = methodTree.getName().toString();
String fixedName;
// regex.Matcher class name collides with errorprone.Matcher
java.util.regex.Matcher matcher = MISSPELLED_NAME.matcher(name);
if (matcher.lookingAt()) {
fixedName = matcher.replaceFirst("test");
} else if (wouldRunInJUnit4.matches(methodTree, state)) {
fixedName = "test" + name.substring(0, 1).toUpperCase() + name.substring(1);
} else {
return Description.NO_MATCH;
}
// We don't have start position for a method symbol, so we replace everything between result
// type and body.
JCMethodDecl decl = (JCMethodDecl) methodTree;
Fix fix = new SuggestedFix().replace(
decl.restype.getStartPosition() + 4, decl.body.getStartPosition(), " " + fixedName + "() ");
return describeMatch(methodTree, fix);
}
示例12: expressionFromUnaryTree
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
/**
* Extracts the expression from a UnaryTree and applies a matcher to it.
*/
private static Matcher<UnaryTree> expressionFromUnaryTree(
final Matcher <ExpressionTree> exprMatcher) {
return new Matcher<UnaryTree>() {
@Override
public boolean matches(UnaryTree tree, VisitorState state) {
return exprMatcher.matches(tree.getExpression(), state);
}
};
}
示例13: expressionFromAssignmentTree
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
/**
* Extracts the expression from an AssignmentTree and applies a matcher to it.
*/
private static Matcher<AssignmentTree> expressionFromAssignmentTree(
final Matcher <ExpressionTree> exprMatcher) {
return new Matcher<AssignmentTree>() {
@Override
public boolean matches(AssignmentTree tree, VisitorState state) {
return exprMatcher.matches(tree.getExpression(), state);
}
};
}
示例14: variableFromCompoundAssignmentTree
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
/**
* Extracts the variable from a CompoundAssignmentTree and applies a matcher to it.
*/
private static Matcher<CompoundAssignmentTree> variableFromCompoundAssignmentTree(
final Matcher <ExpressionTree> exprMatcher) {
return new Matcher<CompoundAssignmentTree>() {
@Override
public boolean matches(CompoundAssignmentTree tree, VisitorState state) {
return exprMatcher.matches(tree.getVariable(), state);
}
};
}
示例15: variableFromAssignmentTree
import com.google.errorprone.matchers.Matcher; //导入方法依赖的package包/类
/**
* Extracts the variable from an AssignmentTree and applies a matcher to it.
*/
private static Matcher<AssignmentTree> variableFromAssignmentTree(
final Matcher <ExpressionTree> exprMatcher) {
return new Matcher<AssignmentTree>() {
@Override
public boolean matches(AssignmentTree tree, VisitorState state) {
return exprMatcher.matches(tree.getVariable(), state);
}
};
}