Skip to content

Instantly share code, notes, and snippets.

View ShikaSD's full-sized avatar
🍍
Being there, doing that

Andrei Shikov ShikaSD

🍍
Being there, doing that
View GitHub Profile
@Composable
fun Example() {
tag("div") {
tag("p") {
text("Paragraph 1")
}
tag("p") {
text("Paragraph 2")
}
tag("button") {
@OptIn(ExperimentalComposeApi::class)
class ServerApplyAdapter(
root: HtmlNode
) : AbstractApplier<HtmlNode>(root) {
override fun insert(index: Int, instance: HtmlNode) {
tag().insertAt(index, instance)
}
override fun remove(index: Int, count: Int) {
tag().remove(index, count)
sealed class HtmlNode {
class Tag(val tag: String): HtmlNode() {
private val children: List<HtmlNode> = mutableListOf()
// wordy implementations copy-pasted from compose
fun insert(index: Int, instance: HtmlNode)
fun move(from: Int, to: Int, count: Int)
fun remove(index: Int, count: Int)
}
private object TestObject : Serializable
@Test
fun `object instance is the same after deserialization`() {
assertEquals(TestObject, serializeDeserialize(TestObject))
}
private fun serializeDeserialize(instance: Serializable): Serializable {
val out = ByteArrayOutputStream()
ObjectOutputStream(out).use {
private val SERIALIZABLE_OBJECT = """
import java.io.Serializable
object Serial : Serializable
""".source()
@Test
fun `adds readResolve to obj extending Serializable`() {
compiler.sources = listOf(SERIALIZABLE_OBJECT)
val result = compiler.compile()
val klass = result.classLoader.loadClass("Serial")
if (codegen.descriptor.needsSerializableFix()) {
val selfType = codegen.typeMapper.mapType(codegen.descriptor)
codegen.addReadResolveFunction {
getstatic(codegen.className, "INSTANCE", selfType.descriptor)
areturn(selfType)
}
}
private fun ImplementationBodyCodegen.addReadResolveFunction(
block: InstructionAdapter.() -> Unit
) {
val visitor = v.newMethod(
NO_ORIGIN,
ACC_PUBLIC or ACC_SYNTHETIC,
SERIALIZABLE_READ.identifier,
"()Ljava/lang/Object;",
null,
EMPTY_STRING_ARRAY
fun ClassDescriptor.hasReadMethod() =
unsubstitutedMemberScope.getContributedFunctions(
SERIALIZABLE_READ,
NoLookupLocation.FROM_BACKEND
)
.any { it.name == SERIALIZABLE_READ && it.valueParameters.isEmpty() }
val SERIALIZABLE_READ = Name.identifier("readResolve")
fun ClassDescriptor.isSerializable(): Boolean =
getSuperInterfaces().any {
it.fqNameSafe == SERIALIZABLE_FQ_NAME || it.isSerializable()
} || getSuperClassNotAny()?.isSerializable() == true
val SERIALIZABLE_FQ_NAME = FqName("java.io.Serializable")
fun ClassDescriptor.needsSerializableFix() =
DescriptorUtils.isObject(this)
&& isSerializable()
&& !hasReadMethod()