├── .gitattributes ├── .gitignore ├── .lein-classpath ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── build └── leiningen │ └── circle.clj ├── circle.yml ├── icepick-processor ├── .gitignore ├── project.clj ├── src │ ├── clojure │ │ └── icepick │ │ │ └── processor.clj │ └── java │ │ └── icepick │ │ └── processor │ │ └── IcepickProcessor.java └── test │ ├── icepick │ └── core_test.clj │ └── test │ ├── boxed │ ├── Test$$Icepick.java │ └── Test.java │ ├── bundler │ ├── Test$$Icepick.java │ └── Test.java │ ├── failures │ ├── Test1.java │ └── Test2.java │ ├── generics │ ├── Test1.java │ ├── Test2$$Icepick.java │ ├── Test2.java │ ├── Test3$$Icepick.java │ ├── Test3.java │ ├── Test4$$Icepick.java │ └── Test4.java │ ├── parent │ ├── Test$$Icepick.java │ ├── Test$Inner$$Icepick.java │ └── Test.java │ ├── simple │ ├── Test$$Icepick.java │ └── Test.java │ └── views │ ├── Test$$Icepick.java │ ├── Test$Inner$$Icepick.java │ └── Test.java ├── icepick ├── .gitignore ├── project.clj └── src │ └── icepick │ ├── Bundler.java │ ├── Icepick.java │ ├── Injector.java │ └── State.java ├── project.clj └── sample ├── .gitignore ├── app ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── ic_launcher-web.png │ ├── java │ └── com │ │ └── github │ │ └── frankiesardo │ │ └── icepick │ │ ├── CustomView.java │ │ ├── Example.java │ │ ├── ExampleBundler.java │ │ ├── MainActivity.java │ │ └── MyBundler.java │ └── res │ ├── drawable-hdpi │ └── ic_launcher.png │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── drawable-xxhdpi │ └── ic_launcher.png │ ├── layout │ └── activity_main.xml │ ├── menu │ └── main.xml │ ├── values-sw720dp-land │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── lib ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── sample │ └── icepick │ └── lib │ ├── BaseActivity.java │ └── BaseCustomView.java └── settings.gradle /.gitattributes: -------------------------------------------------------------------------------- 1 | sample/* linguist-vendored 2 | icepick/* linguist-vendored 3 | icepick-processor/test/* linguist-vendored 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | /logs 11 | .DS_Store 12 | *.iml 13 | .idea -------------------------------------------------------------------------------- /.lein-classpath: -------------------------------------------------------------------------------- 1 | build -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 3.2.0 2 | - Custom Bundler support 3 | 4 | ## 3.1.0 5 | - Change annotation name from `@Icicle` to `@State` 6 | 7 | ## 3.0.2 8 | - Remove debug logs 9 | 10 | ## 3.0.1 11 | - Fix #41, #40 12 | 13 | ## 3.0.0 14 | - Move to clojure 15 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Eclipse Public License -v 1.0 2 | ============================= 3 | 4 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE (“AGREEMENT”). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 5 | 6 | ### 1. Definitions 7 | 8 | “Contribution” means: 9 | * **a)** in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and 10 | * **b)** in the case of each subsequent Contributor: 11 | * **i)** changes to the Program, and 12 | * **ii)** additions to the Program; 13 | where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: **(i)** are separate modules of software distributed in conjunction with the Program under their own license agreement, and **(ii)** are not derivative works of the Program. 14 | 15 | “Contributor” means any person or entity that distributes the Program. 16 | 17 | “Licensed Patents ” mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program. 18 | 19 | “Program” means the Contributions distributed in accordance with this Agreement. 20 | 21 | “Recipient” means anyone who receives the Program under this Agreement, including all Contributors. 22 | 23 | ### 2. Grant of Rights 24 | 25 | **a)** Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form. 26 | 27 | **b)** Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder. 28 | 29 | **c)** Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program. 30 | 31 | **d)** Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement. 32 | 33 | ### 3. Requirements 34 | 35 | A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that: 36 | * **a)** it complies with the terms and conditions of this Agreement; and 37 | * **b)** its license agreement: 38 | * **i)** effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose; 39 | * **ii)** effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits; 40 | * **iii)** states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and 41 | * **iv)** states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange. 42 | 43 | When the Program is made available in source code form: 44 | * **a)** it must be made available under this Agreement; and 45 | * **b)** a copy of this Agreement must be included with each copy of the Program. 46 | 47 | Contributors may not remove or alter any copyright notices contained within the Program. 48 | 49 | Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution. 50 | 51 | ### 4. Commercial Distribution 52 | 53 | Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor (“Commercial Contributor”) hereby agrees to defend and indemnify every other Contributor (“Indemnified Contributor”) against any losses, damages and costs (collectively “Losses”) arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: **a)** promptly notify the Commercial Contributor in writing of such claim, and **b)** allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense. 54 | 55 | For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages. 56 | 57 | ### 5. No Warranty 58 | 59 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement , including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations. 60 | 61 | ### 6. Disclaimer of Liability 62 | 63 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 64 | 65 | ### 7. General 66 | 67 | If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable. 68 | 69 | If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed. 70 | 71 | All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive. 72 | 73 | Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved. 74 | 75 | This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation. 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Icepick 2 | ============ 3 | 4 | Icepick is an Android library that eliminates the boilerplate of saving and restoring instance state. 5 | It uses annotation processing to generate code that does bundle manipulation and key generation, so that you don't have to write it yourself. 6 | 7 | ```java 8 | class ExampleActivity extends Activity { 9 | @State String username; // This will be automatically saved and restored 10 | 11 | @Override public void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | Icepick.restoreInstanceState(this, savedInstanceState); 14 | } 15 | 16 | @Override public void onSaveInstanceState(Bundle outState) { 17 | super.onSaveInstanceState(outState); 18 | Icepick.saveInstanceState(this, outState); 19 | } 20 | 21 | // You can put the calls to Icepick into a BaseActivity 22 | // All Activities extending BaseActivity automatically have state saved/restored 23 | } 24 | ``` 25 | 26 | It works for `Activities`, `Fragments` or any object that needs to serialize its state on a `Bundle` (e.g. mortar's [ViewPresenters](https://github.com/square/mortar/blob/master/mortar/src/main/java/mortar/ViewPresenter.java)) 27 | 28 | Icepick can also generate the instance state code for custom Views: 29 | 30 | ```java 31 | class CustomView extends View { 32 | @State int selectedPosition; // This will be automatically saved and restored 33 | 34 | @Override public Parcelable onSaveInstanceState() { 35 | return Icepick.saveInstanceState(this, super.onSaveInstanceState()); 36 | } 37 | 38 | @Override public void onRestoreInstanceState(Parcelable state) { 39 | super.onRestoreInstanceState(Icepick.restoreInstanceState(this, state)); 40 | } 41 | 42 | // You can put the calls to Icepick into a BaseCustomView and inherit from it 43 | // All Views extending this CustomView automatically have state saved/restored 44 | } 45 | ``` 46 | 47 | Custom Bundler 48 | -------- 49 | 50 | From version `3.2.0` you can supply a class parameter to the `State` annotation. 51 | This class should implement the `Bundler` interface and you can use it to provide custom serialisation and deserialisation for your types. 52 | 53 | ```java 54 | class MyFragment { 55 | @State(MyCustomBundler.class) MyCustomType myCustomType; 56 | } 57 | 58 | class MyCustomBundler implements Bundler { 59 | void put(String key, MyCustomType value, Bundle bundle) { 60 | ... 61 | } 62 | 63 | MyCustomType get(String key, Bundle bundle) { 64 | ... 65 | } 66 | } 67 | ``` 68 | 69 | This is useful if you want to use `icepick` in conjunction with `Parceler`. 70 | 71 | Proguard 72 | -------- 73 | 74 | If Proguard is enabled make sure you add these rules to your configuration: 75 | 76 | ``` 77 | -dontwarn icepick.** 78 | -keep class icepick.** { *; } 79 | -keep class **$$Icepick { *; } 80 | -keepclasseswithmembernames class * { 81 | @icepick.* ; 82 | } 83 | -keepnames class * { @icepick.State *;} 84 | ``` 85 | 86 | Download 87 | -------- 88 | 89 | Icepick needs two libraries: `icepick` and `icepick-processor`. 90 | 91 | [![Clojars Project](http://clojars.org/frankiesardo/icepick/latest-version.svg)](http://clojars.org/frankiesardo/icepick) 92 | 93 | [![Clojars Project](http://clojars.org/frankiesardo/icepick-processor/latest-version.svg)](http://clojars.org/frankiesardo/icepick-processor) 94 | 95 | Gradle: 96 | 97 | ```groovy 98 | repositories { 99 | maven {url "https://clojars.org/repo/"} 100 | } 101 | dependencies { 102 | compile 'frankiesardo:icepick:{{latest-version}}' 103 | provided 'frankiesardo:icepick-processor:{{latest-version}}' 104 | } 105 | ``` 106 | 107 | Maven: 108 | 109 | ```xml 110 | 111 | 112 | clojars 113 | https://clojars.org/repo/ 114 | 115 | true 116 | 117 | 118 | true 119 | 120 | 121 | 122 | 123 | 124 | frankiesardo 125 | icepick 126 | {{latest-version}} 127 | 128 | 129 | frankiesardo 130 | icepick-processor 131 | {{latest-version}} 132 | true 133 | 134 | 135 | ``` 136 | -------------------------------------------------------------------------------- /build/leiningen/circle.clj: -------------------------------------------------------------------------------- 1 | (ns leiningen.circle 2 | (:require [leiningen.core [eval :as eval]] 3 | [leiningen 4 | [release :as release] 5 | [deploy :as deploy]])) 6 | 7 | (defn env [s] 8 | (System/getenv s)) 9 | 10 | (defn circle [project & args] 11 | (condp re-find (env "CIRCLE_BRANCH") 12 | #"master" 13 | (deploy/deploy project "clojars") 14 | 15 | #"(?i)release" 16 | (do 17 | (eval/sh "git" "reset" "--hard" "origin/master") 18 | (release/release project) 19 | (eval/sh "git" "push" "origin" "--delete" (env "CIRCLE_BRANCH"))))) 20 | -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | test: 2 | pre: 3 | - git clone $DOTFILES ~/dotfiles && . ~/dotfiles/init.sh 4 | override: 5 | - lein sub do install, test 6 | post: 7 | - lein sub circle -------------------------------------------------------------------------------- /icepick-processor/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | -------------------------------------------------------------------------------- /icepick-processor/project.clj: -------------------------------------------------------------------------------- 1 | (defproject frankiesardo/icepick-processor "3.2.1-SNAPSHOT" 2 | :description "Compile time processor for Icepick" 3 | :url "https://github.com/frankiesardo/icepick" 4 | :license {:name "Eclipse Public License -v 1.0" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.7.0"] 7 | [frankiesardo/icepick "3.2.1-SNAPSHOT"] 8 | [com.google.auto.service/auto-service "1.0-rc2"] 9 | [stencil "0.3.5"]] 10 | :release-tasks [["vcs" "assert-committed"] 11 | ["change" "version" "leiningen.release/bump-version" "release"] 12 | ["vcs" "commit"] 13 | ["vcs" "tag" "v"] 14 | ["deploy" "clojars"] 15 | ["change" "version" "leiningen.release/bump-version"] 16 | ["vcs" "commit"] 17 | ["vcs" "push"]] 18 | :source-paths ["src/clojure"] 19 | :java-source-paths ["src/java"] 20 | :javac-options ["-target" "1.6" "-source" "1.6"] 21 | :aot :all 22 | :profiles {:dev {:dependencies [[com.google.testing.compile/compile-testing "0.4"]]} 23 | :provided {:dependencies [[com.google.android/android "4.1.1.4"]]}}) 24 | -------------------------------------------------------------------------------- /icepick-processor/src/clojure/icepick/processor.clj: -------------------------------------------------------------------------------- 1 | (ns icepick.processor 2 | (:require [clojure.java.io :as io] 3 | [clojure.string :as str] 4 | [stencil.core :as mustache]) 5 | (:import (icepick State 6 | Icepick) 7 | (javax.tools Diagnostic$Kind) 8 | (javax.lang.model.type TypeMirror 9 | TypeKind) 10 | (javax.lang.model.element Element 11 | TypeElement 12 | Modifier) 13 | (javax.lang.model.util Elements 14 | ElementFilter 15 | Types))) 16 | 17 | (def ^:private ^:dynamic *env*) 18 | 19 | (defn- error [elem msg] 20 | (.. *env* getMessager (printMessage Diagnostic$Kind/ERROR msg elem))) 21 | 22 | (defn- file-object [file-name elem] 23 | (.. *env* getFiler (createSourceFile file-name (into-array Element [elem])))) 24 | 25 | (defn- package-name [^TypeElement elem] 26 | (.. *env* getElementUtils (getPackageOf elem) getQualifiedName toString)) 27 | 28 | (defn- type-element [name] 29 | (.. *env* getElementUtils (getTypeElement name))) 30 | 31 | (defn- wildcard-type [name] 32 | (.. *env* getTypeUtils (getWildcardType (.asType (type-element name)) nil))) 33 | 34 | (defn- declared-type [elem generic-type] 35 | (.. *env* getTypeUtils (getDeclaredType elem (into-array TypeMirror [generic-type])))) 36 | 37 | (defn- assignable? [^TypeMirror type1 ^TypeMirror type2] 38 | (.. *env* getTypeUtils (isAssignable type1 type2))) 39 | 40 | (def ^:private state-annotation "icepick.State" #_(.getName State)) 41 | 42 | ;; emit-class! 43 | 44 | (def ^:private template 45 | "// Generated code from Icepick. Do not modify! 46 | package {{package}}; 47 | import android.os.Bundle; 48 | {{#view?}} 49 | import android.os.Parcelable; 50 | {{/view?}} 51 | import icepick.Bundler; 52 | import icepick.Injector.Helper; 53 | import icepick.Injector.{{type}}; 54 | 55 | import java.util.Map; 56 | import java.util.HashMap; 57 | 58 | public class {{name}} extends {{parent}} { 59 | 60 | private final static Map> BUNDLERS = new HashMap>(); 61 | static { 62 | {{#fields}} {{#bundler}} 63 | BUNDLERS.put(\"{{name}}\", new {{bundler}}()); 64 | {{/bundler}} {{/fields}} 65 | } 66 | 67 | private final static Helper H = new Helper(\"{{package}}.{{name}}.\", BUNDLERS); 68 | 69 | {{^view?}} 70 | @Override public void restore(T target, Bundle state) { 71 | if (state == null) return; 72 | {{#fields}} 73 | target.{{name}} = H.get{{method}}(state, \"{{name}}\"); 74 | {{/fields}} 75 | super.restore(target, state); 76 | } 77 | 78 | @Override public void save(T target, Bundle state) { 79 | super.save(target, state); 80 | {{#fields}} 81 | H.put{{method}}(state, \"{{name}}\", target.{{name}}); 82 | {{/fields}} 83 | } 84 | {{/view?}} 85 | {{#view?}} 86 | @Override public Parcelable restore(T target, Parcelable p) { 87 | Bundle state = (Bundle) p; 88 | {{#fields}} 89 | target.{{name}} = H.get{{method}}(state, \"{{name}}\"); 90 | {{/fields}} 91 | return super.restore(target, H.getParent(state)); 92 | } 93 | 94 | @Override public Parcelable save(T target, Parcelable p) { 95 | Bundle state = H.putParent(super.save(target, p)); 96 | {{#fields}} 97 | H.put{{method}}(state, \"{{name}}\", target.{{name}}); 98 | {{/fields}} 99 | return state; 100 | } 101 | {{/view?}} 102 | }") 103 | 104 | (defn- emit-class! 105 | [[class fields]] 106 | (let [vals {:view? (:view? class) 107 | :type (if (:view? class) "View" "Object") 108 | :package (:package class) 109 | :name (str (:dollar-name class) Icepick/SUFFIX) 110 | :target (:dotted-name class) 111 | :parent (if-let [parent (:qualified-parent-name class)] 112 | (str parent Icepick/SUFFIX) 113 | (if (:view? class) "View" "Object")) 114 | :fields fields} 115 | file-name (str (:package class) "." (:dollar-name class) Icepick/SUFFIX) 116 | file-object (file-object file-name (:element class))] 117 | (doto (.openWriter file-object) 118 | (.write (mustache/render-string template vals)) 119 | (.flush) 120 | (.close)))) 121 | 122 | ;; enclosing class 123 | 124 | (def ^:private analyzed-classes 125 | (atom {:annotated {} 126 | :not-annotated #{}})) 127 | 128 | (defn- view? [elem] 129 | (assignable? (.asType elem) (.asType (type-element "android.view.View")))) 130 | 131 | (defn- class-info [^TypeElement elem] 132 | (let [qualified-name (str (.getQualifiedName elem)) 133 | package (package-name elem) 134 | dotted-name (subs qualified-name (inc (count package))) 135 | dollar-name (str/replace dotted-name #"\." "\\$")] 136 | {:package package 137 | :dotted-name dotted-name 138 | :dollar-name dollar-name})) 139 | 140 | (defn- annotated-class? [^TypeElement elem] 141 | (seq (for [field (ElementFilter/fieldsIn (.getEnclosedElements elem)) 142 | ann (.getAnnotationMirrors field) 143 | :when (= state-annotation (-> ann .getAnnotationType .asElement str))] 144 | field))) 145 | 146 | (defn- qualified-parent-name [^TypeElement elem] 147 | (loop [^TypeMirror type (.getSuperclass elem)] 148 | (when-not (= (.getKind type) TypeKind/NONE) 149 | (let [class-element (.asElement type) 150 | class-name (str class-element)] 151 | (when-not (or (.startsWith class-name Icepick/JAVA_PREFIX) 152 | (.startsWith class-name Icepick/ANDROID_PREFIX)) 153 | (cond 154 | (contains? (:annotated @analyzed-classes) class-name) 155 | (get-in @analyzed-classes [:annotated class-name]) 156 | 157 | (contains? (:not-annotated @analyzed-classes) class-name) 158 | (recur (.getSuperclass class-element)) 159 | 160 | (annotated-class? class-element) 161 | (let [{:keys [package dollar-name]} (class-info class-element) 162 | qualified-name (str package "." dollar-name)] 163 | (swap! analyzed-classes assoc-in 164 | [:annotated class-name] qualified-name) 165 | qualified-name) 166 | 167 | :else 168 | (do 169 | (swap! analyzed-classes update-in [:not-annotated] conj class-name) 170 | (recur (.getSuperclass class-element))))))))) 171 | 172 | (defn- enclosing-class [^TypeElement elem] 173 | (when (some #{Modifier/PRIVATE} (.getModifiers elem)) 174 | (error elem "Enclosing class must not be private")) 175 | (assoc (class-info elem) 176 | :elem elem 177 | :view? (view? elem) 178 | :qualified-parent-name (qualified-parent-name elem))) 179 | 180 | ;; bundle-method 181 | 182 | (def ^:private exact-types 183 | {"boolean" "Boolean", 184 | "boolean[]" "BooleanArray", 185 | "java.lang.Boolean" "BoxedBoolean", 186 | "byte" "Byte", 187 | "byte[]" "ByteArray", 188 | "java.lang.Byte" "BoxedByte", 189 | "char" "Char", 190 | "char[]" "CharArray", 191 | "java.lang.Char" "BoxedChar", 192 | "double" "Double", 193 | "double[]" "DoubleArray", 194 | "java.lang.Double" "BoxedDouble", 195 | "float" "Float", 196 | "float[]" "FloatArray", 197 | "java.lang.Float" "BoxedFloat", 198 | "int" "Int", 199 | "int[]" "IntArray", 200 | "java.lang.Integer" "BoxedInt" 201 | "long" "Long", 202 | "long[]" "LongArray", 203 | "java.lang.Long" "BoxedLong", 204 | "short" "Short", 205 | "short[]" "ShortArray", 206 | "java.lang.Short" "BoxedShort", 207 | 208 | "java.lang.CharSequence" "CharSequence", 209 | "java.lang.CharSequence[]" "CharSequenceArray", 210 | "java.lang.String" "String", 211 | "java.lang.String[]" "StringArray", 212 | "java.util.ArrayList" "CharSequenceArrayList", 213 | "java.util.ArrayList" "IntegerArrayList", 214 | "java.util.ArrayList" "StringArrayList", 215 | 216 | "android.os.Bundle" "Bundle", 217 | "android.os.Parcelable[]" "ParcelableArray"}) 218 | 219 | (defn- parcelable? [type] 220 | (assignable? type (.asType (type-element "android.os.Parcelable")))) 221 | 222 | (defn- serializable? [type] 223 | (assignable? type (.asType (type-element "java.io.Serializable")))) 224 | 225 | (defn- parcelable-array-list? [type] 226 | (and (not= TypeKind/WILDCARD (.getKind type)) 227 | (assignable? type (declared-type 228 | (type-element "java.util.ArrayList") 229 | (wildcard-type "android.os.Parcelable"))))) 230 | 231 | (defn- sparse-parcelable-array? [type] 232 | (and (not= TypeKind/WILDCARD (.getKind type)) 233 | (assignable? type (declared-type 234 | (type-element "android.util.SparseArray") 235 | (wildcard-type "android.os.Parcelable"))))) 236 | 237 | (defn- bundle-method [^TypeMirror type] 238 | (or (get exact-types (str type)) 239 | (and (parcelable? type) "Parcelable") 240 | (and (parcelable-array-list? type) "ParcelableArrayList") 241 | (and (sparse-parcelable-array? type) "SparseParcelableArray") 242 | (and (serializable? type) "Serializable"))) 243 | 244 | (defn- bundler [^Element elem] 245 | (->> (.getAnnotationMirrors elem) 246 | (filter #(= (.. % getAnnotationType toString) state-annotation)) 247 | (mapcat #(.getElementValues %)) 248 | (filter #(= (.. % getKey getSimpleName toString) "value")) 249 | (map #(.. % getValue getValue asElement toString)) 250 | (first))) 251 | 252 | (defn- analyze-field 253 | "Converts javax Element into a suitable representation for code generation." 254 | [^Element elem] 255 | (when (some #{Modifier/PRIVATE Modifier/STATIC Modifier/FINAL} (.getModifiers elem)) 256 | (error elem "Field must not be private, static or final")) 257 | (let [type (.asType elem) 258 | bundler (bundler elem) 259 | bundle-method (bundle-method type)] 260 | (when-not (or bundler bundle-method) 261 | (error elem (str "Don't know how to put a " type " inside a Bundle"))) 262 | (cond-> {:name (.. elem getSimpleName toString) 263 | :enclosing-class (enclosing-class (.getEnclosingElement elem))} 264 | bundler (merge {:bundler bundler 265 | :method "WithBundler"}) 266 | (not bundler) (merge {:bundler false 267 | :method bundle-method})))) 268 | 269 | (defn process 270 | [processing-env annotations env] 271 | (binding [*env* processing-env] 272 | (doseq [ann annotations] 273 | (->> (.getElementsAnnotatedWith env ann) 274 | (map analyze-field) 275 | (group-by :enclosing-class) 276 | (map emit-class!) 277 | (doall))))) 278 | -------------------------------------------------------------------------------- /icepick-processor/src/java/icepick/processor/IcepickProcessor.java: -------------------------------------------------------------------------------- 1 | package icepick.processor; 2 | 3 | import clojure.java.api.Clojure; 4 | import clojure.lang.IFn; 5 | import com.google.auto.service.AutoService; 6 | import icepick.State; 7 | 8 | import javax.annotation.processing.AbstractProcessor; 9 | import javax.annotation.processing.Processor; 10 | import javax.annotation.processing.RoundEnvironment; 11 | import javax.lang.model.SourceVersion; 12 | import javax.lang.model.element.TypeElement; 13 | import java.util.Collections; 14 | import java.util.HashSet; 15 | import java.util.Set; 16 | 17 | @AutoService(Processor.class) 18 | public class IcepickProcessor extends AbstractProcessor { 19 | 20 | private static IFn PROCESS; 21 | 22 | private static void loadClojureFn() { 23 | ClassLoader previous = Thread.currentThread().getContextClassLoader(); 24 | Thread.currentThread().setContextClassLoader(IcepickProcessor.class.getClassLoader()); 25 | try { 26 | Object ns = Clojure.read("icepick.processor"); 27 | IFn require = Clojure.var("clojure.core", "require"); 28 | require.invoke(ns); 29 | PROCESS = Clojure.var("icepick.processor", "process"); 30 | } finally { 31 | Thread.currentThread().setContextClassLoader(previous); 32 | } 33 | } 34 | 35 | static { 36 | loadClojureFn(); 37 | } 38 | 39 | 40 | @Override 41 | public boolean process(Set annotations, RoundEnvironment environment) { 42 | PROCESS.invoke(processingEnv, annotations, environment); 43 | return true; 44 | } 45 | 46 | @Override 47 | public SourceVersion getSupportedSourceVersion() { 48 | return SourceVersion.latestSupported(); 49 | } 50 | 51 | @Override 52 | public Set getSupportedAnnotationTypes() { 53 | return new HashSet(Collections.singletonList(State.class.getName())); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /icepick-processor/test/icepick/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns icepick.core-test 2 | (:require [clojure.test :refer :all] 3 | [clojure.string :as str] 4 | [clojure.java.io :as io]) 5 | (:use icepick.processor) 6 | (:import [icepick.processor IcepickProcessor] 7 | [javax.tools JavaFileObject] 8 | [com.google.testing.compile JavaFileObjects] 9 | [com.google.testing.compile JavaSourceSubjectFactory] 10 | [org.truth0 Truth])) 11 | 12 | (defn- java-source [] 13 | (JavaSourceSubjectFactory/javaSource)) 14 | 15 | (defn- icepick-processors [] 16 | [(IcepickProcessor.)]) 17 | 18 | (defn- make-source [class] 19 | (let [file (str (str/replace class #"\." "/") ".java") 20 | content (slurp (io/resource file))] 21 | (JavaFileObjects/forSourceString class content))) 22 | 23 | (defn- check-fails [input] 24 | (is (-> (Truth/ASSERT) 25 | (.about (java-source)) 26 | (.that (make-source input)) 27 | (.processedWith (icepick-processors)) 28 | (.failsToCompile)))) 29 | 30 | (defn- check-compiles 31 | ([input] 32 | (let [input-source (make-source input)] 33 | (is (-> (Truth/ASSERT) 34 | (.about (java-source)) 35 | (.that input-source) 36 | (.processedWith (icepick-processors)) 37 | (.compilesWithoutError))))) 38 | ([input output & outputs] 39 | (let [[first & rest] (seq (map make-source (cons output outputs)))] 40 | (when-let [compiles (check-compiles input)] 41 | (-> compiles 42 | (.and) 43 | (.generatesSources first (into-array JavaFileObject rest))))))) 44 | 45 | (deftest failures 46 | (testing "private field" 47 | (check-fails "test.failures.Test1")) 48 | (testing "private class" 49 | (check-fails "test.failures.Test2"))) 50 | 51 | (deftest simple 52 | (check-compiles "test.simple.Test" 53 | "test.simple.Test$$Icepick")) 54 | 55 | (deftest boxed 56 | (check-compiles "test.boxed.Test" 57 | "test.boxed.Test$$Icepick")) 58 | 59 | (deftest generics 60 | (testing "T loses type when restored" 61 | (check-fails "test.generics.Test1")) 62 | (testing "unless T is Parcelable" 63 | (check-compiles "test.generics.Test2" 64 | "test.generics.Test2$$Icepick")) 65 | (testing "collection type is inferred" 66 | (check-compiles "test.generics.Test3" 67 | "test.generics.Test3$$Icepick")) 68 | (testing "but extended collections and arrays are treated as Serializable" 69 | (check-compiles "test.generics.Test4" 70 | "test.generics.Test4$$Icepick"))) 71 | 72 | (deftest with-parent 73 | (check-compiles "test.parent.Test" 74 | "test.parent.Test$$Icepick" 75 | "test.parent.Test$Inner$$Icepick")) 76 | 77 | (deftest views 78 | (check-compiles "test.views.Test" 79 | "test.views.Test$$Icepick" 80 | "test.views.Test$Inner$$Icepick")) 81 | 82 | (deftest custom-bundler 83 | (check-compiles "test.bundler.Test" 84 | "test.bundler.Test$$Icepick")) 85 | -------------------------------------------------------------------------------- /icepick-processor/test/test/boxed/Test$$Icepick.java: -------------------------------------------------------------------------------- 1 | package test.boxed; 2 | import android.os.Bundle; 3 | import icepick.Bundler; 4 | import icepick.Injector.Helper; 5 | import icepick.Injector.Object; 6 | 7 | import java.util.Map; 8 | import java.util.HashMap; 9 | 10 | public class Test$$Icepick extends Object { 11 | 12 | private final static Map> BUNDLERS = new HashMap>(); 13 | static { 14 | } 15 | 16 | private final static Helper H = new Helper("test.boxed.Test$$Icepick.", BUNDLERS); 17 | 18 | @Override public void restore(T target, Bundle state) { 19 | if (state == null) return; 20 | target.f1 = H.getBoxedFloat(state, "f1"); 21 | super.restore(target, state); 22 | } 23 | 24 | @Override public void save(T target, Bundle state) { 25 | super.save(target, state); 26 | H.putBoxedFloat(state, "f1", target.f1); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /icepick-processor/test/test/boxed/Test.java: -------------------------------------------------------------------------------- 1 | package test.boxed; 2 | import icepick.State; 3 | public class Test { 4 | @State 5 | Float f1; 6 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/bundler/Test$$Icepick.java: -------------------------------------------------------------------------------- 1 | package test.bundler; 2 | import android.os.Bundle; 3 | import icepick.Bundler; 4 | import icepick.Injector.Helper; 5 | import icepick.Injector.Object; 6 | 7 | import java.util.Map; 8 | import java.util.HashMap; 9 | 10 | public class Test$$Icepick extends Object { 11 | 12 | private final static Map> BUNDLERS = new HashMap>(); 13 | static { 14 | BUNDLERS.put("f1", new test.bundler.Test.MyClassBundler()); 15 | BUNDLERS.put("f2", new test.bundler.Test.StringBundler()); 16 | } 17 | 18 | private final static Helper H = new Helper("test.bundler.Test$$Icepick.", BUNDLERS); 19 | 20 | @Override public void restore(T target, Bundle state) { 21 | if (state == null) return; 22 | target.f1 = H.getWithBundler(state, "f1"); 23 | target.f2 = H.getWithBundler(state, "f2"); 24 | super.restore(target, state); 25 | } 26 | 27 | @Override public void save(T target, Bundle state) { 28 | super.save(target, state); 29 | H.putWithBundler(state, "f1", target.f1); 30 | H.putWithBundler(state, "f2", target.f2); 31 | } 32 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/bundler/Test.java: -------------------------------------------------------------------------------- 1 | package test.bundler; 2 | 3 | import android.os.Bundle; 4 | import icepick.Bundler; 5 | import icepick.State; 6 | 7 | public class Test { 8 | public static class MyClass { 9 | 10 | } 11 | public static class StringBundler implements Bundler { 12 | 13 | @Override 14 | public void put(String s, String s2, Bundle bundle) { 15 | 16 | } 17 | 18 | @Override 19 | public String get(String s, Bundle bundle) { 20 | return null; 21 | } 22 | } 23 | public static class MyClassBundler implements Bundler { 24 | 25 | @Override 26 | public void put(String s, MyClass myClass, Bundle bundle) { 27 | 28 | } 29 | 30 | @Override 31 | public MyClass get(String s, Bundle bundle) { 32 | return null; 33 | } 34 | } 35 | @State(Test.MyClassBundler.class) MyClass f1; 36 | @State(Test.StringBundler.class) String f2; 37 | } 38 | -------------------------------------------------------------------------------- /icepick-processor/test/test/failures/Test1.java: -------------------------------------------------------------------------------- 1 | package test.failures; 2 | import icepick.State; 3 | 4 | public class Test1 { 5 | @State 6 | private int f1; 7 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/failures/Test2.java: -------------------------------------------------------------------------------- 1 | package test.failures; 2 | import icepick.State; 3 | 4 | public class Test2 { 5 | private class Test3 { 6 | @State 7 | public int f1; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /icepick-processor/test/test/generics/Test1.java: -------------------------------------------------------------------------------- 1 | package test.generics; 2 | 3 | import icepick.State; 4 | 5 | public class Test1 { 6 | @State T f1; 7 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/generics/Test2$$Icepick.java: -------------------------------------------------------------------------------- 1 | package test.generics; 2 | import android.os.Bundle; 3 | import icepick.Bundler; 4 | import icepick.Injector.Helper; 5 | import icepick.Injector.Object; 6 | 7 | import java.util.Map; 8 | import java.util.HashMap; 9 | 10 | public class Test2$$Icepick extends Object { 11 | 12 | private final static Map> BUNDLERS = new HashMap>(); 13 | static { 14 | } 15 | 16 | private final static Helper H = new Helper("test.generics.Test2$$Icepick.", BUNDLERS); 17 | 18 | @Override public void restore(T target, Bundle state) { 19 | if (state == null) return; 20 | target.f1 = H.getParcelable(state, "f1"); 21 | super.restore(target, state); 22 | } 23 | 24 | @Override public void save(T target, Bundle state) { 25 | super.save(target, state); 26 | H.putParcelable(state, "f1", target.f1); 27 | } 28 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/generics/Test2.java: -------------------------------------------------------------------------------- 1 | package test.generics; 2 | 3 | 4 | import icepick.State; 5 | import android.os.Parcelable; 6 | 7 | public class Test2 { 8 | @State 9 | T f1; 10 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/generics/Test3$$Icepick.java: -------------------------------------------------------------------------------- 1 | package test.generics; 2 | import android.os.Bundle; 3 | import icepick.Bundler; 4 | import icepick.Injector.Helper; 5 | import icepick.Injector.Object; 6 | 7 | import java.util.Map; 8 | import java.util.HashMap; 9 | 10 | public class Test3$$Icepick extends Object { 11 | 12 | private final static Map> BUNDLERS = new HashMap>(); 13 | static { 14 | } 15 | 16 | private final static Helper H = new Helper("test.generics.Test3$$Icepick.", BUNDLERS); 17 | 18 | @Override public void restore(T target, Bundle state) { 19 | if (state == null) return; 20 | target.f1 = H.getParcelableArrayList(state, "f1"); 21 | target.f2 = H.getSparseParcelableArray(state, "f2"); 22 | super.restore(target, state); 23 | } 24 | 25 | @Override public void save(T target, Bundle state) { 26 | super.save(target, state); 27 | H.putParcelableArrayList(state, "f1", target.f1); 28 | H.putSparseParcelableArray(state, "f2", target.f2); 29 | } 30 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/generics/Test3.java: -------------------------------------------------------------------------------- 1 | package test.generics; 2 | 3 | import icepick.State; 4 | 5 | import java.util.ArrayList; 6 | 7 | import android.util.SparseArray; 8 | import android.os.Parcelable; 9 | 10 | public class Test3 { 11 | @State ArrayList f1; 12 | @State SparseArray f2; 13 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/generics/Test4$$Icepick.java: -------------------------------------------------------------------------------- 1 | package test.generics; 2 | import android.os.Bundle; 3 | import icepick.Bundler; 4 | import icepick.Injector.Helper; 5 | import icepick.Injector.Object; 6 | 7 | import java.util.Map; 8 | import java.util.HashMap; 9 | 10 | public class Test4$$Icepick extends Object { 11 | 12 | private final static Map> BUNDLERS = new HashMap>(); 13 | static { 14 | } 15 | 16 | private final static Helper H = new Helper("test.generics.Test4$$Icepick.", BUNDLERS); 17 | 18 | @Override public void restore(T target, Bundle state) { 19 | if (state == null) return; 20 | target.f1 = H.getSerializable(state, "f1"); 21 | target.f2 = H.getSerializable(state, "f2"); 22 | target.f3 = H.getSerializable(state, "f3"); 23 | target.f4 = H.getSerializable(state, "f4"); 24 | target.f5 = H.getSerializable(state, "f5"); 25 | super.restore(target, state); 26 | } 27 | 28 | @Override public void save(T target, Bundle state) { 29 | super.save(target, state); 30 | H.putSerializable(state, "f1", target.f1); 31 | H.putSerializable(state, "f2", target.f2); 32 | H.putSerializable(state, "f3", target.f3); 33 | H.putSerializable(state, "f4", target.f4); 34 | H.putSerializable(state, "f5", target.f5); 35 | } 36 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/generics/Test4.java: -------------------------------------------------------------------------------- 1 | package test.generics; 2 | 3 | import icepick.State; 4 | import java.util.ArrayList; 5 | import android.os.Bundle; 6 | 7 | public class Test4 { 8 | static class AL extends ArrayList {} 9 | @State AL f1; 10 | @State AL f2; 11 | @State AL f3; 12 | @State Bundle[] f4; 13 | @State StringBuffer[] f5; 14 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/parent/Test$$Icepick.java: -------------------------------------------------------------------------------- 1 | package test.parent; 2 | 3 | import android.os.Bundle; 4 | import icepick.Bundler; 5 | import icepick.Injector.Helper; 6 | import icepick.Injector.Object; 7 | 8 | import java.util.Map; 9 | import java.util.HashMap; 10 | 11 | public class Test$$Icepick extends Object { 12 | 13 | private final static Map> BUNDLERS = new HashMap>(); 14 | static { 15 | } 16 | 17 | private final static Helper H = new Helper("test.parent.Test$$Icepick.", BUNDLERS); 18 | 19 | @Override public void restore(T target, Bundle state) { 20 | if (state == null) return; 21 | target.f1 = H.getParcelable(state, "f1"); 22 | super.restore(target, state); 23 | } 24 | 25 | @Override public void save(T target, Bundle state) { 26 | super.save(target, state); 27 | H.putParcelable(state, "f1", target.f1); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /icepick-processor/test/test/parent/Test$Inner$$Icepick.java: -------------------------------------------------------------------------------- 1 | package test.parent; 2 | 3 | import android.os.Bundle; 4 | import icepick.Bundler; 5 | import icepick.Injector.Helper; 6 | import icepick.Injector.Object; 7 | 8 | import java.util.Map; 9 | import java.util.HashMap; 10 | 11 | public class Test$Inner$$Icepick extends test.parent.Test$$Icepick { 12 | 13 | private final static Map> BUNDLERS = new HashMap>(); 14 | static { 15 | } 16 | 17 | private final static Helper H = new Helper("test.parent.Test$Inner$$Icepick.", BUNDLERS); 18 | 19 | @Override public void restore(T target, Bundle state) { 20 | if (state == null) return; 21 | target.f2 = H.getString(state, "f2"); 22 | super.restore(target, state); 23 | } 24 | 25 | @Override public void save(T target, Bundle state) { 26 | super.save(target, state); 27 | H.putString(state, "f2", target.f2); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /icepick-processor/test/test/parent/Test.java: -------------------------------------------------------------------------------- 1 | package test.parent; 2 | 3 | import icepick.State; 4 | import android.os.Parcelable; 5 | import android.os.Bundle; 6 | public class Test { 7 | @State T f1; 8 | static class Inner extends Test { 9 | @State String f2; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /icepick-processor/test/test/simple/Test$$Icepick.java: -------------------------------------------------------------------------------- 1 | package test.simple; 2 | 3 | import android.os.Bundle; 4 | import icepick.Bundler; 5 | import icepick.Injector.Helper; 6 | import icepick.Injector.Object; 7 | 8 | import java.util.Map; 9 | import java.util.HashMap; 10 | 11 | 12 | public class Test$$Icepick extends Object { 13 | private final static Map> BUNDLERS = new HashMap>(); 14 | static { 15 | } 16 | 17 | private final static Helper H = new Helper("test.simple.Test$$Icepick.", BUNDLERS); 18 | 19 | @Override public void restore(T target, Bundle state) { 20 | if (state == null) return; 21 | target.f1 = H.getInt(state, "f1"); 22 | target.f2 = H.getBoolean(state, "f2"); 23 | target.f3 = H.getCharArray(state, "f3"); 24 | super.restore(target, state); 25 | } 26 | 27 | @Override public void save(T target, Bundle state) { 28 | super.save(target, state); 29 | H.putInt(state, "f1", target.f1); 30 | H.putBoolean(state, "f2", target.f2); 31 | H.putCharArray(state, "f3", target.f3); 32 | } 33 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/simple/Test.java: -------------------------------------------------------------------------------- 1 | package test.simple; 2 | import icepick.State; 3 | public class Test { 4 | @State int f1; 5 | @State boolean f2; 6 | @State char[] f3; 7 | } 8 | -------------------------------------------------------------------------------- /icepick-processor/test/test/views/Test$$Icepick.java: -------------------------------------------------------------------------------- 1 | package test.views; 2 | import android.os.Bundle; 3 | import android.os.Parcelable; 4 | import icepick.Bundler; 5 | import icepick.Injector.Helper; 6 | import icepick.Injector.View; 7 | 8 | import java.util.Map; 9 | import java.util.HashMap; 10 | 11 | public class Test$$Icepick extends View { 12 | 13 | private final static Map> BUNDLERS = new HashMap>(); 14 | static { 15 | } 16 | 17 | private final static Helper H = new Helper("test.views.Test$$Icepick.", BUNDLERS); 18 | 19 | @Override public Parcelable restore(T target, Parcelable p) { 20 | Bundle state = (Bundle) p; 21 | target.f1 = H.getParcelable(state, "f1"); 22 | return super.restore(target, H.getParent(state)); 23 | } 24 | 25 | @Override public Parcelable save(T target, Parcelable p) { 26 | Bundle state = H.putParent(super.save(target, p)); 27 | H.putParcelable(state, "f1", target.f1); 28 | return state; 29 | } 30 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/views/Test$Inner$$Icepick.java: -------------------------------------------------------------------------------- 1 | package test.views; 2 | 3 | import android.os.Bundle; 4 | import android.os.Parcelable; 5 | import icepick.Bundler; 6 | import icepick.Injector.Helper; 7 | import icepick.Injector.View; 8 | 9 | import java.util.Map; 10 | import java.util.HashMap; 11 | 12 | public class Test$Inner$$Icepick extends test.views.Test$$Icepick { 13 | 14 | private final static Map> BUNDLERS = new HashMap>(); 15 | static { 16 | } 17 | 18 | private final static Helper H = new Helper("test.views.Test$Inner$$Icepick.", BUNDLERS); 19 | 20 | @Override public Parcelable restore(T target, Parcelable p) { 21 | Bundle state = (Bundle) p; 22 | target.f2 = H.getString(state, "f2"); 23 | return super.restore(target, H.getParent(state)); 24 | } 25 | 26 | @Override public Parcelable save(T target, Parcelable p) { 27 | Bundle state = H.putParent(super.save(target, p)); 28 | H.putString(state, "f2", target.f2); 29 | return state; 30 | } 31 | } -------------------------------------------------------------------------------- /icepick-processor/test/test/views/Test.java: -------------------------------------------------------------------------------- 1 | package test.views; 2 | 3 | import icepick.State; 4 | import android.view.View; 5 | import android.content.Context; 6 | import android.os.Parcelable; 7 | import android.os.Bundle; 8 | public class Test extends View { 9 | public Test(Context c) {super(c);} 10 | @State T f1; 11 | static class Inner extends Test { 12 | public Inner(Context c) {super(c);} 13 | @State String f2; 14 | } 15 | } -------------------------------------------------------------------------------- /icepick/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | -------------------------------------------------------------------------------- /icepick/project.clj: -------------------------------------------------------------------------------- 1 | (defproject frankiesardo/icepick "3.2.1-SNAPSHOT" 2 | :description "Android instance state made easy" 3 | :url "https://github.com/frankiesardo/icepick" 4 | :license {:name "Eclipse Public License -v 1.0" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :java-source-paths ["src"] 7 | :javac-options ["-target" "1.6" "-source" "1.6"] 8 | :release-tasks [["vcs" "assert-committed"] 9 | ["change" "version" "leiningen.release/bump-version" "release"] 10 | ["vcs" "commit"] 11 | ["vcs" "tag" "v"] 12 | ["deploy" "clojars"] 13 | ["change" "version" "leiningen.release/bump-version"] 14 | ["vcs" "commit"] 15 | ["vcs" "push"]] 16 | :jar-exclusions [#"project.clj"] 17 | :profiles {:provided {:dependencies [[com.google.android/android "4.1.1.4"]]} 18 | :dev {:dependencies [[org.clojure/clojure "1.6.0"]]}}) 19 | -------------------------------------------------------------------------------- /icepick/src/icepick/Bundler.java: -------------------------------------------------------------------------------- 1 | package icepick; 2 | 3 | import android.os.Bundle; 4 | 5 | public interface Bundler { 6 | 7 | void put(String key, T value, Bundle bundle); 8 | 9 | T get(String key, Bundle bundle); 10 | } -------------------------------------------------------------------------------- /icepick/src/icepick/Icepick.java: -------------------------------------------------------------------------------- 1 | package icepick; 2 | 3 | import android.os.Bundle; 4 | import android.os.Parcelable; 5 | import android.util.Log; 6 | import android.view.View; 7 | import java.util.LinkedHashMap; 8 | import java.util.Map; 9 | 10 | public class Icepick { 11 | 12 | public static final String SUFFIX = "$$Icepick"; 13 | public static final String ANDROID_PREFIX = "android."; 14 | public static final String JAVA_PREFIX = "java."; 15 | 16 | private static final String TAG = "Icepick"; 17 | 18 | private static boolean debug = false; 19 | 20 | public static void setDebug(boolean debug) { 21 | Icepick.debug = debug; 22 | } 23 | 24 | private static final Injector.Object DEFAULT_OBJECT_INJECTOR = new Injector.Object(); 25 | private static final Injector.View DEFAULT_VIEW_INJECTOR = new Injector.View(); 26 | private static final Map, Injector> INJECTORS = 27 | new LinkedHashMap, Injector>(); 28 | 29 | private static Injector getInjector(Class cls) 30 | throws IllegalAccessException, InstantiationException { 31 | if (INJECTORS.containsKey(cls)) { 32 | if (debug) Log.d(TAG, "HIT: Cached in injector map."); 33 | return INJECTORS.get(cls); 34 | } 35 | Injector injector = null; 36 | String clsName = cls.getName(); 37 | if (clsName.startsWith(ANDROID_PREFIX) || clsName.startsWith(JAVA_PREFIX)) { 38 | if (debug) Log.d(TAG, "MISS: Reached framework class. Abandoning search."); 39 | return null; 40 | } 41 | try { 42 | Class injectorClass = Class.forName(clsName + SUFFIX); 43 | injector = (Injector) injectorClass.newInstance(); 44 | if (debug) Log.d(TAG, "HIT: Class loaded injection class."); 45 | } catch (ClassNotFoundException e) { 46 | if (debug) Log.d(TAG, "Not found. Trying superclass " + cls.getSuperclass().getName()); 47 | injector = getInjector(cls.getSuperclass()); 48 | } 49 | INJECTORS.put(cls, injector); 50 | return injector; 51 | } 52 | 53 | private static T safeGet(Object target, Injector nop) { 54 | try { 55 | Class targetClass = target.getClass(); 56 | Injector injector = getInjector(targetClass); 57 | if (injector == null) { 58 | injector = nop; 59 | } 60 | return (T) injector; 61 | } catch (Exception e) { 62 | throw new RuntimeException("Unable to inject state for " + target, e); 63 | } 64 | } 65 | 66 | public static void saveInstanceState(T target, Bundle state) { 67 | Injector.Object injector = safeGet(target, DEFAULT_OBJECT_INJECTOR); 68 | injector.save(target, state); 69 | } 70 | 71 | public static void restoreInstanceState(T target, Bundle state) { 72 | Injector.Object injector = safeGet(target, DEFAULT_OBJECT_INJECTOR); 73 | injector.restore(target, state); 74 | } 75 | 76 | public static Parcelable saveInstanceState(T target, Parcelable state) { 77 | Injector.View injector = safeGet(target, DEFAULT_VIEW_INJECTOR); 78 | return injector.save(target, state); 79 | } 80 | 81 | public static Parcelable restoreInstanceState(T target, Parcelable state) { 82 | Injector.View injector = safeGet(target, DEFAULT_VIEW_INJECTOR); 83 | return injector.restore(target, state); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /icepick/src/icepick/Injector.java: -------------------------------------------------------------------------------- 1 | package icepick; 2 | 3 | import android.os.Bundle; 4 | import android.os.Parcelable; 5 | import android.util.SparseArray; 6 | 7 | import java.io.Serializable; 8 | import java.util.ArrayList; 9 | import java.util.Map; 10 | 11 | public class Injector { 12 | 13 | public static class Helper { 14 | 15 | private final String baseKey; 16 | private final Map> bundlers; 17 | 18 | public Helper(String baseKey, Map> bundlers) { 19 | this.baseKey = baseKey; 20 | this.bundlers = bundlers; 21 | } 22 | 23 | public T getWithBundler(Bundle state, String key) { 24 | Bundler b = (Bundler) bundlers.get(key); 25 | return b.get(key + baseKey, state); 26 | } 27 | 28 | public void putWithBundler(Bundle state, String key, T value) { 29 | Bundler b = (Bundler) bundlers.get(key); 30 | b.put(key + baseKey, value, state); 31 | } 32 | 33 | public boolean getBoolean(Bundle state, String key) { 34 | return state.getBoolean(key + baseKey); 35 | } 36 | 37 | public void putBoolean(Bundle state, String key, boolean x) { 38 | state.putBoolean(key + baseKey, x); 39 | } 40 | 41 | public Boolean getBoxedBoolean(Bundle state, String key) { 42 | if (state.containsKey(key + baseKey)) { 43 | return state.getBoolean(key + baseKey); 44 | } 45 | return null; 46 | } 47 | 48 | public void putBoxedBoolean(Bundle state, String key, Boolean x) { 49 | if (x != null) { 50 | state.putBoolean(key + baseKey, x); 51 | } 52 | } 53 | 54 | public boolean[] getBooleanArray(Bundle state, String key) { 55 | return state.getBooleanArray(key + baseKey); 56 | } 57 | 58 | public void putBooleanArray(Bundle state, String key, boolean[] x) { 59 | state.putBooleanArray(key + baseKey, x); 60 | } 61 | 62 | public byte getByte(Bundle state, String key) { 63 | return state.getByte(key + baseKey); 64 | } 65 | 66 | public void putByte(Bundle state, String key, byte x) { 67 | state.putByte(key + baseKey, x); 68 | } 69 | 70 | public Byte getBoxedByte(Bundle state, String key) { 71 | if (state.containsKey(key + baseKey)) { 72 | return state.getByte(key + baseKey); 73 | } 74 | return null; 75 | } 76 | 77 | public void putBoxedByte(Bundle state, String key, Byte x) { 78 | if (x != null) { 79 | state.putByte(key + baseKey, x); 80 | } 81 | } 82 | 83 | public byte[] getByteArray(Bundle state, String key) { 84 | return state.getByteArray(key + baseKey); 85 | } 86 | 87 | public void putByteArray(Bundle state, String key, byte[] x) { 88 | state.putByteArray(key + baseKey, x); 89 | } 90 | 91 | 92 | public short getShort(Bundle state, String key) { 93 | return state.getShort(key + baseKey); 94 | } 95 | 96 | public void putShort(Bundle state, String key, short x) { 97 | state.putShort(key + baseKey, x); 98 | } 99 | 100 | public Short getBoxedShort(Bundle state, String key) { 101 | if (state.containsKey(key + baseKey)) { 102 | return state.getShort(key + baseKey); 103 | } 104 | return null; 105 | } 106 | 107 | public void putBoxedShort(Bundle state, String key, Short x) { 108 | if (x != null) { 109 | state.putShort(key + baseKey, x); 110 | } 111 | } 112 | 113 | public short[] getShortArray(Bundle state, String key) { 114 | return state.getShortArray(key + baseKey); 115 | } 116 | 117 | public void putShortArray(Bundle state, String key, short[] x) { 118 | state.putShortArray(key + baseKey, x); 119 | } 120 | 121 | public int getInt(Bundle state, String key) { 122 | return state.getInt(key + baseKey); 123 | } 124 | 125 | public void putInt(Bundle state, String key, int x) { 126 | state.putInt(key + baseKey, x); 127 | } 128 | 129 | public Integer getBoxedInt(Bundle state, String key) { 130 | if (state.containsKey(key + baseKey)) { 131 | return state.getInt(key + baseKey); 132 | } 133 | return null; 134 | } 135 | 136 | public void putBoxedInt(Bundle state, String key, Integer x) { 137 | if (x != null) { 138 | state.putInt(key + baseKey, x); 139 | } 140 | } 141 | 142 | public int[] getIntArray(Bundle state, String key) { 143 | return state.getIntArray(key + baseKey); 144 | } 145 | 146 | public void putIntArray(Bundle state, String key, int[] x) { 147 | state.putIntArray(key + baseKey, x); 148 | } 149 | 150 | public long getLong(Bundle state, String key) { 151 | return state.getLong(key + baseKey); 152 | } 153 | 154 | public void putLong(Bundle state, String key, long x) { 155 | state.putLong(key + baseKey, x); 156 | } 157 | 158 | public Long getBoxedLong(Bundle state, String key) { 159 | if (state.containsKey(key + baseKey)) { 160 | return state.getLong(key + baseKey); 161 | } 162 | return null; 163 | } 164 | 165 | public void putBoxedLong(Bundle state, String key, Long x) { 166 | if (x != null) { 167 | state.putLong(key + baseKey, x); 168 | } 169 | } 170 | 171 | public long[] getLongArray(Bundle state, String key) { 172 | return state.getLongArray(key + baseKey); 173 | } 174 | 175 | public void putLongArray(Bundle state, String key, long[] x) { 176 | state.putLongArray(key + baseKey, x); 177 | } 178 | 179 | public float getFloat(Bundle state, String key) { 180 | return state.getFloat(key + baseKey); 181 | } 182 | 183 | public void putFloat(Bundle state, String key, float x) { 184 | state.putFloat(key + baseKey, x); 185 | } 186 | 187 | public Float getBoxedFloat(Bundle state, String key) { 188 | if (state.containsKey(key + baseKey)) { 189 | return state.getFloat(key + baseKey); 190 | } 191 | return null; 192 | } 193 | 194 | public void putBoxedFloat(Bundle state, String key, Float x) { 195 | if (x != null) { 196 | state.putFloat(key + baseKey, x); 197 | } 198 | } 199 | 200 | public float[] getFloatArray(Bundle state, String key) { 201 | return state.getFloatArray(key + baseKey); 202 | } 203 | 204 | public void putFloatArray(Bundle state, String key, float[] x) { 205 | state.putFloatArray(key + baseKey, x); 206 | } 207 | 208 | public double getDouble(Bundle state, String key) { 209 | return state.getDouble(key + baseKey); 210 | } 211 | 212 | public void putDouble(Bundle state, String key, double x) { 213 | state.putDouble(key + baseKey, x); 214 | } 215 | 216 | public Double getBoxedDouble(Bundle state, String key) { 217 | if (state.containsKey(key + baseKey)) { 218 | return state.getDouble(key + baseKey); 219 | } 220 | return null; 221 | } 222 | 223 | public void putBoxedDouble(Bundle state, String key, Double x) { 224 | if (x != null) { 225 | state.putDouble(key + baseKey, x); 226 | } 227 | } 228 | 229 | public double[] getDoubleArray(Bundle state, String key) { 230 | return state.getDoubleArray(key + baseKey); 231 | } 232 | 233 | public void putDoubleArray(Bundle state, String key, double[] x) { 234 | state.putDoubleArray(key + baseKey, x); 235 | } 236 | 237 | public char getChar(Bundle state, String key) { 238 | return state.getChar(key + baseKey); 239 | } 240 | 241 | public void putChar(Bundle state, String key, char x) { 242 | state.putChar(key + baseKey, x); 243 | } 244 | 245 | public Character getBoxedChar(Bundle state, String key) { 246 | if (state.containsKey(key + baseKey)) { 247 | return state.getChar(key + baseKey); 248 | } 249 | return null; 250 | } 251 | 252 | public void putBoxedChar(Bundle state, String key, Character x) { 253 | if (x != null) { 254 | state.putChar(key + baseKey, x); 255 | } 256 | } 257 | 258 | public char[] getCharArray(Bundle state, String key) { 259 | return state.getCharArray(key + baseKey); 260 | } 261 | 262 | public void putCharArray(Bundle state, String key, char[] x) { 263 | state.putCharArray(key + baseKey, x); 264 | } 265 | 266 | public String getString(Bundle state, String key) { 267 | return state.getString(key + baseKey); 268 | } 269 | 270 | public void putString(Bundle state, String key, String x) { 271 | state.putString(key + baseKey, x); 272 | } 273 | 274 | public String[] getStringArray(Bundle state, String key) { 275 | return state.getStringArray(key + baseKey); 276 | } 277 | 278 | public void putStringArray(Bundle state, String key, String[] x) { 279 | state.putStringArray(key + baseKey, x); 280 | } 281 | 282 | public CharSequence getCharSequence(Bundle state, String key) { 283 | return state.getCharSequence(key + baseKey); 284 | } 285 | 286 | public void putCharSequence(Bundle state, String key, CharSequence x) { 287 | state.putCharSequence(key + baseKey, x); 288 | } 289 | 290 | public CharSequence[] getCharSequenceArray(Bundle state, String key) { 291 | return state.getCharSequenceArray(key + baseKey); 292 | } 293 | 294 | public void putCharSequenceArray(Bundle state, String key, CharSequence[] x) { 295 | state.putCharSequenceArray(key + baseKey, x); 296 | } 297 | 298 | public Bundle getBundle(Bundle state, String key) { 299 | return state.getBundle(key + baseKey); 300 | } 301 | 302 | public void putBundle(Bundle state, String key, Bundle x) { 303 | state.putBundle(key + baseKey, x); 304 | } 305 | 306 | public T getParcelable(Bundle state, String key) { 307 | return state.getParcelable(key + baseKey); 308 | } 309 | 310 | public void putParcelable(Bundle state, String key, Parcelable x) { 311 | state.putParcelable(key + baseKey, x); 312 | } 313 | 314 | public Parcelable[] getParcelableArray(Bundle state, String key) { 315 | return state.getParcelableArray(key + baseKey); 316 | } 317 | 318 | public void putParcelableArray(Bundle state, String key, Parcelable[] x) { 319 | state.putParcelableArray(key + baseKey, x); 320 | } 321 | 322 | public T getSerializable(Bundle state, String key) { 323 | return (T) state.getSerializable(key + baseKey); 324 | } 325 | 326 | public void putSerializable(Bundle state, String key, Serializable x) { 327 | state.putSerializable(key + baseKey, x); 328 | } 329 | 330 | public ArrayList getIntegerArrayList(Bundle state, String key) { 331 | return state.getIntegerArrayList(key + baseKey); 332 | } 333 | 334 | public void putIntegerArrayList(Bundle state, String key, ArrayList x) { 335 | state.putIntegerArrayList(key + baseKey, x); 336 | } 337 | 338 | public ArrayList getStringArrayList(Bundle state, String key) { 339 | return state.getStringArrayList(key + baseKey); 340 | } 341 | 342 | public void putStringArrayList(Bundle state, String key, ArrayList x) { 343 | state.putStringArrayList(key + baseKey, x); 344 | } 345 | 346 | public ArrayList getCharSequenceArrayList(Bundle state, String key) { 347 | return state.getCharSequenceArrayList(key + baseKey); 348 | } 349 | 350 | public void putCharSequenceArrayList(Bundle state, String key, ArrayList x) { 351 | state.putCharSequenceArrayList(key + baseKey, x); 352 | } 353 | 354 | public ArrayList getParcelableArrayList(Bundle state, String key) { 355 | return state.getParcelableArrayList(key + baseKey); 356 | } 357 | 358 | public void putParcelableArrayList(Bundle state, String key, ArrayList x) { 359 | state.putParcelableArrayList(key + baseKey, x); 360 | } 361 | 362 | public SparseArray getSparseParcelableArray(Bundle state, String key) { 363 | return state.getSparseParcelableArray(key + baseKey); 364 | } 365 | 366 | public void putSparseParcelableArray(Bundle state, String key, SparseArray x) { 367 | state.putSparseParcelableArray(key + baseKey, x); 368 | } 369 | 370 | public Parcelable getParent(Bundle state) { 371 | return state.getParcelable(baseKey + "$$SUPER"); 372 | } 373 | 374 | public Bundle putParent(Parcelable superState) { 375 | Bundle state = new Bundle(); 376 | state.putParcelable(baseKey + "$$SUPER", superState); 377 | return state; 378 | } 379 | } 380 | 381 | public static class Object extends Injector { 382 | 383 | public void restore(T target, Bundle state) { 384 | } 385 | 386 | public void save(T target, Bundle state) { 387 | } 388 | } 389 | 390 | public static class View extends Injector { 391 | 392 | public Parcelable restore(T target, Parcelable state) { 393 | return state; 394 | } 395 | 396 | public Parcelable save(T target, Parcelable state) { 397 | return state; 398 | } 399 | } 400 | } 401 | -------------------------------------------------------------------------------- /icepick/src/icepick/State.java: -------------------------------------------------------------------------------- 1 | package icepick; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.FIELD) 9 | @Retention(RetentionPolicy.CLASS) 10 | public @interface State { 11 | Class value() default Bundler.class; 12 | } 13 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject com.github.frankiesardo/icepick-parent "3.2.1-SNAPSHOT" 2 | :license {:name "Eclipse Public License -v 1.0" 3 | :url "http://www.eclipse.org/legal/epl-v10.html"} 4 | :plugins [[lein-sub "0.3.0"]] 5 | :sub ["icepick" "icepick-processor"]) 6 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /.gradle 2 | /*/build 3 | -------------------------------------------------------------------------------- /sample/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion project.ext.compileSdkVersion 5 | buildToolsVersion project.ext.buildToolsVersion 6 | 7 | defaultConfig { 8 | minSdkVersion project.ext.minSdkVersion 9 | targetSdkVersion project.ext.targetSdkVersion 10 | } 11 | 12 | lintOptions { 13 | abortOnError false 14 | } 15 | } 16 | 17 | dependencies { 18 | compile "frankiesardo:icepick:${icepickVersion}" 19 | provided "frankiesardo:icepick-processor:${icepickVersion}" 20 | compile "org.parceler:parceler-api:${parcelerVersion}" 21 | annotationProcessor "org.parceler:parceler:${parcelerVersion}" 22 | compile project(':lib') 23 | } 24 | -------------------------------------------------------------------------------- /sample/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /sample/app/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiesardo/icepick/7a3fb08f8ef00cc5622c5ca41fdf711f888b633f/sample/app/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /sample/app/src/main/java/com/github/frankiesardo/icepick/CustomView.java: -------------------------------------------------------------------------------- 1 | package com.github.frankiesardo.icepick; 2 | 3 | import android.content.Context; 4 | import android.os.Parcelable; 5 | import android.util.AttributeSet; 6 | import com.sample.icepick.lib.BaseCustomView; 7 | import icepick.State; 8 | 9 | public class CustomView extends BaseCustomView { 10 | 11 | @State Integer textColor; 12 | 13 | public CustomView(Context context) { 14 | super(context); 15 | } 16 | 17 | public CustomView(Context context, AttributeSet attrs) { 18 | super(context, attrs); 19 | } 20 | 21 | public CustomView(Context context, AttributeSet attrs, int defStyle) { 22 | super(context, attrs, defStyle); 23 | } 24 | 25 | public void setTextColorWithAnotherMethod(int color) { 26 | this.textColor = color; 27 | setTextColor(textColor); 28 | } 29 | 30 | @Override 31 | public void onRestoreInstanceState(Parcelable state) { 32 | super.onRestoreInstanceState(state); 33 | if (textColor != null) { 34 | setTextColorWithAnotherMethod(textColor); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /sample/app/src/main/java/com/github/frankiesardo/icepick/Example.java: -------------------------------------------------------------------------------- 1 | package com.github.frankiesardo.icepick; 2 | 3 | import org.parceler.Parcel; 4 | 5 | @Parcel 6 | public class Example { 7 | String name; 8 | int age; 9 | 10 | public Example(){ /*Required empty bean constructor*/ } 11 | 12 | public Example(int age, String name) { 13 | this.age = age; 14 | this.name = name; 15 | } 16 | 17 | public String getName() { return name; } 18 | 19 | public int getAge() { return age; } 20 | } -------------------------------------------------------------------------------- /sample/app/src/main/java/com/github/frankiesardo/icepick/ExampleBundler.java: -------------------------------------------------------------------------------- 1 | package com.github.frankiesardo.icepick; 2 | 3 | import android.os.Bundle; 4 | 5 | import org.parceler.Parcels; 6 | 7 | import icepick.Bundler; 8 | 9 | public class ExampleBundler implements Bundler { 10 | @Override 11 | public void put(String s, Object example, Bundle bundle) { 12 | bundle.putParcelable(s, Parcels.wrap(example)); 13 | } 14 | 15 | @Override 16 | public Object get(String s, Bundle bundle) { 17 | return Parcels.unwrap(bundle.getParcelable(s)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /sample/app/src/main/java/com/github/frankiesardo/icepick/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.frankiesardo.icepick; 2 | 3 | import android.graphics.Color; 4 | import android.os.Bundle; 5 | import android.view.Menu; 6 | import android.view.MenuItem; 7 | import com.sample.icepick.lib.BaseActivity; 8 | import icepick.State; 9 | 10 | public class MainActivity extends BaseActivity { 11 | 12 | @State(MyBundler.class) String message; 13 | 14 | CustomView customView; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | customView = (CustomView) findViewById(R.id.custom_view); 21 | updateText(); 22 | } 23 | 24 | private void updateText() { 25 | String defaultText = message == null || baseMessage == null ? "Use the menu to add some state" 26 | : baseMessage + message; 27 | customView.setText(defaultText); 28 | } 29 | 30 | @Override 31 | public boolean onCreateOptionsMenu(Menu menu) { 32 | getMenuInflater().inflate(R.menu.main, menu); 33 | return super.onPrepareOptionsMenu(menu); 34 | } 35 | 36 | @Override 37 | public boolean onMenuItemSelected(int featureId, MenuItem item) { 38 | if (item.getItemId() == R.id.action_add_state) { 39 | customView.setBackgroundColorWithAnotherMethod(Color.BLUE); 40 | customView.setTextColorWithAnotherMethod(Color.WHITE); 41 | 42 | baseMessage = "This state will be automagically "; 43 | message = "saved and restored"; 44 | updateText(); 45 | return true; 46 | } 47 | return super.onMenuItemSelected(featureId, item); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /sample/app/src/main/java/com/github/frankiesardo/icepick/MyBundler.java: -------------------------------------------------------------------------------- 1 | package com.github.frankiesardo.icepick; 2 | 3 | import android.os.Bundle; 4 | 5 | import icepick.Bundler; 6 | 7 | class MyBundler implements Bundler { 8 | @Override 9 | public void put(String key, String value, Bundle bundle) { 10 | bundle.putString(key, value + "*"); 11 | } 12 | 13 | @Override 14 | public String get(String key, Bundle bundle) { 15 | return bundle.getString(key); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /sample/app/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiesardo/icepick/7a3fb08f8ef00cc5622c5ca41fdf711f888b633f/sample/app/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiesardo/icepick/7a3fb08f8ef00cc5622c5ca41fdf711f888b633f/sample/app/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiesardo/icepick/7a3fb08f8ef00cc5622c5ca41fdf711f888b633f/sample/app/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiesardo/icepick/7a3fb08f8ef00cc5622c5ca41fdf711f888b633f/sample/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /sample/app/src/main/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | -------------------------------------------------------------------------------- /sample/app/src/main/res/values-sw720dp-land/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 128dp 5 | 6 | -------------------------------------------------------------------------------- /sample/app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /sample/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Icepick 5 | 6 | Add some state 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /sample/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | subprojects { 2 | buildscript { 3 | repositories { 4 | jcenter() 5 | } 6 | dependencies { 7 | classpath 'com.android.tools.build:gradle:2.3.0' 8 | } 9 | project.ext { 10 | buildToolsVersion = "25.0.2" 11 | compileSdkVersion = 25 12 | targetSdkVersion = 25 13 | minSdkVersion = 14 14 | } 15 | } 16 | 17 | repositories { 18 | mavenCentral() 19 | maven { 20 | url "https://clojars.org/repo/" 21 | } 22 | mavenLocal() 23 | } 24 | } 25 | 26 | ext { 27 | icepickVersion = '3.2.0-SNAPSHOT' 28 | parcelerVersion = '1.1.6' 29 | } -------------------------------------------------------------------------------- /sample/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiesardo/icepick/7a3fb08f8ef00cc5622c5ca41fdf711f888b633f/sample/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /sample/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Mar 08 09:17:02 GMT 2017 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-3.3-all.zip 7 | -------------------------------------------------------------------------------- /sample/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 | -------------------------------------------------------------------------------- /sample/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 | -------------------------------------------------------------------------------- /sample/lib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion project.ext.compileSdkVersion 5 | buildToolsVersion project.ext.buildToolsVersion 6 | 7 | defaultConfig { 8 | minSdkVersion project.ext.minSdkVersion 9 | targetSdkVersion project.ext.targetSdkVersion 10 | } 11 | 12 | lintOptions { 13 | abortOnError false 14 | } 15 | } 16 | 17 | dependencies { 18 | compile "frankiesardo:icepick:${icepickVersion}" 19 | provided "frankiesardo:icepick-processor:${icepickVersion}" 20 | } 21 | -------------------------------------------------------------------------------- /sample/lib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /sample/lib/src/main/java/com/sample/icepick/lib/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.sample.icepick.lib; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import icepick.Icepick; 6 | import icepick.State; 7 | 8 | public class BaseActivity extends Activity { 9 | @State protected String baseMessage; 10 | 11 | @Override 12 | protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | Icepick.restoreInstanceState(this, savedInstanceState); 15 | } 16 | 17 | @Override 18 | protected void onSaveInstanceState(Bundle outState) { 19 | super.onSaveInstanceState(outState); 20 | Icepick.saveInstanceState(this, outState); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /sample/lib/src/main/java/com/sample/icepick/lib/BaseCustomView.java: -------------------------------------------------------------------------------- 1 | package com.sample.icepick.lib; 2 | 3 | import android.content.Context; 4 | import android.os.Parcelable; 5 | import android.util.AttributeSet; 6 | import android.widget.TextView; 7 | import icepick.Icepick; 8 | import icepick.State; 9 | 10 | public class BaseCustomView extends TextView { 11 | 12 | @State protected Integer backgroundColor; 13 | 14 | public BaseCustomView(Context context) { 15 | super(context); 16 | } 17 | 18 | public BaseCustomView(Context context, AttributeSet attrs) { 19 | super(context, attrs); 20 | } 21 | 22 | public BaseCustomView(Context context, AttributeSet attrs, int defStyle) { 23 | super(context, attrs, defStyle); 24 | } 25 | 26 | public void setBackgroundColorWithAnotherMethod(int color) { 27 | this.backgroundColor = color; 28 | setBackgroundColor(color); 29 | } 30 | 31 | @Override 32 | public Parcelable onSaveInstanceState() { 33 | return Icepick.saveInstanceState(this, super.onSaveInstanceState()); 34 | } 35 | 36 | @Override 37 | public void onRestoreInstanceState(Parcelable state) { 38 | super.onRestoreInstanceState(Icepick.restoreInstanceState(this, state)); 39 | if (backgroundColor != null) { 40 | setBackgroundColorWithAnotherMethod(backgroundColor); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /sample/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':lib', ':app' 2 | --------------------------------------------------------------------------------