Skip to content

Commit

Permalink
Honor project source version for generated annotation type
Browse files Browse the repository at this point in the history
In some cases, both types would be available despite the user desiring to target 8.

Code adapted from AssisteInject.
  • Loading branch information
gm-vm authored and hansenji committed Dec 17, 2020
1 parent d0eab9d commit 3ec124b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Expand Up @@ -49,6 +49,7 @@ import javax.tools.Diagnostic.Kind.WARNING
@AutoService(Processor::class)
class ViewModelInjectProcessor : AbstractProcessor() {

private lateinit var sourceVersion: SourceVersion
private lateinit var messager: Messager
private lateinit var filer: Filer
private lateinit var types: Types
Expand All @@ -66,6 +67,7 @@ class ViewModelInjectProcessor : AbstractProcessor() {

override fun init(env: ProcessingEnvironment) {
super.init(env)
sourceVersion = env.sourceVersion
messager = env.messager
filer = env.filer
types = env.typeUtils
Expand Down Expand Up @@ -216,7 +218,7 @@ class ViewModelInjectProcessor : AbstractProcessor() {
if (!valid) return null

val targetType = targetType.asType().toTypeName()
val generatedAnnotation = createGeneratedAnnotation(elements)
val generatedAnnotation = createGeneratedAnnotation(sourceVersion, elements)
return if (assistedKeys.isEmpty()) {
val factory = ParameterizedTypeName.get(BASIC_FACTORY, targetType)
AssistedInjection(targetType, requests, factory, "create", targetType, emptyList(), generatedAnnotation)
Expand Down Expand Up @@ -264,7 +266,7 @@ class ViewModelInjectProcessor : AbstractProcessor() {
val moduleName = moduleType.toClassName()
val inflationNames = viewModelTypes.map { it.toClassName() }
val public = Modifier.PUBLIC in moduleType.modifiers
val generatedAnnotation = createGeneratedAnnotation(elements)
val generatedAnnotation = createGeneratedAnnotation(sourceVersion, elements)
return ViewModelInjectionModule(moduleName, public, inflationNames, generatedAnnotation)
}

Expand Down
Expand Up @@ -3,18 +3,26 @@ package com.vikingsen.inject.viewmodel.processor.internal
import com.squareup.inject.assisted.processor.internal.toClassName
import com.squareup.javapoet.AnnotationSpec
import javax.annotation.processing.Processor
import javax.lang.model.SourceVersion
import javax.lang.model.SourceVersion.RELEASE_8
import javax.lang.model.util.Elements

/**
* Create a `@Generated` annotation using the correct type based on JDK version and availability on
* the compilation classpath, a `value` with the fully-qualified class name of the calling
* Create a `@Generated` annotation using the correct type based on source version and availability
* on the compilation classpath, a `value` with the fully-qualified class name of the calling
* [Processor], and a comment pointing to this project's GitHub repo. Returns `null` if no
* annotation type is available on the classpath.
*/
fun Processor.createGeneratedAnnotation(elements: Elements, comments: String = "https://github.com/hansenji/ViewModelInject"): AnnotationSpec? {
val generatedType = elements.getTypeElement("javax.annotation.processing.Generated")
?: elements.getTypeElement("javax.annotation.Generated")
?: return null
fun Processor.createGeneratedAnnotation(
sourceVersion: SourceVersion,
elements: Elements,
comments: String = "https://github.com/hansenji/ViewModelInject"
): AnnotationSpec? {
val annotationTypeName = when {
sourceVersion <= RELEASE_8 -> "javax.annotation.Generated"
else -> "javax.annotation.processing.Generated"
}
val generatedType = elements.getTypeElement(annotationTypeName) ?: return null
return AnnotationSpec.builder(generatedType.toClassName())
.addMember("value", "\$S", javaClass.name)
.addMember("comments", "\$S", comments)
Expand Down

0 comments on commit 3ec124b

Please sign in to comment.