Visitar URL original
Fix overly broad classpath scanning in ReflectUtil.getClassesWithAnno… · apache/cloudstack@d4f5dff · GitHub
Skip to content

Commit d4f5dff

Browse files
vishesh92Ganesh Maharaj Mahalingam
andauthored
Fix overly broad classpath scanning in ReflectUtil.getClassesWithAnnotation() (#13427)
* Fix overly broad classpath scanning in ReflectUtil.getClassesWithAnnotation() Production logs show a recurring WARN at startup: "could not get type for name org.conscrypt.AllocatedBuffer" (org.reflections.ReflectionsException / ClassNotFoundException) Root cause: ReflectUtil.getClassesWithAnnotation() uses ClasspathHelper.forPackage() to collect URLs to scan, but this returns all classpath entries (JARs/directories), not just those containing the target package. The Reflections library then scans every .class file in every JAR — including Netty's ConscryptAlpnSslEngine$BufferAdapter which references the optional org.conscrypt.AllocatedBuffer type. During expandSuperTypes(), Reflections tries Class.forName() on that type and fails because Conscrypt is not (and need not be) on the classpath. The fix adds a FilterBuilder that restricts bytecode scanning to only classes whose fully-qualified names match the requested packages. This prevents Reflections from processing unrelated classes (e.g., io.netty.handler.ssl.*), eliminating the warning and reducing startup time by skipping thousands of irrelevant class files. Signed-off-by: Ganesh Maharaj Mahalingam <g.mahalingam@apple.com> * Add missing API command packages to ApiXmlDocWriter's classpath scan list --------- Signed-off-by: Ganesh Maharaj Mahalingam <g.mahalingam@apple.com> Co-authored-by: Ganesh Maharaj Mahalingam <g.mahalingam@apple.com>
1 parent cdd03d7 commit d4f5dff

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

‎server/src/main/java/com/cloud/api/doc/ApiXmlDocWriter.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ private static List<String> setAsyncResponses() {
9292
public static void main(String[] args) {
9393
Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class, new String[] {"org.apache.cloudstack.api", "com.cloud.api",
9494
"com.cloud.api.commands", "com.globo.globodns.cloudstack.api", "org.apache.cloudstack.network.opendaylight.api",
95-
"org.apache.cloudstack.api.command.admin.zone", "org.apache.cloudstack.network.contrail.api.command"});
95+
"org.apache.cloudstack.api.command.admin.zone", "org.apache.cloudstack.network.contrail.api.command",
96+
"org.apache.cloudstack.cloudian.api", "org.apache.cloudstack.framework.extensions.api",
97+
"org.apache.cloudstack.mom.webhook.api.command.user", "org.apache.cloudstack.network.tungsten.api.command",
98+
"org.apache.cloudstack.oauth2.api.command", "org.apache.cloudstack.storage.datastore.util"});
9699

97100
for (Class<?> cmdClass : cmdClasses) {
98101
if(cmdClass.getAnnotation(APICommand.class)==null){

‎utils/src/main/java/com/cloud/utils/ReflectUtil.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.reflections.scanners.TypeAnnotationsScanner;
4343
import org.reflections.util.ClasspathHelper;
4444
import org.reflections.util.ConfigurationBuilder;
45+
import org.reflections.util.FilterBuilder;
4546

4647
import com.cloud.utils.exception.CloudRuntimeException;
4748
import com.google.common.collect.ImmutableSet;
@@ -70,9 +71,12 @@ public static Set<Class<?>> getClassesWithAnnotation(Class<? extends Annotation>
7071
Reflections reflections;
7172
Set<Class<?>> classes = new HashSet<Class<?>>();
7273
ConfigurationBuilder builder=new ConfigurationBuilder();
74+
FilterBuilder filterBuilder = new FilterBuilder();
7375
for (String packageName : packageNames) {
7476
builder.addUrls(ClasspathHelper.forPackage(packageName));
77+
filterBuilder.includePackage(packageName);
7578
}
79+
builder.filterInputsBy(filterBuilder);
7680
builder.setScanners(new SubTypesScanner(),new TypeAnnotationsScanner());
7781
reflections = new Reflections(builder);
7882
classes.addAll(reflections.getTypesAnnotatedWith(annotation));

0 commit comments

Comments
 (0)