From 3ff696d1a9b529348e186fe187a015c8075545e3 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Mon, 11 Nov 2024 22:20:34 +0100 Subject: [PATCH] Fix StackOverFlow error on Kotlin classes without @Extension (#595) --- pf4j/pom.xml | 6 ++++++ pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java | 7 ++++++- .../test/java/org/pf4j/AbstractExtensionFinderTest.java | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pf4j/pom.xml b/pf4j/pom.xml index 7061db6..9286d7e 100644 --- a/pf4j/pom.xml +++ b/pf4j/pom.xml @@ -199,6 +199,12 @@ + + org.jetbrains.kotlin + kotlin-stdlib + 2.0.21 + test + diff --git a/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java b/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java index 59488ba..caa415f 100644 --- a/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java +++ b/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java @@ -358,7 +358,12 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin // search recursively through all annotations for (Annotation annotation : clazz.getAnnotations()) { Class annotationClass = annotation.annotationType(); - if (!annotationClass.getName().startsWith("java.lang.annotation")) { + if (!annotationClass.getName().startsWith("java.lang.annotation") && !annotationClass.getName().startsWith("kotlin")) { + // In case an annotation is annotated with itself, + // an example would be @Target, which is ignored by the check above + // however, other libraries might do similar things + if (annotationClass.equals(clazz)) continue; + Extension extensionAnnotation = findExtensionAnnotation(annotationClass); if (extensionAnnotation != null) { return extensionAnnotation; diff --git a/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java b/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java index 1e4b5eb..5d31f5a 100644 --- a/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java +++ b/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java @@ -15,6 +15,7 @@ */ package org.pf4j; +import kotlin.sequences.Sequence; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -274,4 +275,11 @@ public class AbstractExtensionFinderTest { Assertions.assertNull(extension); } + // This is a regression test, as this caused an StackOverflowError with the previous implementation + @Test + public void runningOnNonExtensionKotlinClassDoesNotThrowException() { + Extension result = AbstractExtensionFinder.findExtensionAnnotation(Sequence.class); + + Assertions.assertNull(result); + } } -- 2.39.5