/
MethodMatchers.java
155 lines (116 loc) · 5.56 KB
/
MethodMatchers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 2015 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.matchers.method;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.predicates.TypePredicate;
import com.google.errorprone.suppliers.Supplier;
import com.sun.source.tree.ExpressionTree;
import com.sun.tools.javac.code.Type;
import java.util.regex.Pattern;
public class MethodMatchers {
// Language definition for fluent method matchers.
public interface InstanceMethodMatcher extends Matcher<ExpressionTree> {
/** Match on types that satisfy the given predicate. */
MethodClassMatcher onClass(TypePredicate predicate);
/** Match on types with the given fully-qualified name. (e.g. java.lang.String) */
MethodClassMatcher onExactClass(String className);
/** Match on the given type exactly. */
MethodClassMatcher onExactClass(Supplier<Type> classType);
/** Match on descendants of the given fully-qualified type name. */
MethodClassMatcher onDescendantOf(String className);
/** Match on descendants of the given type. */
MethodClassMatcher onDescendantOf(Supplier<Type> classType);
/** Match on types that are descendants of any of the given types. */
MethodClassMatcher onDescendantOfAny(String... classTypes);
/** Match on types that are descendants of any of the given types. */
MethodClassMatcher onDescendantOfAny(Iterable<String> classTypes);
/** Match on any class. */
MethodClassMatcher anyClass();
}
public interface StaticMethodMatcher extends Matcher<ExpressionTree> {
/** Match on types that satisfy the given predicate. */
MethodClassMatcher onClass(TypePredicate predicate);
/** Match on types with the given fully-qualified name. (e.g. {@code java.lang.String} */
MethodClassMatcher onClass(String className);
/** Match on the given type exactly. */
MethodClassMatcher onClass(Supplier<Type> classType);
/** Match on types that are equal to any of the given types. */
MethodClassMatcher onClassAny(Iterable<String> classNames);
/** Match on types that are equal to any of the given types. */
MethodClassMatcher onClassAny(String... classNames);
/** Match on any class. */
MethodClassMatcher anyClass();
}
public interface AnyMethodMatcher extends Matcher<ExpressionTree> {
/** Match the given type exactly. */
MethodClassMatcher onClass(TypePredicate predicate);
/** Match on any class. */
MethodClassMatcher anyClass();
}
public interface MethodClassMatcher extends Matcher<ExpressionTree> {
/** Match methods with the given name. (e.g. {@code toString}) */
MethodNameMatcher named(String name);
/** Match methods with any name. */
MethodNameMatcher withAnyName();
/** Match methods with a name that matches the given regular expression. */
MethodNameMatcher withNameMatching(Pattern pattern);
/**
* Match methods with the given signature. The implementation uses javac internals to
* pretty-print the signatures, and the signature format is not well-specified. This matcher
* should be used with caution.
*
* <p>Example: {@code format(java.lang.String,java.lang.Object...)}
*/
MethodSignatureMatcher withSignature(String signature);
}
public interface MethodSignatureMatcher extends Matcher<ExpressionTree> {}
public interface MethodNameMatcher extends Matcher<ExpressionTree> {
/** Match methods whose formal parameters have the given types. */
ParameterMatcher withParameters(String... parameters);
/** Match methods whose formal parameters have the given types. */
ParameterMatcher withParameters(Iterable<String> parameters);
}
public interface ConstructorMatcher extends Matcher<ExpressionTree> {
/** Match on types that satisfy the given predicate. */
ConstructorClassMatcher forClass(TypePredicate predicate);
/** Match on the given type exactly. */
ConstructorClassMatcher forClass(String className);
/** Match on the given type exactly. */
ConstructorClassMatcher forClass(Supplier<Type> classType);
}
public interface ConstructorClassMatcher extends Matcher<ExpressionTree> {
/** Match constructors whose formal parameters have the given types. */
ParameterMatcher withParameters(String... parameters);
/** Match constructors whose formal parameters have the given types. */
ParameterMatcher withParameters(Iterable<String> parameters);
/** Match constructors whose formal parameters have the given types. */
ParameterMatcher withParametersOfType(Iterable<Supplier<Type>> parameters);
}
public interface ParameterMatcher extends Matcher<ExpressionTree> {}
// Method matcher factories
public static StaticMethodMatcher staticMethod() {
return new StaticMethodMatcherImpl();
}
public static InstanceMethodMatcher instanceMethod() {
return new InstanceMethodMatcherImpl();
}
public static AnyMethodMatcher anyMethod() {
return new AnyMethodMatcherImpl();
}
public static ConstructorMatcher constructor() {
return new ConstructorMatcherImpl();
}
}