-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support usage of IdentityTransformation in the middle of an AST
- Loading branch information
1 parent
82a06fa
commit b20cdf6
Showing
3 changed files
with
168 additions
and
18 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
core/src/main/kotlin/com/strumenta/kolasu/transformation/IdentityTransformation.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,50 @@ | ||
package com.strumenta.kolasu.transformation | ||
|
||
import com.strumenta.kolasu.mapping.translateList | ||
import com.strumenta.kolasu.model.Node | ||
import java.lang.reflect.ParameterizedType | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KParameter | ||
import kotlin.reflect.full.isSubclassOf | ||
import kotlin.reflect.full.memberProperties | ||
import kotlin.reflect.full.primaryConstructor | ||
import kotlin.reflect.jvm.javaType | ||
|
||
val IDENTTITY_TRANSFORMATION: (source: Any?, parent: Node?, expectedType: KClass<out Node>, astTransformer: ASTTransformer) -> List<Node> = { | ||
source: Any?, parent: Node?, expectedType: KClass<out Node>, astTransformer: ASTTransformer -> | ||
when (source) { | ||
null -> { | ||
emptyList() | ||
} | ||
is Node -> { | ||
val kClass = source.javaClass.kotlin | ||
val primaryConstructor = kClass.primaryConstructor | ||
?: throw IllegalStateException("No primary constructor found for $kClass: cannot apply " + | ||
"identity transformation") | ||
val params = mutableMapOf<KParameter, Any?>() | ||
primaryConstructor.parameters.forEach { parameter -> | ||
val mt = parameter.type.javaType | ||
val correspondingProperty = source.javaClass.kotlin.memberProperties.find { it.name == parameter.name } ?: throw IllegalStateException("Cannot find property named as parameter ${parameter}") | ||
val originalValue = correspondingProperty.get(source) | ||
//mt is ParameterizedType && mt.rawType == List::class.java -> mutableListOf<Any>() | ||
when { | ||
(parameter.type.classifier as KClass<*>).isSubclassOf(Node::class) -> { | ||
params[parameter] = astTransformer.transform(originalValue) | ||
} | ||
mt is ParameterizedType && mt.rawType == List::class.java | ||
&& (mt.actualTypeArguments.first() as? Class<*>)?.kotlin?.isSubclassOf(Node::class) == true-> { | ||
params[parameter] = astTransformer.translateList<Node>(originalValue as List<Node>) | ||
} | ||
else -> params[parameter] = originalValue | ||
} | ||
} | ||
|
||
val newInstance = primaryConstructor.callBy(params) as Node | ||
newInstance.parent = parent | ||
listOf(newInstance) | ||
} | ||
else -> { | ||
throw IllegalArgumentException("An Identity Transformation expect to receive a Node") | ||
} | ||
} | ||
} |
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
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