Copilot commented on code in PR #1150:
URL: 
https://github.com/apache/maven-plugin-tools/pull/1150#discussion_r3743987581


##########
maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaSourceModel.java:
##########
@@ -0,0 +1,329 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.tools.plugin.extractor.annotations;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import com.github.javaparser.ParseResult;
+import com.github.javaparser.ParserConfiguration;
+import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.body.TypeDeclaration;
+import com.github.javaparser.ast.modules.ModuleExportsDirective;
+import com.github.javaparser.resolution.TypeSolver;
+import 
com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
+import com.github.javaparser.resolution.model.SymbolReference;
+import com.github.javaparser.symbolsolver.JavaSymbolSolver;
+import 
com.github.javaparser.symbolsolver.resolution.typesolvers.ClassLoaderTypeSolver;
+import 
com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
+import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
+import 
com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
+import 
com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
+import com.github.javaparser.utils.SourceRoot;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Source declarations and type resolution used while extracting Javadocs. */
+public final class JavaSourceModel implements Closeable {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(JavaSourceModel.class);
+
+    private static final Set<String> JAVA_RUNTIME_PACKAGES = 
javaRuntimePackages();
+
+    private final Charset encoding;
+    private final Set<Path> sourceDirectories = new LinkedHashSet<>();
+    private final Set<Path> classPathEntries = new LinkedHashSet<>();
+    private final Map<String, TypeDeclaration<?>> types = new 
LinkedHashMap<>();
+    private final Map<String, String> modulesByExportedPackage = new 
LinkedHashMap<>();
+    private final Set<String> packages = new 
LinkedHashSet<>(JAVA_RUNTIME_PACKAGES);
+    private final Set<String> internalPackages = new LinkedHashSet<>();
+
+    private ParserConfiguration parserConfiguration;
+    private CombinedTypeSolver typeSolver;
+    private URLClassLoader classPathLoader;
+    private boolean parsed;
+
+    public JavaSourceModel(Charset encoding) {
+        this.encoding = encoding;
+    }
+
+    public void addSourceDirectory(File directory) throws IOException {
+        if (directory != null && directory.isDirectory()) {
+            sourceDirectories.add(directory.toPath().toRealPath());
+        }
+    }
+
+    public void addClassPathEntry(File entry) throws IOException {
+        if (entry != null && entry.exists()) {
+            classPathEntries.add(entry.toPath().toRealPath());
+        }
+    }
+
+    public void parse() throws IOException {
+        if (parsed) {
+            return;
+        }
+        parsed = true;
+

Review Comment:
   `parsed` is set to `true` before any parsing work happens. If `parse()` 
throws partway through (e.g., `JarTypeSolver`/I/O problems), the model remains 
marked as parsed and `ensureParsed()` will allow operations on a partially 
initialized/empty index, and callers cannot retry `parse()`. `parsed` should 
only flip to `true` after successful completion, or be reset on failure (e.g., 
via `try/finally`).



##########
maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java:
##########
@@ -242,19 +250,19 @@ private Map<String, MojoAnnotatedClass> 
scanAnnotations(PluginToolsRequest reque
         return result;
     }
 
-    private JavaProjectBuilder scanJavadoc(
-            PluginToolsRequest request, Collection<MojoAnnotatedClass> 
mojoAnnotatedClasses)
-            throws ExtractionException {
+    private JavaSourceModel scanJavadoc(PluginToolsRequest request, 
Collection<MojoAnnotatedClass> mojoAnnotatedClasses)
+            throws ExtractionException, IOException {
         // found artifact from reactors to scan sources
         // we currently only scan sources from reactors
         List<MavenProject> mavenProjects = new ArrayList<>();
 
         // if we need to scan sources from external artifacts
         Set<Artifact> externalArtifacts = new HashSet<>();
 
-        JavaProjectBuilder builder = new JavaProjectBuilder(new 
SortedClassLibraryBuilder());
-        builder.setEncoding(request.getEncoding());
-        extendJavaProjectBuilder(request, builder, request.getProject());
+        Charset encoding =
+                request.getEncoding() == null ? StandardCharsets.UTF_8 : 
Charset.forName(request.getEncoding());
+        JavaSourceModel sourceModel = new JavaSourceModel(encoding);
+        extendJavaSourceModel(request, sourceModel, request.getProject());

Review Comment:
   `Charset.forName(request.getEncoding())` can throw 
`IllegalArgumentException` (e.g., unsupported/illegal charset name). This 
bypasses the surrounding `IOException` handling and will fail extraction with 
an uncaught runtime exception and a less helpful message. Consider catching 
this and rethrowing as an `ExtractionException` that includes the configured 
encoding value.



-- 
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