本文整理汇总了Java中com.google.errorprone.matchers.Matcher类的典型用法代码示例。如果您正苦于以下问题:Java Matcher类的具体用法?Java Matcher怎么用?Java Matcher使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Matcher类属于com.google.errorprone.matchers包,在下文中一共展示了Matcher类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: overridesMethodOfClass
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
private static Matcher<MethodTree> overridesMethodOfClass(final Class<?> clazz) {
checkNotNull(clazz);
return new Matcher<MethodTree>() {
@Override
public boolean matches(MethodTree tree, VisitorState state) {
MethodSymbol symbol = getSymbol(tree);
if (symbol == null) {
return false;
}
for (MethodSymbol superMethod : findSuperMethods(symbol, state.getTypes())) {
if (superMethod.owner != null
&& superMethod.owner.getQualifiedName().contentEquals(clazz.getName())) {
return true;
}
}
return false;
}
};
}
开发者ID:google,项目名称:error-prone,代码行数:20,代码来源:AbstractAsyncTypeReturnsNull.java
示例2: MatchType
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
MatchType(
String componentType,
String lifecycleMethod,
ImmutableList<Matcher<VariableTree>> lifecycleMethodParameters,
String staticMethodClass) {
this.lifecycleMethod = lifecycleMethod;
methodMatcher =
allOf(
methodIsNamed(lifecycleMethod),
methodHasParameters(lifecycleMethodParameters),
enclosingClass(isSubtypeOf(componentType)));
methodInvocationMatcher =
instanceMethod().onDescendantOf(componentType).named(lifecycleMethod);
injectMethodMatcher =
staticMethod().onClass(staticMethodClass).named("inject").withParameters(componentType);
}
开发者ID:google,项目名称:error-prone,代码行数:17,代码来源:AndroidInjectionBeforeSuper.java
示例3: receiverSameAsParentsArgument
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
private static Matcher<? super MethodInvocationTree> receiverSameAsParentsArgument() {
return new Matcher<MethodInvocationTree>() {
@Override
public boolean matches(MethodInvocationTree t, VisitorState state) {
ExpressionTree rec = ASTHelpers.getReceiver(t);
if (rec == null) {
return false;
}
if (!ASSERT_THAT.matches(rec, state)) {
return false;
}
if (!ASTHelpers.sameVariable(
getOnlyElement(((MethodInvocationTree) rec).getArguments()),
getOnlyElement(t.getArguments()))) {
return false;
}
return true;
}
};
}
开发者ID:google,项目名称:error-prone,代码行数:21,代码来源:TruthSelfEquals.java
示例4: getReferencedArgumentsP
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
private Set<Integer> getReferencedArgumentsP(String str) {
java.util.regex.Matcher matcher = printfGroup.matcher(str);
Set<Integer> set = new HashSet<Integer>();
int i = 0;
while (matcher.find()) {
// %n is line break and %% is literal percent, they don't reference parameters.
if (!matcher.group().endsWith("n") && !matcher.group().endsWith("%")) {
if (matcher.group(1) != null) {
set.add(Integer.parseInt(matcher.group(1).replaceAll("\\$", "")));
} else {
set.add(i);
i++;
}
}
}
return set;
}
开发者ID:diy1,项目名称:error-prone-aspirator,代码行数:19,代码来源:MisusedFormattingLogger.java
示例5: assignmentIncrementDecrementMatcher
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
/**
* Matches patterns like i = i + 1 and i = i - 1 in which i is volatile, and the pattern is not
* enclosed by a synchronized block.
*/
@SuppressWarnings("unchecked")
private static Matcher<AssignmentTree> assignmentIncrementDecrementMatcher(
ExpressionTree variable) {
return allOf(
variableFromAssignmentTree(
Matchers.<ExpressionTree>hasModifier(Modifier.VOLATILE)),
not(inSynchronized()),
expressionFromAssignmentTree(adaptMatcherType(ExpressionTree.class, BinaryTree.class,
allOf(
anyOf(
kindIs(Kind.PLUS),
kindIs(Kind.MINUS)),
binaryTree(
sameVariable(variable),
Matchers.<ExpressionTree>anything())))));
}
开发者ID:diy1,项目名称:error-prone-aspirator,代码行数:21,代码来源:IncrementDecrementVolatile.java
示例6: 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;
}
开发者ID:uber,项目名称:NullAway,代码行数:39,代码来源:NullAway.java
示例7: create
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
public static UMatches create(Class<? extends Matcher<? super ExpressionTree>> matcherClass,
boolean positive, UExpression expression) {
// Verify that we can instantiate the Matcher
makeMatcher(matcherClass);
return new AutoValue_UMatches(positive, matcherClass, expression);
}
开发者ID:sivakumar-kailasam,项目名称:refactor-faster,代码行数:8,代码来源:UMatches.java
示例8: matchBinaryTree
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
/**
* Given a BinaryTree to match against and a list of two matchers, applies the matchers to the
* operands in both orders. If both matchers match, returns a list with the operand that matched
* each matcher in the corresponding position.
*
* @param tree a BinaryTree AST node
* @param matchers a list of matchers
* @param state the VisitorState
* @return a list of matched operands, or null if at least one did not match
*/
public static List<ExpressionTree> matchBinaryTree(
BinaryTree tree, List<Matcher<ExpressionTree>> matchers, VisitorState state) {
ExpressionTree leftOperand = tree.getLeftOperand();
ExpressionTree rightOperand = tree.getRightOperand();
if (matchers.get(0).matches(leftOperand, state)
&& matchers.get(1).matches(rightOperand, state)) {
return Arrays.asList(leftOperand, rightOperand);
} else if (matchers.get(0).matches(rightOperand, state)
&& matchers.get(1).matches(leftOperand, state)) {
return Arrays.asList(rightOperand, leftOperand);
}
return null;
}
开发者ID:google,项目名称:error-prone,代码行数:24,代码来源:ASTHelpers.java
示例9: create
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
static PlaceholderMethod create(
CharSequence name,
UType returnType,
ImmutableMap<UVariableDecl, ImmutableClassToInstanceMap<Annotation>> parameters,
ClassToInstanceMap<Annotation> annotations) {
final boolean allowsIdentity = annotations.getInstance(Placeholder.class).allowsIdentity();
final Class<? extends Matcher<? super ExpressionTree>> matchesClass =
annotations.containsKey(Matches.class)
? UTemplater.getValue(annotations.getInstance(Matches.class))
: null;
final Class<? extends Matcher<? super ExpressionTree>> notMatchesClass =
annotations.containsKey(NotMatches.class)
? UTemplater.getValue(annotations.getInstance(NotMatches.class))
: null;
final Predicate<Tree.Kind> allowedKinds =
annotations.containsKey(OfKind.class)
? Predicates.<Tree.Kind>in(Arrays.asList(annotations.getInstance(OfKind.class).value()))
: Predicates.<Tree.Kind>alwaysTrue();
class PlaceholderMatcher implements Serializable, Matcher<ExpressionTree> {
@Override
public boolean matches(ExpressionTree t, VisitorState state) {
try {
return (allowsIdentity || !(t instanceof PlaceholderParamIdent))
&& (matchesClass == null || matchesClass.newInstance().matches(t, state))
&& (notMatchesClass == null || !notMatchesClass.newInstance().matches(t, state))
&& allowedKinds.apply(t.getKind());
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
return new AutoValue_PlaceholderMethod(
StringName.of(name),
returnType,
parameters,
new PlaceholderMatcher(),
ImmutableClassToInstanceMap.<Annotation, Annotation>copyOf(annotations));
}
开发者ID:google,项目名称:error-prone,代码行数:40,代码来源:PlaceholderMethod.java
示例10: create
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
public static UMatches create(
Class<? extends Matcher<? super ExpressionTree>> matcherClass,
boolean positive,
UExpression expression) {
// Verify that we can instantiate the Matcher
makeMatcher(matcherClass);
return new AutoValue_UMatches(positive, matcherClass, expression);
}
开发者ID:google,项目名称:error-prone,代码行数:10,代码来源:UMatches.java
示例11: 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;
}
开发者ID:google,项目名称:error-prone,代码行数:9,代码来源:MissingFail.java
示例12: getLongLiteral
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
/**
* Extracts the long literal corresponding to a given {@link LiteralTree} node from the source
* code as a string. Returns null if the source code is not available.
*/
private static String getLongLiteral(LiteralTree literalTree, VisitorState state) {
JCLiteral longLiteral = (JCLiteral) literalTree;
CharSequence sourceFile = state.getSourceCode();
if (sourceFile == null) {
return null;
}
int start = longLiteral.getStartPosition();
java.util.regex.Matcher matcher =
LONG_LITERAL_PATTERN.matcher(sourceFile.subSequence(start, sourceFile.length()));
if (matcher.lookingAt()) {
return matcher.group();
}
return null;
}
开发者ID:google,项目名称:error-prone,代码行数:19,代码来源:LongLiteralLowerCaseSuffix.java
示例13: shouldAllow
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
private static Matcher<Tree> shouldAllow(RestrictedApi api, VisitorState state) {
try {
return anyAnnotation(api.whitelistAnnotations());
} catch (MirroredTypesException e) {
return anyAnnotation(e.getTypeMirrors(), state);
}
}
开发者ID:google,项目名称:error-prone,代码行数:8,代码来源:RestrictedApiChecker.java
示例14: shouldAllowWithWarning
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
private static Matcher<Tree> shouldAllowWithWarning(RestrictedApi api, VisitorState state) {
try {
return anyAnnotation(api.whitelistWithWarningAnnotations());
} catch (MirroredTypesException e) {
return anyAnnotation(e.getTypeMirrors(), state);
}
}
开发者ID:google,项目名称:error-prone,代码行数:8,代码来源:RestrictedApiChecker.java
示例15: anyAnnotation
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
private static Matcher<Tree> anyAnnotation(Class<? extends Annotation>[] annotations) {
ArrayList<Matcher<Tree>> matchers = new ArrayList<>(annotations.length);
for (Class<? extends Annotation> annotation : annotations) {
matchers.add(Matchers.hasAnnotation(annotation));
}
return Matchers.anyOf(matchers);
}
开发者ID:google,项目名称:error-prone,代码行数:8,代码来源:RestrictedApiChecker.java
示例16: 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;
}
开发者ID:google,项目名称:error-prone,代码行数:34,代码来源:NestedInstanceOfConditions.java
示例17: 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();
}
开发者ID:google,项目名称:error-prone,代码行数:34,代码来源:UnnecessarySetDefault.java
示例18: buildMatcher
import com.google.errorprone.matchers.Matcher; //导入依赖的package包/类
private static Matcher<MethodInvocationTree> buildMatcher() {
return anyOf(
allOf(
anyOf(
instanceMethod().onDescendantOf("java.util.Collection").named("addAll"),
instanceMethod().onDescendantOf("java.util.Collection").named("removeAll"),
instanceMethod().onDescendantOf("java.util.Collection").named("containsAll"),
instanceMethod().onDescendantOf("java.util.Collection").named("retainAll")),
receiverSameAsArgument(0)),
allOf(
instanceMethod().onDescendantOf("java.util.Collection").named("addAll"),
receiverSameAsArgument(1)));
}
开发者ID:google,项目名称:error-prone,代码行数:14,代码来源:ModifyingCollectionWithItself.java
示例19: 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 {@code @RunWith} annotation, no {@code @Test}-annotated methods, and is not abstract).
*/
@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
if (!LOOKS_LIKE_TEST_CASE.matches(methodTree, state)) {
return NO_MATCH;
}
List<SuggestedFix> fixes = new ArrayList<>(0);
if (not(methodNameStartsWith("test")).matches(methodTree, state)) {
String fixedName = methodTree.getName().toString();
// N.B. regex.Matcher class name collides with errorprone.Matcher
java.util.regex.Matcher matcher = MISSPELLED_NAME.matcher(fixedName);
if (matcher.lookingAt()) {
fixedName = matcher.replaceFirst("test");
} else if (wouldRunInJUnit4.matches(methodTree, state)) {
fixedName = "test" + fixedName.substring(0, 1).toUpperCase() + fixedName.substring(1);
} else {
return NO_MATCH;
}
// Rename test method appropriately.
fixes.add(renameMethod(methodTree, fixedName, state));
}
// Make method public (if not already public).
addModifiers(methodTree, state, Modifier.PUBLIC).ifPresent(fixes::add);
// Remove any other visibility modifiers (if present).
removeModifiers(methodTree, state, Modifier.PRIVATE, Modifier.PROTECTED).ifPresent(fixes::add);
// Remove static modifier (if present).
// N.B. must occur in separate step because removeModifiers only removes one modifier at a time.
removeModifiers(methodTree, state, Modifier.STATIC).ifPresent(fixes::add);
return describeMatch(methodTree, mergeFixes(fixes));
}
开发者ID:google,项目名称:error-prone,代码行数:39,代码来源:JUnit3TestNotRun.java
示例20: 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);
}
};
}
开发者ID:google,项目名称:error-prone,代码行数:11,代码来源:NonAtomicVolatileUpdate.java
注:本文中的com.google.errorprone.matchers.Matcher类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论