gnodet-bot commented on code in PR #31223:
URL: https://github.com/apache/camel/pull/31223#discussion_r4764794873


##########
components/camel-jandex/src/main/java/org/apache/camel/jandex/JandexPackageScanClassResolver.java:
##########
@@ -115,37 +149,19 @@ private void findByAnnotation(Class<? extends Annotation> 
c, Set<Class<?>> class
         }
     }
 
-    private void findBySubClass(Class<?> c, Set<Class<?>> classes) {
+    private static void addCandidate(ClassInfo ci, Set<String> names) {
+        // annotation types are abstract, so they are skipped too
+        if (ci.nestingType() == ClassInfo.NestingType.TOP_LEVEL && 
!ci.isAbstract()) {
+            names.add(ci.name().toString());
+        }
+    }
+
+    private void findBySubClass(Class<?> c, Set<String> names) {
         for (Index index : indexes) {
-            if (c.isInterface()) {
-                for (var ai : index.getAllKnownImplementations(c)) {
-                    if (!ai.asClass().isAbstract()) {
-                        String currentClass = ai.asClass().name().toString();
-                        for (ClassLoader cl : getClassLoaders()) {
-                            try {
-                                Class<?> clazz = cl.loadClass(currentClass);
-                                classes.add(clazz);
-                                break;
-                            } catch (ClassNotFoundException e) {
-                                // ignore
-                            }
-                        }
-                    }
-                }
-            } else {
-                for (var ai : index.getAllKnownSubclasses(c)) {
-                    if (!ai.isAbstract()) {
-                        String currentClass = ai.asClass().name().toString();
-                        for (ClassLoader cl : getClassLoaders()) {
-                            try {
-                                Class<?> clazz = cl.loadClass(currentClass);
-                                classes.add(clazz);
-                                break;
-                            } catch (ClassNotFoundException e) {
-                                // ignore
-                            }
-                        }
-                    }
+            var found = c.isInterface() ? index.getAllKnownImplementations(c) 
: index.getAllKnownSubclasses(c);
+            for (ClassInfo ci : found) {
+                if (!ci.isAbstract()) {
+                    names.add(ci.name().toString());
                 }
             }
         }

Review Comment:
   **Low:** `findBySubClass` checks only `!ci.isAbstract()` but not 
`ci.nestingType() == TOP_LEVEL`, unlike `addCandidate()` which enforces both 
constraints. A concrete nested class that implements/extends a scanned type 
would be included here but excluded via the annotation path — an inconsistency. 
Use `addCandidate()` for uniformity:
   
   ```suggestion
       private void findBySubClass(Class<?> c, Set<String> names) {
           for (Index index : indexes) {
               var found = c.isInterface() ? 
index.getAllKnownImplementations(c) : index.getAllKnownSubclasses(c);
               for (ClassInfo ci : found) {
                   addCandidate(ci, names);
               }
           }
       }
   ```



##########
components/camel-jandex/src/test/java/org/apache/camel/jandex/JandexPackageScanClassResolverTest.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.camel.jandex;
+
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Set;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.jandex.scan.Greeter;
+import org.apache.camel.jandex.scan.InheritedBase;
+import org.apache.camel.jandex.scan.Scanned;
+import org.apache.camel.jandex.scan.a.AlphaBean;
+import org.apache.camel.jandex.scan.a.sub.SubBean;
+import org.apache.camel.jandex.scan.ab.OtherBean;
+import org.apache.camel.jandex.scan.meta.InheritedScanned;
+import org.apache.camel.jandex.scan.meta.InheritingBean;
+import org.apache.camel.jandex.scan.meta.Stereotype;
+import org.apache.camel.jandex.scan.meta.StereotypedBean;
+import org.apache.camel.support.scan.DefaultPackageScanClassResolver;
+import org.jboss.jandex.IndexWriter;
+import org.jboss.jandex.Indexer;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The resolver answers from the jandex.idx files instead of scanning the 
classpath, so it must still honor the two
+ * things a classpath scan honors by construction: the package to scan, and 
the filter that decides which classes match.
+ */
+public class JandexPackageScanClassResolverTest {
+
+    private static final String PACKAGE_A = "org.apache.camel.jandex.scan.a";
+    private static final String PACKAGE_META = 
"org.apache.camel.jandex.scan.meta";
+
+    @TempDir
+    Path indexDir;
+
+    private URLClassLoader indexLoader;
+    private CamelContext context;
+    private JandexPackageScanClassResolver resolver;
+
+    @BeforeEach
+    public void setUp() throws Exception {
+        // index the fixture classes into a META-INF/jandex.idx that only this 
test sees
+        Indexer indexer = new Indexer();
+        for (Class<?> clazz : new Class<?>[] {
+                AlphaBean.class, SubBean.class, OtherBean.class, 
Stereotype.class, StereotypedBean.class,
+                InheritedScanned.class, InheritedBase.class, 
InheritingBean.class }) {
+            indexer.indexClass(clazz);
+        }
+        Path file = 
Files.createDirectories(indexDir.resolve("META-INF")).resolve("jandex.idx");
+        try (OutputStream os = Files.newOutputStream(file)) {
+            new IndexWriter(os).write(indexer.complete());
+        }
+
+        indexLoader = new URLClassLoader(new URL[] { indexDir.toUri().toURL() 
}, getClass().getClassLoader());
+        context = new DefaultCamelContext();
+        context.setApplicationContextClassLoader(indexLoader);
+
+        resolver = new JandexPackageScanClassResolver();
+        resolver.setCamelContext(context);
+        resolver.start();
+    }
+
+    @AfterEach
+    public void tearDown() throws Exception {
+        resolver.stop();
+        context.stop();
+        indexLoader.close();
+    }
+
+    @Test
+    public void findAnnotatedOnlyInPackageAndSubPackages() {
+        // OtherBean is annotated too, but lives in the sibling package scan.ab
+        assertEquals(Set.of(AlphaBean.class, SubBean.class), 
resolver.findAnnotated(Scanned.class, PACKAGE_A));
+    }
+
+    @Test
+    public void findAnnotatedAnyOnlyInPackage() {
+        assertEquals(Set.of(AlphaBean.class, SubBean.class), 
resolver.findAnnotated(Set.of(Scanned.class), PACKAGE_A));
+    }
+
+    @Test
+    public void findImplementationsOnlyInPackage() {

Review Comment:
   **Low:** `findImplementationsOnlyInPackage` tests the 
`getAllKnownImplementations` branch (interface). The `getAllKnownSubclasses` 
branch (concrete class extension) has no test. Consider adding a fixture with a 
concrete base class and a subclass to verify that `findByFilter(AssignableTo)` 
via a plain class parent is also covered.



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