-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from kategory/rr-include-kategory-annotations
Bring `kategory-annotations` to the `kategory` repo
- Loading branch information
Showing
169 changed files
with
1,539 additions
and
227 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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,20 @@ | ||
import org.gradle.internal.jvm.Jvm | ||
|
||
apply plugin: 'kotlin' | ||
apply plugin: 'kotlin-kapt' | ||
|
||
dependencies { | ||
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion" | ||
compile project(':kategory-annotations') | ||
compile 'com.squareup:kotlinpoet:0.4.0' | ||
compile 'me.eugeniomarletti:kotlin-metadata:1.1.0' | ||
compileOnly 'com.google.auto.service:auto-service:1.0-rc3' | ||
kapt 'com.google.auto.service:auto-service:1.0-rc3' | ||
|
||
testCompile group: 'junit', name: 'junit', version: '4.12' | ||
testCompile "com.google.testing.compile:compile-testing:0.6" | ||
testCompile fileTree(dir: './src/test/libs', includes: ['*.jar']) | ||
testCompile files(Jvm.current().getToolsJar()) | ||
} | ||
|
||
apply from: rootProject.file('gradle/gradle-mvn-push.gradle') |
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,4 @@ | ||
# Maven publishing configuration | ||
POM_NAME=Kategory Annotations Compile Time | ||
POM_ARTIFACT_ID=kategory-annotations-processor | ||
POM_PACKAGING=jar |
4 changes: 4 additions & 0 deletions
4
kategory-annotations-processor/src/main/java/kategory/common/TypeAliases.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,4 @@ | ||
package kategory.common | ||
|
||
typealias Type = String | ||
typealias Package = String |
34 changes: 34 additions & 0 deletions
34
kategory-annotations-processor/src/main/java/kategory/common/messager/MessagerExtensions.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,34 @@ | ||
package kategory.common.messager | ||
|
||
import me.eugeniomarletti.kotlin.processing.KotlinProcessingUtils | ||
import javax.lang.model.element.AnnotationMirror | ||
import javax.lang.model.element.AnnotationValue | ||
import javax.lang.model.element.Element | ||
import javax.tools.Diagnostic.Kind.ERROR | ||
import javax.tools.Diagnostic.Kind.MANDATORY_WARNING | ||
import javax.tools.Diagnostic.Kind.NOTE | ||
import javax.tools.Diagnostic.Kind.WARNING | ||
|
||
fun KotlinProcessingUtils.log(message: CharSequence, | ||
element: Element? = null, | ||
annotationMirror: AnnotationMirror? = null, | ||
annotationValue: AnnotationValue? = null | ||
) = messager.printMessage(NOTE, message, element, annotationMirror, annotationValue) | ||
|
||
fun KotlinProcessingUtils.logW(message: CharSequence, | ||
element: Element? = null, | ||
annotationMirror: AnnotationMirror? = null, | ||
annotationValue: AnnotationValue? = null | ||
) = messager.printMessage(WARNING, message, element, annotationMirror, annotationValue) | ||
|
||
fun KotlinProcessingUtils.logMW(message: CharSequence, | ||
element: Element? = null, | ||
annotationMirror: AnnotationMirror? = null, | ||
annotationValue: AnnotationValue? = null | ||
) = messager.printMessage(MANDATORY_WARNING, message, element, annotationMirror, annotationValue) | ||
|
||
fun KotlinProcessingUtils.logE(message: CharSequence, | ||
element: Element? = null, | ||
annotationMirror: AnnotationMirror? = null, | ||
annotationValue: AnnotationValue? = null | ||
) = messager.printMessage(ERROR, message, element, annotationMirror, annotationValue) |
34 changes: 34 additions & 0 deletions
34
kategory-annotations-processor/src/main/java/kategory/common/utils/AbstractProcessor.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,34 @@ | ||
package kategory.common.utils | ||
|
||
import kategory.common.messager.logE | ||
import me.eugeniomarletti.kotlin.processing.KotlinAbstractProcessor | ||
import javax.annotation.processing.RoundEnvironment | ||
import javax.lang.model.element.Element | ||
import javax.lang.model.element.TypeElement | ||
import me.eugeniomarletti.kotlin.metadata.kaptGeneratedOption | ||
import java.io.File | ||
|
||
class KnownException(message: String, val element: Element?) : RuntimeException(message) { | ||
override val message: String get() = super.message as String | ||
operator fun component1() = message | ||
operator fun component2() = element | ||
} | ||
|
||
abstract class AbstractProcessor : KotlinAbstractProcessor(), ProcessorUtils { | ||
|
||
val generatedDir: File? get() = options[kaptGeneratedOption]?.let(::File) | ||
|
||
override final fun process(annotations: Set<TypeElement>, roundEnv: RoundEnvironment): Boolean { | ||
if (!roundEnv.errorRaised()) { | ||
try { | ||
onProcess(annotations, roundEnv) | ||
} | ||
catch (e: KnownException) { | ||
logE(e.message, e.element) | ||
} | ||
} | ||
return false | ||
} | ||
|
||
protected abstract fun onProcess(annotations: Set<TypeElement>, roundEnv: RoundEnvironment) | ||
} |
49 changes: 49 additions & 0 deletions
49
...ry-annotations-processor/src/main/java/kategory/common/utils/ClassOrPackageDataWrapper.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,49 @@ | ||
package kategory.common.utils | ||
|
||
import org.jetbrains.kotlin.serialization.ClassData | ||
import org.jetbrains.kotlin.serialization.PackageData | ||
import org.jetbrains.kotlin.serialization.ProtoBuf.Constructor | ||
import org.jetbrains.kotlin.serialization.ProtoBuf.Function | ||
import org.jetbrains.kotlin.serialization.ProtoBuf.Property | ||
import org.jetbrains.kotlin.serialization.ProtoBuf.TypeParameter | ||
import org.jetbrains.kotlin.serialization.deserialization.NameResolver | ||
|
||
sealed class ClassOrPackageDataWrapper { | ||
abstract val `package`: String | ||
abstract val nameResolver: NameResolver | ||
abstract val constructorList: List<Constructor> | ||
abstract val functionList: List<Function> | ||
abstract val propertyList: List<Property> | ||
abstract val typeParameters: List<TypeParameter> | ||
abstract fun getTypeParameter(typeParameterIndex: Int): TypeParameter? | ||
|
||
class Package( | ||
override val nameResolver: NameResolver, | ||
val packageProto: org.jetbrains.kotlin.serialization.ProtoBuf.Package, | ||
override val `package`: String | ||
) : ClassOrPackageDataWrapper() { | ||
override val constructorList: List<Constructor> get() = emptyList() | ||
override val functionList: List<Function> get() = packageProto.functionList | ||
override val propertyList: List<Property> get() = packageProto.propertyList | ||
override val typeParameters: List<TypeParameter> = emptyList() | ||
override fun getTypeParameter(typeParameterIndex: Int): TypeParameter? = null | ||
} | ||
|
||
class Class( | ||
override val nameResolver: NameResolver, | ||
val classProto: org.jetbrains.kotlin.serialization.ProtoBuf.Class, | ||
override val `package`: String | ||
) : ClassOrPackageDataWrapper() { | ||
override val constructorList: List<Constructor> get() = classProto.constructorList | ||
override val functionList: List<Function> get() = classProto.functionList | ||
override val propertyList: List<Property> get() = classProto.propertyList | ||
override val typeParameters: List<TypeParameter> = classProto.typeParameterList | ||
override fun getTypeParameter(typeParameterIndex: Int): TypeParameter? = classProto.getTypeParameter(typeParameterIndex) | ||
} | ||
} | ||
|
||
fun ClassData.asClassOrPackageDataWrapper(`package`: String) = | ||
ClassOrPackageDataWrapper.Class(nameResolver, classProto, `package`) | ||
|
||
fun PackageData.asClassOrPackageDataWrapper(`package`: String) = | ||
ClassOrPackageDataWrapper.Package(nameResolver, packageProto, `package`) |
101 changes: 101 additions & 0 deletions
101
kategory-annotations-processor/src/main/java/kategory/common/utils/ProcessorUtils.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,101 @@ | ||
package kategory.common.utils | ||
|
||
import kategory.implicits.implicitAnnotationName | ||
import me.eugeniomarletti.kotlin.metadata.KotlinClassMetadata | ||
import me.eugeniomarletti.kotlin.metadata.KotlinMetadata | ||
import me.eugeniomarletti.kotlin.metadata.KotlinMetadataUtils | ||
import me.eugeniomarletti.kotlin.metadata.KotlinPackageMetadata | ||
import me.eugeniomarletti.kotlin.metadata.getJvmMethodSignature | ||
import me.eugeniomarletti.kotlin.metadata.kotlinMetadata | ||
import me.eugeniomarletti.kotlin.metadata.kotlinPropertyAnnotationsFunPostfix | ||
import org.jetbrains.kotlin.serialization.ProtoBuf | ||
import javax.lang.model.element.Element | ||
import javax.lang.model.element.ExecutableElement | ||
import javax.lang.model.element.TypeElement | ||
import javax.lang.model.element.VariableElement | ||
|
||
interface ProcessorUtils : KotlinMetadataUtils { | ||
|
||
fun KotlinMetadata.asClassOrPackageDataWrapper(classElement: TypeElement): ClassOrPackageDataWrapper? { | ||
val `package` = elementUtils.getPackageOf(classElement).toString() | ||
return when (this) { | ||
is KotlinClassMetadata -> data.asClassOrPackageDataWrapper(`package`) | ||
is KotlinPackageMetadata -> data.asClassOrPackageDataWrapper(`package`) | ||
else -> null | ||
} | ||
} | ||
|
||
fun getClassOrPackageDataWrapper(classElement: TypeElement): ClassOrPackageDataWrapper { | ||
val metadata = classElement.kotlinMetadata ?: knownError("These annotations can only be used in Kotlin") | ||
return metadata.asClassOrPackageDataWrapper(classElement) ?: knownError("This annotation can't be used on this element") | ||
} | ||
|
||
fun ClassOrPackageDataWrapper.getFunction(methodElement: ExecutableElement) = | ||
methodElement.jvmMethodSignature.let { methodSignature -> | ||
functionList | ||
.firstOrNull { methodSignature == it.getJvmMethodSignature(nameResolver) } | ||
?: knownError("Can't find annotated method $methodSignature") | ||
} | ||
} | ||
|
||
fun knownError(message: String, element: Element? = null): Nothing = throw KnownException(message, element) | ||
|
||
fun String.plusIfNotBlank( | ||
postfix: String = "", | ||
prefix: String = "" | ||
) = if (this.isNotBlank()) prefix + this + postfix else this | ||
|
||
val String.escapedClassName | ||
get() = split('/', '.').joinToString("`.`").plusIfNotBlank(prefix = "`", postfix = "`") | ||
|
||
val ProtoBuf.Class.Kind.isCompanionOrObject get() = when (this) { | ||
ProtoBuf.Class.Kind.OBJECT, | ||
ProtoBuf.Class.Kind.COMPANION_OBJECT -> true | ||
else -> false | ||
} | ||
|
||
fun ClassOrPackageDataWrapper.getParameter(function: ProtoBuf.Function, parameterElement: VariableElement) = | ||
parameterElement.simpleName.toString().let { parameterName -> | ||
function.valueParameterList | ||
.firstOrNull { parameterName == nameResolver.getString(it.name) } | ||
?: knownError("Can't find annotated parameter $parameterName in ${function.getJvmMethodSignature(nameResolver)}") | ||
} | ||
|
||
fun ClassOrPackageDataWrapper.getPropertyOrNull(methodElement: ExecutableElement) = | ||
methodElement.simpleName.toString() | ||
.takeIf { it.endsWith(kotlinPropertyAnnotationsFunPostfix) } | ||
?.substringBefore(kotlinPropertyAnnotationsFunPostfix) | ||
?.let { propertyName -> propertyList.firstOrNull { propertyName == nameResolver.getString(it.name) } } | ||
|
||
fun ProtoBuf.Type.extractFullName( | ||
classData: ClassOrPackageDataWrapper, | ||
outputTypeAlias: Boolean = true, | ||
failOnGeneric: Boolean = true | ||
): String { | ||
val nameResolver = classData.nameResolver | ||
|
||
if (failOnGeneric && !hasClassName()) knownError("Generic $implicitAnnotationName types are not yet supported") | ||
|
||
val name = when { | ||
hasTypeParameter() -> classData.getTypeParameter(typeParameter)!!.name | ||
hasTypeParameterName() -> typeParameterName | ||
outputTypeAlias && hasAbbreviatedType() -> abbreviatedType.typeAliasName | ||
else -> className | ||
}.let { nameResolver.getString(it).escapedClassName } | ||
|
||
val argumentList = if (outputTypeAlias && hasAbbreviatedType()) abbreviatedType.argumentList else argumentList | ||
val arguments = argumentList | ||
.takeIf { it.isNotEmpty() } | ||
?.joinToString(prefix = "<", postfix = ">") { | ||
when { | ||
it.hasType() -> it.type.extractFullName(classData, outputTypeAlias, failOnGeneric) | ||
!failOnGeneric -> "*" | ||
else -> knownError("Wildcard $implicitAnnotationName types are not yet supported") | ||
} | ||
} | ||
?: "" | ||
|
||
val nullability = if (nullable) "?" else "" | ||
|
||
return name + arguments + nullability | ||
} |
11 changes: 11 additions & 0 deletions
11
kategory-annotations-processor/src/main/java/kategory/derive/AnnotatedDeriving.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,11 @@ | ||
package kategory.derive | ||
|
||
import kategory.common.utils.ClassOrPackageDataWrapper | ||
import javax.lang.model.element.TypeElement | ||
|
||
class AnnotatedDeriving( | ||
val classElement: TypeElement, | ||
val classOrPackageProto: ClassOrPackageDataWrapper, | ||
val companionClassProto: ClassOrPackageDataWrapper, | ||
val derivingTypeclasses: List<ClassOrPackageDataWrapper>, | ||
val typeclassSuperTypes: Map<ClassOrPackageDataWrapper.Class, List<ClassOrPackageDataWrapper>>) |
7 changes: 7 additions & 0 deletions
7
kategory-annotations-processor/src/main/java/kategory/derive/AnnotationInfo.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,7 @@ | ||
package kategory.derive | ||
|
||
import kategory.deriving | ||
|
||
val derivingAnnotationKClass = deriving::class | ||
val derivingAnnotationClass = derivingAnnotationKClass.java | ||
val derivingAnnotationName = "@" + derivingAnnotationClass.simpleName |
Oops, something went wrong.