From 3aea03254de791396b7a18981f32e8e2b149b8a3 Mon Sep 17 00:00:00 2001 From: Andrea Selva Date: Wed, 16 Jun 2021 12:15:02 +0200 Subject: [PATCH] Fix Reflections stack traces when process yml files in classpath and debug is enabled (#12991) Adds a filter to Reflections library initialization so that when it scan "org.logstash.plugins" it includes only .class files and avoid to load and process AliasRegistry.yml and plugin_aliases.yml Fixes #12992 (cherry picked from commit a6e9a6bcfd400377d0ceb1449f0cc8e18af60825) --- .../logstash/plugins/discovery/PluginRegistry.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/logstash-core/src/main/java/org/logstash/plugins/discovery/PluginRegistry.java b/logstash-core/src/main/java/org/logstash/plugins/discovery/PluginRegistry.java index 7fde7b4c154..4c7fd195c2d 100644 --- a/logstash-core/src/main/java/org/logstash/plugins/discovery/PluginRegistry.java +++ b/logstash-core/src/main/java/org/logstash/plugins/discovery/PluginRegistry.java @@ -20,8 +20,10 @@ package org.logstash.plugins.discovery; +import com.google.common.base.Predicate; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.checkerframework.checker.nullness.compatqual.NullableDecl; import org.logstash.plugins.AliasRegistry; import co.elastic.logstash.api.Codec; import co.elastic.logstash.api.Configuration; @@ -32,6 +34,8 @@ import co.elastic.logstash.api.Output; import org.logstash.plugins.PluginLookup.PluginType; import org.reflections.Reflections; +import org.reflections.util.ClasspathHelper; +import org.reflections.util.ConfigurationBuilder; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; @@ -80,7 +84,12 @@ public static PluginRegistry getInstance(AliasRegistry aliasRegistry) { private void discoverPlugins() { // the constructor of Reflection must be called only by one thread, else there is a // risk that the first thread that completes close the Zip files for the others. - Reflections reflections = new Reflections("org.logstash.plugins"); + // scan all .class present in package classpath + final ConfigurationBuilder configurationBuilder = new ConfigurationBuilder() + .setUrls(ClasspathHelper.forPackage("org.logstash.plugins")) + .filterInputsBy(input -> input.endsWith(".class")); + Reflections reflections = new Reflections(configurationBuilder); + Set> annotated = reflections.getTypesAnnotatedWith(LogstashPlugin.class); for (final Class cls : annotated) { for (final Annotation annotation : cls.getAnnotations()) {