Skip to content

Commit

Permalink
Various fixes, ktfmt, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers committed Feb 3, 2024
1 parent 253f18e commit 58c04fd
Show file tree
Hide file tree
Showing 51 changed files with 323 additions and 376 deletions.
6 changes: 1 addition & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ spotless {
target("**/*.kt")
trimTrailingWhitespace()
endWithNewline()
targetExclude(
"**/Dependencies.kt",
"**/spotless.kt",
"**/build/**",
)
targetExclude("**/Dependencies.kt", "**/spotless.kt", "**/build/**")
}
kotlinGradle {
ktfmt(ktfmtVersion).googleStyle()
Expand Down
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
[versions]
agp = "8.2.1"
autoService = "1.1.1"
gjf = "1.13.0"
gjf = "1.17.0"
incap = "1.0.0"
jdk = "20"
jdk = "21"
jvmTarget = "11"
kotlin = "1.9.22"
kotlinCompileTesting = "0.4.0"
kotlinpoet = "1.15.3"
ksp = "1.9.22-1.0.16"
ktfmt = "0.46"
ktfmt = "0.47"
moshi = "1.15.0"
okhttp = "4.12.0"
retrofit = "2.9.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public annotation class TrackUnknownKeys {
private val shouldTrack: (clazz: Class<*>, annotations: Set<Annotation>) -> Boolean = { _, _ ->
true
},
private val tracker: UnknownKeysTracker
private val tracker: UnknownKeysTracker,
) : JsonAdapter.Factory {
override fun create(type: Type, annotations: Set<Annotation>, moshi: Moshi): JsonAdapter<*>? {
if (type !is Class<*>) return null
Expand All @@ -95,7 +95,7 @@ public annotation class TrackUnknownKeys {
private class TrackUnknownKeysJsonAdapter<T>(
private val delegate: JsonAdapter<T>,
private val clazz: Class<*>,
private val tracker: UnknownKeysTracker
private val tracker: UnknownKeysTracker,
) : JsonAdapter<T>() {
override fun fromJson(reader: JsonReader): T? {
val token = reader.peek()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ class AdaptedByTest {
.isEqualTo(
ClassUsingAnnotatedClasses(
AnnotatedStringAlias("value"),
AnnotatedFactoryStringAlias("value")
AnnotatedFactoryStringAlias("value"),
)
)
}

data class ClassUsingAnnotatedClasses(
val alias1: AnnotatedStringAlias,
val alias2: AnnotatedFactoryStringAlias
val alias2: AnnotatedFactoryStringAlias,
)

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ class JsonStringTest {
}

@JsonClass(generateAdapter = true)
data class ExampleClass(
val type: Int,
@JsonString val rawJson: String,
)
data class ExampleClass(val type: Int, @JsonString val rawJson: String)

@Test
fun nullableCase() {
Expand All @@ -68,10 +65,7 @@ class JsonStringTest {
}

@JsonClass(generateAdapter = true)
data class NullableExampleClass(
val type: Int,
@JsonString val rawJson: String?,
)
data class NullableExampleClass(val type: Int, @JsonString val rawJson: String?)

@Test
fun retrofitServiceMethodCase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ internal class AppliedType private constructor(val type: IrClass) {
* Returns all super classes of this, recursively. Only [IrClass] is used as we can't really use
* other types.
*/
fun superclasses(
pluginContext: IrPluginContext,
): LinkedHashSet<AppliedType> {
fun superclasses(pluginContext: IrPluginContext): LinkedHashSet<AppliedType> {
val result: LinkedHashSet<AppliedType> = LinkedHashSet()
result.add(this)
for (supertype in type.getAllSuperclasses()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.jetbrains.kotlin.name.Name
internal open class BaseSymbols(
protected val irBuiltIns: IrBuiltIns,
protected val moduleFragment: IrModuleFragment,
val pluginContext: IrPluginContext
val pluginContext: IrPluginContext,
) {
constructor(
other: BaseSymbols
Expand Down Expand Up @@ -138,7 +138,7 @@ internal open class BaseSymbols(
protected fun createPackage(packageName: String): IrPackageFragment =
IrExternalPackageFragmentImpl.createEmptyExternalPackageFragment(
moduleFragment.descriptor,
FqName(packageName)
FqName(packageName),
)

protected fun createClass(
Expand All @@ -147,7 +147,7 @@ internal open class BaseSymbols(
classKind: ClassKind,
classModality: Modality,
isValueClass: Boolean = false,
body: IrClass.() -> Unit = {}
body: IrClass.() -> Unit = {},
): IrClassSymbol =
irFactory
.buildClass {
Expand All @@ -169,7 +169,7 @@ internal open class BaseSymbols(
endOffset,
context.irBuiltIns.kClassClass.starProjectedType,
context.irBuiltIns.kClassClass,
classType
classType,
)

private fun IrBuilderWithScope.kClassToJavaClass(kClassReference: IrExpression) =
Expand All @@ -185,7 +185,7 @@ internal open class BaseSymbols(
fun javaClassReference(
irBuilder: IrBuilderWithScope,
classType: IrType,
forceObjectType: Boolean = false
forceObjectType: Boolean = false,
) =
with(irBuilder) {
val kClassReference = kClassReference(classType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,48 +42,43 @@ public class MoshiCommandLineProcessor : CommandLineProcessor {
valueDescription = "<true | false>",
description = KEY_ENABLED.toString(),
required = true,
allowMultipleOccurrences = false
allowMultipleOccurrences = false,
)
val OPTION_DEBUG =
CliOption(
optionName = "debug",
valueDescription = "<true | false>",
description = KEY_DEBUG.toString(),
required = false,
allowMultipleOccurrences = false
allowMultipleOccurrences = false,
)
val OPTION_ENABLE_SEALED =
CliOption(
optionName = "enableSealed",
valueDescription = "<true | false>",
description = KEY_GENERATED_ANNOTATION.toString(),
required = false,
allowMultipleOccurrences = false
allowMultipleOccurrences = false,
)
val OPTION_GENERATED_ANNOTATION =
CliOption(
optionName = "generatedAnnotation",
valueDescription = "String",
description = KEY_ENABLE_SEALED.toString(),
required = false,
allowMultipleOccurrences = false
allowMultipleOccurrences = false,
)
}

override val pluginId: String = "dev.zacsweers.moshix.compiler"

override val pluginOptions: Collection<AbstractCliOption> =
listOf(
OPTION_DEBUG,
OPTION_ENABLED,
OPTION_ENABLE_SEALED,
OPTION_GENERATED_ANNOTATION,
)
listOf(OPTION_DEBUG, OPTION_ENABLED, OPTION_ENABLE_SEALED, OPTION_GENERATED_ANNOTATION)

override fun processOption(
option: AbstractCliOption,
value: String,
configuration: CompilerConfiguration
configuration: CompilerConfiguration,
): Unit =
when (option.optionName) {
"enabled" -> configuration.put(KEY_ENABLED, value.toBoolean())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class MoshiIrGenerationExtension(
private val messageCollector: MessageCollector,
private val generatedAnnotationName: ClassId?,
private val enableSealed: Boolean,
private val debug: Boolean
private val debug: Boolean,
) : IrGenerationExtension {

override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
Expand All @@ -48,7 +48,7 @@ internal class MoshiIrGenerationExtension(
generatedAnnotation,
enableSealed,
deferred,
debug
debug,
)
moduleFragment.transform(moshiTransformer, null)
for ((file, adapters) in deferred.groupBy { it.irFile }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private val TargetProperty.isVisible: Boolean
*/
internal fun TargetProperty.generator(
originalType: IrClass,
errors: MutableList<(logger: MessageCollector) -> Unit>
errors: MutableList<(logger: MessageCollector) -> Unit>,
): PropertyGenerator? {
if (jsonIgnore) {
if (!hasDefault) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ internal class MoshiIrVisitor(

private val moshiSealedSymbols by lazy { MoshiSealedSymbols(moshiSymbols) }

private fun adapterGenerator(
originalType: IrClass,
): MoshiAdapterGenerator? {
private fun adapterGenerator(originalType: IrClass): MoshiAdapterGenerator? {
val type = targetType(originalType, pluginContext, messageCollector) ?: return null

val properties = mutableMapOf<String, PropertyGenerator>()
Expand Down Expand Up @@ -123,7 +121,7 @@ internal class MoshiIrVisitor(
moshiSymbols = moshiSymbols,
moshiSealedSymbols = moshiSealedSymbols,
target = declaration,
labelKey = labelKey
labelKey = labelKey,
)
} else {
return super.visitClassNew(declaration)
Expand All @@ -143,7 +141,7 @@ internal class MoshiIrVisitor(
val irSrc = adapterClass.adapterClass.dumpSrc()
messageCollector.report(
CompilerMessageSeverity.STRONG_WARNING,
"MOSHI: Dumping current IR src for ${adapterClass.adapterClass.name}\n$irSrc"
"MOSHI: Dumping current IR src for ${adapterClass.adapterClass.name}\n$irSrc",
)
}
deferredAddedClasses += GeneratedAdapter(adapterClass.adapterClass, declaration.file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import org.jetbrains.kotlin.name.Name
internal class MoshiSymbols(
irBuiltIns: IrBuiltIns,
moduleFragment: IrModuleFragment,
pluginContext: IrPluginContext
pluginContext: IrPluginContext,
) : BaseSymbols(irBuiltIns, moduleFragment, pluginContext) {
private val moshiPackage: IrPackageFragment by lazy { createPackage("com.squareup.moshi") }
private val moshiInternalPackage: IrPackageFragment by lazy {
Expand All @@ -71,7 +71,7 @@ internal class MoshiSymbols(
addFunction(
Name.identifier("beginObject").identifier,
irBuiltIns.unitType,
Modality.ABSTRACT
Modality.ABSTRACT,
)
addFunction(Name.identifier("endObject").identifier, irBuiltIns.unitType, Modality.ABSTRACT)
addFunction(Name.identifier("skipName").identifier, irBuiltIns.unitType, Modality.ABSTRACT)
Expand All @@ -80,7 +80,7 @@ internal class MoshiSymbols(
addFunction(
Name.identifier("hasNext").identifier,
irBuiltIns.booleanType,
Modality.ABSTRACT
Modality.ABSTRACT,
)

val optionsClass = createClass(this, "Options", ClassKind.CLASS, Modality.FINAL)
Expand Down Expand Up @@ -157,22 +157,22 @@ internal class MoshiSymbols(
addFunction(
Name.identifier("adapter").identifier,
jsonAdapter.typeWith(irBuiltIns.anyNType),
Modality.FINAL
Modality.FINAL,
)
.apply {
addTypeParameter("T", irBuiltIns.anyNType)
addValueParameter("type", type.defaultType)
addValueParameter(
"annotations",
irBuiltIns.setClass.typeWith(irBuiltIns.annotationType)
irBuiltIns.setClass.typeWith(irBuiltIns.annotationType),
)
addValueParameter("fieldName", irBuiltIns.stringType)
}

addFunction(
Name.identifier("newBuilder").identifier,
moshiBuilder.defaultType,
Modality.FINAL
Modality.FINAL,
)
}
.symbol
Expand Down Expand Up @@ -205,7 +205,7 @@ internal class MoshiSymbols(
addFunction(
Name.identifier("fromJson").identifier,
t.defaultType.makeNullable(),
Modality.ABSTRACT
Modality.ABSTRACT,
)
.apply { addValueParameter("reader", jsonReader.defaultType) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ internal fun targetType(
)
}

internal fun primaryConstructor(
targetType: IrClass,
): TargetConstructor? {
internal fun primaryConstructor(targetType: IrClass): TargetConstructor? {
val primaryConstructor = targetType.primaryConstructor ?: return null

val parameters = LinkedHashMap<String, TargetParameter>()
Expand All @@ -180,15 +178,11 @@ internal fun primaryConstructor(
hasDefault = parameter.hasDefaultValue(),
qualifiers = parameter.jsonQualifiers(),
jsonIgnore = parameter.jsonIgnore(),
jsonName = parameter.jsonName()
jsonName = parameter.jsonName(),
)
}

return TargetConstructor(
primaryConstructor,
parameters,
primaryConstructor.visibility,
)
return TargetConstructor(primaryConstructor, parameters, primaryConstructor.visibility)
}

private fun declaredProperties(
Expand All @@ -208,7 +202,7 @@ private fun declaredProperties(
visibility = property.visibility,
jsonName = property.jsonNameFromAnywhere() ?: parameter?.jsonName ?: name,
jsonIgnore =
isTransient || parameter?.jsonIgnore == true || property.jsonIgnoreFromAnywhere()
isTransient || parameter?.jsonIgnore == true || property.jsonIgnoreFromAnywhere(),
)
}

Expand Down
Loading

0 comments on commit 58c04fd

Please sign in to comment.