├── .circleci └── config.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── RELEASING.md ├── auto-value-cursor-annotations ├── build.gradle ├── gradle.properties └── src │ └── main │ └── java │ └── com │ └── gabrielittner │ └── auto │ └── value │ └── cursor │ ├── ColumnAdapter.java │ ├── ColumnName.java │ └── ColumnTypeAdapter.java ├── auto-value-cursor ├── build.gradle ├── gradle.properties └── src │ ├── main │ └── java │ │ └── com │ │ └── gabrielittner │ │ └── auto │ │ └── value │ │ ├── ColumnProperty.java │ │ ├── contentvalues │ │ └── AutoValueContentValuesExtension.java │ │ └── cursor │ │ └── AutoValueCursorExtension.java │ └── test │ └── java │ ├── android │ ├── content │ │ └── ContentValues.java │ └── database │ │ └── Cursor.java │ └── com │ └── gabrielittner │ └── auto │ └── value │ ├── contentvalues │ └── AutoValueContentValuesExtensionTest.java │ └── cursor │ └── AutoValueCursorExtensionTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew └── settings.gradle /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | 4 | references: 5 | cache_key_gradle_wrapper: &cache_key_gradle_wrapper 6 | key: gradle-wrapper-v1-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }} 7 | cache_key_android_sdk: &cache_key_android_sdk 8 | key: android-sdk-v1-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "build.gradle" }} 9 | cache_key_gradle_cache: &cache_key_gradle_cache 10 | key: gradle-cache-v1-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "build.gradle" }} 11 | 12 | 13 | executors: 14 | android: 15 | docker: 16 | - image: gabrielittner/android-sdk:tools-26.1.1-jdk8u222 17 | working_directory: ~/lazythreetenbp 18 | environment: 19 | - GRADLE_OPTS: -Dorg.gradle.jvmargs="-Xmx1536m" -Dorg.gradle.workers.max=2 -Dkotlin.incremental=false 20 | 21 | 22 | commands: 23 | save_gradle_wrapper: 24 | steps: 25 | - save_cache: 26 | name: "Saving Gradle Wrapper to cache" 27 | <<: *cache_key_gradle_wrapper 28 | paths: 29 | - "~/.gradle/wrapper" 30 | 31 | restore_gradle_wrapper: 32 | steps: 33 | - restore_cache: 34 | name: "Restoring Gradle Wrapper from cache" 35 | <<: *cache_key_gradle_wrapper 36 | 37 | save_android_sdk: 38 | steps: 39 | - save_cache: 40 | name: "Saving downloaded Android SDK components to cache" 41 | <<: *cache_key_android_sdk 42 | paths: 43 | #TODO replace ~/android-sdk with $ANDROID_HOME to be less reliant on docker image 44 | - "/android-sdk/build-tools" 45 | - "/android-sdk/platforms" 46 | - "/android-sdk/platform-tools" 47 | 48 | restore_android_sdk: 49 | steps: 50 | - restore_cache: 51 | name: "Restoring downloaded Android SDK components from cache" 52 | <<: *cache_key_android_sdk 53 | 54 | save_gradle_cache: 55 | steps: 56 | - save_cache: 57 | name: "Saving Gradle cache to cache" 58 | <<: *cache_key_gradle_cache 59 | paths: 60 | - "~/.android/build-cache" 61 | - "~/.gradle/caches/" 62 | 63 | restore_gradle_cache: 64 | steps: 65 | - restore_cache: 66 | name: "Restoring Gradle cache from cache" 67 | <<: *cache_key_gradle_cache 68 | 69 | gradle_android_setup: 70 | steps: 71 | - restore_gradle_wrapper 72 | - restore_android_sdk 73 | - restore_gradle_cache 74 | - run: 75 | name: Download Gradle wrapper 76 | command: ./gradlew -v 77 | - save_gradle_wrapper 78 | - run: 79 | name: Download Android SDK components 80 | #TODO https://issuetracker.google.com/issues/64934388 81 | command: ./gradlew help && sdkmanager --list 82 | - save_android_sdk 83 | 84 | 85 | jobs: 86 | build: 87 | executor: android 88 | steps: 89 | - checkout 90 | - gradle_android_setup 91 | - run: ./gradlew build --stacktrace --continue 92 | - save_gradle_cache 93 | 94 | publish_snapshot: 95 | executor: android 96 | steps: 97 | - checkout 98 | - gradle_android_setup 99 | - run: ./gradlew uploadArchives --stacktrace 100 | 101 | workflows: 102 | build: 103 | jobs: 104 | - build 105 | 106 | publish_snapshot: 107 | jobs: 108 | - publish_snapshot: 109 | context: gradle-maven-publish 110 | filters: 111 | branches: 112 | only: main 113 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # IntelliJ IDEA 2 | .idea 3 | *.iml 4 | annotations 5 | 6 | # Gradle 7 | .gradle 8 | gradlew.bat 9 | build 10 | local.properties 11 | 12 | .DS_Store -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Change Log 2 | ========== 3 | 4 | Version 2.0.1 *(2020-07-17)* 5 | ---------------------------- 6 | 7 | #### Supports: AutoValue 1.7.0 8 | 9 | - fix packaging issue 10 | 11 | Version 2.0.0 *(2020-05-03)* 12 | ---------------------------- 13 | 14 | #### Supports: AutoValue 1.7.0 15 | 16 | - support incremental annotation processing 17 | 18 | 19 | Version 2.0.0-rc1 *(2018-11-04)* 20 | ---------------------------- 21 | 22 | #### Supports: AutoValue 1.6.3rc1 23 | 24 | - support incremental annotation processing 25 | - RxJava support now requires you to declare a static `Func1` or `Function` field 26 | 27 | Version 1.1.0 *(2017-04-25)* 28 | ---------------------------- 29 | 30 | #### Supports: AutoValue 1.3 and 1.4 31 | 32 | - support generating a mapper for rxjava2 33 | - fix generating invalid code when adapters have identical names 34 | 35 | Version 1.0.1 *(2016-11-25)* 36 | ---------------------------- 37 | 38 | #### Supports: AutoValue 1.3 39 | 40 | - `@Nullable` properties don't require their column to be present 41 | 42 | Version 1.0.0 *(2016-09-09)* 43 | ---------------------------- 44 | 45 | #### Supports: AutoValue 1.3 46 | 47 | - support for AutoValue 1.3 48 | 49 | Version 1.0.0-rc1 *(2016-06-13)* 50 | ---------------------------- 51 | 52 | #### Supports: AutoValue 1.3-rc1 53 | 54 | - support for AutoValue 1.3-rc1 55 | 56 | Version 0.5.0 *(2016-06-02)* 57 | ---------------------------- 58 | 59 | #### Supports: AutoValue 1.2 60 | 61 | - new `@ColumnAdapter` annotation used in combination with `ColumnTypeAdapter` 62 | - unified annotation/adapter for `Cursor` and `ContentValues` 63 | - allows to reuse your custom type adapters (e.g. a DateAdapter) 64 | - BREAKING: removed `@CursorAdapter` and `@ValuesAdapter` 65 | - added support for AutoValue classes that use `get` and `is` prefixes in properties 66 | - removed dependency on AutoService 67 | 68 | Version 0.4.0 *(2016-04-17)* 69 | ---------------------------- 70 | 71 | #### Supports: AutoValue 1.2 72 | 73 | - added `@ValuesAdapter` to support custom type mapping for `ContentValues` 74 | - when a column is `null` and the property is annotated with `@Nullable` set the property to null instead of using the cursor's default value 75 | 76 | Version 0.3.1 *(2016-04-03)* 77 | ---------------------------- 78 | 79 | - fix opt-in using static method returning Func1 80 | 81 | 82 | Version 0.3.0 *(2016-04-02)* 83 | ---------------------------- 84 | 85 | - optionally generate a `toContentValues()` method 86 | 87 | 88 | Version 0.2.0 *(2016-03-22)* 89 | ---------------------------- 90 | 91 | Initial release. Only guaranteed to support AutoValue 1.2-rc1. 92 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AutoValue: Cursor Extension 2 | 3 | An extension for Google's [AutoValue][auto] that generates a `createFromCursor(Cursor c)` method for AutoValue annotated objects. 4 | 5 | 6 | ## Usage 7 | 8 | Include auto-value-cursor in your project and add a static factory method to your auto-value object. 9 | 10 | ```java 11 | import com.gabrielittner.auto.value.cursor.ColumnName; 12 | 13 | @AutoValue public abstract class User { 14 | abstract String id(); 15 | abstract String name(); 16 | // use the annotation if column name and field name aren't the same 17 | @ColumnName("email_address") abstract String email(); 18 | 19 | public static User create(Cursor cursor) { 20 | return AutoValue_User.createFromCursor(cursor); 21 | } 22 | 23 | // Optional: if your project includes RxJava the extension will generate a Func1 24 | public static Func1 MAPPER = AutoValue_User.MAPPER; 25 | 26 | // Optional: if your project includes RxJava 2 the extension will generate a Function 27 | public static Function MAPPER = AutoValue_User.MAPPER_FUNCTION; 28 | 29 | // Optional: When you include an abstract method that returns ContentValues and doesn't have 30 | // any parameters the extension will implement it for you 31 | abstract ContentValues toContentValues(); 32 | } 33 | ``` 34 | 35 | **Important:** The extension will only be applied when there is 36 | - a static method that returns your value type (`User` in the example) and takes a `Cursor` as parameter 37 | - and/or a static field of type `Func1` 38 | - and/or a static field of type `Function` 39 | 40 | ## Custom types 41 | 42 | The following types are supported by default: 43 | 44 | * `byte[]` 45 | * `double`/`Double` 46 | * `float`/`Float` 47 | * `int`/`Integer` 48 | * `long`/`Long` 49 | * `short`/`Short` 50 | * `String` 51 | * `boolean`/`Boolean` 52 | 53 | For other types, you need to use the `@ColumnAdapter` annotation and specify a factory 54 | class that implements the `ColumnTypeAdapter` interface. 55 | When you need to map multiple columns to one custom type you can simply ignore the given 56 | `columnName`. Eg.: 57 | 58 | `User.java`: 59 | 60 | ```java 61 | @AutoValue public abstract class User { 62 | abstract String id(); 63 | abstract String name(); 64 | @ColumnAdapter(AvatarAdapter.class) Avatar avatar(); 65 | 66 | public static User createFromCursor(Cursor cursor) { 67 | return AutoValue_User.createFromCursor(cursor); 68 | } 69 | } 70 | ``` 71 | 72 | `AvatarAdapter.java`: 73 | 74 | ```java 75 | public class AvatarAdapter implements ColumnTypeAdapter { 76 | public Avatar fromCursor(Cursor cursor, String columnName) { 77 | String smallImageUrl = cursor.getString(cursor.getColumnIndex("small_image_url"); 78 | String largeImageUrl = cursor.getString(cursor.getColumnIndex("large_image_url"); 79 | return new Avatar(smallImageUrl, largeImageUrl); 80 | } 81 | public void toContentValues(ContentValues values, String columnName, Avatar value) { 82 | // leave this empty when you don't use a "toContentValues()" method 83 | values.putString("small_image_url", value.smallImageUrl); 84 | values.putString"large_image_url", value.largeImageUrl); 85 | } 86 | } 87 | ``` 88 | 89 | `Avatar.java`: 90 | 91 | ```java 92 | public class Avatar { 93 | final String smallImageUrl; 94 | final String largeImageUrl; 95 | 96 | public Avatar(String smallImageUrl, String largeImageUrl) { 97 | this.smallImageUrl = smallImageUrl; 98 | this.largeImageUrl = largeImageUrl; 99 | } 100 | } 101 | ``` 102 | 103 | ## Download 104 | 105 | Add a Gradle dependency: 106 | 107 | ```groovy 108 | annotationProcessor 'com.gabrielittner.auto.value:auto-value-cursor:2.0.1' 109 | // if you need the @ColumnName or @ColumnAdapter annotations also include this: 110 | implementation 'com.gabrielittner.auto.value:auto-value-cursor-annotations:2.0.1' 111 | ``` 112 | 113 | Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap]. 114 | 115 | ## License 116 | 117 | This project is heavily based on [Ryan Harter][ryan]'s [auto-value-gson][auto-gson] 118 | 119 | ``` 120 | Copyright 2015 Ryan Harter. 121 | Copyright 2015 Gabriel Ittner. 122 | 123 | Licensed under the Apache License, Version 2.0 (the "License"); 124 | you may not use this file except in compliance with the License. 125 | You may obtain a copy of the License at 126 | 127 | http://www.apache.org/licenses/LICENSE-2.0 128 | 129 | Unless required by applicable law or agreed to in writing, software 130 | distributed under the License is distributed on an "AS IS" BASIS, 131 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132 | See the License for the specific language governing permissions and 133 | limitations under the License. 134 | ``` 135 | 136 | 137 | 138 | [auto]: https://github.com/google/auto 139 | [snap]: https://oss.sonatype.org/content/repositories/snapshots/ 140 | [ryan]: https://github.com/rharter/ 141 | [auto-gson]: https://github.com/rharter/auto-value-gson 142 | -------------------------------------------------------------------------------- /RELEASING.md: -------------------------------------------------------------------------------- 1 | Releasing 2 | ======== 3 | 4 | 1. Change the version in `gradle.properties` to a non-SNAPSHOT version. 5 | 2. Update the `CHANGELOG.md` for the impending release. 6 | 3. Update the `README.md` with the new version. 7 | 4. `git commit -am "Prepare for release X.Y.Z."` (where X.Y.Z is the new version) 8 | 5. `./gradlew clean uploadArchives`. 9 | 6. Visit [Sonatype Nexus](https://oss.sonatype.org/) and promote the artifact. 10 | 7. `git tag -a X.Y.X -m "Version X.Y.Z"` (where X.Y.Z is the new version) 11 | 8. Update the `gradle.properties` to the next SNAPSHOT version. 12 | 9. `git commit -am "Prepare next development version."` 13 | 10. `git push && git push --tags` 14 | 15 | If step 5 or 6 fails, drop the Sonatype repo, fix the problem, commit, and start again at step 5. -------------------------------------------------------------------------------- /auto-value-cursor-annotations/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java-library' 2 | apply plugin: 'com.vanniktech.maven.publish' 3 | 4 | sourceCompatibility = rootProject.ext.javaVersion 5 | targetCompatibility = rootProject.ext.javaVersion 6 | 7 | dependencies { 8 | compileOnly deps.android 9 | } 10 | -------------------------------------------------------------------------------- /auto-value-cursor-annotations/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_ARTIFACT_ID=auto-value-cursor-annotations 2 | POM_NAME=AutoValue: Cursor Extension Annotations 3 | POM_PACKAGING=jar 4 | -------------------------------------------------------------------------------- /auto-value-cursor-annotations/src/main/java/com/gabrielittner/auto/value/cursor/ColumnAdapter.java: -------------------------------------------------------------------------------- 1 | package com.gabrielittner.auto.value.cursor; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.Target; 5 | 6 | import static java.lang.annotation.ElementType.FIELD; 7 | import static java.lang.annotation.ElementType.METHOD; 8 | import static java.lang.annotation.RetentionPolicy.SOURCE; 9 | 10 | @Retention(SOURCE) 11 | @Target({METHOD, FIELD}) 12 | public @interface ColumnAdapter { 13 | Class> value(); 14 | } 15 | -------------------------------------------------------------------------------- /auto-value-cursor-annotations/src/main/java/com/gabrielittner/auto/value/cursor/ColumnName.java: -------------------------------------------------------------------------------- 1 | package com.gabrielittner.auto.value.cursor; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.Target; 5 | 6 | import static java.lang.annotation.ElementType.FIELD; 7 | import static java.lang.annotation.ElementType.METHOD; 8 | import static java.lang.annotation.RetentionPolicy.SOURCE; 9 | 10 | @Retention(SOURCE) 11 | @Target({METHOD, FIELD}) 12 | public @interface ColumnName { 13 | String value(); 14 | } 15 | -------------------------------------------------------------------------------- /auto-value-cursor-annotations/src/main/java/com/gabrielittner/auto/value/cursor/ColumnTypeAdapter.java: -------------------------------------------------------------------------------- 1 | package com.gabrielittner.auto.value.cursor; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | 6 | public interface ColumnTypeAdapter { 7 | 8 | T fromCursor(Cursor cursor, String columnName); 9 | 10 | void toContentValues(ContentValues values, String columnName, T value); 11 | } 12 | -------------------------------------------------------------------------------- /auto-value-cursor/build.gradle: -------------------------------------------------------------------------------- 1 | import org.gradle.internal.jvm.Jvm 2 | 3 | apply plugin: 'java-library' 4 | apply plugin: 'com.vanniktech.maven.publish' 5 | 6 | sourceCompatibility = rootProject.ext.javaVersion 7 | targetCompatibility = rootProject.ext.javaVersion 8 | 9 | dependencies { 10 | implementation project(':auto-value-cursor-annotations') 11 | api deps.auto_value 12 | implementation deps.javapoet 13 | implementation deps.guava 14 | implementation deps.auto_common 15 | implementation deps.auto_ext_util 16 | 17 | compileOnly deps.auto_service_annotations 18 | annotationProcessor deps.auto_service 19 | 20 | testImplementation deps.junit 21 | testImplementation deps.truth 22 | testImplementation deps.compile_testing 23 | testImplementation deps.jsr305 24 | if (!Jvm.current().javaVersion.isJava9Compatible()) { 25 | testImplementation files(Jvm.current().getToolsJar()) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /auto-value-cursor/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_ARTIFACT_ID=auto-value-cursor 2 | POM_NAME=AutoValue: Cursor Extension 3 | POM_PACKAGING=jar 4 | -------------------------------------------------------------------------------- /auto-value-cursor/src/main/java/com/gabrielittner/auto/value/ColumnProperty.java: -------------------------------------------------------------------------------- 1 | package com.gabrielittner.auto.value; 2 | 3 | import com.gabrielittner.auto.value.cursor.ColumnAdapter; 4 | import com.gabrielittner.auto.value.cursor.ColumnName; 5 | import com.gabrielittner.auto.value.util.Property; 6 | import com.google.auto.value.extension.AutoValueExtension; 7 | import com.google.common.collect.ImmutableList; 8 | import com.squareup.javapoet.ClassName; 9 | import com.squareup.javapoet.TypeName; 10 | import java.util.Arrays; 11 | import java.util.List; 12 | import java.util.Map; 13 | import javax.lang.model.element.ExecutableElement; 14 | import javax.lang.model.type.TypeMirror; 15 | 16 | import static com.gabrielittner.auto.value.util.ElementUtil.getAnnotationValue; 17 | 18 | public final class ColumnProperty extends Property { 19 | 20 | public static ImmutableList from(AutoValueExtension.Context context) { 21 | ImmutableList.Builder values = ImmutableList.builder(); 22 | for (Map.Entry entry : context.properties().entrySet()) { 23 | values.add(new ColumnProperty(entry.getKey(), entry.getValue())); 24 | } 25 | return values.build(); 26 | } 27 | 28 | private static final List SUPPORTED_TYPES = 29 | Arrays.asList( 30 | TypeName.get(String.class), 31 | TypeName.get(byte[].class), 32 | TypeName.get(Byte[].class), 33 | TypeName.DOUBLE, 34 | TypeName.DOUBLE.box(), 35 | TypeName.FLOAT, 36 | TypeName.FLOAT.box(), 37 | TypeName.INT, 38 | TypeName.INT.box(), 39 | TypeName.LONG, 40 | TypeName.LONG.box(), 41 | TypeName.SHORT, 42 | TypeName.SHORT.box(), 43 | TypeName.BOOLEAN, 44 | TypeName.BOOLEAN.box()); 45 | 46 | private final String columnName; 47 | private final boolean supportedType; 48 | 49 | private ColumnProperty(String humanName, ExecutableElement element) { 50 | super(humanName, element); 51 | columnName = (String) getAnnotationValue(element, ColumnName.class, "value"); 52 | supportedType = SUPPORTED_TYPES.contains(type()); 53 | } 54 | 55 | public boolean supportedType() { 56 | return supportedType; 57 | } 58 | 59 | public String columnName() { 60 | return columnName != null ? columnName : humanName(); 61 | } 62 | 63 | public ClassName columnAdapter() { 64 | TypeMirror mirror = (TypeMirror) getAnnotationValue(element(), ColumnAdapter.class, "value"); 65 | return mirror != null ? (ClassName) TypeName.get(mirror) : null; 66 | } 67 | 68 | public String cursorMethod() { 69 | if (!supportedType) { 70 | return null; 71 | } 72 | TypeName type = type(); 73 | if (type.equals(TypeName.get(byte[].class)) || type.equals(TypeName.get(Byte[].class))) { 74 | return "cursor.getBlob($L)"; 75 | } 76 | if (type.equals(TypeName.DOUBLE) || type.equals(TypeName.DOUBLE.box())) { 77 | return "cursor.getDouble($L)"; 78 | } 79 | if (type.equals(TypeName.FLOAT) || type.equals(TypeName.FLOAT.box())) { 80 | return "cursor.getFloat($L)"; 81 | } 82 | if (type.equals(TypeName.INT) || type.equals(TypeName.INT.box())) { 83 | return "cursor.getInt($L)"; 84 | } 85 | if (type.equals(TypeName.LONG) || type.equals(TypeName.LONG.box())) { 86 | return "cursor.getLong($L)"; 87 | } 88 | if (type.equals(TypeName.SHORT) || type.equals(TypeName.SHORT.box())) { 89 | return "cursor.getShort($L)"; 90 | } 91 | if (type.equals(TypeName.get(String.class))) { 92 | return "cursor.getString($L)"; 93 | } 94 | if (type.equals(TypeName.BOOLEAN) || type.equals(TypeName.BOOLEAN.box())) { 95 | return "cursor.getInt($L) == 1"; 96 | } 97 | throw new AssertionError( 98 | String.format("supportedType is true but type %s isn't handled", type)); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /auto-value-cursor/src/main/java/com/gabrielittner/auto/value/contentvalues/AutoValueContentValuesExtension.java: -------------------------------------------------------------------------------- 1 | package com.gabrielittner.auto.value.contentvalues; 2 | 3 | import com.gabrielittner.auto.value.ColumnProperty; 4 | import com.google.auto.service.AutoService; 5 | import com.google.auto.value.extension.AutoValueExtension; 6 | import com.google.common.base.Optional; 7 | import com.google.common.collect.ImmutableList; 8 | import com.google.common.collect.ImmutableMap; 9 | import com.squareup.javapoet.ClassName; 10 | import com.squareup.javapoet.FieldSpec; 11 | import com.squareup.javapoet.JavaFile; 12 | import com.squareup.javapoet.MethodSpec; 13 | import com.squareup.javapoet.TypeSpec; 14 | import java.util.Collections; 15 | import java.util.Set; 16 | import javax.annotation.processing.ProcessingEnvironment; 17 | import javax.lang.model.element.ExecutableElement; 18 | 19 | import static com.gabrielittner.auto.value.cursor.AutoValueCursorExtension.addColumnAdaptersToMethod; 20 | import static com.gabrielittner.auto.value.util.AutoValueUtil.error; 21 | import static com.gabrielittner.auto.value.util.AutoValueUtil.newTypeSpecBuilder; 22 | import static com.gabrielittner.auto.value.util.ElementUtil.getMatchingAbstractMethod; 23 | import static javax.lang.model.element.Modifier.PUBLIC; 24 | 25 | @AutoService(AutoValueExtension.class) 26 | public class AutoValueContentValuesExtension extends AutoValueExtension { 27 | 28 | private static final ClassName CONTENT_VALUES = 29 | ClassName.get("android.content", "ContentValues"); 30 | 31 | @Override 32 | public IncrementalExtensionType incrementalType(ProcessingEnvironment processingEnvironment) { 33 | return IncrementalExtensionType.ISOLATING; 34 | } 35 | 36 | @Override 37 | public boolean applicable(Context context) { 38 | return getMatchingAbstractMethod(context.abstractMethods(), CONTENT_VALUES).isPresent(); 39 | } 40 | 41 | @Override 42 | public Set consumeMethods(Context context) { 43 | Optional method = 44 | getMatchingAbstractMethod(context.abstractMethods(), CONTENT_VALUES); 45 | if (method.isPresent()) { 46 | return Collections.singleton(method.get()); 47 | } 48 | return Collections.emptySet(); 49 | } 50 | 51 | @Override 52 | public String generateClass( 53 | Context context, String className, String classToExtend,boolean isFinal) { 54 | Optional method = 55 | getMatchingAbstractMethod(context.abstractMethods(), CONTENT_VALUES); 56 | if (!method.isPresent()) throw new AssertionError("Method is null"); 57 | ImmutableList properties = ColumnProperty.from(context); 58 | 59 | TypeSpec.Builder subclass = 60 | newTypeSpecBuilder(context, className, classToExtend, isFinal) 61 | .addMethod(createToContentValuesMethod(context, method.get(), properties)); 62 | 63 | return JavaFile.builder(context.packageName(), subclass.build()).build().toString(); 64 | } 65 | 66 | private MethodSpec createToContentValuesMethod( 67 | Context context, 68 | ExecutableElement methodToImplement, 69 | ImmutableList properties) { 70 | String methodName = methodToImplement.getSimpleName().toString(); 71 | 72 | MethodSpec.Builder writeMethod = 73 | MethodSpec.methodBuilder(methodName) 74 | .addAnnotation(Override.class) 75 | .addModifiers(PUBLIC) 76 | .returns(CONTENT_VALUES) 77 | .addStatement( 78 | "$1T values = new $1T($2L)", CONTENT_VALUES, properties.size()); 79 | 80 | ImmutableMap columnAdapters = addColumnAdaptersToMethod(writeMethod, properties); 81 | 82 | for (ColumnProperty property : properties) { 83 | if (property.columnAdapter() != null) { 84 | writeMethod.addStatement( 85 | "$L.toContentValues(values, $S, $L())", 86 | columnAdapters.get(property.columnAdapter()), 87 | property.columnName(), 88 | property.methodName()); 89 | } else if (property.supportedType()) { 90 | writeMethod.addStatement( 91 | "values.put($S, $L())", property.columnName(), property.methodName()); 92 | } else { 93 | error(context, property, "Property has type that can't be put into ContentValues."); 94 | } 95 | } 96 | return writeMethod.addStatement("return values").build(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /auto-value-cursor/src/main/java/com/gabrielittner/auto/value/cursor/AutoValueCursorExtension.java: -------------------------------------------------------------------------------- 1 | package com.gabrielittner.auto.value.cursor; 2 | 3 | import com.gabrielittner.auto.value.ColumnProperty; 4 | import com.gabrielittner.auto.value.util.ElementUtil; 5 | import com.google.auto.service.AutoService; 6 | import com.google.auto.value.extension.AutoValueExtension; 7 | import com.google.common.collect.ImmutableList; 8 | import com.google.common.collect.ImmutableMap; 9 | import com.squareup.javapoet.ClassName; 10 | import com.squareup.javapoet.CodeBlock; 11 | import com.squareup.javapoet.FieldSpec; 12 | import com.squareup.javapoet.JavaFile; 13 | import com.squareup.javapoet.MethodSpec; 14 | import com.squareup.javapoet.NameAllocator; 15 | import com.squareup.javapoet.ParameterizedTypeName; 16 | import com.squareup.javapoet.TypeName; 17 | import com.squareup.javapoet.TypeSpec; 18 | import java.util.LinkedHashMap; 19 | import java.util.List; 20 | import java.util.Map; 21 | import javax.annotation.processing.ProcessingEnvironment; 22 | import javax.lang.model.element.TypeElement; 23 | 24 | import static com.gabrielittner.auto.value.util.AutoValueUtil.error; 25 | import static com.gabrielittner.auto.value.util.AutoValueUtil.getAutoValueClassTypeName; 26 | import static com.gabrielittner.auto.value.util.AutoValueUtil.getFinalClassClassName; 27 | import static com.gabrielittner.auto.value.util.AutoValueUtil.newFinalClassConstructorCall; 28 | import static com.gabrielittner.auto.value.util.AutoValueUtil.newTypeSpecBuilder; 29 | import static com.gabrielittner.auto.value.util.ElementUtil.getMatchingStaticField; 30 | import static com.gabrielittner.auto.value.util.ElementUtil.getMatchingStaticMethod; 31 | import static com.google.common.base.Preconditions.checkNotNull; 32 | import static javax.lang.model.element.Modifier.FINAL; 33 | import static javax.lang.model.element.Modifier.PUBLIC; 34 | import static javax.lang.model.element.Modifier.STATIC; 35 | 36 | @AutoService(AutoValueExtension.class) 37 | public class AutoValueCursorExtension extends AutoValueExtension { 38 | 39 | private static final ClassName CURSOR = ClassName.get("android.database", "Cursor"); 40 | private static final ClassName FUNC1 = ClassName.get("rx.functions", "Func1"); 41 | private static final ClassName FUNCTION = ClassName.get("io.reactivex.functions", "Function"); 42 | 43 | private static final String METHOD_NAME = "createFromCursor"; 44 | private static final String FUNC1_FIELD_NAME = "MAPPER"; 45 | private static final String FUNC1_METHOD_NAME = "call"; 46 | private static final String FUNCTION_FIELD_NAME = "MAPPER_FUNCTION"; 47 | private static final String FUNCTION_METHOD_NAME = "apply"; 48 | 49 | @Override 50 | public IncrementalExtensionType incrementalType(ProcessingEnvironment processingEnvironment) { 51 | return IncrementalExtensionType.ISOLATING; 52 | } 53 | 54 | @Override 55 | public boolean applicable(Context context) { 56 | TypeElement valueClass = context.autoValueClass(); 57 | return getMatchingStaticMethod(valueClass, ClassName.get(valueClass), CURSOR).isPresent() 58 | || getMatchingStaticField(valueClass, getFunc1TypeName(context)).isPresent() 59 | || getMatchingStaticField(valueClass, getFunctionTypeName(context)).isPresent(); 60 | } 61 | 62 | @Override 63 | public String generateClass( 64 | Context context, String className, String classToExtend, boolean isFinal) { 65 | ImmutableList properties = ColumnProperty.from(context); 66 | 67 | TypeSpec.Builder subclass = 68 | newTypeSpecBuilder(context, className, classToExtend, isFinal) 69 | .addMethod(createReadMethod(context, properties)); 70 | 71 | TypeName func1TypeName = getFunc1TypeName(context); 72 | if (getMatchingStaticField(context.autoValueClass(), func1TypeName).isPresent()) { 73 | subclass.addField(createRxJava1Mapper(context, func1TypeName)); 74 | } 75 | 76 | TypeName functionTypeName = getFunctionTypeName(context); 77 | if (getMatchingStaticField(context.autoValueClass(), functionTypeName).isPresent()) { 78 | subclass.addField(createRxJava2Mapper(context, functionTypeName)); 79 | } 80 | 81 | return JavaFile.builder(context.packageName(), subclass.build()).build().toString(); 82 | } 83 | 84 | private MethodSpec createReadMethod(Context context, ImmutableList properties) { 85 | MethodSpec.Builder readMethod = 86 | MethodSpec.methodBuilder(METHOD_NAME) 87 | .addModifiers(STATIC) 88 | .returns(getFinalClassClassName(context)) 89 | .addParameter(CURSOR, "cursor"); 90 | 91 | ImmutableMap columnAdapters = addColumnAdaptersToMethod(readMethod, properties); 92 | 93 | String[] names = new String[properties.size()]; 94 | for (int i = 0; i < properties.size(); i++) { 95 | ColumnProperty property = properties.get(i); 96 | names[i] = property.humanName(); 97 | 98 | if (property.columnAdapter() != null) { 99 | readMethod.addStatement( 100 | "$T $N = $L.fromCursor(cursor, $S)", 101 | property.type(), 102 | property.humanName(), 103 | columnAdapters.get(property.columnAdapter()), 104 | property.columnName()); 105 | } else if (property.supportedType()) { 106 | if (property.nullable()) { 107 | readMethod.addCode(readNullableProperty(property)); 108 | } else { 109 | readMethod.addCode(readProperty(property)); 110 | } 111 | } else if (property.nullable()) { 112 | readMethod.addCode( 113 | "$T $N = null; // can't be read from cursor\n", 114 | property.type(), 115 | property.humanName()); 116 | } else { 117 | error(context, property, "Property has type that can't be read from Cursor."); 118 | } 119 | } 120 | return readMethod 121 | .addCode("return ") 122 | .addCode(newFinalClassConstructorCall(context, names)) 123 | .build(); 124 | } 125 | 126 | private CodeBlock readProperty(ColumnProperty property) { 127 | CodeBlock getValue = CodeBlock.of(checkNotNull(property.cursorMethod()), getColumnIndexOrThrow(property)); 128 | return CodeBlock.builder() 129 | .addStatement("$T $N = $L", property.type(), property.humanName(), getValue) 130 | .build(); 131 | } 132 | 133 | private CodeBlock readNullableProperty(ColumnProperty property) { 134 | String columnIndexVar = property.humanName() + "ColumnIndex"; 135 | String cursorMethod = checkNotNull(property.cursorMethod()); 136 | CodeBlock getValue = 137 | CodeBlock.builder() 138 | .add("($L == -1 || cursor.isNull($L)) ? null : ", columnIndexVar, columnIndexVar) 139 | .add(cursorMethod, columnIndexVar) 140 | .build(); 141 | return CodeBlock.builder() 142 | .addStatement("int $L = $L", columnIndexVar, getColumnIndex(property)) 143 | .addStatement("$T $N = $L", property.type(), property.humanName(), getValue) 144 | .build(); 145 | } 146 | 147 | private CodeBlock getColumnIndexOrThrow(ColumnProperty property) { 148 | return CodeBlock.of("cursor.getColumnIndexOrThrow($S)", property.columnName()); 149 | } 150 | 151 | private CodeBlock getColumnIndex(ColumnProperty property) { 152 | return CodeBlock.of("cursor.getColumnIndex($S)", property.columnName()); 153 | } 154 | 155 | private FieldSpec createRxJava1Mapper(Context context, TypeName func1Name) { 156 | MethodSpec func1Method = 157 | MethodSpec.methodBuilder(FUNC1_METHOD_NAME) 158 | .addAnnotation(Override.class) 159 | .addModifiers(PUBLIC) 160 | .addParameter(CURSOR, "c") 161 | .returns(getFinalClassClassName(context)) 162 | .addStatement("return $L($N)", METHOD_NAME, "c") 163 | .build(); 164 | TypeSpec func1 = 165 | TypeSpec.anonymousClassBuilder("") 166 | .addSuperinterface(func1Name) 167 | .addMethod(func1Method) 168 | .build(); 169 | return FieldSpec.builder(func1Name, FUNC1_FIELD_NAME, STATIC, FINAL) 170 | .initializer("$L", func1) 171 | .build(); 172 | } 173 | 174 | private FieldSpec createRxJava2Mapper(Context context, TypeName functionName) { 175 | MethodSpec functionMethod = 176 | MethodSpec.methodBuilder(FUNCTION_METHOD_NAME) 177 | .addAnnotation(Override.class) 178 | .addModifiers(PUBLIC) 179 | .addParameter(CURSOR, "c") 180 | .returns(getFinalClassClassName(context)) 181 | .addStatement("return $L($N)", METHOD_NAME, "c") 182 | .build(); 183 | TypeSpec function = 184 | TypeSpec.anonymousClassBuilder("") 185 | .addSuperinterface(functionName) 186 | .addMethod(functionMethod) 187 | .build(); 188 | return FieldSpec.builder(functionName, FUNCTION_FIELD_NAME, STATIC, FINAL) 189 | .initializer("$L", function) 190 | .build(); 191 | } 192 | 193 | private TypeName getFunc1TypeName(Context context) { 194 | return ParameterizedTypeName.get(FUNC1, CURSOR, getAutoValueClassTypeName(context)); 195 | } 196 | 197 | private TypeName getFunctionTypeName(Context context) { 198 | return ParameterizedTypeName.get(FUNCTION, CURSOR, getAutoValueClassTypeName(context)); 199 | } 200 | 201 | public static ImmutableMap addColumnAdaptersToMethod( 202 | MethodSpec.Builder method, 203 | List properties) { 204 | Map columnAdapters = new LinkedHashMap<>(); 205 | NameAllocator nameAllocator = new NameAllocator(); 206 | for (ColumnProperty property : properties) { 207 | ClassName adapter = property.columnAdapter(); 208 | if (adapter != null && !columnAdapters.containsKey(adapter)) { 209 | String name = nameAllocator.newName(toLowerCase(adapter.simpleName())); 210 | method.addStatement("$1T $2L = new $1T()", adapter, name); 211 | columnAdapters.put(adapter, name); 212 | } 213 | } 214 | return ImmutableMap.copyOf(columnAdapters); 215 | } 216 | 217 | private static String toLowerCase(String s) { 218 | return Character.toLowerCase(s.charAt(0)) + s.substring(1); 219 | } 220 | } 221 | -------------------------------------------------------------------------------- /auto-value-cursor/src/test/java/android/content/ContentValues.java: -------------------------------------------------------------------------------- 1 | package android.content; 2 | 3 | import java.util.Map; 4 | import java.util.Set; 5 | 6 | public final class ContentValues { 7 | public ContentValues() { 8 | } 9 | 10 | public ContentValues(int size) { 11 | } 12 | 13 | public void put(String key, String value) { 14 | } 15 | 16 | public void putAll(ContentValues other) { 17 | } 18 | 19 | public void put(String key, Byte value) { 20 | } 21 | 22 | public void put(String key, Short value) { 23 | } 24 | 25 | public void put(String key, Integer value) { 26 | } 27 | 28 | public void put(String key, Long value) { 29 | } 30 | 31 | public void put(String key, Float value) { 32 | } 33 | 34 | public void put(String key, Double value) { 35 | } 36 | 37 | public void put(String key, Boolean value) { 38 | } 39 | 40 | public void put(String key, byte[] value) { 41 | } 42 | 43 | public void putNull(String key) { 44 | } 45 | 46 | public int size() { 47 | return 0; 48 | } 49 | 50 | public void remove(String key) { 51 | } 52 | 53 | public void clear() { 54 | } 55 | 56 | public boolean containsKey(String key) { 57 | return false; 58 | } 59 | 60 | public Object get(String key) { 61 | return null; 62 | } 63 | 64 | public String getAsString(String key) { 65 | return null; 66 | } 67 | 68 | public Long getAsLong(String key) { 69 | return null; 70 | } 71 | 72 | public Integer getAsInteger(String key) { 73 | return null; 74 | } 75 | 76 | public Short getAsShort(String key) { 77 | return null; 78 | } 79 | 80 | public Byte getAsByte(String key) { 81 | return null; 82 | } 83 | 84 | public Double getAsDouble(String key) { 85 | return null; 86 | } 87 | 88 | public Float getAsFloat(String key) { 89 | return null; 90 | } 91 | 92 | public Boolean getAsBoolean(String key) { 93 | return null; 94 | } 95 | 96 | public byte[] getAsByteArray(String key) { 97 | return new byte[0]; 98 | } 99 | 100 | public Set> valueSet() { 101 | return null; 102 | } 103 | 104 | public Set keySet() { 105 | return null; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /auto-value-cursor/src/test/java/android/database/Cursor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 The Android Open Source Project 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 android.database; 17 | 18 | import java.io.Closeable; 19 | 20 | /** 21 | * This interface provides random read-write access to the result set returned 22 | * by a database query. 23 | *

