Class MethodResolutionLogic
- java.lang.Object
-
- com.github.javaparser.resolution.logic.MethodResolutionLogic
-
public class MethodResolutionLogic extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringJAVA_LANG_OBJECT
-
Constructor Summary
Constructors Constructor Description MethodResolutionLogic()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static booleanareOverride(MethodUsage winningCandidate, MethodUsage other)private static ResolvedArrayTypeconvertToVariadicParameter(ResolvedType type)private static <T> java.util.function.Predicate<T>distinctByKey(java.util.function.Function<? super T,?> keyExtractor)Filters by given function {@param keyExtractor} using a stateful filter mechanism.private static ResolvedTypefindCommonType(java.util.List<ResolvedType> variadicValues)static SymbolReference<ResolvedMethodDeclaration>findMostApplicable(java.util.List<ResolvedMethodDeclaration> methods, java.lang.String name, java.util.List<ResolvedType> argumentsTypes, TypeSolver typeSolver)static SymbolReference<ResolvedMethodDeclaration>findMostApplicable(java.util.List<ResolvedMethodDeclaration> methods, java.lang.String name, java.util.List<ResolvedType> argumentsTypes, TypeSolver typeSolver, boolean wildcardTolerance)static java.util.Optional<MethodUsage>findMostApplicableUsage(java.util.List<MethodUsage> methods, java.lang.String name, java.util.List<ResolvedType> argumentsTypes, TypeSolver typeSolver)private static intgetLastParameterIndex(int countOfMethodParametersDeclared)Returns the index of the last parameter in a parameter list.static ResolvedTypegetMethodsExplicitAndVariadicParameterType(ResolvedMethodLikeDeclaration method, int i)static ResolvedTypegetMethodUsageExplicitAndVariadicParameterType(MethodUsage method, int i)private static java.util.List<ResolvedType>groupTrailingArgumentsIntoArray(ResolvedMethodDeclaration methodDeclaration, java.util.List<ResolvedType> needleArgumentTypes, ResolvedType expectedVariadicParameterType)private static java.util.List<ResolvedType>groupVariadicParamValues(java.util.List<ResolvedType> argumentsTypes, int startVariadic, ResolvedType variadicType)static voidinferTypes(ResolvedType source, ResolvedType target, java.util.Map<ResolvedTypeParameterDeclaration,ResolvedType> mappings)static booleanisApplicable(ResolvedMethodDeclaration method, java.lang.String name, java.util.List<ResolvedType> argumentsTypes, TypeSolver typeSolver)private static booleanisApplicable(ResolvedMethodDeclaration methodDeclaration, java.lang.String needleName, java.util.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, java.lang.String needleName, java.util.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, java.util.Map<java.lang.String,ResolvedType> matchedParameters)static booleanisAssignableMatchTypeParameters(ResolvedType expected, ResolvedType actual, java.util.Map<java.lang.String,ResolvedType> matchedParameters)private static booleanisAssignableMatchTypeParametersMatchingQName(ResolvedReferenceType expected, ResolvedReferenceType actual, java.util.Map<java.lang.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, java.util.List<ResolvedType> argumentsTypes)private static booleanisJavaLangObject(ResolvedType paramType)(package private) static booleanisMoreSpecific(ResolvedMethodLikeDeclaration methodA, ResolvedMethodLikeDeclaration methodB, java.util.List<ResolvedType> argumentTypes)private static booleanisMoreSpecific(MethodUsage methodA, MethodUsage methodB, java.util.List<ResolvedType> argumentTypes)private static booleanmatchTypeVariable(ResolvedTypeVariable typeVariable, ResolvedType type, java.util.Map<java.lang.String,ResolvedType> matchedParameters)static ResolvedTypereplaceTypeParam(ResolvedType type, ResolvedTypeParameterDeclaration tp, TypeSolver typeSolver)static SymbolReference<ResolvedMethodDeclaration>solveMethodInType(ResolvedTypeDeclaration typeDeclaration, java.lang.String name, java.util.List<ResolvedType> argumentsTypes)static SymbolReference<ResolvedMethodDeclaration>solveMethodInType(ResolvedTypeDeclaration typeDeclaration, java.lang.String name, java.util.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, java.util.List<ResolvedTypeParameterDeclaration> typeParams, java.util.List<ResolvedType> typeArgs)Recursively substitutes type variables within a type structure.
-
-
-
Method Detail
-
groupVariadicParamValues
private static java.util.List<ResolvedType> groupVariadicParamValues(java.util.List<ResolvedType> argumentsTypes, int startVariadic, ResolvedType variadicType)
-
findCommonType
private static ResolvedType findCommonType(java.util.List<ResolvedType> variadicValues)
-
isApplicable
public static boolean isApplicable(ResolvedMethodDeclaration method, java.lang.String name, java.util.List<ResolvedType> argumentsTypes, TypeSolver typeSolver)
-
isConflictingLambdaType
private static boolean isConflictingLambdaType(LambdaArgumentTypePlaceholder lambdaPlaceholder, ResolvedType expectedType)
-
isApplicable
private static boolean isApplicable(ResolvedMethodDeclaration methodDeclaration, java.lang.String needleName, java.util.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
private static boolean isArrayOfObject(ResolvedType type)
-
convertToVariadicParameter
private static ResolvedArrayType convertToVariadicParameter(ResolvedType type)
-
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 java.util.List<ResolvedType> groupTrailingArgumentsIntoArray(ResolvedMethodDeclaration methodDeclaration, java.util.List<ResolvedType> needleArgumentTypes, ResolvedType expectedVariadicParameterType)
-
isAssignableMatchTypeParameters
public static boolean isAssignableMatchTypeParameters(ResolvedType expected, ResolvedType actual, java.util.Map<java.lang.String,ResolvedType> matchedParameters)
-
isAssignableMatchTypeParameters
public static boolean isAssignableMatchTypeParameters(ResolvedReferenceType expected, ResolvedReferenceType actual, java.util.Map<java.lang.String,ResolvedType> matchedParameters)
-
isAssignableMatchTypeParametersMatchingQName
private static boolean isAssignableMatchTypeParametersMatchingQName(ResolvedReferenceType expected, ResolvedReferenceType actual, java.util.Map<java.lang.String,ResolvedType> matchedParameters)
-
matchTypeVariable
private static boolean matchTypeVariable(ResolvedTypeVariable typeVariable, ResolvedType type, java.util.Map<java.lang.String,ResolvedType> matchedParameters)
-
replaceTypeParam
public static ResolvedType replaceTypeParam(ResolvedType type, ResolvedTypeParameterDeclaration tp, TypeSolver typeSolver)
-
isApplicable
public static boolean isApplicable(MethodUsage methodUsage, java.lang.String needleName, java.util.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(Consumer 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(Consumer 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, java.util.List<ResolvedTypeParameterDeclaration> typeParams, java.util.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, Map ) - Nested combinations of the above (Consumer super T>, 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 - Consumer super T> -> Consumer super String> - List -> List - Map -> Map (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
private static <T> java.util.function.Predicate<T> distinctByKey(java.util.function.Function<? super T,?> keyExtractor)
Filters by given function {@param keyExtractor} using a stateful filter mechanism.persons.stream().filter(distinctByKey(Person::getName))The example above would return a distinct list of persons containing only one person per name.
-
findMostApplicable
public static SymbolReference<ResolvedMethodDeclaration> findMostApplicable(java.util.List<ResolvedMethodDeclaration> methods, java.lang.String name, java.util.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(java.util.List<ResolvedMethodDeclaration> methods, java.lang.String name, java.util.List<ResolvedType> argumentsTypes, TypeSolver typeSolver, boolean wildcardTolerance)
-
isExactMatch
protected static boolean isExactMatch(ResolvedMethodLikeDeclaration method, java.util.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, java.util.List<ResolvedType> argumentTypes)
-
isJavaLangObject
private static boolean isJavaLangObject(ResolvedType paramType)
-
isMoreSpecific
private static boolean isMoreSpecific(MethodUsage methodA, MethodUsage methodB, java.util.List<ResolvedType> argumentTypes)
-
findMostApplicableUsage
public static java.util.Optional<MethodUsage> findMostApplicableUsage(java.util.List<MethodUsage> methods, java.lang.String name, java.util.List<ResolvedType> argumentsTypes, TypeSolver typeSolver)
-
areOverride
private static boolean areOverride(MethodUsage winningCandidate, MethodUsage other)
-
solveMethodInType
public static SymbolReference<ResolvedMethodDeclaration> solveMethodInType(ResolvedTypeDeclaration typeDeclaration, java.lang.String name, java.util.List<ResolvedType> argumentsTypes)
-
solveMethodInType
public static SymbolReference<ResolvedMethodDeclaration> solveMethodInType(ResolvedTypeDeclaration typeDeclaration, java.lang.String name, java.util.List<ResolvedType> argumentsTypes, boolean staticOnly)
-
inferTypes
public static void inferTypes(ResolvedType source, ResolvedType target, java.util.Map<ResolvedTypeParameterDeclaration,ResolvedType> mappings)
-
-