forMethodParameterWithImplementation static method

ResolvableType forMethodParameterWithImplementation(
  1. MethodDeclaration method,
  2. int parameterIndex,
  3. Type implementationClass
)

Creates a ResolvableType for a method parameter with implementation context.

This factory method creates a ResolvableType for a method parameter that considers a specific implementation class context. This is useful when the parameter type needs to be resolved in the context of a concrete implementation rather than the declaring class.

Parameters:

  • method: The MethodDeclaration containing the parameter
  • parameterIndex: The zero-based index of the parameter
  • implementationClass: The implementation class Type to provide context

Returns:

  • ResolvableType representing the parameter type in the implementation context
  • ResolvableType.NONE if the parameter index is invalid

Example:

abstract class Repository<T> {
  void save(T entity);
  List<T> findByExample(T example);
}

class UserRepository extends Repository<User> {
  // save parameter is User, findByExample parameter is User
}

final saveMethod = getMethodDeclaration(Repository, 'save');
final findByExampleMethod = getMethodDeclaration(Repository, 'findByExample');

// Without implementation context - returns T
final genericSaveParamType = ResolvableType.forMethodParameter(saveMethod, 0);

// With implementation context - returns User
final concreteSaveParamType = ResolvableType.forMethodParameterWithImplementation(
  saveMethod, 
  0,
  UserRepository
);

final concreteExampleParamType = ResolvableType.forMethodParameterWithImplementation(
  findByExampleMethod, 
  0,
  UserRepository
);

print(concreteSaveParamType.resolve()?.getType()); // User
print(concreteExampleParamType.resolve()?.getType()); // User

Implementation

static ResolvableType forMethodParameterWithImplementation(
    MethodDeclaration method, int parameterIndex, Type implementationClass) {
  final params = method.getParameters();
  if (parameterIndex >= 0 && parameterIndex < params.length) {
    final param = params[parameterIndex];
    final owner = forType(implementationClass);
    final resolver = owner.asVariableResolver();
    return forTypeWithProviderAndResolver(param.getType(), _ParameterTypeProvider(param), resolver);
  }
  return NONE;
}