24 | * Cursor implementations are not required to be synchronized so code using a Cursor from multiple 25 | * threads should perform its own synchronization when using the Cursor. 26 | *

27 | */ 28 | public interface Cursor extends Closeable { 29 | /** 30 | * Returns the zero-based index for the given column name, or -1 if the column doesn't exist. 31 | * If you expect the column to exist use {@link #getColumnIndexOrThrow(String)} instead, which 32 | * will make the error more clear. 33 | * 34 | * @param columnName the name of the target column. 35 | * @return the zero-based column index for the given column name, or -1 if 36 | * the column name does not exist. 37 | * @see #getColumnIndexOrThrow(String) 38 | */ 39 | int getColumnIndex(String columnName); 40 | /** 41 | * Returns the zero-based index for the given column name, or throws 42 | * {@link IllegalArgumentException} if the column doesn't exist. If you're not sure if 43 | * a column will exist or not use {@link #getColumnIndex(String)} and check for -1, which 44 | * is more efficient than catching the exceptions. 45 | * 46 | * @param columnName the name of the target column. 47 | * @return the zero-based column index for the given column name 48 | * @see #getColumnIndex(String) 49 | * @throws IllegalArgumentException if the column does not exist 50 | */ 51 | int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException; 52 | 53 | /** 54 | * Returns the value of the requested column as a byte array. 55 | * 56 | *

The result and whether this method throws an exception when the 57 | * column value is null or the column type is not a blob type is 58 | * implementation-defined. 59 | * 60 | * @param columnIndex the zero-based index of the target column. 61 | * @return the value of that column as a byte array. 62 | */ 63 | byte[] getBlob(int columnIndex); 64 | /** 65 | * Returns the value of the requested column as a String. 66 | * 67 | *

The result and whether this method throws an exception when the 68 | * column value is null or the column type is not a string type is 69 | * implementation-defined. 70 | * 71 | * @param columnIndex the zero-based index of the target column. 72 | * @return the value of that column as a String. 73 | */ 74 | String getString(int columnIndex); 75 | 76 | /** 77 | * Returns the value of the requested column as a short. 78 | * 79 | *

The result and whether this method throws an exception when the 80 | * column value is null, the column type is not an integral type, or the 81 | * integer value is outside the range [Short.MIN_VALUE, 82 | * Short.MAX_VALUE] is implementation-defined. 83 | * 84 | * @param columnIndex the zero-based index of the target column. 85 | * @return the value of that column as a short. 86 | */ 87 | short getShort(int columnIndex); 88 | /** 89 | * Returns the value of the requested column as an int. 90 | * 91 | *

The result and whether this method throws an exception when the 92 | * column value is null, the column type is not an integral type, or the 93 | * integer value is outside the range [Integer.MIN_VALUE, 94 | * Integer.MAX_VALUE] is implementation-defined. 95 | * 96 | * @param columnIndex the zero-based index of the target column. 97 | * @return the value of that column as an int. 98 | */ 99 | int getInt(int columnIndex); 100 | /** 101 | * Returns the value of the requested column as a long. 102 | * 103 | *

The result and whether this method throws an exception when the 104 | * column value is null, the column type is not an integral type, or the 105 | * integer value is outside the range [Long.MIN_VALUE, 106 | * Long.MAX_VALUE] is implementation-defined. 107 | * 108 | * @param columnIndex the zero-based index of the target column. 109 | * @return the value of that column as a long. 110 | */ 111 | long getLong(int columnIndex); 112 | /** 113 | * Returns the value of the requested column as a float. 114 | * 115 | *

The result and whether this method throws an exception when the 116 | * column value is null, the column type is not a floating-point type, or the 117 | * floating-point value is not representable as a float value is 118 | * implementation-defined. 119 | * 120 | * @param columnIndex the zero-based index of the target column. 121 | * @return the value of that column as a float. 122 | */ 123 | float getFloat(int columnIndex); 124 | /** 125 | * Returns the value of the requested column as a double. 126 | * 127 | *

The result and whether this method throws an exception when the 128 | * column value is null, the column type is not a floating-point type, or the 129 | * floating-point value is not representable as a double value is 130 | * implementation-defined. 131 | * 132 | * @param columnIndex the zero-based index of the target column. 133 | * @return the value of that column as a double. 134 | */ 135 | double getDouble(int columnIndex); 136 | 137 | boolean isNull (int columnIndex); 138 | } 139 | -------------------------------------------------------------------------------- /auto-value-cursor/src/test/java/com/gabrielittner/auto/value/contentvalues/AutoValueContentValuesExtensionTest.java: -------------------------------------------------------------------------------- 1 | package com.gabrielittner.auto.value.contentvalues; 2 | 3 | import com.google.auto.value.processor.AutoValueProcessor; 4 | import com.google.testing.compile.JavaFileObjects; 5 | import java.util.Arrays; 6 | import java.util.Collections; 7 | import javax.tools.JavaFileObject; 8 | import org.junit.Test; 9 | 10 | import static com.google.common.truth.Truth.assertAbout; 11 | import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources; 12 | 13 | public class AutoValueContentValuesExtensionTest { 14 | 15 | @Test 16 | public void simple() { 17 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 18 | + "package test;\n" 19 | + "import com.google.auto.value.AutoValue;\n" 20 | + "import android.content.ContentValues;\n" 21 | + "@AutoValue public abstract class Test {\n" 22 | + " public abstract int a();\n" 23 | + " public abstract String b();\n" 24 | + " public abstract ContentValues toContentValues();\n" 25 | + "}\n"); 26 | 27 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 28 | + "package test;\n" 29 | + "import android.content.ContentValues;\n" 30 | + "import java.lang.Override;\n" 31 | + "import java.lang.String;\n" 32 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 33 | + " AutoValue_Test(int a, String b) {\n" 34 | + " super(a, b);\n" 35 | + " }\n" 36 | + " @Override\n" 37 | + " public ContentValues toContentValues() {\n" 38 | + " ContentValues values = new ContentValues(2);\n" 39 | + " values.put(\"a\", a());\n" 40 | + " values.put(\"b\", b());\n" 41 | + " return values;\n" 42 | + " }\n" 43 | + "}\n"); 44 | 45 | assertAbout(javaSources()) 46 | .that(Collections.singletonList(source)) 47 | .processedWith(new AutoValueProcessor()) 48 | .compilesWithoutError() 49 | .and() 50 | .generatesSources(expected); 51 | } 52 | 53 | @Test 54 | public void columnName() { 55 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 56 | + "package test;\n" 57 | + "import com.gabrielittner.auto.value.cursor.ColumnName;\n" 58 | + "import com.google.auto.value.AutoValue;\n" 59 | + "import android.content.ContentValues;\n" 60 | + "@AutoValue public abstract class Test {\n" 61 | + " public abstract int a();\n" 62 | + " @ColumnName(\"column_b\") public abstract String b();\n" 63 | + " public abstract ContentValues toContentValues();\n" 64 | + "}\n"); 65 | 66 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 67 | + "package test;\n" 68 | + "import android.content.ContentValues;\n" 69 | + "import java.lang.Override;\n" 70 | + "import java.lang.String;\n" 71 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 72 | + " AutoValue_Test(int a, String b) {\n" 73 | + " super(a, b);\n" 74 | + " }\n" 75 | + " @Override\n" 76 | + " public ContentValues toContentValues() {\n" 77 | + " ContentValues values = new ContentValues(2);\n" 78 | + " values.put(\"a\", a());\n" 79 | + " values.put(\"column_b\", b());\n" 80 | + " return values;\n" 81 | + " }\n" 82 | + "}\n"); 83 | 84 | assertAbout(javaSources()) 85 | .that(Collections.singletonList(source)) 86 | .processedWith(new AutoValueProcessor()) 87 | .compilesWithoutError() 88 | .and() 89 | .generatesSources(expected); 90 | } 91 | 92 | @Test 93 | public void unsupported() { 94 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 95 | + "package test;\n" 96 | + "import com.google.auto.value.AutoValue;\n" 97 | + "import android.content.ContentValues;\n" 98 | + "import javax.annotation.Nullable;\n" 99 | + "@AutoValue public abstract class Test {\n" 100 | + " @Nullable public abstract int[] a();\n" 101 | + " public abstract String b();\n" 102 | + " public abstract ContentValues toContentValues();\n" 103 | + "}\n"); 104 | 105 | assertAbout(javaSources()) 106 | .that(Collections.singletonList(source)) 107 | .processedWith(new AutoValueProcessor()) 108 | .failsToCompile() 109 | .withErrorContaining("Property has type that can't be put into ContentValues."); 110 | } 111 | 112 | @Test 113 | public void allContentValuesTypes() { 114 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 115 | + "package test;\n" 116 | + "import com.google.auto.value.AutoValue;\n" 117 | + "import android.content.ContentValues;\n" 118 | + "@AutoValue public abstract class Test {\n" 119 | + " public abstract String a();\n" 120 | + " public abstract int b();\n" 121 | + " public abstract Integer c();\n" 122 | + " public abstract long d();\n" 123 | + " public abstract Long e();\n" 124 | + " public abstract short f();\n" 125 | + " public abstract Short g();\n" 126 | + " public abstract double h();\n" 127 | + " public abstract Double i();\n" 128 | + " public abstract float j();\n" 129 | + " public abstract Float k();\n" 130 | + " public abstract boolean l();\n" 131 | + " public abstract Boolean m();\n" 132 | + " public abstract byte[] n();\n" 133 | + " public abstract ContentValues contentValues();\n" 134 | + "}\n"); 135 | 136 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 137 | + "package test;\n" 138 | + "import android.content.ContentValues;\n" 139 | + "import java.lang.Boolean;\n" 140 | + "import java.lang.Double;\n" 141 | + "import java.lang.Float;\n" 142 | + "import java.lang.Integer;\n" 143 | + "import java.lang.Long;\n" 144 | + "import java.lang.Override;\n" 145 | + "import java.lang.Short;\n" 146 | + "import java.lang.String;\n" 147 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 148 | + " AutoValue_Test(String a, int b, Integer c, long d, Long e, short f, Short g, double h, Double i, float j, Float k, boolean l, Boolean m, byte[] n) {\n" 149 | + " super(a, b, c, d, e, f, g, h, i, j, k, l, m, n);\n" 150 | + " }\n" 151 | + " @Override\n" 152 | + " public ContentValues contentValues() {\n" 153 | + " ContentValues values = new ContentValues(14);\n" 154 | + " values.put(\"a\", a());\n" 155 | + " values.put(\"b\", b());\n" 156 | + " values.put(\"c\", c());\n" 157 | + " values.put(\"d\", d());\n" 158 | + " values.put(\"e\", e());\n" 159 | + " values.put(\"f\", f());\n" 160 | + " values.put(\"g\", g());\n" 161 | + " values.put(\"h\", h());\n" 162 | + " values.put(\"i\", i());\n" 163 | + " values.put(\"j\", j());\n" 164 | + " values.put(\"k\", k());\n" 165 | + " values.put(\"l\", l());\n" 166 | + " values.put(\"m\", m());\n" 167 | + " values.put(\"n\", n());\n" 168 | + " return values;\n" 169 | + " }\n" 170 | + "}\n"); 171 | 172 | assertAbout(javaSources()) 173 | .that(Collections.singletonList(source)) 174 | .processedWith(new AutoValueProcessor()) 175 | .compilesWithoutError() 176 | .and() 177 | .generatesSources(expected); 178 | } 179 | 180 | @Test 181 | public void valuesAdapter() { 182 | JavaFileObject fooClass = JavaFileObjects.forSourceString("test.Foo", "" 183 | + "package test;\n" 184 | + "import android.content.ContentValues;\n" 185 | + "import android.database.Cursor;\n" 186 | + "import com.gabrielittner.auto.value.cursor.ColumnTypeAdapter;\n" 187 | + "public class Foo {\n" 188 | + " public final String data;\n" 189 | + " public Foo(String data) {\n" 190 | + " this.data = data;\n" 191 | + " }\n" 192 | + " public static class Adapter implements ColumnTypeAdapter {\n" 193 | + " public Foo fromCursor(Cursor cursor, String columnName) {\n" 194 | + " return new Foo(cursor.getString(cursor.getColumnIndex(columnName)));\n" 195 | + " }\n" 196 | + " public void toContentValues(ContentValues values, String columnName, Foo value) {\n" 197 | + " values.put(columnName, value.data);\n" 198 | + " }\n" 199 | + " }\n" 200 | + "}\n"); 201 | JavaFileObject stringFactorySource = JavaFileObjects.forSourceString("test.Adapter", "" 202 | + "package test;\n" 203 | + "import android.content.ContentValues;\n" 204 | + "import android.database.Cursor;\n" 205 | + "import com.gabrielittner.auto.value.cursor.ColumnTypeAdapter;\n" 206 | + "public class Adapter implements ColumnTypeAdapter {\n" 207 | + " public String fromCursor(Cursor cursor, String columnName) {\n" 208 | + " return cursor.getString(cursor.getColumnIndex(columnName));\n" 209 | + " }\n" 210 | + " public void toContentValues(ContentValues values, String columnName, String value) {\n" 211 | + " values.put(columnName, value);\n" 212 | + " }\n" 213 | + "}\n"); 214 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 215 | + "package test;\n" 216 | + "import android.content.ContentValues;\n" 217 | + "import com.gabrielittner.auto.value.cursor.ColumnName;\n" 218 | + "import com.gabrielittner.auto.value.cursor.ColumnAdapter;\n" 219 | + "import com.google.auto.value.AutoValue;\n" 220 | + "import javax.annotation.Nullable;\n" 221 | + "@AutoValue public abstract class Test {\n" 222 | + " public abstract ContentValues toContentValues();\n" 223 | + " @ColumnAdapter(Foo.Adapter.class) public abstract Foo foo();\n" 224 | + " @ColumnAdapter(Adapter.class) public abstract String bar();\n" 225 | + " @ColumnAdapter(Adapter.class) @ColumnName(\"column\") public abstract String columnName();\n" 226 | + "}\n"); 227 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 228 | + "package test;\n" 229 | + "import android.content.ContentValues;\n" 230 | + "import java.lang.Override;\n" 231 | + "import java.lang.String;\n" 232 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 233 | + " AutoValue_Test(Foo foo, String bar, String columnName) {\n" 234 | + " super(foo, bar, columnName);\n" 235 | + " }\n" 236 | + " @Override\n" 237 | + " public ContentValues toContentValues() {\n" 238 | + " ContentValues values = new ContentValues(3);\n" 239 | + " Foo.Adapter adapter = new Foo.Adapter();\n" 240 | + " Adapter adapter_ = new Adapter();\n" 241 | + " adapter.toContentValues(values, \"foo\", foo());\n" 242 | + " adapter_.toContentValues(values, \"bar\", bar());\n" 243 | + " adapter_.toContentValues(values, \"column\", columnName());\n" 244 | + " return values;\n" 245 | + " }\n" 246 | + "}\n"); 247 | 248 | assertAbout(javaSources()) 249 | .that(Arrays.asList(fooClass, stringFactorySource, source)) 250 | .processedWith(new AutoValueProcessor()) 251 | .compilesWithoutError() 252 | .and() 253 | .generatesSources(expected); 254 | } 255 | 256 | @Test 257 | public void baseClass() { 258 | JavaFileObject source = JavaFileObjects.forSourceString("test.BaseTest", "" 259 | + "package test;\n" 260 | + "import android.content.ContentValues;\n" 261 | + "public abstract class BaseTest {\n" 262 | + " public abstract ContentValues toContentValues();\n" 263 | + "}\n"); 264 | JavaFileObject source2 = JavaFileObjects.forSourceString("test.Test", "" 265 | + "package test;\n" 266 | + "import com.google.auto.value.AutoValue;\n" 267 | + "@AutoValue public abstract class Test extends BaseTest {\n" 268 | + " public abstract int a();\n" 269 | + " public abstract String b();\n" 270 | + "}\n"); 271 | 272 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 273 | + "package test;\n" 274 | + "import android.content.ContentValues;\n" 275 | + "import java.lang.Override;\n" 276 | + "import java.lang.String;\n" 277 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 278 | + " AutoValue_Test(int a, String b) {\n" 279 | + " super(a, b);\n" 280 | + " }\n" 281 | + " @Override\n" 282 | + " public ContentValues toContentValues() {\n" 283 | + " ContentValues values = new ContentValues(2);\n" 284 | + " values.put(\"a\", a());\n" 285 | + " values.put(\"b\", b());\n" 286 | + " return values;\n" 287 | + " }\n" 288 | + "}\n"); 289 | 290 | assertAbout(javaSources()) 291 | .that(Arrays.asList(source, source2)) 292 | .processedWith(new AutoValueProcessor()) 293 | .compilesWithoutError() 294 | .and() 295 | .generatesSources(expected); 296 | } 297 | 298 | @Test 299 | public void prefixedMethods() { 300 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 301 | + "package test;\n" 302 | + "import com.google.auto.value.AutoValue;\n" 303 | + "import android.content.ContentValues;\n" 304 | + "@AutoValue public abstract class Test {\n" 305 | + " public abstract int getA();\n" 306 | + " public abstract String getB();\n" 307 | + " public abstract ContentValues toContentValues();\n" 308 | + "}\n"); 309 | 310 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 311 | + "package test;\n" 312 | + "import android.content.ContentValues;\n" 313 | + "import java.lang.Override;\n" 314 | + "import java.lang.String;\n" 315 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 316 | + " AutoValue_Test(int a, String b) {\n" 317 | + " super(a, b);\n" 318 | + " }\n" 319 | + " @Override\n" 320 | + " public ContentValues toContentValues() {\n" 321 | + " ContentValues values = new ContentValues(2);\n" 322 | + " values.put(\"a\", getA());\n" 323 | + " values.put(\"b\", getB());\n" 324 | + " return values;\n" 325 | + " }\n" 326 | + "}\n"); 327 | 328 | assertAbout(javaSources()) 329 | .that(Collections.singletonList(source)) 330 | .processedWith(new AutoValueProcessor()) 331 | .compilesWithoutError() 332 | .and() 333 | .generatesSources(expected); 334 | } 335 | } 336 | -------------------------------------------------------------------------------- /auto-value-cursor/src/test/java/com/gabrielittner/auto/value/cursor/AutoValueCursorExtensionTest.java: -------------------------------------------------------------------------------- 1 | package com.gabrielittner.auto.value.cursor; 2 | 3 | import com.google.auto.value.processor.AutoValueProcessor; 4 | import com.google.testing.compile.JavaFileObjects; 5 | import java.util.Arrays; 6 | import java.util.Collections; 7 | import javax.tools.JavaFileObject; 8 | import org.junit.Test; 9 | 10 | import static com.google.common.truth.Truth.assertAbout; 11 | import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources; 12 | 13 | public class AutoValueCursorExtensionTest { 14 | 15 | @Test 16 | public void simple() { 17 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 18 | + "package test;\n" 19 | + "import com.google.auto.value.AutoValue;\n" 20 | + "import android.database.Cursor;\n" 21 | + "@AutoValue public abstract class Test {\n" 22 | + " public static Test blah(Cursor cursor) { return null; }\n" 23 | + " public abstract int a();\n" 24 | + " public abstract String b();\n" 25 | + "}\n"); 26 | 27 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 28 | + "package test;\n" 29 | + "import android.database.Cursor;\n" 30 | + "import java.lang.String;\n" 31 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 32 | + " AutoValue_Test(int a, String b) {\n" 33 | + " super(a, b);\n" 34 | + " }\n" 35 | + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" 36 | + " int a = cursor.getInt(cursor.getColumnIndexOrThrow(\"a\"));\n" 37 | + " String b = cursor.getString(cursor.getColumnIndexOrThrow(\"b\"));\n" 38 | + " return new AutoValue_Test(a, b);\n" 39 | + " }\n" 40 | + "}\n"); 41 | 42 | assertAbout(javaSources()) 43 | .that(Collections.singletonList(source)) 44 | .processedWith(new AutoValueProcessor()) 45 | .compilesWithoutError() 46 | .and() 47 | .generatesSources(expected); 48 | } 49 | 50 | @Test 51 | public void columnName() { 52 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 53 | + "package test;\n" 54 | + "import com.gabrielittner.auto.value.cursor.ColumnName;\n" 55 | + "import com.google.auto.value.AutoValue;\n" 56 | + "import android.database.Cursor;\n" 57 | + "@AutoValue public abstract class Test {\n" 58 | + " public static Test blah(Cursor cursor) { return null; }\n" 59 | + " public abstract int a();\n" 60 | + " @ColumnName(\"column_b\") public abstract String b();\n" 61 | + "}\n"); 62 | 63 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 64 | + "package test;\n" 65 | + "import android.database.Cursor;\n" 66 | + "import java.lang.String;\n" 67 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 68 | + " AutoValue_Test(int a, String b) {\n" 69 | + " super(a, b);\n" 70 | + " }\n" 71 | + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" 72 | + " int a = cursor.getInt(cursor.getColumnIndexOrThrow(\"a\"));\n" 73 | + " String b = cursor.getString(cursor.getColumnIndexOrThrow(\"column_b\"));\n" 74 | + " return new AutoValue_Test(a, b);\n" 75 | + " }\n" 76 | + "}\n"); 77 | 78 | assertAbout(javaSources()) 79 | .that(Collections.singletonList(source)) 80 | .processedWith(new AutoValueProcessor()) 81 | .compilesWithoutError() 82 | .and() 83 | .generatesSources(expected); 84 | } 85 | 86 | @Test 87 | public void nullable() { 88 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 89 | + "package test;\n" 90 | + "import com.google.auto.value.AutoValue;\n" 91 | + "import android.database.Cursor;\n" 92 | + "import javax.annotation.Nullable;\n" 93 | + "@AutoValue public abstract class Test {\n" 94 | + " public static Test blah(Cursor cursor) { return null; }\n" 95 | + " public abstract int a();\n" 96 | + " @Nullable public abstract String b();\n" 97 | + "}\n"); 98 | 99 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 100 | + "package test;\n" 101 | + "import android.database.Cursor;\n" 102 | + "import java.lang.String;\n" 103 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 104 | + " AutoValue_Test(int a, String b) {\n" 105 | + " super(a, b);\n" 106 | + " }\n" 107 | + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" 108 | + " int a = cursor.getInt(cursor.getColumnIndexOrThrow(\"a\"));\n" 109 | + " int bColumnIndex = cursor.getColumnIndex(\"b\");" 110 | + " String b = (bColumnIndex == -1 || cursor.isNull(bColumnIndex)) ? null : cursor.getString(bColumnIndex);\n" 111 | + " return new AutoValue_Test(a, b);\n" 112 | + " }\n" 113 | + "}\n"); 114 | 115 | assertAbout(javaSources()) 116 | .that(Collections.singletonList(source)) 117 | .processedWith(new AutoValueProcessor()) 118 | .compilesWithoutError() 119 | .and() 120 | .generatesSources(expected); 121 | } 122 | 123 | @Test 124 | public void nullableColumnName() { 125 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 126 | + "package test;\n" 127 | + "import com.gabrielittner.auto.value.cursor.ColumnName;\n" 128 | + "import com.google.auto.value.AutoValue;\n" 129 | + "import android.database.Cursor;\n" 130 | + "import javax.annotation.Nullable;\n" 131 | + "@AutoValue public abstract class Test {\n" 132 | + " public static Test blah(Cursor cursor) { return null; }\n" 133 | + " public abstract int a();\n" 134 | + " @Nullable @ColumnName(\"column_b\") public abstract String b();\n" 135 | + "}\n"); 136 | 137 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 138 | + "package test;\n" 139 | + "import android.database.Cursor;\n" 140 | + "import java.lang.String;\n" 141 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 142 | + " AutoValue_Test(int a, String b) {\n" 143 | + " super(a, b);\n" 144 | + " }\n" 145 | + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" 146 | + " int a = cursor.getInt(cursor.getColumnIndexOrThrow(\"a\"));\n" 147 | + " int bColumnIndex = cursor.getColumnIndex(\"column_b\");" 148 | + " String b = (bColumnIndex == -1 || cursor.isNull(bColumnIndex)) ? null : cursor.getString(bColumnIndex);\n" 149 | + " return new AutoValue_Test(a, b);\n" 150 | + " }\n" 151 | + "}\n"); 152 | 153 | assertAbout(javaSources()) 154 | .that(Collections.singletonList(source)) 155 | .processedWith(new AutoValueProcessor()) 156 | .compilesWithoutError() 157 | .and() 158 | .generatesSources(expected); 159 | } 160 | 161 | @Test 162 | public void unsupported() { 163 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 164 | + "package test;\n" 165 | + "import com.google.auto.value.AutoValue;\n" 166 | + "import android.database.Cursor;\n" 167 | + "@AutoValue public abstract class Test {\n" 168 | + " public static Test blah(Cursor cursor) { return null; }\n" 169 | + " public abstract int[] a();\n" 170 | + " public abstract String b();\n" 171 | + "}\n"); 172 | 173 | assertAbout(javaSources()) 174 | .that(Collections.singletonList(source)) 175 | .processedWith(new AutoValueProcessor()) 176 | .failsToCompile() 177 | .withErrorContaining("Property has type that can't be read from Cursor."); 178 | } 179 | 180 | @Test 181 | public void unsupportedWithNullable() { 182 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 183 | + "package test;\n" 184 | + "import com.google.auto.value.AutoValue;\n" 185 | + "import android.database.Cursor;\n" 186 | + "import javax.annotation.Nullable;\n" 187 | + "@AutoValue public abstract class Test {\n" 188 | + " public static Test blah(Cursor cursor) { return null; }\n" 189 | + " @Nullable public abstract int[] a();\n" 190 | + " public abstract String b();\n" 191 | + "}\n"); 192 | 193 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 194 | + "package test;\n" 195 | + "import android.database.Cursor;\n" 196 | + "import java.lang.String;\n" 197 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 198 | + " AutoValue_Test(int[] a, String b) {\n" 199 | + " super(a, b);\n" 200 | + " }\n" 201 | + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" 202 | + " int[] a = null; // can't be read from cursor\n" 203 | + " String b = cursor.getString(cursor.getColumnIndexOrThrow(\"b\"));\n" 204 | + " return new AutoValue_Test(a, b);\n" 205 | + " }\n" 206 | + "}\n"); 207 | 208 | assertAbout(javaSources()) 209 | .that(Collections.singletonList(source)) 210 | .processedWith(new AutoValueProcessor()) 211 | .compilesWithoutError() 212 | .and() 213 | .generatesSources(expected); 214 | } 215 | 216 | @Test 217 | public void allCursorTypes() { 218 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 219 | + "package test;\n" 220 | + "import com.google.auto.value.AutoValue;\n" 221 | + "import android.database.Cursor;\n" 222 | + "@AutoValue public abstract class Test {\n" 223 | + " public static Test blah(Cursor cursor) { return null; }\n" 224 | + " public abstract String a();\n" 225 | + " public abstract int b();\n" 226 | + " public abstract Integer c();\n" 227 | + " public abstract long d();\n" 228 | + " public abstract Long e();\n" 229 | + " public abstract short f();\n" 230 | + " public abstract Short g();\n" 231 | + " public abstract double h();\n" 232 | + " public abstract Double i();\n" 233 | + " public abstract float j();\n" 234 | + " public abstract Float k();\n" 235 | + " public abstract boolean l();\n" 236 | + " public abstract Boolean m();\n" 237 | + " public abstract byte[] n();\n" 238 | + "}\n"); 239 | 240 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 241 | + "package test;\n" 242 | + "import android.database.Cursor;\n" 243 | + "import java.lang.Boolean;\n" 244 | + "import java.lang.Double;\n" 245 | + "import java.lang.Float;\n" 246 | + "import java.lang.Integer;\n" 247 | + "import java.lang.Long;\n" 248 | + "import java.lang.Short;\n" 249 | + "import java.lang.String;\n" 250 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 251 | + " AutoValue_Test(String a, int b, Integer c, long d, Long e, short f, Short g, double h, Double i, float j, Float k, boolean l, Boolean m, byte[] n) {\n" 252 | + " super(a, b, c, d, e, f, g, h, i, j, k, l, m, n);\n" 253 | + " }\n" 254 | + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" 255 | + " String a = cursor.getString(cursor.getColumnIndexOrThrow(\"a\"));\n" 256 | + " int b = cursor.getInt(cursor.getColumnIndexOrThrow(\"b\"));\n" 257 | + " Integer c = cursor.getInt(cursor.getColumnIndexOrThrow(\"c\"));\n" 258 | + " long d = cursor.getLong(cursor.getColumnIndexOrThrow(\"d\"));\n" 259 | + " Long e = cursor.getLong(cursor.getColumnIndexOrThrow(\"e\"));\n" 260 | + " short f = cursor.getShort(cursor.getColumnIndexOrThrow(\"f\"));\n" 261 | + " Short g = cursor.getShort(cursor.getColumnIndexOrThrow(\"g\"));\n" 262 | + " double h = cursor.getDouble(cursor.getColumnIndexOrThrow(\"h\"));\n" 263 | + " Double i = cursor.getDouble(cursor.getColumnIndexOrThrow(\"i\"));\n" 264 | + " float j = cursor.getFloat(cursor.getColumnIndexOrThrow(\"j\"));\n" 265 | + " Float k = cursor.getFloat(cursor.getColumnIndexOrThrow(\"k\"));\n" 266 | + " boolean l = cursor.getInt(cursor.getColumnIndexOrThrow(\"l\")) == 1;\n" 267 | + " Boolean m = cursor.getInt(cursor.getColumnIndexOrThrow(\"m\")) == 1;\n" 268 | + " byte[] n = cursor.getBlob(cursor.getColumnIndexOrThrow(\"n\"));\n" 269 | + " return new AutoValue_Test(a, b, c, d, e, f, g, h, i, j, k, l, m, n);\n" 270 | + " }\n" 271 | + "}\n"); 272 | 273 | assertAbout(javaSources()) 274 | .that(Collections.singletonList(source)) 275 | .processedWith(new AutoValueProcessor()) 276 | .compilesWithoutError() 277 | .and() 278 | .generatesSources(expected); 279 | } 280 | 281 | @Test 282 | public void cursorAdapter() { 283 | JavaFileObject fooClass = JavaFileObjects.forSourceString("test.Foo", "" 284 | + "package test;\n" 285 | + "import android.content.ContentValues;\n" 286 | + "import android.database.Cursor;\n" 287 | + "import com.gabrielittner.auto.value.cursor.ColumnTypeAdapter;\n" 288 | + "public class Foo {\n" 289 | + " public final String data;\n" 290 | + " public Foo(String data) {\n" 291 | + " this.data = data;\n" 292 | + " }\n" 293 | + " public static class Adapter implements ColumnTypeAdapter {\n" 294 | + " public Foo fromCursor(Cursor cursor, String columnName) {\n" 295 | + " return new Foo(cursor.getString(cursor.getColumnIndex(columnName)));\n" 296 | + " }\n" 297 | + " public void toContentValues(ContentValues values, String columnName, Foo value) {\n" 298 | + " }\n" 299 | + " }\n" 300 | + "}\n"); 301 | JavaFileObject stringFactorySource = JavaFileObjects.forSourceString("test.Adapter", "" 302 | + "package test;\n" 303 | + "import android.content.ContentValues;\n" 304 | + "import android.database.Cursor;\n" 305 | + "import com.gabrielittner.auto.value.cursor.ColumnTypeAdapter;\n" 306 | + "public class Adapter implements ColumnTypeAdapter {\n" 307 | + " public String fromCursor(Cursor cursor, String columnName) {\n" 308 | + " return cursor.getString(cursor.getColumnIndex(columnName));\n" 309 | + " }\n" 310 | + " public void toContentValues(ContentValues values, String columnName, String value) {\n" 311 | + " }\n" 312 | + "}\n"); 313 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 314 | + "package test;\n" 315 | + "import com.gabrielittner.auto.value.cursor.ColumnName;\n" 316 | + "import com.gabrielittner.auto.value.cursor.ColumnAdapter;\n" 317 | + "import com.google.auto.value.AutoValue;\n" 318 | + "import javax.annotation.Nullable;\n" 319 | + "import android.database.Cursor;\n" 320 | + "@AutoValue public abstract class Test {\n" 321 | + " public static Test blah(Cursor cursor) { return null; }\n" 322 | + " @ColumnAdapter(Foo.Adapter.class) public abstract Foo foo();\n" 323 | + " @ColumnAdapter(Adapter.class) public abstract String bar();\n" 324 | + " @ColumnAdapter(Adapter.class) @ColumnName(\"column\") public abstract String columnName();\n" 325 | + "}\n"); 326 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 327 | + "package test;\n" 328 | + "import android.database.Cursor;\n" 329 | + "import java.lang.String;\n" 330 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 331 | + " AutoValue_Test(Foo foo, String bar, String columnName) {\n" 332 | + " super(foo, bar, columnName);\n" 333 | + " }\n" 334 | + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" 335 | + " Foo.Adapter adapter = new Foo.Adapter();\n" 336 | + " Adapter adapter_ = new Adapter();\n" 337 | + " Foo foo = adapter.fromCursor(cursor, \"foo\");\n" 338 | + " String bar = adapter_.fromCursor(cursor, \"bar\");\n" 339 | + " String columnName = adapter_.fromCursor(cursor, \"column\");\n" 340 | + " return new AutoValue_Test(foo, bar, columnName);\n" 341 | + " }\n" 342 | + "}\n"); 343 | 344 | assertAbout(javaSources()) 345 | .that(Arrays.asList(fooClass, stringFactorySource, source)) 346 | .processedWith(new AutoValueProcessor()) 347 | .compilesWithoutError() 348 | .and() 349 | .generatesSources(expected); 350 | } 351 | 352 | @Test 353 | // don't generate anything RxJava specific just because it's on the classpath 354 | public void rxjava2() { 355 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 356 | + "package test;\n" 357 | + "import android.database.Cursor;\n" 358 | + "import com.google.auto.value.AutoValue;\n" 359 | + "@AutoValue public abstract class Test {\n" 360 | + " public static Test blah(Cursor cursor) { return null; }\n" 361 | + " public abstract int a();\n" 362 | + " public abstract String b();\n" 363 | + "}\n"); 364 | 365 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 366 | + "package test;\n" 367 | + "import android.database.Cursor;\n" 368 | + "import java.lang.String;\n" 369 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 370 | + " AutoValue_Test(int a, String b) {\n" 371 | + " super(a, b);\n" 372 | + " }\n" 373 | + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" 374 | + " int a = cursor.getInt(cursor.getColumnIndexOrThrow(\"a\"));\n" 375 | + " String b = cursor.getString(cursor.getColumnIndexOrThrow(\"b\"));\n" 376 | + " return new AutoValue_Test(a, b);\n" 377 | + " }\n" 378 | + "}\n"); 379 | 380 | assertAbout(javaSources()) 381 | .that(Arrays.asList(function(), source)) 382 | .processedWith(new AutoValueProcessor()) 383 | .compilesWithoutError() 384 | .and() 385 | .generatesSources(expected); 386 | } 387 | 388 | @Test 389 | public void rxjava2OptIn() { 390 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 391 | + "package test;\n" 392 | + "import android.database.Cursor;\n" 393 | + "import com.google.auto.value.AutoValue;\n" 394 | + "import io.reactivex.functions.Function;\n" 395 | + "@AutoValue public abstract class Test {\n" 396 | + " public static Function MAPPER = AutoValue_Test.MAPPER_FUNCTION;\n" 397 | + " public abstract int a();\n" 398 | + " public abstract String b();\n" 399 | + "}\n"); 400 | 401 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 402 | + "package test;\n" 403 | + "import android.database.Cursor;\n" 404 | + "import io.reactivex.functions.Function;\n" 405 | + "import java.lang.Override;\n" 406 | + "import java.lang.String;\n" 407 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 408 | + " static final Function MAPPER_FUNCTION = new Function() {\n" 409 | + " @Override\n" 410 | + " public AutoValue_Test apply(Cursor c) {\n" 411 | + " return createFromCursor(c);\n" 412 | + " }\n" 413 | + " };\n" 414 | + " AutoValue_Test(int a, String b) {\n" 415 | + " super(a, b);\n" 416 | + " }\n" 417 | + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" 418 | + " int a = cursor.getInt(cursor.getColumnIndexOrThrow(\"a\"));\n" 419 | + " String b = cursor.getString(cursor.getColumnIndexOrThrow(\"b\"));\n" 420 | + " return new AutoValue_Test(a, b);\n" 421 | + " }\n" 422 | + "}\n"); 423 | 424 | assertAbout(javaSources()) 425 | .that(Arrays.asList(function(), source)) 426 | .processedWith(new AutoValueProcessor()) 427 | .compilesWithoutError() 428 | .and() 429 | .generatesSources(expected); 430 | } 431 | 432 | private JavaFileObject function() { 433 | return JavaFileObjects.forSourceString( 434 | "io.reactivex.functions.Function", "" 435 | + "package io.reactivex.functions;\n" 436 | + "public interface Function {\n" 437 | + " R apply(T t);\n" 438 | + "}\n"); 439 | } 440 | 441 | @Test 442 | // don't generate anything RxJava specific just because it's on the classpath 443 | public void rxjava() { 444 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 445 | + "package test;\n" 446 | + "import com.google.auto.value.AutoValue;\n" 447 | + "import android.database.Cursor;\n" 448 | + "@AutoValue public abstract class Test {\n" 449 | + " public static Test blah(Cursor cursor) { return null; }\n" 450 | + " public abstract int a();\n" 451 | + " public abstract String b();\n" 452 | + "}\n"); 453 | 454 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 455 | + "package test;\n" 456 | + "import android.database.Cursor;\n" 457 | + "import java.lang.String;\n" 458 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 459 | + " AutoValue_Test(int a, String b) {\n" 460 | + " super(a, b);\n" 461 | + " }\n" 462 | + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" 463 | + " int a = cursor.getInt(cursor.getColumnIndexOrThrow(\"a\"));\n" 464 | + " String b = cursor.getString(cursor.getColumnIndexOrThrow(\"b\"));\n" 465 | + " return new AutoValue_Test(a, b);\n" 466 | + " }\n" 467 | + "}\n"); 468 | 469 | assertAbout(javaSources()) 470 | .that(Arrays.asList(func1(), source)) 471 | .processedWith(new AutoValueProcessor()) 472 | .compilesWithoutError() 473 | .and() 474 | .generatesSources(expected); 475 | } 476 | 477 | @Test 478 | public void rxjavaOptIn() { 479 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 480 | + "package test;\n" 481 | + "import com.google.auto.value.AutoValue;\n" 482 | + "import android.database.Cursor;\n" 483 | + "import rx.functions.Func1;\n" 484 | + "@AutoValue public abstract class Test {\n" 485 | + " public static Func1 MAPPER = AutoValue_Test.MAPPER;\n" 486 | + " public abstract int a();\n" 487 | + " public abstract String b();\n" 488 | + "}\n"); 489 | 490 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 491 | + "package test;\n" 492 | + "import android.database.Cursor;\n" 493 | + "import java.lang.Override;\n" 494 | + "import java.lang.String;\n" 495 | + "import rx.functions.Func1;\n" 496 | + "final class AutoValue_Test extends $AutoValue_Test {\n" 497 | + " static final Func1 MAPPER = new Func1() {\n" 498 | + " @Override\n" 499 | + " public AutoValue_Test call(Cursor c) {\n" 500 | + " return createFromCursor(c);\n" 501 | + " }\n" 502 | + " };\n" 503 | + " AutoValue_Test(int a, String b) {\n" 504 | + " super(a, b);\n" 505 | + " }\n" 506 | + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" 507 | + " int a = cursor.getInt(cursor.getColumnIndexOrThrow(\"a\"));\n" 508 | + " String b = cursor.getString(cursor.getColumnIndexOrThrow(\"b\"));\n" 509 | + " return new AutoValue_Test(a, b);\n" 510 | + " }\n" 511 | + "}\n"); 512 | 513 | assertAbout(javaSources()) 514 | .that(Arrays.asList(func1(), source)) 515 | .processedWith(new AutoValueProcessor()) 516 | .compilesWithoutError() 517 | .and() 518 | .generatesSources(expected); 519 | } 520 | 521 | private JavaFileObject func1() { 522 | return JavaFileObjects.forSourceString( 523 | "rx.functions.Func1", "" 524 | + "package rx.functions;\n" 525 | + "public interface Func1 {\n" 526 | + " R call(T t);\n" 527 | + "}\n"); 528 | } 529 | 530 | @Test 531 | public void generatesNothingWithoutOptIn() { 532 | JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" 533 | + "package test;\n" 534 | + "import com.google.auto.value.AutoValue;\n" 535 | + "import android.database.Cursor;\n" 536 | + "@AutoValue public abstract class Test {\n" 537 | + " public abstract int a();\n" 538 | + " public abstract String b();\n" 539 | + "}\n"); 540 | 541 | JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" 542 | + "package test;\n" 543 | + "import javax.annotation.Generated;\n" 544 | + "@Generated(\"com.google.auto.value.processor.AutoValueProcessor\")\n" 545 | + "final class AutoValue_Test extends Test {\n" 546 | + " private final int a;\n" 547 | + " private final String b;\n" 548 | + " AutoValue_Test(\n" 549 | + " int a,\n" 550 | + " String b) {\n" 551 | + " this.a = a;\n" 552 | + " if (b == null) {\n" 553 | + " throw new NullPointerException(\"Null b\");\n" 554 | + " }\n" 555 | + " this.b = b;\n" 556 | + " }\n" 557 | + " @Override\n" 558 | + " public int a() {\n" 559 | + " return a;\n" 560 | + " }\n" 561 | + " @Override\n" 562 | + " public String b() {\n" 563 | + " return b;\n" 564 | + " }\n" 565 | + " @Override\n" 566 | + " public String toString() {\n" 567 | + " return \"Test{\"\n" 568 | + " + \"a=\" + a + \", \"\n" 569 | + " + \"b=\" + b\n" 570 | + " + \"}\";\n" 571 | + " }\n" 572 | + " @Override\n" 573 | + " public boolean equals(Object o) {\n" 574 | + " if (o == this) {\n" 575 | + " return true;\n" 576 | + " }\n" 577 | + " if (o instanceof Test) {\n" 578 | + " Test that = (Test) o;\n" 579 | + " return this.a == that.a()\n" 580 | + " && this.b.equals(that.b());\n" 581 | + " }\n" 582 | + " return false;\n" 583 | + " }\n" 584 | + " @Override\n" 585 | + " public int hashCode() {\n" 586 | + " int h$ = 1;\n" 587 | + " h$ *= 1000003;\n" 588 | + " h$ ^= a;\n" 589 | + " h$ *= 1000003;\n" 590 | + " h$ ^= b.hashCode();\n" 591 | + " return h$;\n" 592 | + " }\n" 593 | + "}\n"); 594 | 595 | assertAbout(javaSources()) 596 | .that(Collections.singleton(source)) 597 | .processedWith(new AutoValueProcessor()) 598 | .compilesWithoutError() 599 | .and() 600 | .generatesSources(expected); 601 | } 602 | } 603 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | jcenter() 5 | } 6 | 7 | dependencies { 8 | classpath 'com.vanniktech:gradle-maven-publish-plugin:0.12.0' 9 | } 10 | } 11 | 12 | allprojects { 13 | repositories { 14 | mavenCentral() 15 | } 16 | } 17 | 18 | ext { 19 | javaVersion = JavaVersion.VERSION_1_7 20 | autoServiceVersion = "1.0-rc7" 21 | } 22 | 23 | ext.deps = [ 24 | javapoet: 'com.squareup:javapoet:1.13.0', 25 | auto_value: 'com.google.auto.value:auto-value:1.7.4', 26 | auto_common: 'com.google.auto:auto-common:0.10', 27 | guava: 'com.google.guava:guava:29.0-jre', 28 | auto_ext_util: 'com.gabrielittner.auto.value:auto-value-extension-util:0.4.0', 29 | 30 | auto_service_annotations: "com.google.auto.service:auto-service-annotations:$autoServiceVersion", 31 | auto_service: "com.google.auto.service:auto-service:$autoServiceVersion", 32 | android: 'com.google.android:android:2.1.2', 33 | 34 | junit: 'junit:junit:4.13', 35 | truth: 'com.google.truth:truth:1.0.1', 36 | compile_testing: 'com.google.testing.compile:compile-testing:0.18', 37 | jsr305: 'com.google.code.findbugs:jsr305:3.0.2', 38 | ] 39 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | GROUP=com.gabrielittner.auto.value 2 | VERSION_NAME=2.1.0-SNAPSHOT 3 | 4 | POM_DESCRIPTION=AutoValue extension to create an AutoValue object from a Cursor 5 | 6 | POM_URL=https://github.com/gabrielittner/auto-value-cursor/ 7 | POM_SCM_URL=https://github.com/gabrielittner/auto-value-cursor/ 8 | POM_SCM_CONNECTION=scm:git:git://github.com/gabrielittner/auto-value-cursor.git 9 | POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/gabrielittner/auto-value-cursor.git 10 | 11 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 12 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 13 | POM_LICENCE_DIST=repo 14 | 15 | POM_DEVELOPER_ID=gabrielittner 16 | POM_DEVELOPER_NAME=Gabriel Ittner 17 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gabrielittner/auto-value-cursor/0bc372680cd49fe9c9b5d8065557482725cf3f66/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | # Determine the Java command to use to start the JVM. 86 | if [ -n "$JAVA_HOME" ] ; then 87 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 88 | # IBM's JDK on AIX uses strange locations for the executables 89 | JAVACMD="$JAVA_HOME/jre/sh/java" 90 | else 91 | JAVACMD="$JAVA_HOME/bin/java" 92 | fi 93 | if [ ! -x "$JAVACMD" ] ; then 94 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 95 | 96 | Please set the JAVA_HOME variable in your environment to match the 97 | location of your Java installation." 98 | fi 99 | else 100 | JAVACMD="java" 101 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 102 | 103 | Please set the JAVA_HOME variable in your environment to match the 104 | location of your Java installation." 105 | fi 106 | 107 | # Increase the maximum file descriptors if we can. 108 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 109 | MAX_FD_LIMIT=`ulimit -H -n` 110 | if [ $? -eq 0 ] ; then 111 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 112 | MAX_FD="$MAX_FD_LIMIT" 113 | fi 114 | ulimit -n $MAX_FD 115 | if [ $? -ne 0 ] ; then 116 | warn "Could not set maximum file descriptor limit: $MAX_FD" 117 | fi 118 | else 119 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 120 | fi 121 | fi 122 | 123 | # For Darwin, add options to specify how the application appears in the dock 124 | if $darwin; then 125 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 126 | fi 127 | 128 | # For Cygwin, switch paths to Windows format before running java 129 | if $cygwin ; then 130 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 131 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 132 | JAVACMD=`cygpath --unix "$JAVACMD"` 133 | 134 | # We build the pattern for arguments to be converted via cygpath 135 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 136 | SEP="" 137 | for dir in $ROOTDIRSRAW ; do 138 | ROOTDIRS="$ROOTDIRS$SEP$dir" 139 | SEP="|" 140 | done 141 | OURCYGPATTERN="(^($ROOTDIRS))" 142 | # Add a user-defined pattern to the cygpath arguments 143 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 144 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 145 | fi 146 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 147 | i=0 148 | for arg in "$@" ; do 149 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 150 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 151 | 152 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 153 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 154 | else 155 | eval `echo args$i`="\"$arg\"" 156 | fi 157 | i=$((i+1)) 158 | done 159 | case $i in 160 | (0) set -- ;; 161 | (1) set -- "$args0" ;; 162 | (2) set -- "$args0" "$args1" ;; 163 | (3) set -- "$args0" "$args1" "$args2" ;; 164 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 165 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 166 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 167 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 168 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 169 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 170 | esac 171 | fi 172 | 173 | # Escape application args 174 | save () { 175 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 176 | echo " " 177 | } 178 | APP_ARGS=$(save "$@") 179 | 180 | # Collect all arguments for the java command, following the shell quoting and substitution rules 181 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 182 | 183 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 184 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 185 | cd "$(dirname "$0")" 186 | fi 187 | 188 | exec "$JAVACMD" "$@" 189 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'auto-value-cursor-root' 2 | 3 | include ':auto-value-cursor', ':auto-value-cursor-annotations' 4 | --------------------------------------------------------------------------------