Class MethodResolutionLogic
java.lang.Object
com.github.javaparser.resolution.logic.MethodResolutionLogic
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static booleanareOverride(MethodUsage winningCandidate, MethodUsage other) private static ResolvedArrayTypeprivate static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) Filters by given function using a stateful filter mechanism.private static ResolvedTypefindCommonType(List<ResolvedType> variadicValues) findMostApplicable(List<ResolvedMethodDeclaration> methods, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver) findMostApplicable(List<ResolvedMethodDeclaration> methods, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, boolean wildcardTolerance) static Optional<MethodUsage> findMostApplicableUsage(List<MethodUsage> methods, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver) private static intgetLastParameterIndex(int countOfMethodParametersDeclared) Returns the index of the last parameter in a parameter list.static ResolvedTypestatic ResolvedTypegetMethodUsageExplicitAndVariadicParameterType(MethodUsage method, int i) private static List<ResolvedType> groupTrailingArgumentsIntoArray(ResolvedMethodDeclaration methodDeclaration, List<ResolvedType> needleArgumentTypes, ResolvedType expectedVariadicParameterType) private static List<ResolvedType> groupVariadicParamValues(List<ResolvedType> argumentsTypes, int startVariadic, ResolvedType variadicType) static voidinferTypes(ResolvedType source, ResolvedType target, Map<ResolvedTypeParameterDeclaration, ResolvedType> mappings) static booleanisApplicable(ResolvedMethodDeclaration method, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver) private static booleanisApplicable(ResolvedMethodDeclaration methodDeclaration, String needleName, List<ResolvedType> needleArgumentTypes, TypeSolver typeSolver, boolean withWildcardTolerance) Note the specific naming here -- parameters are part of the method declaration, while arguments are the values passed when calling a method.static booleanisApplicable(MethodUsage methodUsage, String needleName, List<ResolvedType> needleParameterTypes, TypeSolver typeSolver) Checks if a method usage is applicable for a given method name and parameter types.private static booleanisArrayOfObject(ResolvedType type) static booleanisAssignableMatchTypeParameters(ResolvedReferenceType expected, ResolvedReferenceType actual, Map<String, ResolvedType> matchedParameters) static booleanisAssignableMatchTypeParameters(ResolvedType expected, ResolvedType actual, Map<String, ResolvedType> matchedParameters) private static booleanisAssignableMatchTypeParametersMatchingQName(ResolvedReferenceType expected, ResolvedReferenceType actual, Map<String, ResolvedType> matchedParameters) private static booleanisBoxingCompatibleWithTypeSolver(ResolvedType expectedType, ResolvedType actualType, TypeSolver typeSolver) Checks if a primitive type can be boxed to a reference type (or vice versa).private static booleanisConflictingLambdaType(LambdaArgumentTypePlaceholder lambdaPlaceholder, ResolvedType expectedType) protected static booleanisExactMatch(ResolvedMethodLikeDeclaration method, List<ResolvedType> argumentsTypes) private static booleanisJavaLangObject(ResolvedType paramType) (package private) static booleanisMoreSpecific(ResolvedMethodLikeDeclaration methodA, ResolvedMethodLikeDeclaration methodB, List<ResolvedType> argumentTypes) private static booleanisMoreSpecific(MethodUsage methodA, MethodUsage methodB, List<ResolvedType> argumentTypes) private static booleanmatchTypeVariable(ResolvedTypeVariable typeVariable, ResolvedType type, Map<String, ResolvedType> matchedParameters) static ResolvedTypereplaceTypeParam(ResolvedType type, ResolvedTypeParameterDeclaration tp, TypeSolver typeSolver) solveMethodInType(ResolvedTypeDeclaration typeDeclaration, String name, List<ResolvedType> argumentsTypes) solveMethodInType(ResolvedTypeDeclaration typeDeclaration, String name, List<ResolvedType> argumentsTypes, boolean staticOnly) private static MethodUsagesubstituteDeclaringTypeParameters(MethodUsage methodUsage, TypeSolver typeSolver) Substitutes type variables from the declaring type into the method signature.private static ResolvedTypesubstituteTypeVariables(ResolvedType type, List<ResolvedTypeParameterDeclaration> typeParams, List<ResolvedType> typeArgs) Recursively substitutes type variables within a type structure.
-
Field Details
-
JAVA_LANG_OBJECT
-
-
Constructor Details
-
MethodResolutionLogic
public MethodResolutionLogic()
-
-
Method Details
-
groupVariadicParamValues
private static List<ResolvedType> groupVariadicParamValues(List<ResolvedType> argumentsTypes, int startVariadic, ResolvedType variadicType) -
findCommonType
-
isApplicable
public static boolean isApplicable(ResolvedMethodDeclaration method, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver) -
isConflictingLambdaType
private static boolean isConflictingLambdaType(LambdaArgumentTypePlaceholder lambdaPlaceholder, ResolvedType expectedType) -
isApplicable
private static boolean isApplicable(ResolvedMethodDeclaration methodDeclaration, String needleName, List<ResolvedType> needleArgumentTypes, TypeSolver typeSolver, boolean withWildcardTolerance) Note the specific naming here -- parameters are part of the method declaration, while arguments are the values passed when calling a method. Note that "needle" refers to that value being used as a search/query term to match against.- Returns:
- true, if the given ResolvedMethodDeclaration matches the given name/types (normally obtained from a MethodUsage)
-
isArrayOfObject
-
convertToVariadicParameter
-
getLastParameterIndex
private static int getLastParameterIndex(int countOfMethodParametersDeclared) Returns the index of the last parameter in a parameter list. Helper method to safely get the last parameter index even for empty lists.- Parameters:
countOfMethodParametersDeclared- the total number of parameters- Returns:
- the index of the last parameter (0-based), or 0 if there are no parameters
-
groupTrailingArgumentsIntoArray
private static List<ResolvedType> groupTrailingArgumentsIntoArray(ResolvedMethodDeclaration methodDeclaration, List<ResolvedType> needleArgumentTypes, ResolvedType expectedVariadicParameterType) -
isAssignableMatchTypeParameters
public static boolean isAssignableMatchTypeParameters(ResolvedType expected, ResolvedType actual, Map<String, ResolvedType> matchedParameters) -
isAssignableMatchTypeParameters
public static boolean isAssignableMatchTypeParameters(ResolvedReferenceType expected, ResolvedReferenceType actual, Map<String, ResolvedType> matchedParameters) -
isAssignableMatchTypeParametersMatchingQName
private static boolean isAssignableMatchTypeParametersMatchingQName(ResolvedReferenceType expected, ResolvedReferenceType actual, Map<String, ResolvedType> matchedParameters) -
matchTypeVariable
private static boolean matchTypeVariable(ResolvedTypeVariable typeVariable, ResolvedType type, Map<String, ResolvedType> matchedParameters) -
replaceTypeParam
public static ResolvedType replaceTypeParam(ResolvedType type, ResolvedTypeParameterDeclaration tp, TypeSolver typeSolver) -
isApplicable
public static boolean isApplicable(MethodUsage methodUsage, String needleName, List<ResolvedType> needleParameterTypes, TypeSolver typeSolver) Checks if a method usage is applicable for a given method name and parameter types. This method performs type compatibility checking including generic type variable substitution. Note the specific naming here -- parameters are part of the method declaration, while arguments are the values passed when calling a method. Note that "needle" refers to that value being used as a search/query term to match against.- Returns:
- true, if the given MethodUsage matches the given name/types (normally obtained from a ResolvedMethodDeclaration)
-
isBoxingCompatibleWithTypeSolver
private static boolean isBoxingCompatibleWithTypeSolver(ResolvedType expectedType, ResolvedType actualType, TypeSolver typeSolver) Checks if a primitive type can be boxed to a reference type (or vice versa). Also handles array types for variadic parameters and wildcards. -
substituteDeclaringTypeParameters
private static MethodUsage substituteDeclaringTypeParameters(MethodUsage methodUsage, TypeSolver typeSolver) Substitutes type variables from the declaring type into the method signature. This method handles the case where a method is inherited from a generic ancestor, and the method signature contains type variables from that ancestor that need to be replaced with the type variables (or concrete types) of the current declaring type. Example scenario: - Iterabledeclares: forEach(Consumerinvalid input: '<'? super T> action) - Collection extends Iterable - List extends Collection When we call List .forEach(...): 1. The method signature initially references Iterable's type variable 'T' 2. This needs to be substituted through the inheritance chain: - In Collection : T -> E (Iterable becomes Iterable ) - In List : E remains E - In List : E -> String 3. Final result: forEach(Consumerinvalid input: '<'? super String> action) Without this substitution, we would be comparing incompatible type variables and the method resolution would fail. - Parameters:
methodUsage- the method usage whose signature needs type variable substitutiontypeSolver- the type solver for resolving types during substitution- Returns:
- a new MethodUsage with type variables properly substituted
-
substituteTypeVariables
private static ResolvedType substituteTypeVariables(ResolvedType type, List<ResolvedTypeParameterDeclaration> typeParams, List<ResolvedType> typeArgs) Recursively substitutes type variables within a type structure. This method performs deep substitution of type variables, handling various type structures including: - Simple type variables (T, E, K, V, etc.) - Wildcards with type variable bounds (? super T, ? extends E) - Parameterized types with type variables (List, Mapinvalid input: '<'K, V>) - Nested combinations of the above (Consumerinvalid input: '<'? super T>, Listinvalid input: '<'List >) The substitution is based on a mapping between type parameter declarations (the formal parameters as declared, e.g., in class Foo ) and their actual type arguments (the concrete types used, e.g., String in Foo ). Example transformations: - T -> String (when typeParams contains T and typeArgs contains String) - ? super T -> ? super String - Consumerinvalid input: '<'? super T> -> Consumerinvalid input: '<'? super String> - List -> List - Mapinvalid input: '<'K, V> -> Mapinvalid input: '<'String, Integer> (with appropriate mappings) - Parameters:
type- the type in which to substitute type variablestypeParams- the list of type parameter declarations (formal parameters)typeArgs- the list of actual type arguments to substitute in- Returns:
- a new type with all matching type variables substituted
-
distinctByKey
-
findMostApplicable
public static SymbolReference<ResolvedMethodDeclaration> findMostApplicable(List<ResolvedMethodDeclaration> methods, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver) - Parameters:
methods- we expect the methods to be ordered such that inherited methods are later in the list
-
findMostApplicable
public static SymbolReference<ResolvedMethodDeclaration> findMostApplicable(List<ResolvedMethodDeclaration> methods, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, boolean wildcardTolerance) -
isExactMatch
protected static boolean isExactMatch(ResolvedMethodLikeDeclaration method, List<ResolvedType> argumentsTypes) -
getMethodsExplicitAndVariadicParameterType
public static ResolvedType getMethodsExplicitAndVariadicParameterType(ResolvedMethodLikeDeclaration method, int i) -
getMethodUsageExplicitAndVariadicParameterType
public static ResolvedType getMethodUsageExplicitAndVariadicParameterType(MethodUsage method, int i) -
isMoreSpecific
static boolean isMoreSpecific(ResolvedMethodLikeDeclaration methodA, ResolvedMethodLikeDeclaration methodB, List<ResolvedType> argumentTypes) -
isJavaLangObject
-
isMoreSpecific
private static boolean isMoreSpecific(MethodUsage methodA, MethodUsage methodB, List<ResolvedType> argumentTypes) -
findMostApplicableUsage
public static Optional<MethodUsage> findMostApplicableUsage(List<MethodUsage> methods, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver) -
areOverride
-
solveMethodInType
public static SymbolReference<ResolvedMethodDeclaration> solveMethodInType(ResolvedTypeDeclaration typeDeclaration, String name, List<ResolvedType> argumentsTypes) -
solveMethodInType
public static SymbolReference<ResolvedMethodDeclaration> solveMethodInType(ResolvedTypeDeclaration typeDeclaration, String name, List<ResolvedType> argumentsTypes, boolean staticOnly) -
inferTypes
public static void inferTypes(ResolvedType source, ResolvedType target, Map<ResolvedTypeParameterDeclaration, ResolvedType> mappings)
-