This file contains hidden or 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
@Composable | |
fun Example() { | |
tag("div") { | |
tag("p") { | |
text("Paragraph 1") | |
} | |
tag("p") { | |
text("Paragraph 2") | |
} | |
tag("button") { |
This file contains hidden or 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
@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) |
This file contains hidden or 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
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) | |
} | |
This file contains hidden or 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
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 { |
This file contains hidden or 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
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") |
This file contains hidden or 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
if (codegen.descriptor.needsSerializableFix()) { | |
val selfType = codegen.typeMapper.mapType(codegen.descriptor) | |
codegen.addReadResolveFunction { | |
getstatic(codegen.className, "INSTANCE", selfType.descriptor) | |
areturn(selfType) | |
} | |
} |
This file contains hidden or 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
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 |
This file contains hidden or 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
fun ClassDescriptor.hasReadMethod() = | |
unsubstitutedMemberScope.getContributedFunctions( | |
SERIALIZABLE_READ, | |
NoLookupLocation.FROM_BACKEND | |
) | |
.any { it.name == SERIALIZABLE_READ && it.valueParameters.isEmpty() } | |
val SERIALIZABLE_READ = Name.identifier("readResolve") |
This file contains hidden or 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
fun ClassDescriptor.isSerializable(): Boolean = | |
getSuperInterfaces().any { | |
it.fqNameSafe == SERIALIZABLE_FQ_NAME || it.isSerializable() | |
} || getSuperClassNotAny()?.isSerializable() == true | |
val SERIALIZABLE_FQ_NAME = FqName("java.io.Serializable") |
This file contains hidden or 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
fun ClassDescriptor.needsSerializableFix() = | |
DescriptorUtils.isObject(this) | |
&& isSerializable() | |
&& !hasReadMethod() |