private T loadV4Mojo()

in impl/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java [533:666]


    private <T> T loadV4Mojo(
            Class<T> mojoInterface,
            MavenSession session,
            MojoExecution mojoExecution,
            MojoDescriptor mojoDescriptor,
            PluginDescriptor pluginDescriptor,
            ClassRealm pluginRealm)
            throws PluginContainerException, PluginConfigurationException {
        T mojo;

        InternalMavenSession sessionV4 = InternalMavenSession.from(session.getSession());
        Project project = sessionV4.getProject(session.getCurrentProject());

        org.apache.maven.api.MojoExecution execution = new DefaultMojoExecution(sessionV4, mojoExecution);
        org.apache.maven.api.plugin.Log log = new DefaultLog(
                LoggerFactory.getLogger(mojoExecution.getMojoDescriptor().getFullGoalName()));
        try {
            Injector injector = Injector.create();
            injector.discover(pluginRealm);
            // Add known classes
            // TODO: get those from the existing plexus scopes ?
            injector.bindInstance(Session.class, sessionV4);
            injector.bindInstance(Project.class, project);
            injector.bindInstance(org.apache.maven.api.MojoExecution.class, execution);
            injector.bindInstance(org.apache.maven.api.plugin.Log.class, log);
            mojo = mojoInterface.cast(injector.getInstance(
                    Key.of(mojoDescriptor.getImplementationClass(), mojoDescriptor.getRoleHint())));

        } catch (Exception e) {
            throw new PluginContainerException(mojoDescriptor, pluginRealm, "Unable to lookup Mojo", e);
        }

        XmlNode dom = mojoExecution.getConfiguration() != null
                ? mojoExecution.getConfiguration().getDom()
                : null;

        PlexusConfiguration pomConfiguration;

        if (dom == null) {
            pomConfiguration = new DefaultPlexusConfiguration("configuration");
        } else {
            pomConfiguration = XmlPlexusConfiguration.toPlexusConfiguration(dom);
        }

        ExpressionEvaluator expressionEvaluator =
                new PluginParameterExpressionEvaluatorV4(sessionV4, project, execution);

        for (MavenPluginConfigurationValidator validator : configurationValidators) {
            validator.validate(session, mojoDescriptor, mojo.getClass(), pomConfiguration, expressionEvaluator);
        }

        populateMojoExecutionFields(
                mojo,
                mojoExecution.getExecutionId(),
                mojoDescriptor,
                pluginRealm,
                pomConfiguration,
                expressionEvaluator);

        for (Resolution resolution : mojoDescriptor.getMojoDescriptorV4().getResolutions()) {
            Field field = null;
            for (Class<?> clazz = mojo.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
                try {
                    field = clazz.getDeclaredField(resolution.getField());
                    break;
                } catch (NoSuchFieldException e) {
                    // continue
                }
            }
            if (field == null) {
                throw new PluginConfigurationException(
                        pluginDescriptor,
                        "Unable to find field '" + resolution.getField() + "' annotated with @Resolution");
            }
            field.setAccessible(true);
            String pathScope = resolution.getPathScope();
            Object result = null;
            if (pathScope != null && !pathScope.isEmpty()) {
                // resolution
                PathScope ps = sessionV4.getService(PathScopeRegistry.class).require(pathScope);
                DependencyResolverResult res =
                        sessionV4.getService(DependencyResolver.class).resolve(sessionV4, project, ps);
                if (field.getType() == DependencyResolverResult.class) {
                    result = res;
                } else if (field.getType() == Node.class) {
                    result = res.getRoot();
                } else if (field.getType() == List.class && field.getGenericType() instanceof ParameterizedType pt) {
                    Type t = pt.getActualTypeArguments()[0];
                    if (t == Node.class) {
                        result = res.getNodes();
                    } else if (t == Path.class) {
                        result = res.getPaths();
                    }
                } else if (field.getType() == Map.class && field.getGenericType() instanceof ParameterizedType pt) {
                    Type k = pt.getActualTypeArguments()[0];
                    Type v = pt.getActualTypeArguments()[1];
                    if (k == PathType.class
                            && v instanceof ParameterizedType ptv
                            && ptv.getRawType() == List.class
                            && ptv.getActualTypeArguments()[0] == Path.class) {
                        result = res.getDispatchedPaths();
                    } else if (k == Dependency.class && v == Path.class) {
                        result = res.getDependencies();
                    }
                }
            } else {
                // collection
                DependencyResolverResult res = sessionV4
                        .getService(DependencyResolver.class)
                        .collect(sessionV4, project, PathScope.MAIN_RUNTIME);
                if (field.getType() == DependencyResolverResult.class) {
                    result = res;
                } else if (field.getType() == Node.class) {
                    result = res.getRoot();
                }
            }
            if (result == null) {
                throw new PluginConfigurationException(
                        pluginDescriptor,
                        "Unable to inject field '" + resolution.getField()
                                + "' annotated with @Dependencies. Unsupported type " + field.getGenericType());
            }
            try {
                field.set(mojo, result);
            } catch (IllegalAccessException e) {
                throw new PluginConfigurationException(
                        pluginDescriptor,
                        "Unable to inject field '" + resolution.getField() + "' annotated with @Dependencies",
                        e);
            }
        }

        return mojo;
    }