├── .circleci └── config.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── inversionCodGen ├── .gitignore ├── build.gradle └── src │ ├── main │ ├── java │ │ └── inversion │ │ │ └── codgen │ │ │ ├── Elements.kt │ │ │ ├── EnvUtils.kt │ │ │ ├── ImplElementCalculator.kt │ │ │ ├── InversionProcessor.kt │ │ │ └── ServicesFiles.kt │ └── resources │ │ └── META-INF │ │ └── gradle │ │ └── incremental.annotation.processors │ └── test │ └── java │ └── inversion │ └── codgen │ ├── InversionProcessorTest.kt │ ├── MultiBindingInterface.kt │ ├── MultiBindingInterface_Factory.kt │ └── cases │ ├── errorOnMultipleBindingsWithNoKeyOnImpl │ ├── MyImpl.kt │ ├── MyInterface.kt │ └── error.txt │ ├── errorOnMultipleBindingsWithNoKeyOnImplOnExternalModule │ ├── MyImpl.kt │ └── error.txt │ ├── errorOnMultipleBindingsWithNoKeyOnProvider │ ├── MyImpl.kt │ ├── MyInterface.kt │ └── error.txt │ ├── errorOnMultipleBindingsWithNoKeyOnProviderOnExternalModule │ ├── MyImpl.kt │ └── error.txt │ ├── errorOnMultipleSuperclasses │ ├── TestCase.kt │ └── error.txt │ ├── errorWhenDefIsNotAnnotated │ ├── TestCase.kt │ └── error.txt │ ├── errorWhenImplentationDoesNotDeclareSuperclass │ ├── TestCase.kt │ └── error.txt │ ├── generateDef │ ├── Inversion_ext_inversion_codgen_cases_generateDef_MyInterface_Factory.kt │ ├── MyInterface.kt │ └── MyInterface_Factory.kt │ ├── generateDefAsClassProperty │ ├── Inversion_ext_inversion_codgen_cases_generateDefAsClassProperty_MyInterface_Factory.kt │ ├── MyInterface.kt │ └── MyInterface_Factory.kt │ ├── generateDefWithParams │ ├── Inversion_ext_inversion_codgen_cases_generateDefWithParams_MyInterface_Factory.kt │ ├── MyInterface.kt │ └── MyInterface_Factory.kt │ ├── generateImpl │ ├── MyImpl.kt │ ├── MyInterface.kt │ ├── MyInterface_Factory.kt │ └── MyInterface_FactoryImpl.kt │ ├── generateImplBasedOnProvider │ ├── MyImpl.kt │ ├── MyInterface.kt │ ├── MyInterface_Factory.kt │ └── MyInterface_FactoryImpl.kt │ ├── generateImplOnCompanionObject │ ├── MyImpl.kt │ ├── MyInterface.kt │ ├── MyInterface_Factory.kt │ └── MyInterface_FactoryImpl.kt │ ├── generateImplWitParams │ ├── MyImpl.kt │ ├── MyInterface.kt │ ├── MyInterface_Factory.kt │ └── MyInterface_FactoryImpl.kt │ ├── generateImplWitReceiver │ ├── MyImpl.kt │ ├── MyInterface.kt │ ├── MyInterface_Factory.kt │ └── MyInterface_FactoryImpl.kt │ ├── multipleClassesInASingleFile │ ├── MyImpl.kt │ ├── MyInterface.kt │ ├── MyInterface2_Factory.kt │ └── MyInterface_Factory.kt │ ├── multipleNames │ ├── Inversion_ext_inversion_codgen_cases_multipleNames_MyInterface_Factory.kt │ ├── MyImpl.kt │ ├── MyInterface.kt │ ├── MyInterface_Factory.kt │ ├── MyInterface_FactoryImpl_A.kt │ └── MyInterface_FactoryImpl_B.kt │ └── noErrorOnMultipleSuperclassesWithDef │ └── TestCase.kt ├── inversionLib ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── inversion │ ├── Inversion.kt │ └── internal │ ├── InversionDelegates.kt │ ├── InversionValidator.kt │ ├── InversionValidatorAdapter.kt │ └── NamedGeneratedFactory.kt ├── inversionLibTest ├── .gitignore ├── build.gradle └── src │ ├── main │ └── java │ │ └── inversion │ │ └── test │ │ └── DefWithNoImpl.kt │ └── test │ └── java │ └── inversion │ └── test │ └── DefWithNoImplTest.kt ├── sample ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── demo │ │ │ │ └── MainActivity.kt │ │ └── res │ │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── drawable │ │ │ └── ic_launcher_background.xml │ │ │ ├── layout │ │ │ └── activity_main.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test │ │ └── java │ │ └── demo │ │ └── InversionTest.kt ├── libimpl │ ├── .gitignore │ ├── build.gradle │ └── src │ │ └── main │ │ └── java │ │ └── demo │ │ └── libimpl │ │ └── MyImpl.kt └── libinterface │ ├── .gitignore │ ├── build.gradle │ └── src │ └── main │ └── java │ └── demo │ └── libinterface │ └── MyInterface.kt └── settings.gradle /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | orbs: 3 | codecov: codecov/codecov@1.0.5 4 | jobs: 5 | build: 6 | working_directory: ~/code 7 | docker: 8 | - image: circleci/android:api-28 9 | environment: 10 | JVM_OPTS: -Xmx3200m 11 | steps: 12 | - checkout 13 | - restore_cache: 14 | key: jars-{{ checksum "build.gradle" }}-{{ checksum "sample/app/build.gradle" }} 15 | - run: 16 | name: Download Dependencies 17 | command: ./gradlew androidDependencies 18 | - save_cache: 19 | paths: 20 | - ~/.gradle 21 | key: jars-{{ checksum "build.gradle" }}-{{ checksum "sample/app/build.gradle" }} 22 | - run: 23 | name: Run Tests 24 | command: ./gradlew test jacocoTestReport 25 | - store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/ 26 | path: sample/app/build/reports 27 | destination: reports 28 | - store_test_results: # for display in Test Summary: https://circleci.com/docs/2.0/collect-test-data/ 29 | path: inversionCodGen/build/test-results 30 | - codecov/upload: 31 | file: inversionCodGen/build/reports/jacoco/report.xml 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/* 5 | /.idea/caches 6 | /.idea/libraries 7 | /.idea/modules.xml 8 | /.idea/workspace.xml 9 | /.idea/navEditor.xml 10 | /.idea/assetWizardSettings.xml 11 | .DS_Store 12 | /build 13 | /captures 14 | .externalNativeBuild 15 | .cxx 16 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | #### 0.3.1 2 | - Support for `InversionProvider` annotated methods in companion objects 3 | 4 | #### 0.3 5 | - Incremental annotation processor 6 | 7 | #### 0.2 8 | - Initial release 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Inversion [![CircleCI](https://circleci.com/gh/fabioCollini/Inversion.svg?style=svg)](https://circleci.com/gh/fabioCollini/Inversion) [![](https://jitpack.io/v/fabioCollini/Inversion.svg)](https://jitpack.io/#fabioCollini/Inversion) [![codecov](https://codecov.io/gh/fabioCollini/Inversion/branch/master/graph/badge.svg)](https://codecov.io/gh/fabioCollini/Inversion) 2 | 3 | 4 | Inversion simplifies the `ServiceLoader` usage to retrieve all the implementations of a certain interface. 5 | Using Inversion it's easy to use the dependency inversion in a multi module project. 6 | 7 | ## Basic example 8 | 9 | A first module defines an interface and a `create` field (annotated with `InversionDef`) to create the real implementation of the interface: 10 | 11 | ```kotlin 12 | interface MyInterface { 13 | fun doSomething(): String 14 | 15 | companion object { 16 | @get:InversionDef 17 | val create by Inversion.of(MyInterface::class) 18 | } 19 | } 20 | ``` 21 | 22 | The `create` field is a `() -> MyInterface` lambda, it can be used to create a new instance. The first module 23 | doesn't contain any real implementation of `MyInterface`. 24 | 25 | A second module defines the real implementation annotated with `InversionImpl`: 26 | 27 | ```kotlin 28 | @InversionImpl 29 | class MyImpl : MyInterface { 30 | override fun doSomething() = "Hello world!" 31 | } 32 | ``` 33 | 34 | And that's all! Now the real implementation can be retrieved invoking `create`: 35 | 36 | ```kotlin 37 | @InversionValidate 38 | class MainActivity : AppCompatActivity() { 39 | 40 | override fun onCreate(savedInstanceState: Bundle?) { 41 | super.onCreate(savedInstanceState) 42 | setContentView(R.layout.activity_main) 43 | val impl = MyInterface.create() 44 | findViewById(R.id.text).text = impl.doSomething() 45 | } 46 | } 47 | ``` 48 | 49 | ## Custom implementation creation 50 | 51 | In case an extra logic is needed to create the real implementation, an `InversionProvider` annotated method can be used: 52 | 53 | ```kotlin 54 | @InversionProvider 55 | fun provideImpl(): MyInterface = MyImpl() 56 | ``` 57 | 58 | If a parameter is needed the `InversionDef` annotated property can be defined as an extension property: 59 | 60 | ```kotlin 61 | interface MyInterface { 62 | fun doSomething() 63 | } 64 | 65 | @get:InversionDef 66 | val Application.factory by Inversion.of(MyInterface::class) 67 | ``` 68 | 69 | In this example the `Application` instance can be used to create the implementation in the `InversionProvider` 70 | annotated method: 71 | 72 | ```kotlin 73 | class MyImpl(val app: Application) : MyInterface { 74 | override fun doSomething() { 75 | //... 76 | } 77 | } 78 | 79 | @InversionProvider 80 | fun Application.provideImpl(): MyInterface = MyImpl(this) 81 | ``` 82 | 83 | ## Multi binding 84 | 85 | Multiple implementations can be managed using the `mapOf` method instead of `of`: 86 | 87 | ```kotlin 88 | @get:InversionDef 89 | val allValues by Inversion.mapOf(MyInterface::class) 90 | ``` 91 | 92 | In this way the `allValues` field is a `() -> Map` that can be used to retrieve a map with all the implementations. 93 | 94 | Multiple implementations can be defined specifying in the annotation a `String` that will be used as the key in the map: 95 | 96 | ```kotlin 97 | @InversionProvider("A") 98 | fun MyClass.provideImplA(): MyInterface = MyImplA() 99 | 100 | @InversionProvider("B") 101 | fun MyClass.provideImplB(): MyInterface = MyImplB() 102 | ``` 103 | 104 | A multi binding example is available in this [plaid fork](https://github.com/fabioCollini/plaid/), here the [commits](https://github.com/fabioCollini/plaid/compare/original-master...fabioCollini:master) 105 | that introduces Inversion and removes some reflection calls. 106 | 107 | ## Internal implementation 108 | 109 | Under the hood Inversion is an annotation processor that generates the configuration to simplify the `ServiceLoader` usage. `ServiceLoader` is 110 | a standard Java class that can be used to discover the implementations of an interface based on some config file. An explanation on how to 111 | use a `ServiceLoader` on Android is available in the post 112 | [Patterns for accessing code from Dynamic Feature Modules](https://medium.com/androiddevelopers/patterns-for-accessing-code-from-dynamic-feature-modules-7e5dca6f9123), 113 | here there are the [commits](https://github.com/fabioCollini/android-dynamic-code-loading/compare/initial-master...fabioCollini:master) to introduce 114 | Inversion in the demo project. 115 | 116 | `ServiceLoader` reads some resource files to retrieve the implementations to use, on Android the disk read can be avoided using R8. 117 | R8 removes the `ServiceLoader` calls and replaces it with the direct instantiations. Looking at the code of the last example 118 | in the apk optimized with R8 we can find the following code instead of the `ServiceLoader` invocation: 119 | 120 | ```java 121 | Iterator it = Arrays.asList(new MultiInstanceInterface_Factory[] { 122 | new MultiInstanceInterface_FactoryImpl_B(), 123 | new MultiInstanceInterface_FactoryImpl_A() 124 | }).iterator(); 125 | ``` 126 | 127 | `MultiInstanceInterface_FactoryImpl_A` and `MultiInstanceInterface_FactoryImpl_B` are two classes generated by Inversion. 128 | 129 | ## Validation 130 | 131 | The Inversion annotation processor executes some basic validations to raise a compilation error in case of any misconfiguration. 132 | A `validate` method in the `Inversion` object can be invoked to verify that there is an implementation defined for each definition. 133 | This method uses reflection calls so it's better to invoke it in a test or in the application startup only in the debug build. 134 | 135 | ## JitPack configuration 136 | 137 | Inversion is available on [JitPack](https://jitpack.io/#fabioCollini/Inversion/), 138 | you can use it adding the JitPack repository in your `build.gradle` (in top level dir): 139 | ```gradle 140 | repositories { 141 | maven { url "https://jitpack.io" } 142 | } 143 | ``` 144 | and the dependencies in the `build.gradle` of the modules: 145 | 146 | ```gradle 147 | dependencies { 148 | kapt 'com.github.fabioCollini.inversion:inversionCodGen:0.3.1' 149 | implementation 'com.github.fabioCollini.inversion:inversionLib:0.3.1' 150 | } 151 | ``` 152 | 153 | ## License 154 | 155 | Copyright 2019 Fabio Collini 156 | 157 | Licensed under the Apache License, Version 2.0 (the "License"); 158 | you may not use this file except in compliance with the License. 159 | You may obtain a copy of the License at 160 | 161 | http://www.apache.org/licenses/LICENSE-2.0 162 | 163 | Unless required by applicable law or agreed to in writing, software 164 | distributed under the License is distributed on an "AS IS" BASIS, 165 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 166 | See the License for the specific language governing permissions and 167 | limitations under the License. -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 18 | 19 | buildscript { 20 | ext.kotlin_version = '1.3.61' 21 | repositories { 22 | google() 23 | jcenter() 24 | 25 | } 26 | dependencies { 27 | classpath 'com.android.tools.build:gradle:3.5.3' 28 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 29 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' 30 | // NOTE: Do not place your application dependencies here; they belong 31 | // in the individual module build.gradle files 32 | } 33 | } 34 | 35 | allprojects { 36 | repositories { 37 | google() 38 | jcenter() 39 | 40 | } 41 | } 42 | 43 | task clean(type: Delete) { 44 | delete rootProject.buildDir 45 | } 46 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2019 Fabio Collini. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | # Project-wide Gradle settings. 18 | # IDE (e.g. Android Studio) users: 19 | # Gradle settings configured through the IDE *will override* 20 | # any settings specified in this file. 21 | # For more details on how to configure your build environment visit 22 | # http://www.gradle.org/docs/current/userguide/build_environment.html 23 | # Specifies the JVM arguments used for the daemon process. 24 | # The setting is particularly useful for tweaking memory settings. 25 | org.gradle.jvmargs=-Xmx1536m 26 | # When configured, Gradle will run in incubating parallel mode. 27 | # This option should only be used with decoupled projects. More details, visit 28 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 29 | # org.gradle.parallel=true 30 | # AndroidX package structure to make it clearer which packages are bundled with the 31 | # Android operating system, and which are packaged with your app's APK 32 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 33 | android.useAndroidX=true 34 | # Automatically convert third-party libraries to use AndroidX 35 | android.enableJetifier=true 36 | # Kotlin code style for this project: "official" or "obsolete": 37 | kotlin.code.style=official 38 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabioCollini/Inversion/2a56c2e33597e8a2ef0129d7b93ffe9d97c7f6df/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2019 Fabio Collini. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | #Mon Jul 15 14:23:02 CEST 2019 18 | distributionBase=GRADLE_USER_HOME 19 | distributionPath=wrapper/dists 20 | zipStoreBase=GRADLE_USER_HOME 21 | zipStorePath=wrapper/dists 22 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip 23 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /inversionCodGen/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /inversionCodGen/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'java-library' 18 | apply plugin: 'kotlin' 19 | apply plugin: 'kotlin-kapt' 20 | apply plugin: 'jacoco' 21 | apply plugin: 'com.github.dcendents.android-maven' 22 | 23 | group='com.github.fabioCollini.inversion' 24 | 25 | dependencies { 26 | implementation project(':inversionLib') 27 | 28 | implementation files(org.gradle.internal.jvm.Jvm.current().toolsJar) 29 | 30 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 31 | 32 | implementation 'com.google.auto.service:auto-service-annotations:1.0-rc6' 33 | kapt 'com.google.auto.service:auto-service:1.0-rc6' 34 | 35 | api 'com.squareup:kotlinpoet:1.5.0' 36 | implementation 'com.squareup:kotlinpoet-metadata:1.5.0' 37 | 38 | testImplementation 'junit:junit:4.13' 39 | testImplementation "org.permissionsdispatcher:kompile-testing:0.1.3" 40 | } 41 | 42 | sourceSets { 43 | main { 44 | java { 45 | srcDir "${buildDir.absolutePath}/tmp/kapt/main/kotlinGenerated/" 46 | } 47 | } 48 | } 49 | 50 | sourceCompatibility = "8" 51 | targetCompatibility = "8" 52 | 53 | task sourcesJar(type: Jar, dependsOn: classes) { 54 | classifier = 'sources' 55 | from sourceSets.main.allSource 56 | } 57 | 58 | task javadocJar(type: Jar, dependsOn: javadoc) { 59 | classifier = 'javadoc' 60 | from javadoc.destinationDir 61 | } 62 | 63 | artifacts { 64 | archives sourcesJar 65 | archives javadocJar 66 | } 67 | 68 | test.finalizedBy(jacocoTestReport) 69 | 70 | jacocoTestReport { 71 | reports { 72 | xml.enabled true 73 | xml.destination = file("${buildDir}/reports/jacoco/report.xml") 74 | csv.enabled false 75 | } 76 | } 77 | 78 | tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { 79 | kotlinOptions { 80 | jvmTarget = "1.8" 81 | } 82 | } 83 | 84 | compileKotlin { 85 | kotlinOptions { 86 | freeCompilerArgs += "-Xuse-experimental=kotlin.Experimental" 87 | } 88 | } -------------------------------------------------------------------------------- /inversionCodGen/src/main/java/inversion/codgen/Elements.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen 18 | 19 | import com.squareup.kotlinpoet.ClassName 20 | import com.squareup.kotlinpoet.ParameterizedTypeName 21 | import com.squareup.kotlinpoet.asTypeName 22 | import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview 23 | import com.squareup.kotlinpoet.metadata.isCompanionObject 24 | import com.squareup.kotlinpoet.metadata.toImmutableKmClass 25 | import inversion.InversionImpl 26 | import inversion.InversionProvider 27 | import javax.lang.model.element.* 28 | 29 | private val Element.isCompanionObject: Boolean 30 | @UseExperimental(KotlinPoetMetadataPreview::class) 31 | get() { 32 | return try { 33 | (this as? TypeElement)?.toImmutableKmClass()?.isCompanionObject ?: false 34 | } catch (e: Exception) { 35 | false 36 | } 37 | } 38 | 39 | private fun factoryInterface(type: ClassName) = 40 | ClassName(type.packageName, type.simpleName + "_Factory") 41 | 42 | interface ImplElement { 43 | val packageName: String 44 | val defClass: ClassName 45 | val parameters: List 46 | val simpleName: Name 47 | val factoryInterface: ClassName get() = factoryInterface(defClass) 48 | val instanceName: String 49 | val objectOwner: Element? 50 | } 51 | 52 | class ImplExecutableElement( 53 | private val element: ExecutableElement, 54 | override val packageName: String, 55 | override val defClass: ClassName 56 | ) : ImplElement { 57 | override val parameters: List = element.parameters 58 | override val simpleName: Name = element.simpleName 59 | override val instanceName = element.getAnnotation(InversionProvider::class.java).value 60 | override val objectOwner: Element? 61 | get() = if (element.enclosingElement.isCompanionObject) element.enclosingElement else null 62 | } 63 | 64 | class ImplClassElement( 65 | element: TypeElement, 66 | override val packageName: String, 67 | override val defClass: ClassName 68 | ) : ImplElement { 69 | override val parameters: List = emptyList() 70 | override val simpleName: Name = element.simpleName 71 | override val instanceName = element.getAnnotation(InversionImpl::class.java).value 72 | override val objectOwner: Element? = null 73 | } 74 | 75 | class DefElement( 76 | private val element: ExecutableElement, 77 | val packageName: String 78 | ) { 79 | val receiver: Element? 80 | get() = element.parameters.getOrNull(0) 81 | ?: when { 82 | element.modifiers.contains(Modifier.STATIC) -> null 83 | element.enclosingElement.isCompanionObject -> null 84 | else -> element.enclosingElement 85 | } 86 | val factoryType get() = element.returnType.asTypeName() as ParameterizedTypeName 87 | val defClass: ClassName 88 | get() { 89 | val ret = factoryType.typeArguments.last() 90 | return if (ret is ParameterizedTypeName) { 91 | ret.typeArguments.last() as ClassName 92 | } else { 93 | ret as ClassName 94 | } 95 | } 96 | val isReturningMap: Boolean 97 | get() { 98 | val ret = factoryType.typeArguments.last() 99 | return ret is ParameterizedTypeName 100 | } 101 | val factoryInterface get() = factoryInterface(defClass) 102 | } 103 | -------------------------------------------------------------------------------- /inversionCodGen/src/main/java/inversion/codgen/EnvUtils.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen 18 | 19 | import javax.annotation.processing.ProcessingEnvironment 20 | import javax.lang.model.element.AnnotationMirror 21 | import javax.lang.model.element.Element 22 | import javax.tools.Diagnostic 23 | 24 | fun ProcessingEnvironment.log(msg: String) { 25 | if (options.containsKey("debug")) { 26 | messager.printMessage(Diagnostic.Kind.NOTE, msg) 27 | } 28 | } 29 | 30 | fun ProcessingEnvironment.warn(msg: String, element: Element, annotation: AnnotationMirror? = null) { 31 | messager.printMessage(Diagnostic.Kind.WARNING, msg, element, annotation) 32 | } 33 | 34 | fun ProcessingEnvironment.error(msg: String, element: Element, annotation: AnnotationMirror? = null) { 35 | messager.printMessage(Diagnostic.Kind.ERROR, msg, element, annotation) 36 | } 37 | 38 | fun ProcessingEnvironment.fatalError(msg: String) { 39 | messager.printMessage(Diagnostic.Kind.ERROR, "FATAL ERROR: $msg") 40 | } 41 | 42 | fun ProcessingEnvironment.getPackageName(it: Element) = 43 | elementUtils.getPackageOf(it).toString() 44 | -------------------------------------------------------------------------------- /inversionCodGen/src/main/java/inversion/codgen/ImplElementCalculator.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen 18 | 19 | import com.squareup.kotlinpoet.ClassName 20 | import com.squareup.kotlinpoet.asTypeName 21 | import inversion.InversionImpl 22 | import inversion.InversionProvider 23 | import inversion.internal.NamedGeneratedFactory 24 | import javax.annotation.processing.ProcessingEnvironment 25 | import javax.lang.model.element.Element 26 | import javax.lang.model.element.ExecutableElement 27 | import javax.lang.model.element.TypeElement 28 | import javax.lang.model.type.MirroredTypeException 29 | 30 | class ImplElementCalculator( 31 | private val processingEnv: ProcessingEnvironment, 32 | private val defs: List 33 | ) { 34 | fun calculateFromImpl(element: TypeElement): ImplElement? { 35 | val defAnnotationClass = extractClassFromImplAnnotation(element) 36 | val interfaces = defAnnotationClass?.let { listOf(it) } 37 | ?: (element.interfaces + element.superclass).filter { it.toString() != "java.lang.Object" }.map { 38 | it.asTypeName().toString() 39 | } 40 | 41 | return when { 42 | interfaces.isEmpty() -> { 43 | processingEnv.error("No superclass or interface found for $element", element) 44 | null 45 | } 46 | interfaces.size > 1 -> { 47 | processingEnv.error( 48 | "Multiple superclasses/interfaces found for $element, please use the def parameter in InversionImpl annotation", 49 | element 50 | ) 51 | null 52 | } 53 | else -> { 54 | val defClassName = interfaces[0] 55 | val annotationValue = 56 | element.getAnnotationsByType(InversionImpl::class.java).firstOrNull()?.value 57 | checkAndCreateImpl(defClassName, element, annotationValue) { 58 | createImpl(defClassName, element) 59 | } 60 | } 61 | } 62 | } 63 | 64 | private inline fun checkAndCreateImpl( 65 | defClassName: String, 66 | element: Element, 67 | annotationValue: String?, 68 | f: () -> ImplElement? 69 | ): ImplElement? { 70 | val def = defs.firstOrNull { it.defClass.canonicalName == defClassName } 71 | return if (def != null) { 72 | if (checkMultiMap(annotationValue, element, def.isReturningMap)) 73 | f() 74 | else 75 | null 76 | } else { 77 | val factory = processingEnv.elementUtils.getTypeElement("${defClassName}_Factory") 78 | if (factory != null) { 79 | val multiBinding = factory.interfaces.map { it.toString() } 80 | .contains(NamedGeneratedFactory::class.java.canonicalName) 81 | if (checkMultiMap(annotationValue, element, multiBinding)) 82 | f() 83 | else 84 | null 85 | } else { 86 | processingEnv.error("No definition found for $defClassName", element) 87 | null 88 | } 89 | } 90 | } 91 | 92 | private fun checkMultiMap( 93 | annotationValue: String?, 94 | element: Element, 95 | multiMap: Boolean 96 | ): Boolean { 97 | return if (multiMap && annotationValue.isNullOrEmpty()) { 98 | processingEnv.error( 99 | "No key defined for implementation, use the annotation value to define it", 100 | element 101 | ) 102 | false 103 | } else { 104 | true 105 | } 106 | } 107 | 108 | private fun createImpl( 109 | defClassName: String, 110 | element: TypeElement 111 | ): ImplClassElement { 112 | val defClass = 113 | processingEnv.elementUtils.getTypeElement(defClassName).asType().asTypeName() as ClassName 114 | return ImplClassElement(element, processingEnv.getPackageName(element), defClass) 115 | } 116 | 117 | private fun extractClassFromImplAnnotation(element: TypeElement): String? { 118 | val value = try { 119 | val implAnnotation = element.getAnnotation(InversionImpl::class.java) 120 | implAnnotation.def.java.canonicalName 121 | } catch (e: MirroredTypeException) { 122 | e.typeMirror.toString() 123 | } 124 | return value.takeIf { it != "java.lang.Void" } 125 | } 126 | 127 | fun calculateFromProvider(element: ExecutableElement): ImplElement? { 128 | val returnType = element.returnType.asTypeName() as ClassName 129 | val annotationValue = 130 | element.getAnnotationsByType(InversionProvider::class.java).firstOrNull()?.value 131 | return checkAndCreateImpl(returnType.canonicalName, element, annotationValue) { 132 | ImplExecutableElement(element, processingEnv.getPackageName(element), returnType) 133 | } 134 | } 135 | } -------------------------------------------------------------------------------- /inversionCodGen/src/main/java/inversion/codgen/InversionProcessor.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen 18 | 19 | import com.google.auto.service.AutoService 20 | import com.squareup.kotlinpoet.* 21 | import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy 22 | import inversion.Inversion 23 | import inversion.InversionDef 24 | import inversion.InversionImpl 25 | import inversion.InversionProvider 26 | import inversion.internal.InversionDelegates 27 | import inversion.internal.InversionValidator 28 | import inversion.internal.InversionValidatorAdapter 29 | import inversion.internal.NamedGeneratedFactory 30 | import java.util.* 31 | import javax.annotation.processing.AbstractProcessor 32 | import javax.annotation.processing.Processor 33 | import javax.annotation.processing.RoundEnvironment 34 | import javax.annotation.processing.SupportedSourceVersion 35 | import javax.lang.model.SourceVersion 36 | import javax.lang.model.element.ExecutableElement 37 | import javax.lang.model.element.TypeElement 38 | import javax.lang.model.element.VariableElement 39 | import kotlin.reflect.KClass 40 | 41 | 42 | @AutoService(Processor::class) 43 | @SupportedSourceVersion(SourceVersion.RELEASE_8) 44 | class InversionProcessor : AbstractProcessor() { 45 | 46 | override fun getSupportedAnnotationTypes() = 47 | mutableSetOf( 48 | InversionImpl::class.java.name, 49 | InversionProvider::class.java.name, 50 | InversionDef::class.java.name 51 | ) 52 | 53 | override fun getSupportedSourceVersion(): SourceVersion = SourceVersion.latest() 54 | 55 | override fun process( 56 | set: MutableSet?, 57 | roundEnvironment: RoundEnvironment 58 | ): Boolean { 59 | val defs = roundEnvironment.getElementsAnnotatedWith(InversionDef::class.java) 60 | .filterIsInstance() 61 | .map { DefElement(it, processingEnv.getPackageName(it)) } 62 | 63 | val implElementCalculator = ImplElementCalculator(processingEnv, defs) 64 | 65 | val impls = roundEnvironment.getElementsAnnotatedWith(InversionProvider::class.java) 66 | .filterIsInstance() 67 | .mapNotNull { implElementCalculator.calculateFromProvider(it) } + 68 | roundEnvironment.getElementsAnnotatedWith(InversionImpl::class.java) 69 | .filterIsInstance() 70 | .mapNotNull { implElementCalculator.calculateFromImpl(it) } 71 | 72 | impls.map { generateImpl(it) } 73 | .groupBy( 74 | keySelector = { it.first }, 75 | valueTransform = { it.second } 76 | ) 77 | .forEach { (key, list) -> generateConfigFiles(processingEnv, key, list) } 78 | 79 | val validatorsToBeGenerated = defs.map { generateDefClass(it) } 80 | 81 | generateConfigFiles( 82 | processingEnv, 83 | InversionValidator::class.java.canonicalName, 84 | validatorsToBeGenerated 85 | ) 86 | 87 | return true 88 | } 89 | 90 | private inline fun loadServiceList(): List = 91 | loadServiceList(T::class.java) 92 | 93 | private fun loadServiceList(c: Class): List { 94 | return try { 95 | val provider = ServiceLoader.load(c, c.classLoader) 96 | val ret = mutableListOf() 97 | val iterator = provider.iterator() 98 | while (iterator.hasNext()) { 99 | ret.add(iterator.next()) 100 | } 101 | ret 102 | } catch (e: ServiceConfigurationError) { 103 | emptyList() 104 | } 105 | } 106 | 107 | private fun VariableElement.isReceiver() = simpleName.toString().contains('$') 108 | 109 | private fun generateImpl(element: ImplElement): Pair { 110 | val factoryInterface = element.factoryInterface 111 | val suffix = if (element.instanceName.isEmpty()) "" else "_${element.instanceName}" 112 | val factoryClassName = "${factoryInterface.simpleName}Impl$suffix" 113 | FileSpec.builder(element.packageName, factoryClassName) 114 | .addType( 115 | TypeSpec.classBuilder(factoryClassName) 116 | .addSuperinterface(factoryInterface) 117 | .addFunction( 118 | FunSpec.builder("invoke") 119 | .addModifiers(KModifier.OVERRIDE) 120 | .returns(element.defClass) 121 | .apply { 122 | element.parameters.forEach { 123 | addParameter( 124 | if (it.isReceiver()) "param" else it.simpleName.toString(), 125 | it.asType().asTypeName() 126 | ) 127 | } 128 | } 129 | .apply { 130 | val objectOwner = element.objectOwner 131 | when { 132 | element.parameters.getOrNull(0)?.isReceiver() == true -> 133 | addStatement("return param.${element.simpleName}()") 134 | objectOwner != null -> 135 | addStatement( 136 | "return %T.${element.simpleName}(%L)", 137 | objectOwner, 138 | element.parameters.joinToString { it.simpleName.toString() } 139 | ) 140 | else -> 141 | addStatement( 142 | "return ${element.simpleName}(%L)", 143 | element.parameters.joinToString { it.simpleName.toString() } 144 | ) 145 | } 146 | } 147 | .build() 148 | ) 149 | .run { 150 | if (element.instanceName.isEmpty()) 151 | this 152 | else 153 | addProperty( 154 | PropertySpec.builder("name", String::class, KModifier.OVERRIDE) 155 | .initializer("\"${element.instanceName}\"") 156 | .build() 157 | ) 158 | } 159 | .build() 160 | ) 161 | .build() 162 | .writeTo(processingEnv.filer) 163 | 164 | return factoryInterface.canonicalName to "${element.packageName}.$factoryClassName" 165 | } 166 | 167 | private fun generateDefClass(element: DefElement): String { 168 | val returnType = element.defClass 169 | val receiver = element.receiver 170 | val realFactoryType = LambdaTypeName.get( 171 | returnType = returnType, 172 | parameters = *listOfNotNull(receiver?.asType()?.asTypeName()).toTypedArray() 173 | ) 174 | val factoryInterface = element.factoryInterface 175 | val validatorClass = ClassName( 176 | returnType.packageName, 177 | returnType.simpleName + "_FactoryValidator" 178 | ) 179 | FileSpec.builder(element.packageName, factoryInterface.simpleName) 180 | .addType( 181 | TypeSpec.interfaceBuilder(factoryInterface) 182 | .addSuperinterface(realFactoryType) 183 | .run { 184 | if (element.isReturningMap) 185 | addSuperinterface(NamedGeneratedFactory::class.java) 186 | else 187 | this 188 | } 189 | .build() 190 | ) 191 | .addType( 192 | TypeSpec.classBuilder(validatorClass) 193 | .superclass(InversionValidatorAdapter::class) 194 | .addSuperclassConstructorParameter("%T::class", factoryInterface) 195 | .addSuperclassConstructorParameter("%T::class", returnType) 196 | .build() 197 | ) 198 | .build() 199 | .writeTo(processingEnv.filer) 200 | 201 | FileSpec.builder( 202 | "inversion", 203 | "Inversion_ext_${factoryInterface.canonicalName.replace('.', '_')}" 204 | ) 205 | .addFunction( 206 | FunSpec.builder(if (element.isReturningMap) "mapOf" else "of") 207 | .addAnnotation( 208 | AnnotationSpec.builder(JvmName::class) 209 | .addMember("\"factory_${returnType.toString().replace('.', '_')}\"") 210 | .build() 211 | ) 212 | .receiver(Inversion::class) 213 | .addParameter( 214 | ParameterSpec.builder( 215 | "c", 216 | KClass::class.asClassName().parameterizedBy(returnType) 217 | ) 218 | .addAnnotation( 219 | AnnotationSpec.builder(Suppress::class) 220 | .addMember( 221 | "\"UNUSED_PARAMETER\"" 222 | ) 223 | .build() 224 | ) 225 | .build() 226 | ) 227 | .let { 228 | val prefix = if (element.isReturningMap) "mapDelegate" else "delegate" 229 | val suffix = 230 | if (element.isReturningMap) ".asSequence().toList()" else ".next()" 231 | if (receiver == null) 232 | it.addStatement( 233 | "return %T.$prefix(%T.load(%T::class.java, %T::class.java.classLoader).iterator()$suffix)", 234 | InversionDelegates::class, 235 | ServiceLoader::class, 236 | factoryInterface, 237 | factoryInterface 238 | ) 239 | else 240 | it.addStatement( 241 | "return %T.${prefix}WithReceiver(%T.load(%T::class.java, %T::class.java.classLoader).iterator()$suffix)", 242 | InversionDelegates::class, 243 | ServiceLoader::class, 244 | factoryInterface, 245 | factoryInterface 246 | ) 247 | } 248 | .build() 249 | ) 250 | .build() 251 | .writeTo(processingEnv.filer) 252 | 253 | return validatorClass.canonicalName 254 | } 255 | } -------------------------------------------------------------------------------- /inversionCodGen/src/main/java/inversion/codgen/ServicesFiles.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package inversion.codgen 17 | 18 | import java.io.* 19 | import java.nio.charset.StandardCharsets 20 | import javax.annotation.processing.ProcessingEnvironment 21 | import javax.tools.StandardLocation 22 | 23 | /** 24 | * A helper class for reading and writing Services files. 25 | * Kotlin conversion of the AutoService class 26 | * https://github.com/google/auto/blob/master/service/processor/src/main/java/com/google/auto/service/processor/ServicesFiles.java 27 | */ 28 | internal object ServicesFiles { 29 | /** 30 | * Reads the set of service classes from a service file. 31 | * 32 | * @param input not `null`. Closed after use. 33 | * @return a not `null Set` of service class names. 34 | * @throws IOException 35 | */ 36 | fun readServiceFile(input: InputStream): Set { 37 | return BufferedReader( 38 | InputStreamReader( 39 | input, 40 | StandardCharsets.UTF_8 41 | ) 42 | ).useLines { sequence -> 43 | sequence.mapNotNull { fullLine -> 44 | removeComment(fullLine) 45 | .takeIf { it.isNotEmpty() } 46 | } 47 | }.toSet() 48 | } 49 | 50 | private fun removeComment(it: String): String { 51 | val commentStart = it.indexOf('#') 52 | val line = if (commentStart >= 0) { 53 | it.substring(0, commentStart) 54 | } else { 55 | it 56 | } 57 | return line.trim { it <= ' ' } 58 | } 59 | 60 | /** 61 | * Writes the set of service class names to a service file. 62 | * 63 | * @param output not `null`. Not closed after use. 64 | * @param services a not `null Collection` of service class names. 65 | * @throws IOException 66 | */ 67 | fun writeServiceFile(services: Collection, output: OutputStream) { 68 | val writer = BufferedWriter(OutputStreamWriter(output, StandardCharsets.UTF_8)) 69 | for (service in services) { 70 | writer.write(service) 71 | writer.newLine() 72 | } 73 | writer.flush() 74 | } 75 | } 76 | 77 | /** 78 | * Kotlin conversion of the AutoService method 79 | * https://github.com/google/auto/blob/master/service/processor/src/main/java/com/google/auto/service/processor/AutoServiceProcessor.java 80 | */ 81 | fun generateConfigFiles(processingEnv: ProcessingEnvironment, providerInterface: String, newServices: List) { 82 | val resourceFile = getResourceFile(providerInterface) 83 | processingEnv.log("Working on resource file: $resourceFile") 84 | try { 85 | val allServices = readImplementationsFromRes(processingEnv, resourceFile) 86 | 87 | if (allServices.containsAll(newServices)) { 88 | processingEnv.log("No new service entries being added.") 89 | return 90 | } 91 | 92 | allServices.addAll(newServices) 93 | processingEnv.log("New service file contents: $allServices") 94 | val fileObject = processingEnv.filer.createResource( 95 | StandardLocation.CLASS_OUTPUT, "", 96 | resourceFile 97 | ) 98 | val out = fileObject.openOutputStream() 99 | ServicesFiles.writeServiceFile(allServices, out) 100 | out.close() 101 | processingEnv.log("Wrote to: " + fileObject.toUri()) 102 | } catch (e: IOException) { 103 | processingEnv.fatalError("Unable to create $resourceFile, $e") 104 | return 105 | } 106 | } 107 | 108 | fun readImplementationsFromRes(processingEnv: ProcessingEnvironment, resourceFile: String): MutableSet { 109 | val allServices = mutableSetOf() 110 | try { 111 | // would like to be able to print the full path 112 | // before we attempt to get the resource in case the behavior 113 | // of filer.getResource does change to match the spec, but there's 114 | // no good way to resolve CLASS_OUTPUT without first getting a resource. 115 | val existingFile = processingEnv.filer.getResource( 116 | StandardLocation.CLASS_OUTPUT, "", 117 | resourceFile 118 | ) 119 | processingEnv.log("Looking for existing resource file at " + existingFile.toUri()) 120 | val oldServices = ServicesFiles.readServiceFile(existingFile.openInputStream()) 121 | processingEnv.log("Existing service entries: $oldServices") 122 | allServices.addAll(oldServices) 123 | } catch (e: IOException) { 124 | // According to the javadoc, Filer.getResource throws an exception 125 | // if the file doesn't already exist. In practice this doesn't 126 | // appear to be the case. Filer.getResource will happily return a 127 | // FileObject that refers to a non-existent file but will throw 128 | // IOException if you try to open an input stream for it. 129 | processingEnv.log("Resource file did not already exist.") 130 | } 131 | return allServices 132 | } 133 | 134 | fun getResourceFile(providerInterface: String) = 135 | "META-INF/services/$providerInterface" 136 | 137 | -------------------------------------------------------------------------------- /inversionCodGen/src/main/resources/META-INF/gradle/incremental.annotation.processors: -------------------------------------------------------------------------------- 1 | inversion.codgen.InversionProcessor,aggregating -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/InversionProcessorTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen 18 | 19 | import kompile.testing.SuccessfulCompilationClause 20 | import kompile.testing.kotlinc 21 | import org.junit.Test 22 | import org.junit.runner.RunWith 23 | import org.junit.runners.Parameterized 24 | import java.io.File 25 | 26 | private const val CASES_DIR = "inversion/codgen/cases/" 27 | 28 | fun kompile.testing.Compiler.addSources( 29 | dir: String, 30 | vararg names: String 31 | ): kompile.testing.Compiler { 32 | var ret = this 33 | names.forEach { name -> 34 | val fileName = "$CASES_DIR$dir/$name" 35 | ret = addKotlin( 36 | fileName, 37 | File("src/test/java/$fileName").readText() 38 | ) 39 | } 40 | return ret 41 | } 42 | 43 | fun SuccessfulCompilationClause.generatedFiles(dir: String, vararg names: String) { 44 | names.forEach { name -> 45 | val fileName = "$CASES_DIR$dir/$name" 46 | val generatedFileName = if (name.startsWith("Inversion_")) 47 | "inversion/$name" 48 | else 49 | fileName 50 | generatedFile(generatedFileName).hasSourceEquivalentTo(File("src/test/java/$fileName").readText()) 51 | } 52 | } 53 | 54 | fun isResultFile(name: String) = 55 | name.startsWith("Inversion_") || name.endsWith("_Factory.kt") || name.contains("_FactoryImpl") 56 | 57 | fun verifyDir(dir: String) { 58 | val files = 59 | File("src/test/java/$CASES_DIR$dir").listFiles().map { it.name } 60 | 61 | val compiler = kotlinc().withProcessors(InversionProcessor()) 62 | .addSources(dir, *files.filter { it != "error.txt" && !isResultFile(it) }.toTypedArray()) 63 | 64 | val error = files.filter { it == "error.txt" }.getOrNull(0) 65 | 66 | if (error == null) { 67 | val results = files.filter { it != "error.txt" && isResultFile(it) } 68 | compiler.compile() 69 | .succeeded() 70 | .generatedFiles(dir, *results.toTypedArray()) 71 | } else { 72 | compiler 73 | .compile() 74 | .failed() 75 | .withErrorContaining(File("src/test/java/$CASES_DIR$dir/error.txt").readText()) 76 | } 77 | } 78 | 79 | @RunWith(Parameterized::class) 80 | class InversionProcessorTest(private val dir: String) { 81 | @Test 82 | fun generateClasses() { 83 | verifyDir(dir) 84 | } 85 | 86 | companion object { 87 | @JvmStatic 88 | @Parameterized.Parameters(name = "{0}") 89 | fun params(): List { 90 | return File("src/test/java/$CASES_DIR").listFiles().map { it.name } 91 | } 92 | } 93 | } -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/MultiBindingInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.mapOf 22 | 23 | interface MultiBindingInterface { 24 | fun doSomething() 25 | } 26 | 27 | @get:InversionDef 28 | val factory by Inversion.mapOf(MultiBindingInterface::class) 29 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/MultiBindingInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen 18 | 19 | import inversion.internal.InversionValidatorAdapter 20 | import inversion.internal.NamedGeneratedFactory 21 | 22 | interface MultiBindingInterface_Factory : () -> MultiBindingInterface, NamedGeneratedFactory 23 | 24 | class MultiBindingInterface_FactoryValidator : InversionValidatorAdapter(MultiBindingInterface_Factory::class, 25 | MultiBindingInterface::class) 26 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleBindingsWithNoKeyOnImpl/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.errorOnMultipleBindingsWithNoKeyOnImpl 18 | 19 | import inversion.InversionImpl 20 | import inversion.InversionProvider 21 | 22 | @InversionImpl 23 | class MyImpl : MyInterface { 24 | override fun doSomething() { 25 | println("Hello world!") 26 | } 27 | } 28 | 29 | @InversionProvider("B") 30 | fun provideImplB(): MyInterface = MyImpl() 31 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleBindingsWithNoKeyOnImpl/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.errorOnMultipleBindingsWithNoKeyOnImpl 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.mapOf 22 | 23 | interface MyInterface { 24 | fun doSomething() 25 | } 26 | 27 | @get:InversionDef 28 | val factory by Inversion.mapOf(MyInterface::class) 29 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleBindingsWithNoKeyOnImpl/error.txt: -------------------------------------------------------------------------------- 1 | error: No key defined for implementation, use the annotation value to define it 2 | public final class MyImpl implements inversion.codgen.cases.errorOnMultipleBindingsWithNoKeyOnImpl.MyInterface { -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleBindingsWithNoKeyOnImplOnExternalModule/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.errorOnMultipleBindingsWithNoKeyOnImplOnExternalModule 18 | 19 | import inversion.InversionImpl 20 | import inversion.InversionProvider 21 | import inversion.codgen.MultiBindingInterface 22 | 23 | @InversionImpl 24 | class MyImpl : MultiBindingInterface { 25 | override fun doSomething() { 26 | println("Hello world!") 27 | } 28 | } 29 | 30 | @InversionProvider("B") 31 | fun provideImplB(): MultiBindingInterface = MyImpl() 32 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleBindingsWithNoKeyOnImplOnExternalModule/error.txt: -------------------------------------------------------------------------------- 1 | error: No key defined for implementation, use the annotation value to define it 2 | public final class MyImpl implements inversion.codgen.MultiBindingInterface { -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleBindingsWithNoKeyOnProvider/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.errorOnMultipleBindingsWithNoKeyOnProvider 18 | 19 | import inversion.InversionProvider 20 | 21 | class MyImpl : MyInterface { 22 | override fun doSomething() { 23 | println("Hello world!") 24 | } 25 | } 26 | 27 | @InversionProvider 28 | fun provideImplA(): MyInterface = MyImpl() 29 | 30 | @InversionProvider("B") 31 | fun provideImplB(): MyInterface = MyImpl() 32 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleBindingsWithNoKeyOnProvider/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.errorOnMultipleBindingsWithNoKeyOnProvider 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.mapOf 22 | 23 | interface MyInterface { 24 | fun doSomething() 25 | } 26 | 27 | @get:InversionDef 28 | val factory by Inversion.mapOf(MyInterface::class) 29 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleBindingsWithNoKeyOnProvider/error.txt: -------------------------------------------------------------------------------- 1 | error: No key defined for implementation, use the annotation value to define it 2 | public static final inversion.codgen.cases.errorOnMultipleBindingsWithNoKeyOnProvider.MyInterface provideImplA() { -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleBindingsWithNoKeyOnProviderOnExternalModule/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.errorOnMultipleBindingsWithNoKeyOnProviderOnExternalModule 18 | 19 | import inversion.InversionProvider 20 | import inversion.codgen.MultiBindingInterface 21 | 22 | class MyImpl : MultiBindingInterface { 23 | override fun doSomething() { 24 | println("Hello world!") 25 | } 26 | } 27 | 28 | @InversionProvider 29 | fun provideImplA(): MultiBindingInterface = MyImpl() 30 | 31 | @InversionProvider("B") 32 | fun provideImplB(): MultiBindingInterface = MyImpl() 33 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleBindingsWithNoKeyOnProviderOnExternalModule/error.txt: -------------------------------------------------------------------------------- 1 | error: No key defined for implementation, use the annotation value to define it 2 | public static final inversion.codgen.MultiBindingInterface provideImplA() { -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleSuperclasses/TestCase.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.errorOnMultipleSuperclasses 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.InversionImpl 22 | import inversion.of 23 | 24 | @InversionImpl 25 | class MyImplWithSuperClassAndAnotherInterface: BaseClass(), MyInterface 26 | 27 | open class BaseClass 28 | 29 | interface MyInterface { 30 | 31 | companion object { 32 | @get:InversionDef 33 | val factory by Inversion.of(MyInterface::class) 34 | } 35 | } -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorOnMultipleSuperclasses/error.txt: -------------------------------------------------------------------------------- 1 | Multiple superclasses/interfaces found for inversion.codgen.cases.errorOnMultipleSuperclasses.MyImplWithSuperClassAndAnotherInterface, please use the def parameter in InversionImpl annotation -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorWhenDefIsNotAnnotated/TestCase.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.errorWhenDefIsNotAnnotated 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.InversionImpl 22 | import inversion.of 23 | 24 | @InversionImpl 25 | class MyImplWithInterface: ExtraInterface 26 | 27 | interface ExtraInterface 28 | 29 | interface MyInterface { 30 | 31 | companion object { 32 | @get:InversionDef 33 | val factory by Inversion.of(MyInterface::class) 34 | } 35 | } -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorWhenDefIsNotAnnotated/error.txt: -------------------------------------------------------------------------------- 1 | No definition found for inversion.codgen.cases.errorWhenDefIsNotAnnotated.ExtraInterface -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorWhenImplentationDoesNotDeclareSuperclass/TestCase.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.errorWhenImplentationDoesNotDeclareSuperclass 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.InversionImpl 22 | import inversion.of 23 | import inversion.codgen.cases.noErrorOnMultipleSuperclassesWithDef.MyInterface 24 | 25 | @InversionImpl 26 | class MyEmptyImpl 27 | 28 | interface MyInterface { 29 | 30 | companion object { 31 | @get:InversionDef 32 | val factory by Inversion.of(MyInterface::class) 33 | } 34 | } -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/errorWhenImplentationDoesNotDeclareSuperclass/error.txt: -------------------------------------------------------------------------------- 1 | No superclass or interface found for inversion.codgen.cases.errorWhenImplentationDoesNotDeclareSuperclass.MyEmptyImpl -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateDef/Inversion_ext_inversion_codgen_cases_generateDef_MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion 2 | 3 | import inversion.codgen.cases.generateDef.MyInterface 4 | import inversion.codgen.cases.generateDef.MyInterface_Factory 5 | import inversion.internal.InversionDelegates 6 | import java.util.ServiceLoader 7 | import kotlin.Suppress 8 | import kotlin.jvm.JvmName 9 | import kotlin.reflect.KClass 10 | 11 | @JvmName("factory_inversion_codgen_cases_generateDef_MyInterface") 12 | fun Inversion.of(@Suppress("UNUSED_PARAMETER") c: KClass) = 13 | InversionDelegates.delegate(ServiceLoader.load(MyInterface_Factory::class.java, 14 | MyInterface_Factory::class.java.classLoader).iterator().next()) 15 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateDef/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateDef 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.of 22 | 23 | interface MyInterface { 24 | fun doSomething() 25 | 26 | companion object { 27 | @get:InversionDef 28 | val factory by Inversion.of(MyInterface::class) 29 | } 30 | } -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateDef/MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateDef 2 | 3 | import inversion.internal.InversionValidatorAdapter 4 | 5 | interface MyInterface_Factory : () -> MyInterface 6 | 7 | class MyInterface_FactoryValidator : InversionValidatorAdapter(MyInterface_Factory::class, 8 | MyInterface::class) 9 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateDefAsClassProperty/Inversion_ext_inversion_codgen_cases_generateDefAsClassProperty_MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion 2 | 3 | import inversion.codgen.cases.generateDefAsClassProperty.MyInterface 4 | import inversion.codgen.cases.generateDefAsClassProperty.MyInterface_Factory 5 | import inversion.internal.InversionDelegates 6 | import java.util.ServiceLoader 7 | import kotlin.Suppress 8 | import kotlin.jvm.JvmName 9 | import kotlin.reflect.KClass 10 | 11 | @JvmName("factory_inversion_codgen_cases_generateDefAsClassProperty_MyInterface") 12 | fun Inversion.of(@Suppress("UNUSED_PARAMETER") c: KClass) = 13 | InversionDelegates.delegateWithReceiver(ServiceLoader.load(MyInterface_Factory::class.java, 14 | MyInterface_Factory::class.java.classLoader).iterator().next()) 15 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateDefAsClassProperty/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateDefAsClassProperty 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.of 22 | 23 | class MyClass { 24 | @get:InversionDef 25 | val factory by Inversion.of(MyInterface::class) 26 | } 27 | 28 | interface MyInterface { 29 | fun doSomething() 30 | } 31 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateDefAsClassProperty/MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateDefAsClassProperty 2 | 3 | import inversion.internal.InversionValidatorAdapter 4 | 5 | interface MyInterface_Factory : (MyClass) -> MyInterface 6 | 7 | class MyInterface_FactoryValidator : InversionValidatorAdapter(MyInterface_Factory::class, 8 | MyInterface::class) 9 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateDefWithParams/Inversion_ext_inversion_codgen_cases_generateDefWithParams_MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion 2 | 3 | import inversion.codgen.cases.generateDefWithParams.MyInterface 4 | import inversion.codgen.cases.generateDefWithParams.MyInterface_Factory 5 | import inversion.internal.InversionDelegates 6 | import java.util.ServiceLoader 7 | import kotlin.Suppress 8 | import kotlin.jvm.JvmName 9 | import kotlin.reflect.KClass 10 | 11 | @JvmName("factory_inversion_codgen_cases_generateDefWithParams_MyInterface") 12 | fun Inversion.of(@Suppress("UNUSED_PARAMETER") c: KClass) = 13 | InversionDelegates.delegateWithReceiver(ServiceLoader.load(MyInterface_Factory::class.java, 14 | MyInterface_Factory::class.java.classLoader).iterator().next()) 15 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateDefWithParams/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateDefWithParams 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.of 22 | 23 | class MyClass 24 | 25 | interface MyInterface { 26 | fun doSomething() 27 | } 28 | 29 | @get:InversionDef 30 | val MyClass.factory by Inversion.of(MyInterface::class) 31 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateDefWithParams/MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateDefWithParams 2 | 3 | import inversion.internal.InversionValidatorAdapter 4 | 5 | interface MyInterface_Factory : (MyClass) -> MyInterface 6 | 7 | class MyInterface_FactoryValidator : InversionValidatorAdapter(MyInterface_Factory::class, 8 | MyInterface::class) 9 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImpl/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateImpl 18 | 19 | import inversion.InversionImpl 20 | 21 | @InversionImpl 22 | class MyImpl : MyInterface { 23 | override fun doSomething() { 24 | println("Hello world!") 25 | } 26 | } -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImpl/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateImpl 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.of 22 | 23 | interface MyInterface { 24 | fun doSomething() 25 | 26 | companion object { 27 | @get:InversionDef 28 | val factory by Inversion.of(MyInterface::class) 29 | } 30 | } -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImpl/MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateImpl 2 | 3 | import inversion.internal.InversionValidatorAdapter 4 | 5 | interface MyInterface_Factory : () -> MyInterface 6 | 7 | class MyInterface_FactoryValidator : InversionValidatorAdapter(MyInterface_Factory::class, 8 | MyInterface::class) 9 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImpl/MyInterface_FactoryImpl.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateImpl 2 | 3 | class MyInterface_FactoryImpl : MyInterface_Factory { 4 | override fun invoke(): MyInterface = MyImpl() 5 | } 6 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplBasedOnProvider/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateImplBasedOnProvider 18 | 19 | import inversion.InversionProvider 20 | 21 | class MyImpl : MyInterface { 22 | override fun doSomething() { 23 | println("Hello world!") 24 | } 25 | } 26 | 27 | @InversionProvider 28 | fun provideImpl(): MyInterface = MyImpl() 29 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplBasedOnProvider/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateImplBasedOnProvider 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.of 22 | 23 | interface MyInterface { 24 | fun doSomething() 25 | 26 | companion object { 27 | @get:InversionDef 28 | val factory by Inversion.of(MyInterface::class) 29 | } 30 | } -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplBasedOnProvider/MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateImplBasedOnProvider 2 | 3 | import inversion.internal.InversionValidatorAdapter 4 | 5 | interface MyInterface_Factory : () -> MyInterface 6 | 7 | class MyInterface_FactoryValidator : InversionValidatorAdapter(MyInterface_Factory::class, 8 | MyInterface::class) 9 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplBasedOnProvider/MyInterface_FactoryImpl.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateImplBasedOnProvider 2 | 3 | class MyInterface_FactoryImpl : MyInterface_Factory { 4 | override fun invoke(): MyInterface = provideImpl() 5 | } 6 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplOnCompanionObject/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateImplOnCompanionObject 18 | 19 | import inversion.InversionProvider 20 | 21 | class MyImpl : MyInterface { 22 | override fun doSomething() { 23 | println("Hello world!") 24 | } 25 | 26 | companion object { 27 | @InversionProvider 28 | fun provideImpl(): MyInterface = MyImpl() 29 | } 30 | } -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplOnCompanionObject/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateImplOnCompanionObject 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.of 22 | 23 | interface MyInterface { 24 | fun doSomething() 25 | 26 | companion object { 27 | @get:InversionDef 28 | val factory by Inversion.of(MyInterface::class) 29 | } 30 | } -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplOnCompanionObject/MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateImplOnCompanionObject 2 | 3 | import inversion.internal.InversionValidatorAdapter 4 | 5 | interface MyInterface_Factory : () -> MyInterface 6 | 7 | class MyInterface_FactoryValidator : InversionValidatorAdapter(MyInterface_Factory::class, 8 | MyInterface::class) 9 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplOnCompanionObject/MyInterface_FactoryImpl.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateImplOnCompanionObject 2 | 3 | class MyInterface_FactoryImpl : MyInterface_Factory { 4 | override fun invoke(): MyInterface = MyImpl.Companion.provideImpl() 5 | } 6 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplWitParams/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateImplWitParams 18 | 19 | import inversion.InversionProvider 20 | 21 | class MyImpl : MyInterface { 22 | override fun doSomething() { 23 | println("Hello world!") 24 | } 25 | } 26 | 27 | @InversionProvider 28 | fun provideImpl(@Suppress("UNUSED_PARAMETER") param: MyClass): MyInterface = MyImpl() 29 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplWitParams/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateImplWitParams 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.of 22 | 23 | class MyClass 24 | 25 | interface MyInterface { 26 | fun doSomething() 27 | } 28 | 29 | @get:InversionDef 30 | val MyClass.factory by Inversion.of(MyInterface::class) 31 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplWitParams/MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateImplWitParams 2 | 3 | import inversion.internal.InversionValidatorAdapter 4 | 5 | interface MyInterface_Factory : (MyClass) -> MyInterface 6 | 7 | class MyInterface_FactoryValidator : InversionValidatorAdapter(MyInterface_Factory::class, 8 | MyInterface::class) 9 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplWitParams/MyInterface_FactoryImpl.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateImplWitParams 2 | 3 | class MyInterface_FactoryImpl : MyInterface_Factory { 4 | override fun invoke(param: MyClass): MyInterface = provideImpl(param) 5 | } 6 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplWitReceiver/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateImplWitReceiver 18 | 19 | import inversion.InversionProvider 20 | 21 | class MyImpl : MyInterface { 22 | override fun doSomething() { 23 | println("Hello world!") 24 | } 25 | } 26 | 27 | @InversionProvider 28 | fun MyClass.provideImpl(): MyInterface = MyImpl() 29 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplWitReceiver/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.generateImplWitReceiver 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.of 22 | 23 | interface MyClass 24 | 25 | interface MyInterface { 26 | fun doSomething() 27 | } 28 | 29 | @get:InversionDef 30 | val MyClass.factory by Inversion.of(MyInterface::class) 31 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplWitReceiver/MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateImplWitReceiver 2 | 3 | import inversion.internal.InversionValidatorAdapter 4 | 5 | interface MyInterface_Factory : (MyClass) -> MyInterface 6 | 7 | class MyInterface_FactoryValidator : InversionValidatorAdapter(MyInterface_Factory::class, 8 | MyInterface::class) 9 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/generateImplWitReceiver/MyInterface_FactoryImpl.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.generateImplWitReceiver 2 | 3 | class MyInterface_FactoryImpl : MyInterface_Factory { 4 | override fun invoke(param: MyClass): MyInterface = param.provideImpl() 5 | } 6 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/multipleClassesInASingleFile/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.multipleClassesInASingleFile 18 | 19 | import inversion.InversionProvider 20 | 21 | class MyImpl : MyInterface { 22 | override fun doSomething() { 23 | println("Hello world!") 24 | } 25 | } 26 | 27 | @InversionProvider("A") 28 | fun provideImplA(): MyInterface = MyImpl() 29 | 30 | @InversionProvider("B") 31 | fun provideImplB(): MyInterface = MyImpl() 32 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/multipleClassesInASingleFile/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.multipleClassesInASingleFile 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.of 22 | 23 | interface MyInterface { 24 | fun doSomething() 25 | 26 | companion object { 27 | @get:InversionDef 28 | val factory by Inversion.of(MyInterface::class) 29 | } 30 | } 31 | 32 | interface MyInterface2 { 33 | fun doSomething() 34 | 35 | companion object { 36 | @get:InversionDef 37 | val factory by Inversion.of(MyInterface2::class) 38 | } 39 | } -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/multipleClassesInASingleFile/MyInterface2_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.multipleClassesInASingleFile 2 | 3 | import inversion.internal.InversionValidatorAdapter 4 | 5 | interface MyInterface2_Factory : () -> MyInterface2 6 | 7 | class MyInterface2_FactoryValidator : InversionValidatorAdapter(MyInterface2_Factory::class, 8 | MyInterface2::class) 9 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/multipleClassesInASingleFile/MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.multipleClassesInASingleFile 2 | 3 | import inversion.internal.InversionValidatorAdapter 4 | 5 | interface MyInterface_Factory : () -> MyInterface 6 | 7 | class MyInterface_FactoryValidator : InversionValidatorAdapter(MyInterface_Factory::class, 8 | MyInterface::class) 9 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/multipleNames/Inversion_ext_inversion_codgen_cases_multipleNames_MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion 2 | 3 | import inversion.codgen.cases.multipleNames.MyInterface 4 | import inversion.codgen.cases.multipleNames.MyInterface_Factory 5 | import inversion.internal.InversionDelegates 6 | import java.util.ServiceLoader 7 | import kotlin.Suppress 8 | import kotlin.jvm.JvmName 9 | import kotlin.reflect.KClass 10 | 11 | @JvmName("factory_inversion_codgen_cases_multipleNames_MyInterface") 12 | fun Inversion.mapOf(@Suppress("UNUSED_PARAMETER") c: KClass) = 13 | InversionDelegates.mapDelegateWithReceiver(ServiceLoader.load(MyInterface_Factory::class.java, 14 | MyInterface_Factory::class.java.classLoader).iterator().asSequence().toList()) 15 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/multipleNames/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.multipleNames 18 | 19 | import inversion.InversionProvider 20 | 21 | class MyImpl : MyInterface { 22 | override fun doSomething() { 23 | println("Hello world!") 24 | } 25 | } 26 | 27 | @InversionProvider("A") 28 | fun MyClass.provideImplA(): MyInterface = MyImpl() 29 | 30 | @InversionProvider("B") 31 | fun MyClass.provideImplB(): MyInterface = MyImpl() 32 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/multipleNames/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.multipleNames 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.mapOf 22 | 23 | interface MyClass 24 | 25 | interface MyInterface { 26 | fun doSomething() 27 | } 28 | 29 | @get:InversionDef 30 | val MyClass.factory by Inversion.mapOf(MyInterface::class) 31 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/multipleNames/MyInterface_Factory.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.multipleNames 2 | 3 | import inversion.internal.InversionValidatorAdapter 4 | import inversion.internal.NamedGeneratedFactory 5 | 6 | interface MyInterface_Factory : (MyClass) -> MyInterface, NamedGeneratedFactory 7 | 8 | class MyInterface_FactoryValidator : InversionValidatorAdapter(MyInterface_Factory::class, 9 | MyInterface::class) 10 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/multipleNames/MyInterface_FactoryImpl_A.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.multipleNames 2 | 3 | import kotlin.String 4 | 5 | class MyInterface_FactoryImpl_A : MyInterface_Factory { 6 | override val name: String = "A" 7 | 8 | override fun invoke(param: MyClass): MyInterface = param.provideImplA() 9 | } 10 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/multipleNames/MyInterface_FactoryImpl_B.kt: -------------------------------------------------------------------------------- 1 | package inversion.codgen.cases.multipleNames 2 | 3 | import kotlin.String 4 | 5 | class MyInterface_FactoryImpl_B : MyInterface_Factory { 6 | override val name: String = "B" 7 | 8 | override fun invoke(param: MyClass): MyInterface = param.provideImplB() 9 | } 10 | -------------------------------------------------------------------------------- /inversionCodGen/src/test/java/inversion/codgen/cases/noErrorOnMultipleSuperclassesWithDef/TestCase.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.codgen.cases.noErrorOnMultipleSuperclassesWithDef 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.InversionImpl 22 | import inversion.of 23 | 24 | @InversionImpl(def = MyInterface::class) 25 | class MyImplWithSuperClassAndAnotherInterface: BaseClass(), MyInterface 26 | 27 | open class BaseClass 28 | 29 | interface MyInterface { 30 | 31 | companion object { 32 | @get:InversionDef 33 | val factory by Inversion.of(MyInterface::class) 34 | } 35 | } -------------------------------------------------------------------------------- /inversionLib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /inversionLib/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'java-library' 18 | apply plugin: 'kotlin' 19 | apply plugin: 'com.github.dcendents.android-maven' 20 | 21 | group='com.github.fabioCollini.inversion' 22 | 23 | dependencies { 24 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 25 | } 26 | 27 | sourceCompatibility = "8" 28 | targetCompatibility = "8" 29 | 30 | task sourcesJar(type: Jar, dependsOn: classes) { 31 | classifier = 'sources' 32 | from sourceSets.main.allSource 33 | } 34 | 35 | task javadocJar(type: Jar, dependsOn: javadoc) { 36 | classifier = 'javadoc' 37 | from javadoc.destinationDir 38 | } 39 | 40 | artifacts { 41 | archives sourcesJar 42 | archives javadocJar 43 | } -------------------------------------------------------------------------------- /inversionLib/src/main/java/inversion/Inversion.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion 18 | 19 | import inversion.internal.InversionValidator 20 | import java.util.* 21 | import kotlin.properties.ReadOnlyProperty 22 | import kotlin.reflect.KClass 23 | 24 | object Inversion { 25 | fun validate() { 26 | val classLoader = InversionValidator::class.java.classLoader 27 | val missingImpl = ServiceLoader.load(InversionValidator::class.java, classLoader).mapNotNull { validator -> 28 | if (ServiceLoader.load(validator.factoryClass.java, classLoader).iterator().hasNext()) { 29 | null 30 | } else { 31 | validator.wrappedClass 32 | } 33 | } 34 | if (missingImpl.isNotEmpty()) { 35 | throw InversionValidationError(missingImpl) 36 | } 37 | } 38 | } 39 | 40 | class InversionValidationError(val missingImpls: List>) : Exception( 41 | "Implementation not found for classes:\n" + missingImpls.joinToString("\n") { "\t\t" + it.java.canonicalName } 42 | ) 43 | 44 | @Target(AnnotationTarget.PROPERTY_GETTER) 45 | annotation class InversionDef 46 | 47 | @Target(AnnotationTarget.CLASS) 48 | annotation class InversionImpl(val value: String = "", val def: KClass<*> = Nothing::class) 49 | 50 | @Target(AnnotationTarget.FUNCTION) 51 | annotation class InversionProvider(val value: String = "") 52 | 53 | @Deprecated("Not supported anymore, please invoke Inversion.validate() instead") 54 | annotation class InversionValidate 55 | 56 | fun Inversion.of(@Suppress("UNUSED_PARAMETER") c: KClass): ReadOnlyProperty T> = 57 | throw Exception("This method shouldn't never be invoked, there are some problems in the Inversion annotation processor") 58 | 59 | fun Inversion.mapOf(@Suppress("UNUSED_PARAMETER") c: KClass): ReadOnlyProperty Map> = 60 | throw Exception("This method shouldn't never be invoked, there are some problems in the Inversion annotation processor") 61 | -------------------------------------------------------------------------------- /inversionLib/src/main/java/inversion/internal/InversionDelegates.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.internal 18 | 19 | import kotlin.properties.ReadOnlyProperty 20 | import kotlin.reflect.KProperty 21 | 22 | object InversionDelegates { 23 | 24 | fun delegate(factoryImpl: () -> T): ReadOnlyProperty T> = 25 | object : ReadOnlyProperty T> { 26 | override fun getValue(thisRef: Any?, property: KProperty<*>): () -> T = 27 | factoryImpl 28 | } 29 | 30 | fun mapDelegate(factoryImpl: List): ReadOnlyProperty Map> 31 | where F : () -> T, F : NamedGeneratedFactory { 32 | return object : ReadOnlyProperty Map> { 33 | override fun getValue(thisRef: Any?, property: KProperty<*>): () -> Map = 34 | { 35 | factoryImpl.associate { it.name to it() } 36 | } 37 | } 38 | } 39 | 40 | fun delegateWithReceiver(factoryImpl: (R) -> T): ReadOnlyProperty T> { 41 | return object : ReadOnlyProperty T> { 42 | override fun getValue(thisRef: R, property: KProperty<*>): () -> T = 43 | { factoryImpl(thisRef) } 44 | } 45 | } 46 | 47 | fun mapDelegateWithReceiver(factoryImpl: List): ReadOnlyProperty Map> 48 | where F : (R) -> T, F : NamedGeneratedFactory { 49 | return object : ReadOnlyProperty Map> { 50 | override fun getValue(thisRef: R, property: KProperty<*>): () -> Map = 51 | { 52 | factoryImpl.associate { it.name to it(thisRef) } 53 | } 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /inversionLib/src/main/java/inversion/internal/InversionValidator.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.internal 18 | 19 | import kotlin.reflect.KClass 20 | 21 | interface InversionValidator { 22 | val wrappedClass: KClass<*> 23 | 24 | val factoryClass: KClass<*> 25 | } -------------------------------------------------------------------------------- /inversionLib/src/main/java/inversion/internal/InversionValidatorAdapter.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.internal 18 | 19 | import kotlin.reflect.KClass 20 | 21 | open class InversionValidatorAdapter( 22 | override val factoryClass: KClass<*>, 23 | override val wrappedClass: KClass<*> 24 | ) : InversionValidator -------------------------------------------------------------------------------- /inversionLib/src/main/java/inversion/internal/NamedGeneratedFactory.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.internal 18 | 19 | interface NamedGeneratedFactory { 20 | val name: String 21 | } -------------------------------------------------------------------------------- /inversionLibTest/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /inversionLibTest/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'java-library' 18 | apply plugin: 'kotlin' 19 | apply plugin: 'kotlin-kapt' 20 | 21 | dependencies { 22 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 23 | 24 | api project(':inversionLib') 25 | kapt project(':inversionCodGen') 26 | 27 | testImplementation 'junit:junit:4.13' 28 | testImplementation "com.google.truth:truth:1.0.1" 29 | } 30 | 31 | sourceCompatibility = "1.8" 32 | targetCompatibility = "1.8" -------------------------------------------------------------------------------- /inversionLibTest/src/main/java/inversion/test/DefWithNoImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.test 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.of 22 | 23 | interface DefWithNoImpl { 24 | fun doSomething(): String 25 | 26 | companion object { 27 | @get:InversionDef 28 | val factory by Inversion.of(DefWithNoImpl::class) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /inversionLibTest/src/test/java/inversion/test/DefWithNoImplTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package inversion.test 18 | 19 | import com.google.common.truth.Truth.assertThat 20 | import inversion.Inversion 21 | import inversion.InversionValidationError 22 | import org.junit.Assert.assertThrows 23 | import org.junit.Test 24 | 25 | class DefWithNoImplTest { 26 | @Test 27 | fun throwErrorWhenNoImplIsAvailable() { 28 | val exception = assertThrows(InversionValidationError::class.java) { 29 | Inversion.validate() 30 | } 31 | assertThat(exception.missingImpls).contains(DefWithNoImpl::class) 32 | } 33 | } -------------------------------------------------------------------------------- /sample/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/app/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'com.android.application' 18 | apply plugin: 'kotlin-android' 19 | apply plugin: 'kotlin-kapt' 20 | 21 | android { 22 | compileSdkVersion 29 23 | defaultConfig { 24 | applicationId "com.inversion" 25 | minSdkVersion 21 26 | targetSdkVersion 29 27 | versionCode 1 28 | versionName "1.0" 29 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 30 | } 31 | buildTypes { 32 | debug { 33 | //set to true here to verify that R8 removes the reflection calls 34 | minifyEnabled false 35 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 36 | } 37 | release { 38 | minifyEnabled true 39 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 40 | } 41 | } 42 | } 43 | 44 | dependencies { 45 | implementation project(':sample:libimpl') 46 | 47 | api project(':inversionLib') 48 | kapt project(':inversionCodGen') 49 | 50 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 51 | implementation 'androidx.appcompat:appcompat:1.1.0' 52 | implementation 'androidx.core:core-ktx:1.1.0' 53 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 54 | testImplementation 'junit:junit:4.13' 55 | } 56 | -------------------------------------------------------------------------------- /sample/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | 23 | -dontobfuscate 24 | -------------------------------------------------------------------------------- /sample/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 20 | 21 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /sample/app/src/main/java/demo/MainActivity.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package demo 18 | 19 | import android.os.Bundle 20 | import android.widget.TextView 21 | import androidx.appcompat.app.AppCompatActivity 22 | import com.inversion.BuildConfig 23 | import demo.libinterface.Container 24 | import demo.libinterface.factory 25 | import demo.libinterface.multipleInstancesMap 26 | import com.inversion.R 27 | import inversion.Inversion 28 | 29 | object MyContainer : Container { 30 | private var last: Any? = null 31 | @Suppress("UNCHECKED_CAST") 32 | override fun getOrCreate(f: () -> V): V = 33 | if (last == null) 34 | f().also { last = it } 35 | else 36 | last as V 37 | } 38 | 39 | class MainActivity : AppCompatActivity() { 40 | 41 | override fun onCreate(savedInstanceState: Bundle?) { 42 | super.onCreate(savedInstanceState) 43 | if (BuildConfig.DEBUG) { 44 | Inversion.validate() 45 | } 46 | setContentView(R.layout.activity_main) 47 | val impl = MyContainer.factory() 48 | val map = multipleInstancesMap() 49 | findViewById(R.id.text).text = impl.doSomething() + map 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /sample/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 23 | 28 | 29 | 35 | 38 | 41 | 42 | 43 | 44 | 50 | 51 | -------------------------------------------------------------------------------- /sample/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 23 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 176 | 181 | 186 | 187 | -------------------------------------------------------------------------------- /sample/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 24 | 25 | 33 | 34 | -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabioCollini/Inversion/2a56c2e33597e8a2ef0129d7b93ffe9d97c7f6df/sample/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabioCollini/Inversion/2a56c2e33597e8a2ef0129d7b93ffe9d97c7f6df/sample/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabioCollini/Inversion/2a56c2e33597e8a2ef0129d7b93ffe9d97c7f6df/sample/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabioCollini/Inversion/2a56c2e33597e8a2ef0129d7b93ffe9d97c7f6df/sample/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabioCollini/Inversion/2a56c2e33597e8a2ef0129d7b93ffe9d97c7f6df/sample/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabioCollini/Inversion/2a56c2e33597e8a2ef0129d7b93ffe9d97c7f6df/sample/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabioCollini/Inversion/2a56c2e33597e8a2ef0129d7b93ffe9d97c7f6df/sample/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabioCollini/Inversion/2a56c2e33597e8a2ef0129d7b93ffe9d97c7f6df/sample/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabioCollini/Inversion/2a56c2e33597e8a2ef0129d7b93ffe9d97c7f6df/sample/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabioCollini/Inversion/2a56c2e33597e8a2ef0129d7b93ffe9d97c7f6df/sample/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | #008577 20 | #00574B 21 | #D81B60 22 | 23 | -------------------------------------------------------------------------------- /sample/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | inversion 19 | 20 | -------------------------------------------------------------------------------- /sample/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /sample/app/src/test/java/demo/InversionTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package demo 18 | 19 | import inversion.Inversion 20 | import org.junit.Test 21 | 22 | class InversionTest { 23 | @Test 24 | fun validateAllDefinitions() { 25 | Inversion.validate() 26 | } 27 | } -------------------------------------------------------------------------------- /sample/libimpl/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/libimpl/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'java-library' 18 | apply plugin: 'kotlin' 19 | apply plugin: 'kotlin-kapt' 20 | 21 | dependencies { 22 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 23 | api project(':sample:libinterface') 24 | 25 | api project(':inversionLib') 26 | kapt project(':inversionCodGen') 27 | } 28 | 29 | sourceSets { 30 | main { 31 | java { 32 | srcDir "${buildDir.absolutePath}/generated/source/kaptKotlin/main/" 33 | } 34 | } 35 | } 36 | 37 | 38 | sourceCompatibility = "8" 39 | targetCompatibility = "8" 40 | -------------------------------------------------------------------------------- /sample/libimpl/src/main/java/demo/libimpl/MyImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package demo.libimpl 18 | 19 | import inversion.InversionImpl 20 | import inversion.InversionProvider 21 | import demo.libinterface.Container 22 | import demo.libinterface.MultiInstanceInterface 23 | import demo.libinterface.MyInterface 24 | 25 | class MyImpl : MyInterface { 26 | private val s = "Hello world!!! " + System.currentTimeMillis() 27 | 28 | override fun doSomething(): String = s 29 | } 30 | 31 | @InversionProvider 32 | fun Container.provideImpl(): MyInterface = getOrCreate { MyImpl() } 33 | 34 | @InversionImpl("A") 35 | class MultiInstanceInterfaceImpl : MultiInstanceInterface { 36 | override fun toString() = "InstanceA" 37 | } 38 | 39 | @InversionProvider("B") 40 | fun provideImplB(): MultiInstanceInterface = 41 | object : MultiInstanceInterface { 42 | override fun toString() = "InstanceB" 43 | } -------------------------------------------------------------------------------- /sample/libinterface/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/libinterface/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'java-library' 18 | apply plugin: 'kotlin' 19 | apply plugin: 'kotlin-kapt' 20 | 21 | dependencies { 22 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 23 | 24 | implementation project(':inversionLib') 25 | kapt project(':inversionCodGen') 26 | } 27 | 28 | sourceSets { 29 | main { 30 | java { 31 | srcDir "${buildDir.absolutePath}/generated/source/kaptKotlin/main/" 32 | } 33 | } 34 | } 35 | 36 | sourceCompatibility = "8" 37 | targetCompatibility = "8" 38 | -------------------------------------------------------------------------------- /sample/libinterface/src/main/java/demo/libinterface/MyInterface.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package demo.libinterface 18 | 19 | import inversion.Inversion 20 | import inversion.InversionDef 21 | import inversion.mapOf 22 | import inversion.of 23 | 24 | interface Container { 25 | fun getOrCreate(f: () -> V): V 26 | } 27 | 28 | interface MyInterface { 29 | fun doSomething(): String 30 | 31 | // companion object { 32 | // @get:InversionDef 33 | // val factory by Inversion.of(MyInterface::class) 34 | // } 35 | } 36 | 37 | @get:InversionDef 38 | val Container.factory by Inversion.of(MyInterface::class) 39 | 40 | 41 | interface MultiInstanceInterface 42 | 43 | @get:InversionDef 44 | val multipleInstancesMap by Inversion.mapOf(MultiInstanceInterface::class) -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Fabio Collini. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | include ':inversionLibTest' 17 | 18 | include ':sample:app', ':sample:libinterface', ':sample:libimpl', ':inversionLib', ':inversionCodGen', ':inversionLibTest' 19 | rootProject.name='inversion' 20 | --------------------------------------------------------------------------------