├── .gitignore ├── CHANGELOG.md ├── LICENSE.txt ├── META-INF └── plugin.xml ├── ParcelableTestApp ├── .gitignore ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── example │ │ │ └── sampleapp │ │ │ └── app │ │ │ ├── ApplicationTest.java │ │ │ └── PrimitivesParcelableTest.java │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── sampleapp │ │ │ └── app │ │ │ ├── BoxedPrimitivesParcelable.java │ │ │ ├── BundleParcelable.java │ │ │ ├── DateParcelable.java │ │ │ ├── EnumParcelable.java │ │ │ ├── GenericListParcelable.java │ │ │ ├── MapParcelable.java │ │ │ ├── NestedParcelable.java │ │ │ ├── PrimitiveArrayParcelable.java │ │ │ ├── PrimitivesParcelable.java │ │ │ ├── SerializableParcelable.java │ │ │ └── SparseParcelable.java │ │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle ├── README.md ├── android-parcelable-intellij-plugin.iml ├── screenshot.png └── src └── pl └── charmas └── parcelablegenerator ├── CodeGenerator.java ├── GenerateDialog.java ├── ParcelableAction.java ├── typeserializers ├── BundleSerializerFactory.java ├── ChainSerializerFactory.java ├── DateSerializerFactory.java ├── EnumerationSerializerFactory.java ├── ListSerializerFactory.java ├── MapSerializerFactory.java ├── ParcelableSerializerFactory.java ├── PrimitiveArraySerializerFactory.java ├── PrimitiveTypeArraySerializerFactory.java ├── PrimitiveTypeSerializerFactory.java ├── SerializableSerializerFactory.java ├── SerializableValue.java ├── SparseArraySerializerFactory.java ├── TypeSerializer.java ├── TypeSerializerFactory.java └── serializers │ ├── BooleanPrimitiveSerializer.java │ ├── BooleanSparseArraySerializer.java │ ├── BundleSerializer.java │ ├── CharPrimitiveSerializer.java │ ├── DateSerializer.java │ ├── EnumerationSerializer.java │ ├── GenericListSerializer.java │ ├── MapSerializer.java │ ├── NullablePrimitivesArraySerializer.java │ ├── NullablePrimitivesSerializer.java │ ├── ParcelableArraySerializer.java │ ├── ParcelableListSerializer.java │ ├── ParcelableObjectSerializer.java │ ├── PrimitiveArraySerializer.java │ ├── PrimitiveTypeSerializer.java │ ├── SerializableObjectSerializer.java │ ├── ShortPrimitiveSerializer.java │ └── SparseArraySerializer.java └── util └── PsiUtils.java /.gitignore: -------------------------------------------------------------------------------- 1 | out/ 2 | .idea/ 3 | *.iml 4 | .DS_Store 5 | build/ 6 | *.zip 7 | 8 | # Build output 9 | android-parcelable-intellij-plugin.jar 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ##Version 0.7.0 4 | 5 | * Added map parcelation 6 | * Parcelation is not generated for fields with transient modifier 7 | * Date parcelation fixes 8 | * Enum parcelation fixes 9 | 10 | ##Version 0.6.3 11 | 12 | * Now generating parcelable also for base cass fields 13 | * Fixed primitive type arrays serialisation 14 | * Added @Override annotation on Creator methods 15 | * Added short support 16 | * Minor bugfixes 17 | 18 | ##Version 0.6.2 19 | 20 | * Fixed list serialization 21 | * Added support for parcelable array serialization 22 | * Added support for string list serization 23 | * Added inheritance support 24 | 25 | ##Version 0.6 26 | 27 | * CREATOR is now final - proguard will not touch it now 28 | * fixed parcelable class loader 29 | 30 | ##Version 0.2 31 | 32 | * added nullable primitives support 33 | * added primitive array support 34 | * added boolean sparse array support 35 | * added list list serialization 36 | 37 | ##Version 0.1 38 | 39 | * working primitive type serialization 40 | * initial release - proof of concept 41 | -------------------------------------------------------------------------------- /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. 203 | -------------------------------------------------------------------------------- /META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 18 | 19 | pl.charmas.parcelablegenerator 20 | Android Parcelable code generator 21 | 0.7.0 22 | Michal Charmas 23 | 24 | 27 | 28 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /ParcelableTestApp/.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | .idea 3 | /local.properties 4 | .DS_Store 5 | /build 6 | /captures 7 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | dependencies { 6 | classpath 'com.android.tools.build:gradle:1.2.3' 7 | } 8 | } 9 | apply plugin: 'com.android.application' 10 | 11 | repositories { 12 | jcenter() 13 | } 14 | 15 | android { 16 | compileSdkVersion 23 17 | buildToolsVersion "23.0.2" 18 | 19 | defaultConfig { 20 | applicationId "com.example.sampleapp.app" 21 | minSdkVersion 9 22 | targetSdkVersion 23 23 | versionCode 1 24 | versionName "1.0" 25 | } 26 | 27 | compileOptions { 28 | sourceCompatibility JavaVersion.VERSION_1_6 29 | targetCompatibility JavaVersion.VERSION_1_6 30 | } 31 | buildTypes { 32 | release { 33 | minifyEnabled false 34 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 35 | } 36 | } 37 | } 38 | 39 | dependencies { 40 | compile fileTree(dir: 'libs', include: ['*.jar']) 41 | compile 'com.android.support:appcompat-v7:23.1.1' 42 | } 43 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/mcharmas/Development/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/androidTest/java/com/example/sampleapp/app/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/androidTest/java/com/example/sampleapp/app/PrimitivesParcelableTest.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | import android.test.AndroidTestCase; 6 | 7 | public class PrimitivesParcelableTest extends AndroidTestCase { 8 | 9 | public void testParcelsPrimitives() throws Exception { 10 | assertParcels(PrimitivesParcelable.create(), PrimitivesParcelable.CREATOR); 11 | } 12 | 13 | public void testParcelsPrimitiveArrays() throws Exception { 14 | assertParcels(PrimitiveArrayParcelable.create(), PrimitiveArrayParcelable.CREATOR); 15 | } 16 | 17 | public void testParcelsBoxedPrimitives() throws Exception { 18 | assertParcels(BoxedPrimitivesParcelable.create(), BoxedPrimitivesParcelable.CREATOR); 19 | } 20 | 21 | public void testParcelsBundleParcelable() throws Exception { 22 | assertParcels(BundleParcelable.create(), BundleParcelable.CREATOR); 23 | } 24 | 25 | public void testParcelsDateParcelable() throws Exception { 26 | assertParcels(DateParcelable.create(), DateParcelable.CREATOR); 27 | } 28 | 29 | public void testParcelsEnumParcelable() throws Exception { 30 | assertParcels(EnumParcelable.create(), EnumParcelable.CREATOR); 31 | } 32 | 33 | public void testParcelsSerializableParcelable() throws Exception { 34 | assertParcels(SerializableParcelable.create(), SerializableParcelable.CREATOR); 35 | } 36 | 37 | public void testParcelsNestedParcelables() throws Exception { 38 | NestedParcelable toParcel = NestedParcelable.create(); 39 | NestedParcelable readParcelable = parcelAndRead(toParcel, NestedParcelable.CREATOR); 40 | assertEquals(toParcel.getBitmap().getWidth(), readParcelable.getBitmap().getWidth()); 41 | assertEquals(toParcel.getBitmapList().size(), readParcelable.getBitmapList().size()); 42 | } 43 | 44 | public void testParcelsMapParcelables() throws Exception { 45 | MapParcelable toParcel = MapParcelable.create(); 46 | MapParcelable readParcelalbe = parcelAndRead(toParcel, MapParcelable.CREATOR); 47 | assertEquals(toParcel.getSampleMap().size(), readParcelalbe.getSampleMap().size()); 48 | assertEquals(toParcel.getMapWithParcelableValues().size(), readParcelalbe.getMapWithParcelableValues().size()); 49 | } 50 | 51 | public void testParcelsGenericListParcelables() throws Exception { 52 | assertParcels(GenericListParcelable.create(), GenericListParcelable.CREATOR); 53 | } 54 | 55 | public void testParcelsSparseParcelable() throws Exception { 56 | SparseParcelable sparseParcelable = SparseParcelable.create(); 57 | SparseParcelable readParcelable = parcelAndRead(sparseParcelable, SparseParcelable.CREATOR); 58 | assertEquals(sparseParcelable.getSampleSparseArray().size(), readParcelable.getSampleSparseArray().size()); 59 | assertEquals(sparseParcelable.getSparseBooleanArray().size(), readParcelable.getSparseBooleanArray().size()); 60 | } 61 | 62 | private void assertParcels(T parcelable, Parcelable.Creator creator) { 63 | Parcelable fromParcel = parcelAndRead(parcelable, creator); 64 | assertEquals(parcelable, fromParcel); 65 | } 66 | 67 | private T parcelAndRead(T parcelable, Parcelable.Creator creator) { 68 | Parcel parcel = Parcel.obtain(); 69 | parcelable.writeToParcel(parcel, 0); 70 | parcel.setDataPosition(0); 71 | return creator.createFromParcel(parcel); 72 | } 73 | } -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/java/com/example/sampleapp/app/BoxedPrimitivesParcelable.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | 6 | public class BoxedPrimitivesParcelable implements Parcelable { 7 | private final Integer a; 8 | private final Double b; 9 | private final String c; 10 | private final Short d; 11 | private final Float e; 12 | private final Boolean f; 13 | private final Byte g; 14 | 15 | private BoxedPrimitivesParcelable(Integer a, Double b, String c, Short d, Float e, Boolean f, Byte g) { 16 | this.a = a; 17 | this.b = b; 18 | this.c = c; 19 | this.d = d; 20 | this.e = e; 21 | this.f = f; 22 | this.g = g; 23 | } 24 | 25 | public static BoxedPrimitivesParcelable create() { 26 | return new BoxedPrimitivesParcelable(0, 1.0, "2", (short) 3, 6f, true, (byte) 1); 27 | } 28 | 29 | @Override 30 | public boolean equals(Object o) { 31 | if (this == o) return true; 32 | if (o == null || getClass() != o.getClass()) return false; 33 | 34 | BoxedPrimitivesParcelable that = (BoxedPrimitivesParcelable) o; 35 | 36 | if (a != null ? !a.equals(that.a) : that.a != null) return false; 37 | if (b != null ? !b.equals(that.b) : that.b != null) return false; 38 | if (c != null ? !c.equals(that.c) : that.c != null) return false; 39 | if (d != null ? !d.equals(that.d) : that.d != null) return false; 40 | if (e != null ? !e.equals(that.e) : that.e != null) return false; 41 | if (f != null ? !f.equals(that.f) : that.f != null) return false; 42 | return g != null ? g.equals(that.g) : that.g == null; 43 | 44 | } 45 | 46 | @Override 47 | public int hashCode() { 48 | int result = a != null ? a.hashCode() : 0; 49 | result = 31 * result + (b != null ? b.hashCode() : 0); 50 | result = 31 * result + (c != null ? c.hashCode() : 0); 51 | result = 31 * result + (d != null ? d.hashCode() : 0); 52 | result = 31 * result + (e != null ? e.hashCode() : 0); 53 | result = 31 * result + (f != null ? f.hashCode() : 0); 54 | result = 31 * result + (g != null ? g.hashCode() : 0); 55 | return result; 56 | } 57 | 58 | @Override 59 | public int describeContents() { 60 | return 0; 61 | } 62 | 63 | @Override 64 | public void writeToParcel(Parcel dest, int flags) { 65 | dest.writeValue(this.a); 66 | dest.writeValue(this.b); 67 | dest.writeString(this.c); 68 | dest.writeValue(this.d); 69 | dest.writeValue(this.e); 70 | dest.writeValue(this.f); 71 | dest.writeValue(this.g); 72 | } 73 | 74 | protected BoxedPrimitivesParcelable(Parcel in) { 75 | this.a = (Integer) in.readValue(Integer.class.getClassLoader()); 76 | this.b = (Double) in.readValue(Double.class.getClassLoader()); 77 | this.c = in.readString(); 78 | this.d = (Short) in.readValue(Short.class.getClassLoader()); 79 | this.e = (Float) in.readValue(Float.class.getClassLoader()); 80 | this.f = (Boolean) in.readValue(Boolean.class.getClassLoader()); 81 | this.g = (Byte) in.readValue(Byte.class.getClassLoader()); 82 | } 83 | 84 | public static final Creator CREATOR = new Creator() { 85 | @Override 86 | public BoxedPrimitivesParcelable createFromParcel(Parcel source) { 87 | return new BoxedPrimitivesParcelable(source); 88 | } 89 | 90 | @Override 91 | public BoxedPrimitivesParcelable[] newArray(int size) { 92 | return new BoxedPrimitivesParcelable[size]; 93 | } 94 | }; 95 | } 96 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/java/com/example/sampleapp/app/BundleParcelable.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.os.Bundle; 4 | import android.os.Parcel; 5 | import android.os.Parcelable; 6 | 7 | public class BundleParcelable implements Parcelable { 8 | private final Bundle bundle; 9 | 10 | private BundleParcelable(Bundle bundle) { 11 | this.bundle = bundle; 12 | } 13 | 14 | public static BundleParcelable create() { 15 | Bundle bundle = new Bundle(); 16 | bundle.putString("sample", "string"); 17 | return new BundleParcelable(bundle); 18 | } 19 | 20 | @Override 21 | public boolean equals(Object o) { 22 | if (this == o) return true; 23 | if (o == null || getClass() != o.getClass()) return false; 24 | 25 | BundleParcelable that = (BundleParcelable) o; 26 | 27 | return bundle != null && bundle.keySet().equals(((BundleParcelable) o).bundle.keySet()); 28 | 29 | } 30 | 31 | @Override 32 | public int hashCode() { 33 | return bundle != null ? bundle.hashCode() : 0; 34 | } 35 | 36 | @Override 37 | public int describeContents() { 38 | return 0; 39 | } 40 | 41 | @Override 42 | public void writeToParcel(Parcel dest, int flags) { 43 | dest.writeBundle(this.bundle); 44 | } 45 | 46 | protected BundleParcelable(Parcel in) { 47 | this.bundle = in.readBundle(); 48 | } 49 | 50 | public static final Creator CREATOR = new Creator() { 51 | @Override 52 | public BundleParcelable createFromParcel(Parcel source) { 53 | return new BundleParcelable(source); 54 | } 55 | 56 | @Override 57 | public BundleParcelable[] newArray(int size) { 58 | return new BundleParcelable[size]; 59 | } 60 | }; 61 | } 62 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/java/com/example/sampleapp/app/DateParcelable.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | 6 | import java.util.Date; 7 | 8 | public class DateParcelable implements Parcelable { 9 | private final Date date; 10 | 11 | private DateParcelable(Date date) { 12 | this.date = date; 13 | } 14 | 15 | public static DateParcelable create() { 16 | return new DateParcelable(new Date()); 17 | } 18 | 19 | @Override 20 | public boolean equals(Object o) { 21 | if (this == o) return true; 22 | if (o == null || getClass() != o.getClass()) return false; 23 | 24 | DateParcelable that = (DateParcelable) o; 25 | 26 | return date != null ? date.equals(that.date) : that.date == null; 27 | 28 | } 29 | 30 | @Override 31 | public int hashCode() { 32 | return date != null ? date.hashCode() : 0; 33 | } 34 | 35 | 36 | @Override 37 | public int describeContents() { 38 | return 0; 39 | } 40 | 41 | @Override 42 | public void writeToParcel(Parcel dest, int flags) { 43 | dest.writeLong(this.date != null ? this.date.getTime() : -1); 44 | } 45 | 46 | protected DateParcelable(Parcel in) { 47 | long tmpDate = in.readLong(); 48 | this.date = tmpDate == -1 ? null : new Date(tmpDate); 49 | } 50 | 51 | public static final Creator CREATOR = new Creator() { 52 | @Override 53 | public DateParcelable createFromParcel(Parcel source) { 54 | return new DateParcelable(source); 55 | } 56 | 57 | @Override 58 | public DateParcelable[] newArray(int size) { 59 | return new DateParcelable[size]; 60 | } 61 | }; 62 | } 63 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/java/com/example/sampleapp/app/EnumParcelable.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | 6 | public class EnumParcelable implements Parcelable { 7 | private final SampleEnum valueX; 8 | private final SampleEnum valueY; 9 | private final SampleEnum nullValue; 10 | 11 | public EnumParcelable(SampleEnum valueX, SampleEnum valueY, SampleEnum nullValue) { 12 | this.valueX = valueX; 13 | this.valueY = valueY; 14 | this.nullValue = nullValue; 15 | } 16 | 17 | @Override 18 | public boolean equals(Object o) { 19 | if (this == o) return true; 20 | if (o == null || getClass() != o.getClass()) return false; 21 | 22 | EnumParcelable that = (EnumParcelable) o; 23 | 24 | if (valueX != that.valueX) return false; 25 | if (valueY != that.valueY) return false; 26 | return nullValue == that.nullValue; 27 | 28 | } 29 | 30 | @Override 31 | public int hashCode() { 32 | int result = valueX != null ? valueX.hashCode() : 0; 33 | result = 31 * result + (valueY != null ? valueY.hashCode() : 0); 34 | result = 31 * result + (nullValue != null ? nullValue.hashCode() : 0); 35 | return result; 36 | } 37 | 38 | public static EnumParcelable create() { 39 | return new EnumParcelable(SampleEnum.ABC, SampleEnum.DEF, null); 40 | } 41 | 42 | public enum SampleEnum { 43 | ABC, 44 | DEF 45 | } 46 | 47 | @Override 48 | public int describeContents() { 49 | return 0; 50 | } 51 | 52 | @Override 53 | public void writeToParcel(Parcel dest, int flags) { 54 | dest.writeInt(this.valueX == null ? -1 : this.valueX.ordinal()); 55 | dest.writeInt(this.valueY == null ? -1 : this.valueY.ordinal()); 56 | dest.writeInt(this.nullValue == null ? -1 : this.nullValue.ordinal()); 57 | } 58 | 59 | protected EnumParcelable(Parcel in) { 60 | int tmpValueX = in.readInt(); 61 | this.valueX = tmpValueX == -1 ? null : SampleEnum.values()[tmpValueX]; 62 | int tmpValueY = in.readInt(); 63 | this.valueY = tmpValueY == -1 ? null : SampleEnum.values()[tmpValueY]; 64 | int tmpNullValue = in.readInt(); 65 | this.nullValue = tmpNullValue == -1 ? null : SampleEnum.values()[tmpNullValue]; 66 | } 67 | 68 | public static final Creator CREATOR = new Creator() { 69 | @Override 70 | public EnumParcelable createFromParcel(Parcel source) { 71 | return new EnumParcelable(source); 72 | } 73 | 74 | @Override 75 | public EnumParcelable[] newArray(int size) { 76 | return new EnumParcelable[size]; 77 | } 78 | }; 79 | } 80 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/java/com/example/sampleapp/app/GenericListParcelable.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | public class GenericListParcelable implements Parcelable { 11 | private final List stringList; 12 | private final List intList; 13 | 14 | private GenericListParcelable(List stringList, List intList) { 15 | this.stringList = stringList; 16 | this.intList = intList; 17 | } 18 | 19 | public static GenericListParcelable create() { 20 | return new GenericListParcelable( 21 | Arrays.asList("a", "b", "c"), 22 | Arrays.asList(1,2,3,4) 23 | ); 24 | } 25 | 26 | @Override 27 | public boolean equals(Object o) { 28 | if (this == o) return true; 29 | if (o == null || getClass() != o.getClass()) return false; 30 | 31 | GenericListParcelable that = (GenericListParcelable) o; 32 | 33 | if (stringList != null ? !stringList.equals(that.stringList) : that.stringList != null) return false; 34 | return intList != null ? intList.equals(that.intList) : that.intList == null; 35 | 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | int result = stringList != null ? stringList.hashCode() : 0; 41 | result = 31 * result + (intList != null ? intList.hashCode() : 0); 42 | return result; 43 | } 44 | 45 | 46 | @Override 47 | public int describeContents() { 48 | return 0; 49 | } 50 | 51 | @Override 52 | public void writeToParcel(Parcel dest, int flags) { 53 | dest.writeStringList(this.stringList); 54 | dest.writeList(this.intList); 55 | } 56 | 57 | protected GenericListParcelable(Parcel in) { 58 | this.stringList = in.createStringArrayList(); 59 | this.intList = new ArrayList(); 60 | in.readList(this.intList, Integer.class.getClassLoader()); 61 | } 62 | 63 | public static final Creator CREATOR = new Creator() { 64 | @Override 65 | public GenericListParcelable createFromParcel(Parcel source) { 66 | return new GenericListParcelable(source); 67 | } 68 | 69 | @Override 70 | public GenericListParcelable[] newArray(int size) { 71 | return new GenericListParcelable[size]; 72 | } 73 | }; 74 | } 75 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/java/com/example/sampleapp/app/MapParcelable.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.graphics.Bitmap; 4 | import android.os.Parcel; 5 | import android.os.Parcelable; 6 | 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | public class MapParcelable implements Parcelable { 11 | private final Map sampleMap; 12 | private final Map mapWithParcelableValues; 13 | 14 | private MapParcelable(Map sampleMap, Map mapWithParcelableValues) { 15 | this.sampleMap = sampleMap; 16 | this.mapWithParcelableValues = mapWithParcelableValues; 17 | } 18 | 19 | public static MapParcelable create() { 20 | HashMap stringMap = new HashMap(); 21 | stringMap.put("a", "b"); 22 | 23 | HashMap bitmapMap = new HashMap(); 24 | bitmapMap.put(1L, Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888)); 25 | 26 | return new MapParcelable(stringMap, bitmapMap); 27 | } 28 | 29 | public Map getSampleMap() { 30 | return sampleMap; 31 | } 32 | 33 | public Map getMapWithParcelableValues() { 34 | return mapWithParcelableValues; 35 | } 36 | 37 | 38 | @Override 39 | public int describeContents() { 40 | return 0; 41 | } 42 | 43 | @Override 44 | public void writeToParcel(Parcel dest, int flags) { 45 | dest.writeInt(this.sampleMap.size()); 46 | for (Map.Entry entry : this.sampleMap.entrySet()) { 47 | dest.writeString(entry.getKey()); 48 | dest.writeString(entry.getValue()); 49 | } 50 | dest.writeInt(this.mapWithParcelableValues.size()); 51 | for (Map.Entry entry : this.mapWithParcelableValues.entrySet()) { 52 | dest.writeValue(entry.getKey()); 53 | dest.writeParcelable(entry.getValue(), flags); 54 | } 55 | } 56 | 57 | protected MapParcelable(Parcel in) { 58 | int sampleMapSize = in.readInt(); 59 | this.sampleMap = new HashMap(sampleMapSize); 60 | for (int i = 0; i < sampleMapSize; i++) { 61 | String key = in.readString(); 62 | String value = in.readString(); 63 | this.sampleMap.put(key, value); 64 | } 65 | int mapWithParcelableValuesSize = in.readInt(); 66 | this.mapWithParcelableValues = new HashMap(mapWithParcelableValuesSize); 67 | for (int i = 0; i < mapWithParcelableValuesSize; i++) { 68 | Long key = (Long) in.readValue(Long.class.getClassLoader()); 69 | Bitmap value = in.readParcelable(Bitmap.class.getClassLoader()); 70 | this.mapWithParcelableValues.put(key, value); 71 | } 72 | } 73 | 74 | public static final Creator CREATOR = new Creator() { 75 | @Override 76 | public MapParcelable createFromParcel(Parcel source) { 77 | return new MapParcelable(source); 78 | } 79 | 80 | @Override 81 | public MapParcelable[] newArray(int size) { 82 | return new MapParcelable[size]; 83 | } 84 | }; 85 | } 86 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/java/com/example/sampleapp/app/NestedParcelable.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.graphics.Bitmap; 4 | import android.os.Parcel; 5 | import android.os.Parcelable; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | public class NestedParcelable implements Parcelable { 11 | private final Bitmap bitmap; 12 | private final List bitmapList; 13 | 14 | private NestedParcelable(Bitmap bitmap, List bitmapList) { 15 | this.bitmap = bitmap; 16 | this.bitmapList = bitmapList; 17 | } 18 | 19 | public static NestedParcelable create() { 20 | return new NestedParcelable( 21 | Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888), 22 | Arrays.asList( 23 | Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888), 24 | Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888) 25 | ) 26 | ); 27 | } 28 | 29 | public Bitmap getBitmap() { 30 | return bitmap; 31 | } 32 | 33 | public List getBitmapList() { 34 | return bitmapList; 35 | } 36 | 37 | @Override 38 | public int describeContents() { 39 | return 0; 40 | } 41 | 42 | @Override 43 | public void writeToParcel(Parcel dest, int flags) { 44 | dest.writeParcelable(this.bitmap, flags); 45 | dest.writeTypedList(this.bitmapList); 46 | } 47 | 48 | protected NestedParcelable(Parcel in) { 49 | this.bitmap = in.readParcelable(Bitmap.class.getClassLoader()); 50 | this.bitmapList = in.createTypedArrayList(Bitmap.CREATOR); 51 | } 52 | 53 | public static final Creator CREATOR = new Creator() { 54 | @Override 55 | public NestedParcelable createFromParcel(Parcel source) { 56 | return new NestedParcelable(source); 57 | } 58 | 59 | @Override 60 | public NestedParcelable[] newArray(int size) { 61 | return new NestedParcelable[size]; 62 | } 63 | }; 64 | } 65 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/java/com/example/sampleapp/app/PrimitiveArrayParcelable.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | 6 | import java.util.Arrays; 7 | 8 | public class PrimitiveArrayParcelable implements Parcelable { 9 | private final int[] a; 10 | private final double[] b; 11 | private final String[] c; 12 | private final float[] e; 13 | private final boolean[] f; 14 | private final byte[] g; 15 | 16 | private PrimitiveArrayParcelable(int[] a, double[] b, String[] c, float[] e, boolean[] f, byte[] g) { 17 | this.a = a; 18 | this.b = b; 19 | this.c = c; 20 | this.e = e; 21 | this.f = f; 22 | this.g = g; 23 | } 24 | 25 | public static PrimitiveArrayParcelable create() { 26 | return new PrimitiveArrayParcelable( 27 | new int[]{1, 2, 3, 4, 5}, 28 | new double[]{1.0, 2.0}, 29 | new String[]{"a", "b"}, 30 | new float[]{1f, 2f}, 31 | new boolean[]{true, false, true}, 32 | new byte[]{(byte) 1, (byte) 2} 33 | ); 34 | } 35 | 36 | @Override 37 | public boolean equals(Object o) { 38 | if (this == o) return true; 39 | if (o == null || getClass() != o.getClass()) return false; 40 | 41 | PrimitiveArrayParcelable that = (PrimitiveArrayParcelable) o; 42 | 43 | if (!Arrays.equals(a, that.a)) return false; 44 | if (!Arrays.equals(b, that.b)) return false; 45 | // Probably incorrect - comparing Object[] arrays with Arrays.equals 46 | if (!Arrays.equals(c, that.c)) return false; 47 | if (!Arrays.equals(e, that.e)) return false; 48 | if (!Arrays.equals(f, that.f)) return false; 49 | return Arrays.equals(g, that.g); 50 | 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | int result = Arrays.hashCode(a); 56 | result = 31 * result + Arrays.hashCode(b); 57 | result = 31 * result + Arrays.hashCode(c); 58 | result = 31 * result + Arrays.hashCode(e); 59 | result = 31 * result + Arrays.hashCode(f); 60 | result = 31 * result + Arrays.hashCode(g); 61 | return result; 62 | } 63 | 64 | @Override 65 | public int describeContents() { 66 | return 0; 67 | } 68 | 69 | @Override 70 | public void writeToParcel(Parcel dest, int flags) { 71 | dest.writeIntArray(this.a); 72 | dest.writeDoubleArray(this.b); 73 | dest.writeStringArray(this.c); 74 | dest.writeFloatArray(this.e); 75 | dest.writeBooleanArray(this.f); 76 | dest.writeByteArray(this.g); 77 | } 78 | 79 | protected PrimitiveArrayParcelable(Parcel in) { 80 | this.a = in.createIntArray(); 81 | this.b = in.createDoubleArray(); 82 | this.c = in.createStringArray(); 83 | this.e = in.createFloatArray(); 84 | this.f = in.createBooleanArray(); 85 | this.g = in.createByteArray(); 86 | } 87 | 88 | public static final Creator CREATOR = new Creator() { 89 | @Override 90 | public PrimitiveArrayParcelable createFromParcel(Parcel source) { 91 | return new PrimitiveArrayParcelable(source); 92 | } 93 | 94 | @Override 95 | public PrimitiveArrayParcelable[] newArray(int size) { 96 | return new PrimitiveArrayParcelable[size]; 97 | } 98 | }; 99 | } 100 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/java/com/example/sampleapp/app/PrimitivesParcelable.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | 6 | public class PrimitivesParcelable implements Parcelable { 7 | private final int a; 8 | private final double b; 9 | private final String c; 10 | private final short d; 11 | private final float e; 12 | private final boolean f; 13 | private final byte g; 14 | 15 | private PrimitivesParcelable(int a, double b, String c, short d, float e, boolean f, byte g) { 16 | this.a = a; 17 | this.b = b; 18 | this.c = c; 19 | this.d = d; 20 | this.e = e; 21 | this.f = f; 22 | this.g = g; 23 | } 24 | 25 | public static PrimitivesParcelable create() { 26 | return new PrimitivesParcelable(0, 1.0, "2", (short) 3, 6f, true, (byte) 1); 27 | } 28 | 29 | @Override 30 | public boolean equals(Object o) { 31 | if (this == o) return true; 32 | if (o == null || getClass() != o.getClass()) return false; 33 | 34 | PrimitivesParcelable that = (PrimitivesParcelable) o; 35 | 36 | if (a != that.a) return false; 37 | if (Double.compare(that.b, b) != 0) return false; 38 | if (d != that.d) return false; 39 | if (Float.compare(that.e, e) != 0) return false; 40 | if (f != that.f) return false; 41 | if (g != that.g) return false; 42 | return c != null ? c.equals(that.c) : that.c == null; 43 | 44 | } 45 | 46 | @Override 47 | public int hashCode() { 48 | int result; 49 | long temp; 50 | result = a; 51 | temp = Double.doubleToLongBits(b); 52 | result = 31 * result + (int) (temp ^ (temp >>> 32)); 53 | result = 31 * result + (c != null ? c.hashCode() : 0); 54 | result = 31 * result + (int) d; 55 | result = 31 * result + (e != +0.0f ? Float.floatToIntBits(e) : 0); 56 | result = 31 * result + (f ? 1 : 0); 57 | result = 31 * result + (int) g; 58 | return result; 59 | } 60 | 61 | @Override 62 | public int describeContents() { 63 | return 0; 64 | } 65 | 66 | @Override 67 | public void writeToParcel(Parcel dest, int flags) { 68 | dest.writeInt(this.a); 69 | dest.writeDouble(this.b); 70 | dest.writeString(this.c); 71 | dest.writeInt(this.d); 72 | dest.writeFloat(this.e); 73 | dest.writeByte(this.f ? (byte) 1 : (byte) 0); 74 | dest.writeByte(this.g); 75 | } 76 | 77 | protected PrimitivesParcelable(Parcel in) { 78 | this.a = in.readInt(); 79 | this.b = in.readDouble(); 80 | this.c = in.readString(); 81 | this.d = (short) in.readInt(); 82 | this.e = in.readFloat(); 83 | this.f = in.readByte() != 0; 84 | this.g = in.readByte(); 85 | } 86 | 87 | public static final Creator CREATOR = new Creator() { 88 | @Override 89 | public PrimitivesParcelable createFromParcel(Parcel source) { 90 | return new PrimitivesParcelable(source); 91 | } 92 | 93 | @Override 94 | public PrimitivesParcelable[] newArray(int size) { 95 | return new PrimitivesParcelable[size]; 96 | } 97 | }; 98 | } 99 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/java/com/example/sampleapp/app/SerializableParcelable.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | 6 | import java.io.Serializable; 7 | 8 | public class SerializableParcelable implements Parcelable { 9 | private final SerializableData serializableData; 10 | private final SerializableData serializableDataNullable; 11 | 12 | private SerializableParcelable(SerializableData serializableData, SerializableData serializableDataNullable) { 13 | this.serializableData = serializableData; 14 | this.serializableDataNullable = serializableDataNullable; 15 | } 16 | 17 | public static SerializableParcelable create() { 18 | return new SerializableParcelable(new SerializableData(10), null); 19 | } 20 | 21 | @Override 22 | public boolean equals(Object o) { 23 | if (this == o) return true; 24 | if (o == null || getClass() != o.getClass()) return false; 25 | 26 | SerializableParcelable that = (SerializableParcelable) o; 27 | 28 | if (serializableData != null ? !serializableData.equals(that.serializableData) : that.serializableData != null) 29 | return false; 30 | return serializableDataNullable != null ? serializableDataNullable.equals(that.serializableDataNullable) : that.serializableDataNullable == null; 31 | 32 | } 33 | 34 | @Override 35 | public int hashCode() { 36 | int result = serializableData != null ? serializableData.hashCode() : 0; 37 | result = 31 * result + (serializableDataNullable != null ? serializableDataNullable.hashCode() : 0); 38 | return result; 39 | } 40 | 41 | public static class SerializableData implements Serializable { 42 | private final int data; 43 | 44 | public SerializableData(int data) { 45 | this.data = data; 46 | } 47 | 48 | @Override 49 | public boolean equals(Object o) { 50 | if (this == o) return true; 51 | if (o == null || getClass() != o.getClass()) return false; 52 | 53 | SerializableData that = (SerializableData) o; 54 | 55 | return data == that.data; 56 | 57 | } 58 | 59 | @Override 60 | public int hashCode() { 61 | return data; 62 | } 63 | } 64 | 65 | @Override 66 | public int describeContents() { 67 | return 0; 68 | } 69 | 70 | @Override 71 | public void writeToParcel(Parcel dest, int flags) { 72 | dest.writeSerializable(this.serializableData); 73 | dest.writeSerializable(this.serializableDataNullable); 74 | } 75 | 76 | protected SerializableParcelable(Parcel in) { 77 | this.serializableData = (SerializableData) in.readSerializable(); 78 | this.serializableDataNullable = (SerializableData) in.readSerializable(); 79 | } 80 | 81 | public static final Creator CREATOR = new Creator() { 82 | @Override 83 | public SerializableParcelable createFromParcel(Parcel source) { 84 | return new SerializableParcelable(source); 85 | } 86 | 87 | @Override 88 | public SerializableParcelable[] newArray(int size) { 89 | return new SerializableParcelable[size]; 90 | } 91 | }; 92 | } 93 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/java/com/example/sampleapp/app/SparseParcelable.java: -------------------------------------------------------------------------------- 1 | package com.example.sampleapp.app; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | import android.util.SparseArray; 6 | import android.util.SparseBooleanArray; 7 | 8 | public class SparseParcelable implements Parcelable { 9 | private final SparseArray sampleSparseArray; 10 | private final SparseBooleanArray sparseBooleanArray; 11 | 12 | private SparseParcelable(SparseArray sampleSparseArray, SparseBooleanArray sparseBooleanArray) { 13 | this.sampleSparseArray = sampleSparseArray; 14 | this.sparseBooleanArray = sparseBooleanArray; 15 | } 16 | 17 | public static SparseParcelable create() { 18 | SparseArray sampleSparseArray = new SparseArray(10); 19 | sampleSparseArray.put(10, "abcd"); 20 | 21 | SparseBooleanArray sparseBooleanArray = new SparseBooleanArray(); 22 | sparseBooleanArray.put(1, false); 23 | 24 | return new SparseParcelable( 25 | sampleSparseArray, 26 | sparseBooleanArray 27 | ); 28 | } 29 | 30 | public SparseArray getSampleSparseArray() { 31 | return sampleSparseArray; 32 | } 33 | 34 | public SparseBooleanArray getSparseBooleanArray() { 35 | return sparseBooleanArray; 36 | } 37 | 38 | @Override 39 | public int describeContents() { 40 | return 0; 41 | } 42 | 43 | @Override 44 | public void writeToParcel(Parcel dest, int flags) { 45 | dest.writeSparseArray((SparseArray) this.sampleSparseArray); 46 | dest.writeSparseBooleanArray(this.sparseBooleanArray); 47 | } 48 | 49 | protected SparseParcelable(Parcel in) { 50 | this.sampleSparseArray = in.readSparseArray(String.class.getClassLoader()); 51 | this.sparseBooleanArray = in.readSparseBooleanArray(); 52 | } 53 | 54 | public static final Creator CREATOR = new Creator() { 55 | @Override 56 | public SparseParcelable createFromParcel(Parcel source) { 57 | return new SparseParcelable(source); 58 | } 59 | 60 | @Override 61 | public SparseParcelable[] newArray(int size) { 62 | return new SparseParcelable[size]; 63 | } 64 | }; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 5 | 9 | 10 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcharmas/android-parcelable-intellij-plugin/018765c4b9af791fabfcad38a75f9b83e6c1045d/ParcelableTestApp/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcharmas/android-parcelable-intellij-plugin/018765c4b9af791fabfcad38a75f9b83e6c1045d/ParcelableTestApp/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcharmas/android-parcelable-intellij-plugin/018765c4b9af791fabfcad38a75f9b83e6c1045d/ParcelableTestApp/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcharmas/android-parcelable-intellij-plugin/018765c4b9af791fabfcad38a75f9b83e6c1045d/ParcelableTestApp/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SampleApp 3 | 4 | Hello world! 5 | Settings 6 | 7 | -------------------------------------------------------------------------------- /ParcelableTestApp/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ParcelableTestApp/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.2.3' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ParcelableTestApp/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /ParcelableTestApp/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcharmas/android-parcelable-intellij-plugin/018765c4b9af791fabfcad38a75f9b83e6c1045d/ParcelableTestApp/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /ParcelableTestApp/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip 7 | -------------------------------------------------------------------------------- /ParcelableTestApp/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /ParcelableTestApp/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /ParcelableTestApp/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IntelliJ/Android Studio Plugin for Android Parcelable boilerplate code generation 2 | 3 | This tool generates an Android [Parcelable](https://developer.android.com/reference/android/os/Parcelable.html) implementation based on fields in the class. 4 | 5 | ## Installation 6 | 7 | Plugin is uploaded to plugin repository. 8 | If you like, you can install it manually: 9 | 10 | 0. Download `ParcelableGenerator` [release](https://github.com/mcharmas/android-parcelable-intellij-plugin/releases/tag/v0.7.0) 11 | 0. Open IntelliJ/Android Studio 12 | 0. *Preferences* -> *Plugins* -> *Install plugin from disk...*. 13 | 0. Choose the downloaded jar file 14 | 15 | ## Usage 16 | 17 | Just press **ALT + Insert** (or your equivalent keybinding for code generation) in your editor and select **Parcelable**. It allows you to select the fields to be parceled. 18 | 19 | ![Screenshot](screenshot.png) 20 | 21 | ## Supported parcelable types 22 | 23 | * Types implementing Parcelable 24 | * Custom support (avoids `Serializable`/`Parcelable` implementation) for: `Date`, `Bundle` 25 | * Types implementing Serializable 26 | * List of `Parcelable` objects 27 | * Enumerations 28 | * Primitive types: `long`, `short`, `int`, `float`, `double`, `boolean`, `byte`, `String` 29 | * Primitive type wrappers (written with `Parcel.writeValue(Object)`): `Short`, `Integer`, `Long`, `Float`, `Double`, `Boolean`, `Byte` 30 | * Primitive type arrays: `boolean[]`, `byte[]`, `char[]`, `double[]`, `float[]`, `int[]`, `long[]` 31 | * List type of any object (**Warning: validation is not performed**) 32 | * Map support — **Note: this implementation always assumes `HashMap` is desired** 33 | * SparseArray support 34 | 35 | ## TODO 36 | 37 | * Validation of List arguments 38 | * Display warning about not serialized fields 39 | * Active Objects support (Binders and stuff) 40 | 41 | ## Contributors 42 | 43 | * [Michał Charmas](https://github.com/mcharmas/) 44 | * [Dallas Gutauckis](http://github.com/dallasgutauckis) 45 | 46 | ## License 47 | 48 | ``` 49 | Copyright (C) 2015 Michał Charmas (http://blog.charmas.pl) 50 | Copyright (C) 2015 Dallas Gutauckis (http://dallasgutauckis.com) 51 | 52 | Licensed under the Apache License, Version 2.0 (the "License"); 53 | you may not use this file except in compliance with the License. 54 | You may obtain a copy of the License at 55 | 56 | http://www.apache.org/licenses/LICENSE-2.0 57 | 58 | Unless required by applicable law or agreed to in writing, software 59 | distributed under the License is distributed on an "AS IS" BASIS, 60 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 61 | See the License for the specific language governing permissions and 62 | limitations under the License. 63 | ``` 64 | -------------------------------------------------------------------------------- /android-parcelable-intellij-plugin.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcharmas/android-parcelable-intellij-plugin/018765c4b9af791fabfcad38a75f9b83e6c1045d/screenshot.png -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/CodeGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator; 17 | 18 | import com.intellij.psi.*; 19 | import com.intellij.psi.codeStyle.JavaCodeStyleManager; 20 | import pl.charmas.parcelablegenerator.typeserializers.*; 21 | import pl.charmas.parcelablegenerator.util.PsiUtils; 22 | 23 | import java.util.List; 24 | 25 | 26 | /** 27 | * Quite a few changes here by Dallas Gutauckis [dallas@gutauckis.com] 28 | */ 29 | public class CodeGenerator { 30 | public static final String CREATOR_NAME = "CREATOR"; 31 | public static final String TYPE_PARCEL = "android.os.Parcel"; 32 | 33 | private final PsiClass mClass; 34 | private final List mFields; 35 | private final TypeSerializerFactory mTypeSerializerFactory; 36 | 37 | public CodeGenerator(PsiClass psiClass, List fields) { 38 | mClass = psiClass; 39 | mFields = fields; 40 | 41 | ChainSerializerFactory baseChain = new ChainSerializerFactory( 42 | new BundleSerializerFactory(), 43 | new DateSerializerFactory(), 44 | new EnumerationSerializerFactory(), 45 | new PrimitiveTypeSerializerFactory(), 46 | new PrimitiveArraySerializerFactory(), 47 | new PrimitiveTypeArraySerializerFactory(), 48 | new ParcelableSerializerFactory(), 49 | new ListSerializerFactory(), 50 | new SerializableSerializerFactory(), 51 | new SparseArraySerializerFactory() 52 | ); 53 | this.mTypeSerializerFactory = baseChain.extend(new MapSerializerFactory(baseChain)); 54 | } 55 | 56 | private String generateStaticCreator(PsiClass psiClass) { 57 | StringBuilder sb = new StringBuilder("public static final android.os.Parcelable.Creator<"); 58 | 59 | String className = psiClass.getName(); 60 | 61 | sb.append(className).append("> CREATOR = new android.os.Parcelable.Creator<").append(className).append(">(){") 62 | .append("@Override ") 63 | .append("public ").append(className).append(" createFromParcel(android.os.Parcel source) {") 64 | .append("return new ").append(className).append("(source);}") 65 | .append("@Override ") 66 | .append("public ").append(className).append("[] newArray(int size) {") 67 | .append("return new ").append(className).append("[size];}") 68 | .append("};"); 69 | return sb.toString(); 70 | } 71 | 72 | private String generateConstructor(List fields, PsiClass psiClass) { 73 | String className = psiClass.getName(); 74 | 75 | StringBuilder sb = new StringBuilder("protected "); 76 | 77 | // Create the Parcelable-required constructor 78 | sb.append(className).append("(android.os.Parcel in) {"); 79 | 80 | if (hasParcelableSuperclass() && hasParcelableSuperConstructor()) { 81 | sb.append("super(in);"); 82 | } 83 | 84 | // Creates all of the deserialization methods for the given fields 85 | for (PsiField field : fields) { 86 | sb.append(getSerializerForType(field).readValue(SerializableValue.member(field), "in")); 87 | } 88 | 89 | sb.append("}"); 90 | return sb.toString(); 91 | } 92 | 93 | private boolean hasParcelableSuperConstructor() { 94 | PsiMethod[] constructors = mClass.getSuperClass() != null ? mClass.getSuperClass().getConstructors() : new PsiMethod[0]; 95 | for (PsiMethod constructor : constructors) { 96 | PsiParameterList parameterList = constructor.getParameterList(); 97 | if (parameterList.getParametersCount() == 1 98 | && parameterList.getParameters()[0].getType().getCanonicalText().equals(TYPE_PARCEL)) { 99 | return true; 100 | } 101 | } 102 | return false; 103 | } 104 | 105 | private String generateWriteToParcel(List fields) { 106 | StringBuilder sb = new StringBuilder("@Override public void writeToParcel(android.os.Parcel dest, int flags) {"); 107 | if (hasParcelableSuperclass() && hasSuperMethod("writeToParcel")) { 108 | sb.append("super.writeToParcel(dest, flags);"); 109 | } 110 | for (PsiField field : fields) { 111 | sb.append(getSerializerForType(field).writeValue(SerializableValue.member(field), "dest", "flags")); 112 | } 113 | 114 | sb.append("}"); 115 | 116 | return sb.toString(); 117 | } 118 | 119 | private boolean hasSuperMethod(String methodName) { 120 | if (methodName == null) return false; 121 | 122 | PsiMethod[] superclassMethods = mClass.getSuperClass() != null ? mClass.getAllMethods() : new PsiMethod[0]; 123 | for (PsiMethod superclassMethod : superclassMethods) { 124 | if (superclassMethod.getBody() == null) continue; 125 | 126 | String name = superclassMethod.getName(); 127 | if (name != null && name.equals(methodName)) { 128 | return true; 129 | } 130 | } 131 | return false; 132 | } 133 | 134 | private TypeSerializer getSerializerForType(PsiField field) { 135 | return mTypeSerializerFactory.getSerializer(field.getType()); 136 | } 137 | 138 | private String generateDescribeContents() { 139 | return "@Override public int describeContents() { return 0; }"; 140 | } 141 | 142 | public void generate() { 143 | PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(mClass.getProject()); 144 | 145 | removeExistingParcelableImplementation(mClass); 146 | 147 | // Describe contents method 148 | PsiMethod describeContentsMethod = elementFactory.createMethodFromText(generateDescribeContents(), mClass); 149 | // Method for writing to the parcel 150 | PsiMethod writeToParcelMethod = elementFactory.createMethodFromText(generateWriteToParcel(mFields), mClass); 151 | 152 | // Default constructor if needed 153 | String defaultConstructorString = generateDefaultConstructor(mClass); 154 | PsiMethod defaultConstructor = null; 155 | 156 | if (defaultConstructorString != null) { 157 | defaultConstructor = elementFactory.createMethodFromText(defaultConstructorString, mClass); 158 | } 159 | 160 | // Constructor 161 | PsiMethod constructor = elementFactory.createMethodFromText(generateConstructor(mFields, mClass), mClass); 162 | // CREATOR 163 | PsiField creatorField = elementFactory.createFieldFromText(generateStaticCreator(mClass), mClass); 164 | 165 | JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(mClass.getProject()); 166 | 167 | // Shorten all class references 168 | styleManager.shortenClassReferences(mClass.addBefore(describeContentsMethod, mClass.getLastChild())); 169 | styleManager.shortenClassReferences(mClass.addBefore(writeToParcelMethod, mClass.getLastChild())); 170 | 171 | // Only adds if available 172 | if (defaultConstructor != null) { 173 | styleManager.shortenClassReferences(mClass.addBefore(defaultConstructor, mClass.getLastChild())); 174 | } 175 | 176 | styleManager.shortenClassReferences(mClass.addBefore(constructor, mClass.getLastChild())); 177 | styleManager.shortenClassReferences(mClass.addBefore(creatorField, mClass.getLastChild())); 178 | 179 | makeClassImplementParcelable(elementFactory); 180 | } 181 | 182 | private boolean hasParcelableSuperclass() { 183 | PsiClassType[] superTypes = mClass.getSuperTypes(); 184 | for (PsiClassType superType : superTypes) { 185 | if (PsiUtils.isOfType(superType, "android.os.Parcelable")) { 186 | return true; 187 | } 188 | } 189 | return false; 190 | } 191 | 192 | /** 193 | * Strips the 194 | * 195 | * @param psiClass 196 | */ 197 | private void removeExistingParcelableImplementation(PsiClass psiClass) { 198 | PsiField[] allFields = psiClass.getAllFields(); 199 | 200 | // Look for an existing CREATOR and remove it 201 | for (PsiField field : allFields) { 202 | if (field.getName().equals(CREATOR_NAME)) { 203 | // Creator already exists, need to remove/replace it 204 | field.delete(); 205 | } 206 | } 207 | 208 | findAndRemoveMethod(psiClass, psiClass.getName(), TYPE_PARCEL); 209 | findAndRemoveMethod(psiClass, "describeContents"); 210 | findAndRemoveMethod(psiClass, "writeToParcel", TYPE_PARCEL, "int"); 211 | } 212 | 213 | private String generateDefaultConstructor(PsiClass clazz) { 214 | // Check for any constructors; if none exist, we'll make a default one 215 | if (clazz.getConstructors().length == 0) { 216 | // No constructors exist, make a default one for convenience 217 | return "public " + clazz.getName() + "(){}" + '\n'; 218 | } else { 219 | return null; 220 | } 221 | } 222 | 223 | private void makeClassImplementParcelable(PsiElementFactory elementFactory) { 224 | if (hasParcelableSuperclass()) return; 225 | 226 | final PsiClassType[] implementsListTypes = mClass.getImplementsListTypes(); 227 | final String implementsType = "android.os.Parcelable"; 228 | 229 | for (PsiClassType implementsListType : implementsListTypes) { 230 | PsiClass resolved = implementsListType.resolve(); 231 | 232 | // Already implements Parcelable, no need to add it 233 | if (resolved != null && implementsType.equals(resolved.getQualifiedName())) { 234 | return; 235 | } 236 | } 237 | 238 | PsiJavaCodeReferenceElement implementsReference = elementFactory.createReferenceFromText(implementsType, mClass); 239 | PsiReferenceList implementsList = mClass.getImplementsList(); 240 | 241 | if (implementsList != null) { 242 | implementsList.add(implementsReference); 243 | } 244 | } 245 | 246 | 247 | private static void findAndRemoveMethod(PsiClass clazz, String methodName, String... arguments) { 248 | // Maybe there's an easier way to do this with mClass.findMethodBySignature(), but I'm not an expert on Psi* 249 | PsiMethod[] methods = clazz.findMethodsByName(methodName, false); 250 | 251 | for (PsiMethod method : methods) { 252 | PsiParameterList parameterList = method.getParameterList(); 253 | 254 | if (parameterList.getParametersCount() == arguments.length) { 255 | boolean shouldDelete = true; 256 | 257 | PsiParameter[] parameters = parameterList.getParameters(); 258 | 259 | for (int i = 0; i < arguments.length; i++) { 260 | if (!parameters[i].getType().getCanonicalText().equals(arguments[i])) { 261 | shouldDelete = false; 262 | } 263 | } 264 | 265 | if (shouldDelete) { 266 | method.delete(); 267 | } 268 | } 269 | } 270 | } 271 | } 272 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/GenerateDialog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator; 17 | 18 | import com.intellij.ide.util.DefaultPsiElementCellRenderer; 19 | import com.intellij.openapi.ui.DialogWrapper; 20 | import com.intellij.openapi.ui.LabeledComponent; 21 | import com.intellij.psi.PsiClass; 22 | import com.intellij.psi.PsiField; 23 | import com.intellij.psi.PsiModifier; 24 | import com.intellij.ui.CollectionListModel; 25 | import com.intellij.ui.ToolbarDecorator; 26 | import com.intellij.ui.components.JBCheckBox; 27 | import com.intellij.ui.components.JBList; 28 | import com.intellij.ui.components.panels.VerticalBox; 29 | 30 | import org.jetbrains.annotations.Nullable; 31 | 32 | import java.awt.event.ActionEvent; 33 | import java.awt.event.ActionListener; 34 | import java.util.ArrayList; 35 | import java.util.List; 36 | 37 | import javax.swing.JComponent; 38 | import javax.swing.JPanel; 39 | 40 | public class GenerateDialog extends DialogWrapper { 41 | 42 | private final CollectionListModel fieldsCollection; 43 | private final LabeledComponent fieldsComponent; 44 | private final JBCheckBox includeSubclasses; 45 | private final boolean showCheckbox; 46 | 47 | protected GenerateDialog(final PsiClass psiClass) { 48 | super(psiClass.getProject()); 49 | setTitle("Select Fields for Parcelable Generation"); 50 | 51 | fieldsCollection = new CollectionListModel(); 52 | final JBList fieldList = new JBList(fieldsCollection); 53 | fieldList.setCellRenderer(new DefaultPsiElementCellRenderer()); 54 | final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(fieldList).disableAddAction(); 55 | final JPanel panel = decorator.createPanel(); 56 | 57 | fieldsComponent = LabeledComponent.create(panel, "Fields to include in Parcelable"); 58 | 59 | includeSubclasses = new JBCheckBox("Include fields from base classes"); 60 | setupCheckboxClickAction(psiClass); 61 | showCheckbox = psiClass.getFields().length != psiClass.getAllFields().length; 62 | 63 | updateFieldsDisplay(psiClass); 64 | init(); 65 | } 66 | 67 | /** 68 | * Hookup action listener for {@link #includeSubclasses} checkbox. 69 | */ 70 | private void setupCheckboxClickAction(final PsiClass psiClass) { 71 | includeSubclasses.addActionListener(new ActionListener() { 72 | @Override 73 | public void actionPerformed(ActionEvent event) { 74 | updateFieldsDisplay(psiClass); 75 | } 76 | }); 77 | } 78 | 79 | /** 80 | * Update {@link #fieldsCollection} with class fields. 81 | */ 82 | private void updateFieldsDisplay(PsiClass psiClass) { 83 | final List fields; 84 | if (includeSubclasses.isSelected()) { 85 | fields = getClassFields(psiClass.getAllFields()); 86 | } else { 87 | fields = getClassFields(psiClass.getFields()); 88 | } 89 | fieldsCollection.removeAll(); 90 | fieldsCollection.add(fields); 91 | } 92 | 93 | /** 94 | * Exclude static fields. 95 | */ 96 | private List getClassFields(PsiField[] allFields) { 97 | final List fields = new ArrayList(); 98 | for (PsiField field : allFields) { 99 | if (!field.hasModifierProperty(PsiModifier.STATIC) && !field.hasModifierProperty(PsiModifier.TRANSIENT)) { 100 | fields.add(field); 101 | } 102 | } 103 | return fields; 104 | } 105 | 106 | @Nullable 107 | @Override 108 | protected JComponent createCenterPanel() { 109 | return fieldsComponent; 110 | } 111 | 112 | @Nullable 113 | @Override 114 | protected JComponent createSouthPanel() { 115 | JComponent southPanel = super.createSouthPanel(); 116 | if(showCheckbox && southPanel != null) { 117 | final VerticalBox combinedView = new VerticalBox(); 118 | combinedView.add(includeSubclasses); 119 | combinedView.add(southPanel); 120 | return combinedView; 121 | } else { 122 | return southPanel; 123 | } 124 | } 125 | 126 | public List getSelectedFields() { 127 | return fieldsCollection.getItems(); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/ParcelableAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator; 17 | 18 | import com.intellij.openapi.actionSystem.AnAction; 19 | import com.intellij.openapi.actionSystem.AnActionEvent; 20 | import com.intellij.openapi.actionSystem.LangDataKeys; 21 | import com.intellij.openapi.actionSystem.PlatformDataKeys; 22 | import com.intellij.openapi.command.WriteCommandAction; 23 | import com.intellij.openapi.editor.Editor; 24 | import com.intellij.psi.PsiClass; 25 | import com.intellij.psi.PsiElement; 26 | import com.intellij.psi.PsiField; 27 | import com.intellij.psi.PsiFile; 28 | import com.intellij.psi.util.PsiTreeUtil; 29 | 30 | import java.util.List; 31 | 32 | public class ParcelableAction extends AnAction { 33 | 34 | @Override 35 | public void actionPerformed(AnActionEvent e) { 36 | PsiClass psiClass = getPsiClassFromContext(e); 37 | 38 | GenerateDialog dlg = new GenerateDialog(psiClass); 39 | dlg.show(); 40 | 41 | if (dlg.isOK()) { 42 | generateParcelable(psiClass, dlg.getSelectedFields()); 43 | } 44 | } 45 | 46 | private void generateParcelable(final PsiClass psiClass, final List fields) { 47 | new WriteCommandAction.Simple(psiClass.getProject(), psiClass.getContainingFile()) { 48 | @Override 49 | protected void run() throws Throwable { 50 | new CodeGenerator(psiClass, fields).generate(); 51 | } 52 | }.execute(); 53 | } 54 | 55 | 56 | @Override 57 | public void update(AnActionEvent e) { 58 | PsiClass psiClass = getPsiClassFromContext(e); 59 | e.getPresentation().setEnabled(psiClass != null && !psiClass.isEnum() && !psiClass.isInterface()); 60 | } 61 | 62 | private PsiClass getPsiClassFromContext(AnActionEvent e) { 63 | PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE); 64 | Editor editor = e.getData(PlatformDataKeys.EDITOR); 65 | 66 | if (psiFile == null || editor == null) { 67 | return null; 68 | } 69 | 70 | int offset = editor.getCaretModel().getOffset(); 71 | PsiElement element = psiFile.findElementAt(offset); 72 | 73 | return PsiTreeUtil.getParentOfType(element, PsiClass.class); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/BundleSerializerFactory.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers; 2 | 3 | import com.intellij.psi.PsiType; 4 | 5 | import pl.charmas.parcelablegenerator.typeserializers.serializers.BundleSerializer; 6 | 7 | /** 8 | * Custom serializer factory for Date objects 9 | * 10 | * @author Dallas Gutauckis [dallas@gutauckis.com] 11 | */ 12 | public class BundleSerializerFactory implements TypeSerializerFactory { 13 | private final BundleSerializer mSerializer; 14 | 15 | public BundleSerializerFactory() { 16 | mSerializer = new BundleSerializer(); 17 | } 18 | 19 | @Override 20 | public TypeSerializer getSerializer(PsiType psiType) { 21 | if ("android.os.Bundle".equals(psiType.getCanonicalText())) { 22 | return mSerializer; 23 | } 24 | 25 | return null; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/ChainSerializerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers; 17 | 18 | import com.intellij.psi.PsiType; 19 | import pl.charmas.parcelablegenerator.typeserializers.serializers.ParcelableObjectSerializer; 20 | 21 | import java.util.ArrayList; 22 | import java.util.Arrays; 23 | import java.util.Collections; 24 | import java.util.List; 25 | 26 | public class ChainSerializerFactory implements TypeSerializerFactory { 27 | 28 | private final List factories; 29 | 30 | public ChainSerializerFactory(TypeSerializerFactory... factories) { 31 | this.factories = Arrays.asList(factories); 32 | } 33 | 34 | private ChainSerializerFactory(List factories) { 35 | this.factories = Collections.unmodifiableList(factories); 36 | } 37 | 38 | @Override 39 | public TypeSerializer getSerializer(PsiType psiType) { 40 | for (TypeSerializerFactory factory : factories) { 41 | TypeSerializer serializer = factory.getSerializer(psiType); 42 | if (serializer != null) { 43 | return serializer; 44 | } 45 | } 46 | return new ParcelableObjectSerializer(); 47 | } 48 | 49 | public TypeSerializerFactory extend(MapSerializerFactory factoryToAdd) { 50 | List factories = new ArrayList(this.factories); 51 | factories.add(factoryToAdd); 52 | return new ChainSerializerFactory(factories); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/DateSerializerFactory.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers; 2 | 3 | import com.intellij.psi.PsiType; 4 | 5 | import pl.charmas.parcelablegenerator.typeserializers.serializers.DateSerializer; 6 | 7 | /** 8 | * Custom serializer factory for Date objects 9 | * 10 | * @author Dallas Gutauckis [dallas@gutauckis.com] 11 | */ 12 | public class DateSerializerFactory implements TypeSerializerFactory { 13 | private final DateSerializer mSerializer; 14 | 15 | public DateSerializerFactory() { 16 | mSerializer = new DateSerializer(); 17 | } 18 | 19 | @Override 20 | public TypeSerializer getSerializer(PsiType psiType) { 21 | if ("java.util.Date".equals(psiType.getCanonicalText())) { 22 | return mSerializer; 23 | } 24 | 25 | return null; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/EnumerationSerializerFactory.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers; 2 | 3 | import com.intellij.psi.PsiType; 4 | import com.intellij.psi.impl.source.PsiClassReferenceType; 5 | 6 | import pl.charmas.parcelablegenerator.typeserializers.serializers.EnumerationSerializer; 7 | 8 | /** 9 | * Modified by Dallas Gutauckis [dallas@gutauckis.com] 10 | */ 11 | public class EnumerationSerializerFactory implements TypeSerializerFactory { 12 | private TypeSerializer mSerializer = new EnumerationSerializer(); 13 | 14 | @Override 15 | public TypeSerializer getSerializer(PsiType psiType) { 16 | if (psiType instanceof PsiClassReferenceType && ((PsiClassReferenceType) psiType).resolve().isEnum()) { 17 | return mSerializer; 18 | } 19 | 20 | return null; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/ListSerializerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers; 17 | 18 | import com.intellij.psi.PsiType; 19 | 20 | import pl.charmas.parcelablegenerator.typeserializers.serializers.GenericListSerializer; 21 | import pl.charmas.parcelablegenerator.util.PsiUtils; 22 | 23 | public class ListSerializerFactory implements TypeSerializerFactory { 24 | private TypeSerializer mSerializer = new GenericListSerializer(); 25 | 26 | @Override 27 | public TypeSerializer getSerializer(PsiType psiType) { 28 | if (PsiUtils.isOfType(psiType, "java.util.List")) { 29 | return mSerializer; 30 | } 31 | 32 | return null; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/MapSerializerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Dallas Gutauckis (http://dallasgutauckis.com) 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 pl.charmas.parcelablegenerator.typeserializers; 17 | 18 | import com.intellij.psi.PsiType; 19 | import pl.charmas.parcelablegenerator.typeserializers.serializers.MapSerializer; 20 | import pl.charmas.parcelablegenerator.util.PsiUtils; 21 | 22 | public class MapSerializerFactory implements TypeSerializerFactory { 23 | private final TypeSerializer serializer; 24 | 25 | public MapSerializerFactory(TypeSerializerFactory typeSerializers) { 26 | this.serializer = new MapSerializer(typeSerializers); 27 | } 28 | 29 | @Override 30 | public TypeSerializer getSerializer(PsiType psiType) { 31 | if (PsiUtils.isOfType(psiType, "java.util.Map")) { 32 | return serializer; 33 | } 34 | 35 | return null; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/ParcelableSerializerFactory.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers; 2 | 3 | import com.intellij.psi.PsiType; 4 | 5 | import java.util.List; 6 | 7 | import pl.charmas.parcelablegenerator.typeserializers.serializers.ParcelableArraySerializer; 8 | import pl.charmas.parcelablegenerator.typeserializers.serializers.ParcelableListSerializer; 9 | import pl.charmas.parcelablegenerator.typeserializers.serializers.ParcelableObjectSerializer; 10 | import pl.charmas.parcelablegenerator.util.PsiUtils; 11 | 12 | /** 13 | * Serializer factory for Parcelable objects 14 | * 15 | * @author Dallas Gutauckis [dallas@gutauckis.com] 16 | * @author Michał Charmas [micha@charmas.pl] 17 | */ 18 | public class ParcelableSerializerFactory implements TypeSerializerFactory { 19 | private TypeSerializer mSerializer = new ParcelableObjectSerializer(); 20 | private TypeSerializer listSerializer = new ParcelableListSerializer(); 21 | private TypeSerializer arraySerializer = new ParcelableArraySerializer(); 22 | 23 | @Override 24 | public TypeSerializer getSerializer(PsiType psiType) { 25 | if (PsiUtils.isOfType(psiType, "android.os.Parcelable[]")) { 26 | return arraySerializer; 27 | } 28 | 29 | if (PsiUtils.isOfType(psiType, "android.os.Parcelable")) { 30 | return mSerializer; 31 | } 32 | 33 | if (PsiUtils.isOfType(psiType, "java.util.List")) { 34 | List resolvedGenerics = PsiUtils.getResolvedGenerics(psiType); 35 | for (PsiType resolvedGeneric : resolvedGenerics) { 36 | if (PsiUtils.isOfType(resolvedGeneric, "android.os.Parcelable")) { 37 | return listSerializer; 38 | } 39 | } 40 | } 41 | 42 | return null; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/PrimitiveArraySerializerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers; 17 | 18 | import com.intellij.psi.PsiType; 19 | 20 | import java.util.HashMap; 21 | 22 | import pl.charmas.parcelablegenerator.typeserializers.serializers.BooleanSparseArraySerializer; 23 | import pl.charmas.parcelablegenerator.typeserializers.serializers.PrimitiveArraySerializer; 24 | 25 | public class PrimitiveArraySerializerFactory implements TypeSerializerFactory { 26 | private final HashMap handledTypes; 27 | 28 | public PrimitiveArraySerializerFactory() { 29 | handledTypes = new HashMap(); 30 | handledTypes.put("boolean[]", new PrimitiveArraySerializer("Boolean")); 31 | handledTypes.put("byte[]", new PrimitiveArraySerializer("Byte")); 32 | handledTypes.put("char[]", new PrimitiveArraySerializer("Char")); 33 | handledTypes.put("double[]", new PrimitiveArraySerializer("Double")); 34 | handledTypes.put("float[]", new PrimitiveArraySerializer("Float")); 35 | handledTypes.put("int[]", new PrimitiveArraySerializer("Int")); 36 | handledTypes.put("long[]", new PrimitiveArraySerializer("Long")); 37 | handledTypes.put("java.lang.String[]", new PrimitiveArraySerializer("String")); 38 | handledTypes.put("android.util.SparseBooleanArray", new BooleanSparseArraySerializer()); 39 | } 40 | 41 | @Override 42 | public TypeSerializer getSerializer(PsiType psiType) { 43 | return handledTypes.get(psiType.getCanonicalText()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/PrimitiveTypeArraySerializerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers; 17 | 18 | import com.intellij.psi.PsiType; 19 | 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | import pl.charmas.parcelablegenerator.typeserializers.serializers.NullablePrimitivesArraySerializer; 24 | 25 | public class PrimitiveTypeArraySerializerFactory implements TypeSerializerFactory { 26 | 27 | private final Map writeMethodsForTypes = new HashMap(); 28 | 29 | public PrimitiveTypeArraySerializerFactory() { 30 | initPrimitives(); 31 | } 32 | 33 | private void initPrimitives() { 34 | writeMethodsForTypes.put("java.lang.Byte[]", new NullablePrimitivesArraySerializer("java.lang.Byte[]")); 35 | writeMethodsForTypes.put("java.lang.Double[]", new NullablePrimitivesArraySerializer("java.lang.Double[]")); 36 | writeMethodsForTypes.put("java.lang.Float[]", new NullablePrimitivesArraySerializer("java.lang.Float[]")); 37 | writeMethodsForTypes.put("java.lang.Integer[]", new NullablePrimitivesArraySerializer("java.lang.Integer[]")); 38 | writeMethodsForTypes.put("java.lang.Long[]", new NullablePrimitivesArraySerializer("java.lang.Long[]")); 39 | writeMethodsForTypes.put("java.lang.Boolean[]", new NullablePrimitivesArraySerializer("java.lang.Boolean[]")); 40 | writeMethodsForTypes.put("java.lang.Char[]", new NullablePrimitivesArraySerializer("java.lang.Char[]")); 41 | } 42 | 43 | @Override 44 | public TypeSerializer getSerializer(PsiType psiType) { 45 | return writeMethodsForTypes.get(psiType.getCanonicalText()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/PrimitiveTypeSerializerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers; 17 | 18 | import com.intellij.psi.PsiType; 19 | 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | import pl.charmas.parcelablegenerator.typeserializers.serializers.BooleanPrimitiveSerializer; 24 | import pl.charmas.parcelablegenerator.typeserializers.serializers.CharPrimitiveSerializer; 25 | import pl.charmas.parcelablegenerator.typeserializers.serializers.NullablePrimitivesSerializer; 26 | import pl.charmas.parcelablegenerator.typeserializers.serializers.PrimitiveTypeSerializer; 27 | import pl.charmas.parcelablegenerator.typeserializers.serializers.ShortPrimitiveSerializer; 28 | 29 | public class PrimitiveTypeSerializerFactory implements TypeSerializerFactory { 30 | 31 | private final Map writeMethodsForTypes = new HashMap(); 32 | 33 | public PrimitiveTypeSerializerFactory() { 34 | initPrimitives(); 35 | initNullablePrimitives(); 36 | } 37 | 38 | private void initNullablePrimitives() { 39 | writeMethodsForTypes.put("java.lang.Byte", new NullablePrimitivesSerializer("java.lang.Byte")); 40 | writeMethodsForTypes.put("java.lang.Double", new NullablePrimitivesSerializer("java.lang.Double")); 41 | writeMethodsForTypes.put("java.lang.Float", new NullablePrimitivesSerializer("java.lang.Float")); 42 | writeMethodsForTypes.put("java.lang.Short", new NullablePrimitivesSerializer("java.lang.Short")); 43 | writeMethodsForTypes.put("java.lang.Integer", new NullablePrimitivesSerializer("java.lang.Integer")); 44 | writeMethodsForTypes.put("java.lang.Long", new NullablePrimitivesSerializer("java.lang.Long")); 45 | writeMethodsForTypes.put("java.lang.Boolean", new NullablePrimitivesSerializer("java.lang.Boolean")); 46 | writeMethodsForTypes.put("java.lang.Char", new NullablePrimitivesSerializer("java.lang.Char")); 47 | } 48 | 49 | private void initPrimitives() { 50 | writeMethodsForTypes.put("byte", new PrimitiveTypeSerializer("Byte")); 51 | writeMethodsForTypes.put("double", new PrimitiveTypeSerializer("Double")); 52 | writeMethodsForTypes.put("float", new PrimitiveTypeSerializer("Float")); 53 | writeMethodsForTypes.put("short", new ShortPrimitiveSerializer()); 54 | writeMethodsForTypes.put("int", new PrimitiveTypeSerializer("Int")); 55 | writeMethodsForTypes.put("long", new PrimitiveTypeSerializer("Long")); 56 | writeMethodsForTypes.put("java.lang.String", new PrimitiveTypeSerializer("String")); 57 | writeMethodsForTypes.put("boolean", new BooleanPrimitiveSerializer()); 58 | writeMethodsForTypes.put("char", new CharPrimitiveSerializer()); 59 | } 60 | 61 | @Override 62 | public TypeSerializer getSerializer(PsiType psiType) { 63 | return writeMethodsForTypes.get(psiType.getCanonicalText()); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/SerializableSerializerFactory.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers; 2 | 3 | import com.intellij.psi.PsiType; 4 | 5 | import pl.charmas.parcelablegenerator.typeserializers.serializers.SerializableObjectSerializer; 6 | import pl.charmas.parcelablegenerator.util.PsiUtils; 7 | 8 | /** 9 | * Modified by Dallas Gutauckis [dallas@gutauckis.com] 10 | */ 11 | public class SerializableSerializerFactory implements TypeSerializerFactory { 12 | private TypeSerializer mSerializer; 13 | 14 | public SerializableSerializerFactory() { 15 | mSerializer = new SerializableObjectSerializer(); 16 | } 17 | 18 | @Override 19 | public TypeSerializer getSerializer(PsiType psiType) { 20 | if (PsiUtils.isOfType(psiType, "java.io.Serializable")) { 21 | return mSerializer; 22 | } 23 | 24 | return null; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/SerializableValue.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers; 2 | 3 | import com.intellij.psi.PsiField; 4 | import com.intellij.psi.PsiType; 5 | 6 | public abstract class SerializableValue { 7 | public abstract PsiType getType(); 8 | 9 | public abstract String getName(); 10 | 11 | public static SerializableValue member(PsiField field) { 12 | return new MemberSerializableValue(field); 13 | } 14 | 15 | public static SerializableValue statement(String statement, PsiType type) { 16 | return new StatementSerializableValue(statement, type); 17 | } 18 | 19 | public static SerializableValue variable(String name, PsiType type) { 20 | return new VariableSerializableValue(name, type); 21 | } 22 | 23 | public abstract String getSimpleName(); 24 | 25 | private static class MemberSerializableValue extends SerializableValue { 26 | private final PsiField field; 27 | 28 | public MemberSerializableValue(PsiField field) { 29 | this.field = field; 30 | } 31 | 32 | @Override 33 | public PsiType getType() { 34 | return field.getType(); 35 | } 36 | 37 | @Override 38 | public String getName() { 39 | return "this." + field.getName(); 40 | } 41 | 42 | @Override 43 | public String getSimpleName() { 44 | return field.getName(); 45 | } 46 | } 47 | 48 | private static class StatementSerializableValue extends SerializableValue { 49 | private final String statement; 50 | private final PsiType type; 51 | 52 | public StatementSerializableValue(String statement, PsiType type) { 53 | this.statement = statement; 54 | this.type = type; 55 | } 56 | 57 | @Override 58 | public PsiType getType() { 59 | return type; 60 | } 61 | 62 | @Override 63 | public String getName() { 64 | return statement; 65 | } 66 | 67 | @Override 68 | public String getSimpleName() { 69 | return statement; 70 | } 71 | } 72 | 73 | private static class VariableSerializableValue extends SerializableValue { 74 | private final String variableName; 75 | private final PsiType type; 76 | 77 | public VariableSerializableValue(String variableName, PsiType type) { 78 | this.variableName = variableName; 79 | this.type = type; 80 | } 81 | 82 | @Override 83 | public PsiType getType() { 84 | return type; 85 | } 86 | 87 | @Override 88 | public String getName() { 89 | return type.getCanonicalText() + " " + variableName; 90 | } 91 | 92 | @Override 93 | public String getSimpleName() { 94 | return variableName; 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/SparseArraySerializerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Dallas Gutauckis (http://dallasgutauckis.com) 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 pl.charmas.parcelablegenerator.typeserializers; 17 | 18 | import com.intellij.psi.PsiType; 19 | 20 | import pl.charmas.parcelablegenerator.typeserializers.serializers.SparseArraySerializer; 21 | import pl.charmas.parcelablegenerator.util.PsiUtils; 22 | 23 | public class SparseArraySerializerFactory implements TypeSerializerFactory { 24 | private TypeSerializer mSerializer = new SparseArraySerializer(); 25 | 26 | @Override 27 | public TypeSerializer getSerializer(PsiType psiType) { 28 | if (PsiUtils.isOfType(psiType, "android.util.SparseArray")) { 29 | return mSerializer; 30 | } 31 | 32 | return null; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/TypeSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers; 17 | 18 | public interface TypeSerializer { 19 | 20 | String writeValue(SerializableValue field, String parcel, String flags); 21 | 22 | String readValue(SerializableValue field, String parcel); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/TypeSerializerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers; 17 | 18 | import com.intellij.psi.PsiType; 19 | 20 | public interface TypeSerializerFactory { 21 | public TypeSerializer getSerializer(PsiType psiType); 22 | } 23 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/BooleanPrimitiveSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers.serializers; 17 | 18 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 19 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 20 | 21 | public class BooleanPrimitiveSerializer implements TypeSerializer { 22 | 23 | @Override 24 | public String writeValue(SerializableValue field, String parcel, String flags) { 25 | return parcel + ".writeByte(" + field.getName() + " ? (byte) 1 : (byte) 0);"; 26 | } 27 | 28 | @Override 29 | public String readValue(SerializableValue field, String parcel) { 30 | return field.getName() + " = " + parcel + ".readByte() != 0;"; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/BooleanSparseArraySerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers.serializers; 17 | 18 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 19 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 20 | 21 | public class BooleanSparseArraySerializer implements TypeSerializer { 22 | 23 | @Override 24 | public String writeValue(SerializableValue field, String parcel, String flags) { 25 | return parcel + ".writeSparseBooleanArray(" + field.getName() + ");"; 26 | } 27 | 28 | @Override 29 | public String readValue(SerializableValue field, String parcel) { 30 | return field.getName() + " = " + parcel + ".readSparseBooleanArray();"; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/BundleSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers.serializers; 17 | 18 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 19 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 20 | 21 | /** 22 | * Custom serializer for Date objects to simplify parceling 23 | * 24 | * @author Dallas Gutauckis [dallas@gutauckis.com] 25 | */ 26 | public class BundleSerializer implements TypeSerializer { 27 | 28 | @Override 29 | public String writeValue(SerializableValue field, String parcel, String flags) { 30 | return parcel + ".writeBundle(" + field.getName() + ");"; 31 | } 32 | 33 | @Override 34 | public String readValue(SerializableValue field, String parcel) { 35 | return field.getName() + " = " + parcel + ".readBundle();"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/CharPrimitiveSerializer.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers.serializers; 2 | 3 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 4 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 5 | 6 | public class CharPrimitiveSerializer implements TypeSerializer { 7 | @Override 8 | public String writeValue(SerializableValue field, String parcel, String flags) { 9 | return parcel + ".writeInt(" + field.getName() + ");"; 10 | } 11 | 12 | @Override 13 | public String readValue(SerializableValue field, String parcel) { 14 | return field.getName() + " = (char) " + parcel + ".readInt();"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/DateSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers.serializers; 17 | 18 | import org.apache.xmlbeans.impl.common.NameUtil; 19 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 20 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 21 | 22 | /** 23 | * Custom serializer for Date objects to simplify parceling 24 | * 25 | * @author Dallas Gutauckis [dallas@gutauckis.com] 26 | */ 27 | public class DateSerializer implements TypeSerializer { 28 | 29 | private static final String NULL_VALUE = "-1"; 30 | 31 | @Override 32 | public String writeValue(SerializableValue field, String parcel, String flags) { 33 | String fieldName = field.getName(); 34 | return String.format("%s.writeLong(%s != null ? %s.getTime() : %s);", parcel, fieldName, fieldName, NULL_VALUE); 35 | } 36 | 37 | @Override 38 | public String readValue(SerializableValue field, String parcel) { 39 | String tmpFieldName = NameUtil.upperCaseFirstLetter(field.getSimpleName()); 40 | return String.format("long tmp%s = %s.readLong(); " + 41 | "%s = tmp%s == %s ? null : new java.util.Date(tmp%s);", tmpFieldName, parcel, field.getName(), tmpFieldName, NULL_VALUE, tmpFieldName); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/EnumerationSerializer.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers.serializers; 2 | 3 | import org.apache.xmlbeans.impl.common.NameUtil; 4 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 5 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 6 | 7 | /** 8 | * Modified by Dallas Gutauckis [dallas@gutauckis.com] 9 | */ 10 | public class EnumerationSerializer implements TypeSerializer { 11 | @Override 12 | public String writeValue(SerializableValue field, String parcel, String flags) { 13 | String fieldName = field.getName(); 14 | return String.format("%s.writeInt(%s == null ? -1 : %s.ordinal());", parcel, fieldName, fieldName); 15 | } 16 | 17 | @Override 18 | public String readValue(SerializableValue field, String parcel) { 19 | String tmpFieldName = NameUtil.upperCaseFirstLetter(field.getSimpleName()); 20 | String format = "int tmp%s = %s.readInt();" 21 | + "%s = tmp%s == -1 ? null : %s.values()[tmp%s];"; 22 | return String.format(format, tmpFieldName, parcel, field.getName(), tmpFieldName, field.getType().getCanonicalText(), tmpFieldName); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/GenericListSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers.serializers; 17 | 18 | import com.intellij.psi.PsiType; 19 | import com.intellij.psi.impl.source.PsiClassReferenceType; 20 | 21 | import org.jetbrains.annotations.NotNull; 22 | 23 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 24 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 25 | 26 | public class GenericListSerializer implements TypeSerializer { 27 | 28 | public static final String STRING_TYPE_NAME = "java.lang.String"; 29 | 30 | public String writeValue(String parcel, String valueType, String valueVarName) { 31 | String method = "writeList"; 32 | if (valueType.equals(STRING_TYPE_NAME)) { 33 | method = "writeStringList"; 34 | } 35 | return parcel + "." + method + "(" + valueVarName + ");"; 36 | } 37 | 38 | public String readValue(String parcel, String valueType, String valueVarName) { 39 | StringBuilder statement = new StringBuilder(); 40 | if (valueType.equals(STRING_TYPE_NAME)) { 41 | statement.append(valueVarName).append("=").append(parcel).append(".createStringArrayList();"); 42 | } else { 43 | String listConstructor = !valueType.isEmpty() 44 | ? "new java.util.ArrayList<" + valueType + ">();" 45 | : "new java.util.ArrayList();"; 46 | 47 | statement.append(valueVarName).append(" = ").append(listConstructor); 48 | statement.append(parcel).append(".readList(").append(valueVarName).append(", ").append(valueType).append(".class.getClassLoader());"); 49 | } 50 | 51 | return statement.toString(); 52 | } 53 | 54 | @Override 55 | public String writeValue(SerializableValue field, String parcel, String flags) { 56 | return writeValue(parcel, getGenericType(field), field.getName()); 57 | } 58 | 59 | @Override 60 | public String readValue(SerializableValue field, String parcel) { 61 | final String valueType = getGenericType(field); 62 | final String valueVarName = field.getName(); 63 | return readValue(parcel, valueType, valueVarName); 64 | } 65 | 66 | @NotNull 67 | private String getGenericType(SerializableValue field) { 68 | String genericType = ""; 69 | try { 70 | PsiType[] parameters = ((PsiClassReferenceType) field.getType()).getParameters(); 71 | if (parameters.length > 0) { 72 | genericType = parameters[0].getCanonicalText(); 73 | } 74 | } catch (Exception ignored) { 75 | } 76 | return genericType; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/MapSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Dallas Gutauckis (http://dallasgutauckis.com) 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 pl.charmas.parcelablegenerator.typeserializers.serializers; 17 | 18 | import com.intellij.psi.PsiType; 19 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 20 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 21 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializerFactory; 22 | import pl.charmas.parcelablegenerator.util.PsiUtils; 23 | 24 | import java.util.List; 25 | 26 | @SuppressWarnings("StringBufferReplaceableByString") 27 | public class MapSerializer implements TypeSerializer { 28 | private final TypeSerializerFactory typeSerializerFactory; 29 | 30 | public MapSerializer(TypeSerializerFactory typeSerializerFactory) { 31 | this.typeSerializerFactory = typeSerializerFactory; 32 | } 33 | 34 | 35 | @Override 36 | public String writeValue(SerializableValue field, String parcel, String flags) { 37 | final List resolvedGenerics = PsiUtils.getResolvedGenerics(field.getType()); 38 | PsiType keyType = resolvedGenerics.get(1); 39 | PsiType valueType = resolvedGenerics.get(0); 40 | 41 | final String fieldName = field.getName(); 42 | 43 | return new StringBuilder() 44 | .append(parcel).append(".writeInt(").append(fieldName).append(".size());") 45 | .append(String.format("for(Map.Entry<%s,%s> entry : %s.entrySet()) {", keyType.getCanonicalText(), valueType.getCanonicalText(), field.getName())) 46 | .append(typeSerializerFactory 47 | .getSerializer(keyType) 48 | .writeValue(SerializableValue.statement("entry.getKey()", keyType), parcel, flags) 49 | ) 50 | .append(typeSerializerFactory 51 | .getSerializer(valueType) 52 | .writeValue(SerializableValue.statement("entry.getValue()", valueType), parcel, flags) 53 | ) 54 | .append("}") 55 | .toString(); 56 | } 57 | 58 | @Override 59 | public String readValue(SerializableValue field, String parcel) { 60 | final List resolvedGenerics = PsiUtils.getResolvedGenerics(field.getType()); 61 | PsiType keyType = resolvedGenerics.get(1); 62 | PsiType valueType = resolvedGenerics.get(0); 63 | 64 | String sizeVariableName = field.getSimpleName() + "Size"; 65 | return new StringBuilder() 66 | .append(String.format("int %s = %s.readInt();", sizeVariableName, parcel)) 67 | .append(field.getName()).append(String.format(" = new java.util.HashMap<%s, %s>(%s);", keyType.getCanonicalText(), valueType.getCanonicalText(), sizeVariableName)) 68 | .append(String.format("for(int i = 0; i < %s; i++) {", sizeVariableName)) 69 | .append(typeSerializerFactory.getSerializer(keyType).readValue(SerializableValue.variable("key", keyType), parcel)) 70 | .append(typeSerializerFactory.getSerializer(valueType).readValue(SerializableValue.variable("value", valueType), parcel)) 71 | .append(field.getName()).append(".put(key, value);") 72 | .append("}") 73 | .toString(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/NullablePrimitivesArraySerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers.serializers; 17 | 18 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 19 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 20 | 21 | public class NullablePrimitivesArraySerializer implements TypeSerializer { 22 | 23 | private final String typeName; 24 | 25 | public NullablePrimitivesArraySerializer(String typeName) { 26 | this.typeName = typeName; 27 | } 28 | 29 | @Override 30 | public String writeValue(SerializableValue field, String parcel, String flags) { 31 | return parcel + ".writeArray(" + field.getName() + ");"; 32 | } 33 | 34 | @Override 35 | public String readValue(SerializableValue field, String parcel) { 36 | return field.getName() + " = (" + typeName + ")" + parcel + ".readArray(" + typeName + ".class.getClassLoader());"; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/NullablePrimitivesSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers.serializers; 17 | 18 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 19 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 20 | 21 | public class NullablePrimitivesSerializer implements TypeSerializer { 22 | 23 | private final String typeName; 24 | 25 | public NullablePrimitivesSerializer(String typeName) { 26 | this.typeName = typeName; 27 | } 28 | 29 | @Override 30 | public String writeValue(SerializableValue field, String parcel, String flags) { 31 | return parcel + ".writeValue(" + field.getName() + ");"; 32 | } 33 | 34 | @Override 35 | public String readValue(SerializableValue field, String parcel) { 36 | return field.getName() + " = (" + typeName + ")" + parcel + ".readValue(" + typeName + ".class.getClassLoader());"; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/ParcelableArraySerializer.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers.serializers; 2 | 3 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 4 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 5 | 6 | public class ParcelableArraySerializer implements TypeSerializer { 7 | @Override 8 | public String writeValue(SerializableValue field, String parcel, String flags) { 9 | return parcel + ".writeTypedArray(" + field.getName() + ", flags);"; 10 | } 11 | 12 | @Override 13 | public String readValue(SerializableValue field, String parcel) { 14 | return field.getName() + " = " + parcel + ".createTypedArray(" + field.getType().getDeepComponentType().getCanonicalText() + ".CREATOR);"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/ParcelableListSerializer.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers.serializers; 2 | 3 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 4 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 5 | import pl.charmas.parcelablegenerator.util.PsiUtils; 6 | 7 | /** 8 | * @author Dallas Gutauckis [dallas@gutauckis.com] 9 | * @author Michał Charmas [michal@charmas.pl] 10 | */ 11 | public class ParcelableListSerializer implements TypeSerializer { 12 | @Override 13 | public String writeValue(SerializableValue field, String parcel, String flags) { 14 | return String.format("%s.writeTypedList(%s);", parcel, field.getName()); 15 | } 16 | 17 | @Override 18 | public String readValue(SerializableValue field, String parcel) { 19 | String paramType = PsiUtils.getResolvedGenerics(field.getType()).get(0).getCanonicalText(); 20 | return String.format("%s = %s.createTypedArrayList(%s.CREATOR);", field.getName(), parcel, paramType); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/ParcelableObjectSerializer.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers.serializers; 2 | 3 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 4 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 5 | 6 | /** 7 | * Serializer for types implementing Parcelable 8 | * 9 | * @author Dallas Gutauckis [dallas@gutauckis.com] 10 | */ 11 | public class ParcelableObjectSerializer implements TypeSerializer { 12 | @Override 13 | public String writeValue(SerializableValue field, String parcel, String flags) { 14 | return parcel + ".writeParcelable(" + field.getName() + ", " + flags + ");"; 15 | } 16 | 17 | @Override 18 | public String readValue(SerializableValue field, String parcel) { 19 | return field.getName() + " = " + parcel + ".readParcelable(" + field.getType().getCanonicalText() + ".class.getClassLoader());"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/PrimitiveArraySerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers.serializers; 17 | 18 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 19 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 20 | 21 | public class PrimitiveArraySerializer implements TypeSerializer { 22 | 23 | private final String type; 24 | 25 | public PrimitiveArraySerializer(String type) { 26 | this.type = type; 27 | } 28 | 29 | @Override 30 | public String writeValue(SerializableValue field, String parcel, String flags) { 31 | return parcel + ".write" + type + "Array(" + field.getName() + ");"; 32 | } 33 | 34 | @Override 35 | public String readValue(SerializableValue field, String parcel) { 36 | return field.getName() + " = " + parcel + ".create" + type + "Array();"; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/PrimitiveTypeSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Michał Charmas (http://blog.charmas.pl) 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 pl.charmas.parcelablegenerator.typeserializers.serializers; 17 | 18 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 19 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 20 | 21 | public class PrimitiveTypeSerializer implements TypeSerializer { 22 | 23 | private final String typeName; 24 | 25 | public PrimitiveTypeSerializer(String typeName) { 26 | this.typeName = typeName; 27 | } 28 | 29 | @Override 30 | public String writeValue(SerializableValue field, String parcel, String flags) { 31 | return parcel + ".write" + typeName + "(" + field.getName() + ");"; 32 | } 33 | 34 | @Override 35 | public String readValue(SerializableValue field, String parcel) { 36 | return field.getName() + " = " + parcel + ".read" + typeName + "();"; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/SerializableObjectSerializer.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers.serializers; 2 | 3 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 4 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 5 | 6 | /** 7 | * Modified by Dallas Gutauckis [dallas@gutauckis.com] 8 | */ 9 | public class SerializableObjectSerializer implements TypeSerializer { 10 | @Override 11 | public String writeValue(SerializableValue field, String parcel, String flags) { 12 | return parcel + ".writeSerializable(" + field.getName() + ");"; 13 | } 14 | 15 | @Override 16 | public String readValue(SerializableValue field, String parcel) { 17 | return field.getName() + " = (" + field.getType().getCanonicalText() + ") " + parcel + ".readSerializable();"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/ShortPrimitiveSerializer.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.typeserializers.serializers; 2 | 3 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 4 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 5 | 6 | public class ShortPrimitiveSerializer implements TypeSerializer { 7 | 8 | @Override 9 | public String writeValue(SerializableValue field, String parcel, String flags) { 10 | return parcel + ".writeInt(" + field.getName() + ");"; 11 | } 12 | 13 | @Override 14 | public String readValue(SerializableValue field, String parcel) { 15 | return field.getName() + " = (short) " + parcel + ".readInt();"; 16 | } 17 | } -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/typeserializers/serializers/SparseArraySerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Dallas Gutauckis (http://dallasgutauckis.com) 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 pl.charmas.parcelablegenerator.typeserializers.serializers; 17 | 18 | import pl.charmas.parcelablegenerator.typeserializers.SerializableValue; 19 | import pl.charmas.parcelablegenerator.typeserializers.TypeSerializer; 20 | import pl.charmas.parcelablegenerator.util.PsiUtils; 21 | 22 | public class SparseArraySerializer implements TypeSerializer { 23 | @Override 24 | public String writeValue(SerializableValue field, String parcel, String flags) { 25 | return String.format("%s.writeSparseArray((android.util.SparseArray) %s);", parcel, field.getName()); 26 | } 27 | 28 | @Override 29 | public String readValue(SerializableValue field, String parcel) { 30 | String paramType = PsiUtils.getResolvedGenerics(field.getType()).get(0).getCanonicalText(); 31 | return String.format("%s = %s.readSparseArray(%s.class.getClassLoader());", field.getName(), parcel, paramType); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/pl/charmas/parcelablegenerator/util/PsiUtils.java: -------------------------------------------------------------------------------- 1 | package pl.charmas.parcelablegenerator.util; 2 | 3 | import com.intellij.psi.PsiClass; 4 | import com.intellij.psi.PsiClassType; 5 | import com.intellij.psi.PsiType; 6 | import com.intellij.psi.util.PsiTypesUtil; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | /** 12 | * Utils for introspecting Psi* stuff 13 | * 14 | * @author Dallas Gutauckis [dallas@gutauckis.com] 15 | */ 16 | final public class PsiUtils { 17 | private PsiUtils() { 18 | } 19 | 20 | /** 21 | * Checks that the given type is an implementer of the given canonicalName with the given typed parameters 22 | * 23 | * @param type what we're checking against 24 | * @param canonicalName the type must extend/implement this generic 25 | * @param canonicalParamNames the type that the generic(s) must be (in this order) 26 | * @return 27 | */ 28 | public static boolean isTypedClass(PsiType type, String canonicalName, String... canonicalParamNames) { 29 | PsiClass parameterClass = PsiTypesUtil.getPsiClass(type); 30 | 31 | if (parameterClass == null) { 32 | return false; 33 | } 34 | 35 | // This is a safe cast, for if parameterClass != null, the type was checked in PsiTypesUtil#getPsiClass(...) 36 | PsiClassType pct = (PsiClassType) type; 37 | 38 | // Main class name doesn't match; exit early 39 | if (!canonicalName.equals(parameterClass.getQualifiedName())) { 40 | return false; 41 | } 42 | 43 | List psiTypes = new ArrayList(pct.resolveGenerics().getSubstitutor().getSubstitutionMap().values()); 44 | 45 | for (int i = 0; i < canonicalParamNames.length; i++) { 46 | if (!isOfType(psiTypes.get(i), canonicalParamNames[i])) { 47 | return false; 48 | } 49 | } 50 | 51 | // Passed all screenings; must be a match! 52 | return true; 53 | } 54 | 55 | /** 56 | * Resolves generics on the given type and returns them (if any) or null if there are none 57 | * 58 | * @param type 59 | * @return 60 | */ 61 | public static List getResolvedGenerics(PsiType type) { 62 | List psiTypes = null; 63 | 64 | if (type instanceof PsiClassType) { 65 | PsiClassType pct = (PsiClassType) type; 66 | psiTypes = new ArrayList(pct.resolveGenerics().getSubstitutor().getSubstitutionMap().values()); 67 | } 68 | 69 | return psiTypes; 70 | } 71 | 72 | public static boolean isOfType(PsiType type, String canonicalName) { 73 | if (type.getCanonicalText().equals(canonicalName)) { 74 | return true; 75 | } 76 | 77 | final String nonGenericType = getNonGenericType(type); 78 | 79 | if (nonGenericType != null && nonGenericType.equals(canonicalName)) { 80 | return true; 81 | } 82 | 83 | for (PsiType iterType : type.getSuperTypes()) { 84 | if (isOfType(iterType, canonicalName)) { 85 | return true; 86 | } 87 | } 88 | 89 | return false; 90 | } 91 | 92 | public static String getNonGenericType(PsiType type) { 93 | if (type instanceof PsiClassType) { 94 | PsiClassType pct = (PsiClassType) type; 95 | final PsiClass psiClass = pct.resolve(); 96 | 97 | return psiClass != null ? psiClass.getQualifiedName() : null; 98 | } 99 | 100 | return type.getCanonicalText(); 101 | } 102 | } 103 | --------------------------------------------------------------------------------