gnodet-bot commented on code in PR #13982:
URL: https://github.com/apache/maven/pull/13982#discussion_r4561106155


##########
impl/maven-plugin-mixin/src/test/java/org/apache/maven/plugin/processor/MojoDescriptorProcessorTest.java:
##########
@@ -61,60 +63,25 @@
  */
 class MojoDescriptorProcessorTest {
 
-    /** Resolved classpath jars needed for compiling test sources. */
-    private static List<File> classpathJars;
+    private static List<Path> classpathEntries;
 
-    /** The processor JAR (contains the compiled MojoDescriptorProcessor). */
-    private static File processorJar;
+    private static Path processorEntry;
 
     @BeforeAll
     static void resolveClasspath() {
-        String version = System.getProperty("processor.version", 
"4.2.0-SNAPSHOT");
-
-        // Processor JAR — built from this module
-        processorJar = resolveFile(
-                "impl/maven-plugin-mixin/target/maven-plugin-mixin-" + version 
+ ".jar",
-                "maven-plugin-mixin/" + version + "/maven-plugin-mixin-" + 
version + ".jar");
-
-        // Annotation JARs needed on the compilation classpath
-        File apiCore = resolveFile(
-                "api/maven-api-core/target/maven-api-core-" + version + ".jar",
-                "maven-api-core/" + version + "/maven-api-core-" + version + 
".jar");
-        File apiAnnotations = resolveFile(
-                "api/maven-api-annotations/target/maven-api-annotations-" + 
version + ".jar",
-                "maven-api-annotations/" + version + "/maven-api-annotations-" 
+ version + ".jar");
-
-        classpathJars = List.of(apiCore, apiAnnotations);
+        processorEntry = classpathEntryOf(MojoDescriptorProcessor.class);
+        classpathEntries = List.of(
+                
classpathEntryOf(org.apache.maven.api.plugin.annotations.Mojo.class),
+                
classpathEntryOf(org.apache.maven.api.annotations.Nonnull.class));
     }
 
-    /**
-     * Try to resolve a JAR first from the Maven multi-module project build 
output, then from the local Maven repo.
-     *
-     * <p>In a Maven build, {@code maven.multiModuleProjectDirectory} points 
to the project root, so
-     * {@code worktreeRelative} is resolved against that directory to find 
freshly-compiled JARs.
-     * The {@code processor.worktree} system property can override the root 
for IDE / manual runs.</p>
-     */
-    private static File resolveFile(String worktreeRelative, String 
m2Relative) {
-        // Prefer the Maven multi-module project directory (set by Maven 
during build)
-        String worktreeBase =
-                System.getProperty("processor.worktree", 
System.getProperty("maven.multiModuleProjectDirectory", ""));
-        String m2Base = System.getProperty(
-                "processor.m2", System.getProperty("user.home") + 
"/.m2/repository/org/apache/maven");
-
-        if (worktreeRelative != null && !worktreeBase.isEmpty()) {
-            File f = new File(worktreeBase, worktreeRelative);
-            if (f.isFile()) {
-                return f;
-            }
-        }
-        if (m2Relative != null) {
-            File f = new File(m2Base, m2Relative);
-            if (f.isFile()) {
-                return f;
-            }
+    private static Path classpathEntryOf(Class<?> anchor) {
+        try {
+            return Paths.get(
+                    
anchor.getProtectionDomain().getCodeSource().getLocation().toURI());
+        } catch (URISyntaxException e) {

Review Comment:
   `ProtectionDomain.getCodeSource()` can return `null` (bootstrap classes, 
custom classloaders, some test runners). The chained 
`.getCodeSource().getLocation().toURI()` will throw an unhelpful `NPE` in those 
cases, bypassing the `catch (URISyntaxException e)` block. Consider guarding:
   
   ```java
   CodeSource cs = anchor.getProtectionDomain().getCodeSource();
   if (cs == null) {
       return fail("Cannot resolve classpath entry for " + anchor.getName() + 
": no code source");
   }
   return Paths.get(cs.getLocation().toURI());
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to