Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Classes should not be loaded from the plugin #19010

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft

Conversation

Artur-
Copy link
Member

@Artur- Artur- commented Mar 21, 2024

Related-to #19009

Copy link

sonarcloud bot commented Mar 21, 2024

Quality Gate Passed Quality Gate passed

Issues
9 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link

github-actions bot commented Mar 21, 2024

Test Results

1 142 files  ± 0  1 142 suites  ±0   1h 22m 17s ⏱️ - 5m 10s
7 456 tests ± 0  7 406 ✅ ± 0  50 💤 ±0  0 ❌ ±0 
7 748 runs   - 78  7 688 ✅  - 78  60 💤 ±0  0 ❌ ±0 

Results for commit 4ba9429. ± Comparison against base commit b2993c2.

♻️ This comment has been updated with latest results.

@Artur-
Copy link
Member Author

Artur- commented Mar 27, 2024

Does @mcollovati, @platosha or somebody else know why this should not be done? The current implementation finds Flow classes the plugin was built with and not from the project. No tests here fail, does Hilla rely on this somehow?

@mcollovati
Copy link
Collaborator

The only reason I can see against null parent is that the provided URLs might not cover all possible location for parent classes, so a specific class cannot be loaded because the classloader cannot find one ancestor.

Looking at the flow maven plugin code, it looks like we provide all runtime dependencies (compile + runtime scopes) but we filter out provided and system to only get portlet and servlet-api.
However, according to the documentation, the plugin class loader should not contain the project dependencies, so the execution may fail anyway for dependencies excluded from URL. Unless the same dependencies are defined as plugin dependencies.

I wonder if we should provide to ReflectionsClassFinder also a class loader, and create that one based on all MavenProject dependencies.
Or temporarily change the context class loader during mojo execution.

@mcollovati
Copy link
Collaborator

The Surefire plugin for in-process execution uses a custom class loader, temporarily set as the current thread context class loader. The classloader is built on project dependencies ( getProject().getArtifacts()).

        @Override
        public RunResult invoke(Object forkTestSet) throws ReporterException, InvocationTargetException {
            ClassLoader current = swapClassLoader(testsClassLoader);
            try {
                Method invoke = getMethod(providerInOtherClassLoader.getClass(), "invoke", INVOKE_PARAMETERS);
                Object result = invokeMethodWithArray2(providerInOtherClassLoader, invoke, forkTestSet);
                return (RunResult) surefireReflector.convertIfRunResult(result);
            } finally {
                if (System.getSecurityManager() == null) {
                    Thread.currentThread().setContextClassLoader(current);
                }
            }
        }

        private ClassLoader swapClassLoader(ClassLoader newClassLoader) {
            ClassLoader current = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(newClassLoader);
            return current;
        }

@Artur-
Copy link
Member Author

Artur- commented Apr 3, 2024

and presumably the built test classloader does not use the plugin classloader as its parent?

@Artur-
Copy link
Member Author

Artur- commented Aug 13, 2024

Added the platform classloader as parent..

Copy link

sonarcloud bot commented Aug 13, 2024

@Artur-
Copy link
Member Author

Artur- commented Sep 24, 2024

Should we merge this for 24.6 or try to figure out if it breaks something or is there some better approach?

Copy link

sonarcloud bot commented Oct 11, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants