Class InvocationMatcher
- java.lang.Object
-
- net.sourceforge.pmd.lang.java.types.InvocationMatcher
-
public final class InvocationMatcher extends Object
Matches a method or constructor call against a particular overload. Useparse(String)
to create one. For example,java.lang.String#toString() // match calls to toString on String instances _#toString() // match calls to toString on any receiver _#_() // match all calls to a method with no parameters _#toString(_*) // match calls to a "toString" method with any number of parameters _#eq(_, _) // match calls to an "eq" method that has 2 parameters of unspecified type _#eq(java.lang.String, _) // like the previous, but the first parameter must be String java.util.ArrayList#new(int) // match constructor calls of this overload of the ArrayList constructor
The receiver matcher (first half) is matched against the static type of the receiver of the call, and not the declaration site of the method, unless the called method is static, or a constructor.
The parameters are matched against the declared parameters types of the called overload, and not the actual argument types. In particular, for vararg methods, the signature should mention a single parameter, with an array type.
For example
Integer.valueOf('0')
will be matched by_#valueOf(int)
but not_#valueOf(char)
, which is an overload that does not exist (the char is widened to an int, so the int overload is selected).Full EBNF grammar
(no whitespace is tolerated anywhere):
sig ::= type '#' method_name param_list type ::= qname ( '[]' )* | '_' method_name ::= '_' | ident | 'new' param_list ::= '(_*)' | '(' type (',' type )* ')' qname ::= java binary name
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
InvocationMatcher.CompoundInvocationMatcher
A compound of several matchers (logical OR).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
matchesCall(@Nullable InvocationNode node)
Returns true if the call matches this matcher.boolean
matchesCall(@Nullable JavaNode node)
static InvocationMatcher
parse(String sig)
Parses anInvocationMatcher
.static InvocationMatcher.CompoundInvocationMatcher
parseAll(String first, String... rest)
Parses aInvocationMatcher.CompoundInvocationMatcher
which matches any of the provided matchers.
-
-
-
Method Detail
-
matchesCall
public boolean matchesCall(@Nullable JavaNode node)
-
matchesCall
public boolean matchesCall(@Nullable InvocationNode node)
Returns true if the call matches this matcher. This means, the called overload is the one identified by the argument matchers, and the actual qualifier type is a subtype of the one mentioned by the qualifier matcher.
-
parseAll
public static InvocationMatcher.CompoundInvocationMatcher parseAll(String first, String... rest)
Parses aInvocationMatcher.CompoundInvocationMatcher
which matches any of the provided matchers.- Parameters:
first
- First signature, in the format described on this classrest
- Other signatures, in the format described on this class- Returns:
- A sig matcher
- Throws:
IllegalArgumentException
- If any signature is malformed (see EBNF)NullPointerException
- If any signature is null- See Also:
parse(String)
-
parse
public static InvocationMatcher parse(String sig)
Parses anInvocationMatcher
.- Parameters:
sig
- A signature in the format described on this class- Returns:
- A sig matcher
- Throws:
IllegalArgumentException
- If the signature is malformed (see EBNF)NullPointerException
- If the signature is null- See Also:
parseAll(String, String...)
-
-