# instantiateUsingFactoryMethod method

1. @protected

Future<Object> instantiateUsingFactoryMethod(

1. String podName,
2. RootPodDefinition root,
3. List<ArgumentValue>? args

)

Instantiates a pod using a factory method defined in the pod definition.

@param podName The name of the pod @param root The pod definition containing factory method information @param args Optional arguments for the factory method @return The instance created by the factory method

## Implementation

    @protected
    Future<Object> instantiateUsingFactoryMethod(String podName, RootPodDefinition root, List<ArgumentValue>? args) async {
      Object factoryInstance = await getPod(root.factoryMethod.podName, args);

      final result = await _executableStrategy.determineFactoryMethod(root, podName, args);
      final method = result.executable as Method;

      try {
        if (method.isStatic()) {
          throw PodIsAbstractException(
            "Factory method '${method.getName()}' in pod '${root.factoryMethod.podName}' is static and cannot be invoked",
            name: podName,
          );
        }

        if (logger.getIsTraceEnabled()) {
          logger.trace(
            "Instantiating pod '$podName' using factory method '${method.getName()}' "
            "from pod '${root.factoryMethod.podName}' "
            "with args: named=${result.namedArgs}, positional=${result.positionalArgs}",
          );
        }

        return method.invoke(factoryInstance, result.namedArgs, result.positionalArgs);
      } on PodException catch (e, st) {
        if(logger.getIsTraceEnabled()) {
          logger.trace("Pod instantiation failed for '$podName' via factory method '${method.getName()}': $e", error: e, stacktrace: st);
        }

        rethrow;
      } catch (e, st) {
        if(logger.getIsTraceEnabled()) {
          logger.trace("Unexpected error while instantiating pod '$podName' via factory method '${method.getName()}': $e\n$st", error: e, stacktrace: st);
        }

        rethrow;
      }
    }