99import static datadog .instrument .classmatch .InternalMatchers .ALL_METHODS ;
1010import static datadog .instrument .classmatch .InternalMatchers .declaresAnnotation ;
1111import static datadog .instrument .classmatch .InternalMatchers .declaresAnnotationOneOf ;
12- import static datadog .instrument .classmatch .InternalMatchers .declaresParameter ;
1312import static datadog .instrument .classmatch .InternalMatchers .descriptor ;
13+ import static datadog .instrument .classmatch .InternalMatchers .hasParamDescriptor ;
14+ import static datadog .instrument .classmatch .InternalMatchers .hasParamType ;
15+ import static datadog .instrument .classmatch .InternalMatchers .hasReturnType ;
1416import static java .util .Arrays .asList ;
1517
1618import java .util .Collection ;
@@ -74,18 +76,31 @@ static MethodMatcher staticInitializer() {
7476 * @param accessMatcher the access matcher
7577 * @return matcher of methods with matching access
7678 */
77- default MethodMatcher withAccess (IntPredicate accessMatcher ) {
79+ default MethodMatcher access (IntPredicate accessMatcher ) {
7880 return and (m -> accessMatcher .test (m .access ));
7981 }
8082
83+ /**
84+ * Matches methods with no parameters.
85+ *
86+ * @return matcher of methods with no parameters
87+ */
88+ default MethodMatcher noParameters () {
89+ return and (m -> m .descriptor .charAt (1 ) == ')' );
90+ }
91+
8192 /**
8293 * Matches methods with the given parameter count.
8394 *
8495 * @param paramCount the parameter count
8596 * @return matcher of methods with the same parameter count
8697 */
87- default MethodMatcher withParameters (int paramCount ) {
88- return and (m -> m .descriptorBoundaries ().length == paramCount );
98+ default MethodMatcher parameters (int paramCount ) {
99+ if (paramCount == 0 ) {
100+ return noParameters ();
101+ } else {
102+ return and (m -> m .descriptorBoundaries ().length == paramCount );
103+ }
89104 }
90105
91106 /**
@@ -94,13 +109,13 @@ default MethodMatcher withParameters(int paramCount) {
94109 * @param paramTypes the parameter types
95110 * @return matcher of methods with the same parameter types
96111 */
97- default MethodMatcher withParameters (String ... paramTypes ) {
112+ default MethodMatcher parameters (String ... paramTypes ) {
98113 StringBuilder buf = new StringBuilder ().append ('(' );
99114 for (String paramType : paramTypes ) {
100115 buf .append (descriptor (paramType ));
101116 }
102- String descriptorPrefix = buf .append (')' ).toString ();
103- return and (m -> m .descriptor .startsWith (descriptorPrefix ));
117+ String prefix = buf .append (')' ).toString ();
118+ return and (m -> m .descriptor .startsWith (prefix ));
104119 }
105120
106121 /**
@@ -109,13 +124,13 @@ default MethodMatcher withParameters(String... paramTypes) {
109124 * @param paramTypes the parameter types
110125 * @return matcher of methods with the same parameter types
111126 */
112- default MethodMatcher withParameters (Class <?>... paramTypes ) {
127+ default MethodMatcher parameters (Class <?>... paramTypes ) {
113128 StringBuilder buf = new StringBuilder ().append ('(' );
114129 for (Class <?> paramType : paramTypes ) {
115130 buf .append (descriptor (paramType ));
116131 }
117- String descriptorPrefix = buf .append (')' ).toString ();
118- return and (m -> m .descriptor .startsWith (descriptorPrefix ));
132+ String prefix = buf .append (')' ).toString ();
133+ return and (m -> m .descriptor .startsWith (prefix ));
119134 }
120135
121136 /**
@@ -125,9 +140,13 @@ default MethodMatcher withParameters(Class<?>... paramTypes) {
125140 * @param paramType the parameter type
126141 * @return matcher of methods with the same parameter type at the same position
127142 */
128- default MethodMatcher withParameter (int paramIndex , String paramType ) {
143+ default MethodMatcher parameter (int paramIndex , String paramType ) {
129144 String paramDescriptor = descriptor (paramType );
130- return and (m -> declaresParameter (m , paramIndex , paramDescriptor ));
145+ if (paramIndex == 0 ) {
146+ return and (m -> m .descriptor .startsWith (paramDescriptor , 1 ));
147+ } else {
148+ return and (m -> hasParamDescriptor (m , paramIndex , paramDescriptor ));
149+ }
131150 }
132151
133152 /**
@@ -137,9 +156,24 @@ default MethodMatcher withParameter(int paramIndex, String paramType) {
137156 * @param paramType the parameter type
138157 * @return matcher of methods with the same parameter type at the same position
139158 */
140- default MethodMatcher withParameter (int paramIndex , Class <?> paramType ) {
159+ default MethodMatcher parameter (int paramIndex , Class <?> paramType ) {
141160 String paramDescriptor = descriptor (paramType );
142- return and (m -> declaresParameter (m , paramIndex , paramDescriptor ));
161+ if (paramIndex == 0 ) {
162+ return and (m -> m .descriptor .startsWith (paramDescriptor , 1 ));
163+ } else {
164+ return and (m -> hasParamDescriptor (m , paramIndex , paramDescriptor ));
165+ }
166+ }
167+
168+ /**
169+ * Matches methods with a parameter type matching the given criteria.
170+ *
171+ * @param paramIndex the parameter index
172+ * @param typeMatcher the parameter type matcher
173+ * @return matcher of methods with a matching parameter type
174+ */
175+ default MethodMatcher parameter (int paramIndex , Predicate <String > typeMatcher ) {
176+ return and (m -> hasParamType (m , paramIndex , typeMatcher ));
143177 }
144178
145179 /**
@@ -149,7 +183,7 @@ default MethodMatcher withParameter(int paramIndex, Class<?> paramType) {
149183 * @return matcher of methods returning the same type
150184 */
151185 default MethodMatcher returning (String returnType ) {
152- String returnDescriptor = ')' + descriptor (returnType );
186+ String returnDescriptor = descriptor (returnType );
153187 return and (m -> m .descriptor .endsWith (returnDescriptor ));
154188 }
155189
@@ -160,10 +194,20 @@ default MethodMatcher returning(String returnType) {
160194 * @return matcher of methods returning the same type
161195 */
162196 default MethodMatcher returning (Class <?> returnType ) {
163- String returnDescriptor = ')' + descriptor (returnType );
197+ String returnDescriptor = descriptor (returnType );
164198 return and (m -> m .descriptor .endsWith (returnDescriptor ));
165199 }
166200
201+ /**
202+ * Matches methods returning a type matching the given criteria.
203+ *
204+ * @param typeMatcher the return type matcher
205+ * @return matcher of methods with a matching return type
206+ */
207+ default MethodMatcher returning (Predicate <String > typeMatcher ) {
208+ return and (m -> hasReturnType (m , typeMatcher ));
209+ }
210+
167211 /**
168212 * Matches methods annotated with the given type.
169213 *
0 commit comments