forMethodParameter static method

ResolvableType forMethodParameter(
  1. MethodDeclaration method,
  2. int parameterIndex
)

Creates a ResolvableType for a method parameter.

This factory method creates a ResolvableType representing the type of a specific parameter in a method declaration. This is useful for reflection-based operations and method analysis.

Parameters:

  • method: The MethodDeclaration containing the parameter
  • parameterIndex: The zero-based index of the parameter

Returns:

  • ResolvableType representing the parameter's type
  • ResolvableType.NONE if the parameter index is invalid

Example:

class UserService {
  User createUser(String name, int age, List<String> emails);
  void updateUser(User user, Map<String, dynamic> updates);
}

final createUserMethod = getMethodDeclaration(UserService, 'createUser');
final updateUserMethod = getMethodDeclaration(UserService, 'updateUser');

final nameParamType = ResolvableType.forMethodParameter(createUserMethod, 0);
final ageParamType = ResolvableType.forMethodParameter(createUserMethod, 1);
final emailsParamType = ResolvableType.forMethodParameter(createUserMethod, 2);

print(nameParamType.resolve()?.getType()); // String
print(ageParamType.resolve()?.getType()); // int
print(emailsParamType.resolve()?.getType()); // List<String>
print(emailsParamType.hasGenerics()); // true
print(emailsParamType.getGeneric().resolve()?.getType()); // String

final userParamType = ResolvableType.forMethodParameter(updateUserMethod, 0);
final updatesParamType = ResolvableType.forMethodParameter(updateUserMethod, 1);

print(userParamType.resolve()?.getType()); // User
print(updatesParamType.resolve()?.getType()); // Map<String, dynamic>

Implementation

static ResolvableType forMethodParameter(MethodDeclaration method, int parameterIndex) {
  final params = method.getParameters();
  if (parameterIndex >= 0 && parameterIndex < params.length) {
    final param = params[parameterIndex];
    final typeProvider = _ParameterTypeProvider(param);
    return forTypeWithProviderAndResolver(param.getType(), typeProvider, null);
  }
  return NONE;
}