Skip to content

Commit

Permalink
feat: add check classes exists condition on event listener annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
duruer committed Jul 5, 2024
1 parent 53b2b99 commit d9d8504
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package co.statu.parsek.api.annotation

import co.statu.parsek.api.condition.CheckClassesExists
import org.springframework.context.annotation.Conditional
import org.springframework.stereotype.Component

@Target(AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@Component
@Conditional(CheckClassesExists::class)
annotation class EventListener(
val value: String = ""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package co.statu.parsek.api.condition

import org.springframework.context.annotation.Condition
import org.springframework.context.annotation.ConditionContext
import org.springframework.core.type.AnnotatedTypeMetadata
import org.springframework.core.type.ClassMetadata

class CheckClassesExists : Condition {
override fun matches(context: ConditionContext, metadata: AnnotatedTypeMetadata): Boolean {
if (metadata is ClassMetadata) {
val className = metadata.className
try {
val clazz = Class.forName(className)
clazz.interfaces // check if any of them are not exists
return true
} catch (e: NoClassDefFoundError) {
return false
} catch (e: ClassNotFoundException) {
return false
}
}

return false
}
}

0 comments on commit d9d8504

Please sign in to comment.