diff --git a/build.gradle b/build.gradle index 812d5ce..8b69634 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.0.0-beta04' + classpath 'com.android.tools.build:gradle:4.0.0-rc01' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong @@ -24,7 +24,7 @@ allprojects { subprojects { // group = 'com.juullabs.exercise' group = 'com.github.juullabs-oss.android-exercise' - version = '0.6.0' + version = '0.6.1' } task clean(type: Delete) { diff --git a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/GetParameterClassCodeGenerator.kt b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/GetParameterClassCodeGenerator.kt index 00f7bbf..c1f0bf8 100644 --- a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/GetParameterClassCodeGenerator.kt +++ b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/GetParameterClassCodeGenerator.kt @@ -7,11 +7,12 @@ import com.juullabs.exercise.compile.addProperty import com.juullabs.exercise.compile.getter import com.juullabs.exercise.compile.primaryConstructor import com.squareup.kotlinpoet.ClassName -import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.KModifier +import javax.lang.model.element.Element internal abstract class GetParameterClassCodeGenerator( + private val originatingElement: Element, private val targetClass: ClassName, private val params: Parameters ) : CodeGenerator { @@ -22,6 +23,7 @@ internal abstract class GetParameterClassCodeGenerator( override fun addTo(fileSpec: FileSpec.Builder) { val className = ClassName(fileSpec.packageName, "${targetClass.simpleName}Params") fileSpec.addClass(className) { + originatingElements += originatingElement primaryConstructor { addParameter("instance", targetClass) } addProperty("instance", targetClass, KModifier.PRIVATE) { initializer("instance") } for (param in params.all) { @@ -42,6 +44,7 @@ internal abstract class GetParameterClassCodeGenerator( } } fileSpec.addProperty(extensionName, className) { + originatingElements += originatingElement receiver(targetClass) getter { addStatement("return %T(this)", className) } } @@ -49,17 +52,19 @@ internal abstract class GetParameterClassCodeGenerator( } internal class GetArgumentsClassCodeGenerator( + originatingElement: Element, targetClass: ClassName, params: Parameters -) : GetParameterClassCodeGenerator(targetClass, params) { +) : GetParameterClassCodeGenerator(originatingElement, targetClass, params) { override val extensionName = "args" override val retriever = "instance.arguments?.get(%1S)" } internal class GetExtrasClassCodeGenerator( + originatingElement: Element, targetClass: ClassName, params: Parameters -) : GetParameterClassCodeGenerator(targetClass, params) { +) : GetParameterClassCodeGenerator(originatingElement, targetClass, params) { override val extensionName = "extras" override val retriever = "instance.intent?.extras?.get(\"\${instance.packageName}.%1L\")" } diff --git a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/NewFragmentFunctionCodeGenerator.kt b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/NewFragmentFunctionCodeGenerator.kt index 4ae870e..e02dd62 100644 --- a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/NewFragmentFunctionCodeGenerator.kt +++ b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/NewFragmentFunctionCodeGenerator.kt @@ -6,14 +6,17 @@ import com.juullabs.exercise.compile.asBundleOf import com.juullabs.exercise.compile.asParameterSpecs import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.FileSpec +import javax.lang.model.element.Element internal class NewFragmentFunctionCodeGenerator( + private val originatingElement: Element, private val targetClass: ClassName, private val params: Parameters ) : CodeGenerator { override fun addTo(fileSpec: FileSpec.Builder) { fileSpec.addFunction("new${targetClass.simpleName}") { + originatingElements += originatingElement returns(targetClass) addParameters(params.asParameterSpecs()) addStatement("val instance = %T()", targetClass) diff --git a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ParameterizedIntentClassCodeGenerator.kt b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ParameterizedIntentClassCodeGenerator.kt index 7d91e5f..c072514 100644 --- a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ParameterizedIntentClassCodeGenerator.kt +++ b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ParameterizedIntentClassCodeGenerator.kt @@ -13,8 +13,10 @@ import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.TypeName import com.squareup.kotlinpoet.TypeSpec +import javax.lang.model.element.Element internal class ParameterizedIntentClassCodeGenerator( + private val originatingElement: Element, private val targetClass: ClassName, private val params: Parameters ) : CodeGenerator { @@ -22,6 +24,7 @@ internal class ParameterizedIntentClassCodeGenerator( override fun addTo(fileSpec: FileSpec.Builder) { val className = ClassName(fileSpec.packageName, "${targetClass.simpleName}Intent") fileSpec.addClass(className) { + originatingElements += originatingElement superclass(intentTypeName) addIntentConstructor("context", contextTypeName, CodeBlock.of("context.packageName")) addIntentConstructor("packageName", stringTypeName, CodeBlock.of("packageName")) diff --git a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultClassCodeGenerator.kt b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultClassCodeGenerator.kt index 548994e..57db408 100644 --- a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultClassCodeGenerator.kt +++ b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultClassCodeGenerator.kt @@ -16,16 +16,19 @@ import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.TypeSpec +import javax.lang.model.element.Element private const val RETRIEVER = "data.get(\"\$packageName.%1L\")" internal class ResultClassCodeGenerator( + private val originatingElement: Element, private val targetClass: ClassName, private val resultKinds: List ) : CodeGenerator { override fun addTo(fileSpec: FileSpec.Builder) { val className = ClassName(fileSpec.packageName, "${targetClass.simpleName}Result") fileSpec.addClass(className) { + originatingElements += originatingElement addModifiers(KModifier.SEALED) primaryConstructor { addParameter("data", bundleTypeName) } addProperty("data", bundleTypeName) { initializer("data") } diff --git a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultContractClassCodeGenerator.kt b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultContractClassCodeGenerator.kt index 022ce85..3e7e134 100644 --- a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultContractClassCodeGenerator.kt +++ b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultContractClassCodeGenerator.kt @@ -14,8 +14,10 @@ import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy +import javax.lang.model.element.Element internal class ResultContractClassCodeGenerator( + private val originatingElement: Element, private val targetClass: ClassName, private val resultKinds: List ): CodeGenerator { @@ -24,6 +26,7 @@ internal class ResultContractClassCodeGenerator( val intentName = ClassName(fileSpec.packageName, "${targetClass.simpleName}Intent") val resultName = ClassName(fileSpec.packageName, "${targetClass.simpleName}Result") fileSpec.addClass(className) { + originatingElements += originatingElement superclass(activityResultContractTypeName.parameterizedBy(intentName, resultName.asNullable)) primaryConstructor { addParameter("context", contextTypeName) } @@ -56,7 +59,6 @@ internal class ResultContractClassCodeGenerator( ) endControlFlow() } - } } } diff --git a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultSugarFunctionsCodeGenerator.kt b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultSugarFunctionsCodeGenerator.kt index 40d2995..83d5835 100644 --- a/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultSugarFunctionsCodeGenerator.kt +++ b/compile/src/main/java/com/juullabs/exercise/compile/generator/code/ResultSugarFunctionsCodeGenerator.kt @@ -1,52 +1,20 @@ package com.juullabs.exercise.compile.generator.code -import com.juullabs.exercise.compile.ResultKind -import com.juullabs.exercise.compile.addFunction -import com.juullabs.exercise.compile.asBundleOf -import com.juullabs.exercise.compile.intentTypeName -import com.juullabs.exercise.compile.reifyResultCode +import com.juullabs.exercise.compile.* import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.FileSpec +import javax.lang.model.element.Element -/*fileSpecBuilder.addFunction("finish") { - receiver(typeClassName) - addParameter("result", resultTypeName) - beginControlFlow("val code = when (result) {") - for (kind in resultKinds) { - addStatement( - "is %T -> %L", - resultTypeName.nestedClass(kind.name), - reifyResultCode(kind.code) - ) - } - endControlFlow() - addStatement("val data = Intent().apply { replaceExtras(result.data) }") - addStatement("setResult(code, data)") - addStatement("finish()") -} - -for (kind in resultKinds) { - fileSpecBuilder.addFunction("finishWith${kind.name}") { - receiver(typeClassName) - for (param in kind.params) { - addParameter(param.name, param.combinedTypeName) - } - addCode( - "finish(%T(packageName, %L))", - resultTypeName.nestedClass(kind.name), - kind.params.toBundle("packageName") - ) - } -} -*/ internal class ResultSugarFunctionsCodeGenerator( + private val originatingElement: Element, private val targetClass: ClassName, private val resultKinds: List ) : CodeGenerator { override fun addTo(fileSpec: FileSpec.Builder) { val className = ClassName(fileSpec.packageName, "${targetClass.simpleName}Result") fileSpec.addFunction("finishWith") { + originatingElements += originatingElement receiver(targetClass) addParameter("result", className) beginControlFlow("val code = when (result) {") diff --git a/compile/src/main/java/com/juullabs/exercise/compile/generator/file/ActivityFileGenerator.kt b/compile/src/main/java/com/juullabs/exercise/compile/generator/file/ActivityFileGenerator.kt index 820635b..03b2616 100644 --- a/compile/src/main/java/com/juullabs/exercise/compile/generator/file/ActivityFileGenerator.kt +++ b/compile/src/main/java/com/juullabs/exercise/compile/generator/file/ActivityFileGenerator.kt @@ -31,16 +31,16 @@ internal class ActivityFileGenerator( val targetClass = element.asClassName() return FileSpec.build(targetClass.packageName, "${targetClass.simpleName}Exercise") { if (!element.modifiers.contains(Modifier.ABSTRACT)) { - addFrom(ParameterizedIntentClassCodeGenerator(targetClass, parameters)) + addFrom(ParameterizedIntentClassCodeGenerator(element, targetClass, parameters)) } - addFrom(GetExtrasClassCodeGenerator(targetClass, parameters)) + addFrom(GetExtrasClassCodeGenerator(element, targetClass, parameters)) val resultContractMirror = element.getAnnotation() if (resultContractMirror != null) { val kinds = getResultKinds(resultContractMirror) - addFrom(ResultContractClassCodeGenerator(targetClass, kinds)) - addFrom(ResultClassCodeGenerator(targetClass, kinds)) - addFrom(ResultSugarFunctionsCodeGenerator(targetClass, kinds)) + addFrom(ResultContractClassCodeGenerator(element, targetClass, kinds)) + addFrom(ResultClassCodeGenerator(element, targetClass, kinds)) + addFrom(ResultSugarFunctionsCodeGenerator(element, targetClass, kinds)) } } } diff --git a/compile/src/main/java/com/juullabs/exercise/compile/generator/file/AsStubFileGenerator.kt b/compile/src/main/java/com/juullabs/exercise/compile/generator/file/AsStubFileGenerator.kt index cdcf433..8d6fe7b 100644 --- a/compile/src/main/java/com/juullabs/exercise/compile/generator/file/AsStubFileGenerator.kt +++ b/compile/src/main/java/com/juullabs/exercise/compile/generator/file/AsStubFileGenerator.kt @@ -30,13 +30,13 @@ internal class AsStubFileGenerator( val annotation = checkNotNull(element.getAnnotation()) val targetClass = ClassName(annotation["packageName"] as String, annotation["className"] as String) return FileSpec.build(targetClass.packageName, "${targetClass.simpleName}ExerciseStubs") { - addFrom(ParameterizedIntentClassCodeGenerator(targetClass, parameters)) + addFrom(ParameterizedIntentClassCodeGenerator(element, targetClass, parameters)) val resultContractMirror = element.getAnnotation() if (resultContractMirror != null) { val kinds = getResultKinds(resultContractMirror) - addFrom(ResultContractClassCodeGenerator(targetClass, kinds)) - addFrom(ResultClassCodeGenerator(targetClass, kinds)) + addFrom(ResultContractClassCodeGenerator(element, targetClass, kinds)) + addFrom(ResultClassCodeGenerator(element, targetClass, kinds)) } } } diff --git a/compile/src/main/java/com/juullabs/exercise/compile/generator/file/FragmentFileGenerator.kt b/compile/src/main/java/com/juullabs/exercise/compile/generator/file/FragmentFileGenerator.kt index 0fdc77d..c854a06 100644 --- a/compile/src/main/java/com/juullabs/exercise/compile/generator/file/FragmentFileGenerator.kt +++ b/compile/src/main/java/com/juullabs/exercise/compile/generator/file/FragmentFileGenerator.kt @@ -4,11 +4,8 @@ import com.juullabs.exercise.compile.Parameters import com.juullabs.exercise.compile.build import com.juullabs.exercise.compile.generator.code.GetArgumentsClassCodeGenerator import com.juullabs.exercise.compile.generator.code.NewFragmentFunctionCodeGenerator -import com.juullabs.exercise.compile.generator.code.GetParameterClassCodeGenerator import com.juullabs.exercise.compile.generator.code.addFrom import com.juullabs.exercise.compile.isSubtypeOfAny -import com.squareup.kotlinpoet.ClassName -import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.asClassName import javax.annotation.processing.ProcessingEnvironment @@ -28,9 +25,9 @@ internal class FragmentFileGenerator( val targetClass = element.asClassName() return FileSpec.build(targetClass.packageName, "${targetClass.simpleName}Exercise") { if (!element.modifiers.contains(Modifier.ABSTRACT)) { - addFrom(NewFragmentFunctionCodeGenerator(targetClass, parameters)) + addFrom(NewFragmentFunctionCodeGenerator(element, targetClass, parameters)) } - addFrom(GetArgumentsClassCodeGenerator(targetClass, parameters)) + addFrom(GetArgumentsClassCodeGenerator(element, targetClass, parameters)) } } } diff --git a/compile/src/main/java/com/juullabs/exercise/compile/generator/file/FromStubFileGenerator.kt b/compile/src/main/java/com/juullabs/exercise/compile/generator/file/FromStubFileGenerator.kt index e7025db..836cc8b 100644 --- a/compile/src/main/java/com/juullabs/exercise/compile/generator/file/FromStubFileGenerator.kt +++ b/compile/src/main/java/com/juullabs/exercise/compile/generator/file/FromStubFileGenerator.kt @@ -32,12 +32,12 @@ internal class FromStubFileGenerator( val parameters = Parameters(environment, source) val targetClass = element.asClassName() return FileSpec.build(targetClass.packageName, "${targetClass.simpleName}Exercise") { - addFrom(GetExtrasClassCodeGenerator(targetClass, parameters)) + addFrom(GetExtrasClassCodeGenerator(element, targetClass, parameters)) val resultContractMirror = source.getAnnotation() if (resultContractMirror != null) { val kinds = getResultKinds(resultContractMirror) - addFrom(ResultSugarFunctionsCodeGenerator(targetClass, kinds)) + addFrom(ResultSugarFunctionsCodeGenerator(element, targetClass, kinds)) } } }