-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for generic sealed classes (#152)
- Loading branch information
Showing
16 changed files
with
585 additions
and
206 deletions.
There are no files selected for viewing
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
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
23 changes: 23 additions & 0 deletions
23
compiler/src/main/kotlin/se/ansman/kotshi/MetadataAccessor.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,23 @@ | ||
package se.ansman.kotshi | ||
|
||
import com.squareup.kotlinpoet.TypeSpec | ||
import com.squareup.kotlinpoet.metadata.ImmutableKmClass | ||
import com.squareup.kotlinpoet.metadata.specs.ClassInspector | ||
import com.squareup.kotlinpoet.metadata.specs.toTypeSpec | ||
import com.squareup.kotlinpoet.metadata.toImmutableKmClass | ||
import javax.lang.model.element.Element | ||
|
||
class MetadataAccessor(private val classInspector: ClassInspector) { | ||
private val metadataPerType = mutableMapOf<Element, ImmutableKmClass>() | ||
private val typeSpecPerType = mutableMapOf<Element, TypeSpec>() | ||
|
||
fun getMetadata(type: Element): ImmutableKmClass = | ||
metadataPerType.getOrPut(type) { | ||
type.metadata.toImmutableKmClass() | ||
} | ||
|
||
fun getTypeSpec(type: Element): TypeSpec = | ||
typeSpecPerType.getOrPut(type) { | ||
getMetadata(type).toTypeSpec(classInspector) | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
compiler/src/main/kotlin/se/ansman/kotshi/SealedClassSubtype.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,84 @@ | ||
package se.ansman.kotshi | ||
|
||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.CodeBlock | ||
import com.squareup.kotlinpoet.Dynamic | ||
import com.squareup.kotlinpoet.LambdaTypeName | ||
import com.squareup.kotlinpoet.ParameterizedTypeName | ||
import com.squareup.kotlinpoet.TypeName | ||
import com.squareup.kotlinpoet.TypeVariableName | ||
import com.squareup.kotlinpoet.WildcardTypeName | ||
import com.squareup.kotlinpoet.asClassName | ||
import com.squareup.kotlinpoet.tag | ||
import se.ansman.kotshi.generators.typesParameter | ||
import java.lang.reflect.ParameterizedType | ||
import javax.lang.model.element.TypeElement | ||
|
||
class SealedClassSubtype( | ||
metadataAccessor: MetadataAccessor, | ||
val type: TypeElement, | ||
val label: String | ||
) : TypeRenderer() { | ||
val className = type.asClassName() | ||
val typeSpec = metadataAccessor.getTypeSpec(type) | ||
|
||
override fun renderTypeVariable(typeVariable: TypeVariableName): CodeBlock { | ||
val superParameters = (typeSpec.superclass as? ParameterizedTypeName) | ||
?.typeArguments | ||
?: emptyList() | ||
|
||
fun TypeName.findAccessor(typesIndex: Int): CodeBlock? { | ||
return when (this) { | ||
is ClassName, | ||
Dynamic, | ||
is LambdaTypeName -> null | ||
is WildcardTypeName -> { | ||
for (outType in outTypes) { | ||
outType.findAccessor(typesIndex)?.let { return it } | ||
} | ||
for (inType in inTypes) { | ||
inType.findAccessor(typesIndex)?.let { return it } | ||
} | ||
null | ||
} | ||
is TypeVariableName -> { | ||
if (name.contentEquals(typeVariable.name)) { | ||
CodeBlock.of("") | ||
} else { | ||
for (bound in bounds) { | ||
bound.findAccessor(typesIndex)?.let { return it } | ||
} | ||
null | ||
} | ||
} | ||
is ParameterizedTypeName -> { | ||
typeArguments.forEachIndexed { index, typeName -> | ||
val accessor = typeName.findAccessor(typesIndex) ?: return@forEachIndexed | ||
return CodeBlock.builder() | ||
.addControlFlow(".let") { | ||
add("it as? %T\n", ParameterizedType::class.java) | ||
indent() | ||
add("?: throw %T(%P)\n", IllegalArgumentException::class.java, "The type \${${typesParameter.name}[$typesIndex]} is not a valid type constraint for the \$this") | ||
unindent() | ||
} | ||
.add(".actualTypeArguments[%L]", index) | ||
.add(accessor) | ||
.build() | ||
} | ||
null | ||
} | ||
} | ||
} | ||
|
||
superParameters.forEachIndexed { index, superParameter -> | ||
val accessor = superParameter.findAccessor(index) ?: return@forEachIndexed | ||
return CodeBlock.builder() | ||
.add("%N[%L]\n", typesParameter, index) | ||
.indent() | ||
.add(accessor) | ||
.unindent() | ||
.build() | ||
} | ||
throw ProcessingError("Could not determine type variable type", typeVariable.tag() ?: type) | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
compiler/src/main/kotlin/se/ansman/kotshi/TypeRenderer.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,114 @@ | ||
/* | ||
* Copyright (C) 2018 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package se.ansman.kotshi | ||
|
||
import com.squareup.kotlinpoet.ARRAY | ||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.CodeBlock | ||
import com.squareup.kotlinpoet.ParameterizedTypeName | ||
import com.squareup.kotlinpoet.TypeName | ||
import com.squareup.kotlinpoet.TypeVariableName | ||
import com.squareup.kotlinpoet.WildcardTypeName | ||
import com.squareup.moshi.Types | ||
|
||
/** | ||
* Renders literals like `Types.newParameterizedType(List::class.java, String::class.java)`. | ||
* Rendering is pluggable so that type variables can either be resolved or emitted as other code | ||
* blocks. | ||
*/ | ||
abstract class TypeRenderer { | ||
abstract fun renderTypeVariable(typeVariable: TypeVariableName): CodeBlock | ||
|
||
fun render(typeName: TypeName, forceBox: Boolean = false): CodeBlock = | ||
when { | ||
typeName.annotations.isNotEmpty() -> render(typeName.copy(annotations = emptyList()), forceBox) | ||
typeName.isNullable -> renderObjectType(typeName.copy(nullable = false)) | ||
else -> when (typeName) { | ||
is ClassName -> { | ||
if (forceBox) { | ||
renderObjectType(typeName) | ||
} else { | ||
CodeBlock.of("%T::class.java", typeName) | ||
} | ||
} | ||
|
||
is ParameterizedTypeName -> { | ||
// If it's an Array type, we shortcut this to return Types.arrayOf() | ||
if (typeName.rawType == ARRAY) { | ||
CodeBlock.of( | ||
"%T.arrayOf(%L)", | ||
Types::class, | ||
renderObjectType(typeName.typeArguments[0]) | ||
) | ||
} else { | ||
val builder = CodeBlock.builder().apply { | ||
add("%T.", Types::class) | ||
val enclosingClassName = typeName.rawType.enclosingClassName() | ||
if (enclosingClassName != null) { | ||
add("newParameterizedTypeWithOwner(%L, ", render(enclosingClassName)) | ||
} else { | ||
add("newParameterizedType(") | ||
} | ||
add("%T::class.java", typeName.rawType) | ||
for (typeArgument in typeName.typeArguments) { | ||
add(", %L", renderObjectType(typeArgument)) | ||
} | ||
add(")") | ||
} | ||
builder.build() | ||
} | ||
} | ||
|
||
is WildcardTypeName -> { | ||
val target: TypeName | ||
val method: String | ||
when { | ||
typeName.inTypes.size == 1 -> { | ||
target = typeName.inTypes[0] | ||
method = "supertypeOf" | ||
} | ||
typeName.outTypes.size == 1 -> { | ||
target = typeName.outTypes[0] | ||
method = "subtypeOf" | ||
} | ||
else -> throw IllegalArgumentException( | ||
"Unrepresentable wildcard type. Cannot have more than one bound: $typeName" | ||
) | ||
} | ||
CodeBlock.of("%T.%L(%L)", Types::class, method, render(target, forceBox = true)) | ||
} | ||
|
||
is TypeVariableName -> renderTypeVariable(typeName) | ||
|
||
else -> throw IllegalArgumentException("Unrepresentable type: $typeName") | ||
} | ||
} | ||
|
||
private fun renderObjectType(typeName: TypeName): CodeBlock = | ||
if (typeName.isPrimitive) { | ||
CodeBlock.of("%T::class.javaObjectType", typeName) | ||
} else { | ||
render(typeName) | ||
} | ||
|
||
companion object { | ||
operator fun invoke(renderer: (TypeVariableName) -> CodeBlock): TypeRenderer = | ||
object : TypeRenderer() { | ||
override fun renderTypeVariable(typeVariable: TypeVariableName): CodeBlock = renderer(typeVariable) | ||
} | ||
} | ||
} |
Oops, something went wrong.