-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add check classes exists condition on event listener annotation
- Loading branch information
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/co/statu/parsek/api/annotation/EventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" | ||
) |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/co/statu/parsek/api/condition/CheckClassesExists.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |