Skip to content

Commit

Permalink
Add classloader parameter to PolymorphicAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
serivesmejia committed Oct 29, 2024
1 parent 8470509 commit 47bd2c7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class PluginOutput(
"Maven repository"
else "local file"

val sourceEnabled = if(loader.shouldEnable) "It was loaded from a $source." else "It is disabled, it comes from a $source."
val sourceEnabled = if(loader.shouldEnable) "It was LOADED from a $source." else "It is DISABLED, it comes from a $source."

val superAccess = if(loader.hasSuperAccess)
"It has super access."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import java.lang.reflect.Type

private val gson = Gson()

open class PolymorphicAdapter<T>(val name: String) : JsonSerializer<T>, JsonDeserializer<T> {
open class PolymorphicAdapter<T>(
val name: String,
val classloader: ClassLoader = PolymorphicAdapter::class.java.classLoader
) : JsonSerializer<T>, JsonDeserializer<T> {

override fun serialize(src: T, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
val obj = JsonObject()
Expand All @@ -42,7 +45,7 @@ open class PolymorphicAdapter<T>(val name: String) : JsonSerializer<T>, JsonDese
@Suppress("UNCHECKED_CAST")
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): T {
val className = json.asJsonObject.get("${name}Class").asString
val clazz = Class.forName(className)
val clazz = classloader.loadClass(className)

return gson.fromJson(json.asJsonObject.get(name), clazz) as T
}
Expand Down

0 comments on commit 47bd2c7

Please sign in to comment.