├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── compiler ├── .gitignore ├── build.gradle └── src │ └── main │ ├── java │ └── com │ │ └── baoyz │ │ └── pg │ │ ├── PGProcessor.java │ │ ├── ProxyInfo.java │ │ └── ProxyTemplate.java │ └── resources │ └── META-INF │ └── services │ └── javax.annotation.processing.Processor ├── core ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── baoyz │ └── pg │ ├── PG.java │ ├── PGUtils.java │ ├── ParcelIgnore.java │ ├── ParcelInfo.java │ ├── ParcelProxyInfo.java │ ├── ParcelUtil.java │ └── Parcelable.java ├── demo ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── baoyz │ │ └── parcelablegenerator │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── baoyz │ │ └── parcelablegenerator │ │ ├── MainActivity.java │ │ ├── ShowClassroomActivity.java │ │ ├── ShowUserActivity.java │ │ └── model │ │ ├── Classroom.java │ │ ├── Person.java │ │ ├── Student.java │ │ ├── Teacher.java │ │ └── User.java │ └── res │ ├── layout │ ├── activity_main.xml │ └── activity_show.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── gradle └── wrapper │ └── gradle-wrapper.jar ├── gradlew ├── gradlew.bat ├── pg-2.0.1.jar ├── pg-2.0.2.jar └── settings.gradle /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Specific files to exclude 2 | com_crashlytics_export_strings.xml 3 | 4 | # Any keystores 5 | *.keystore 6 | 7 | ### Android 8 | ########### 9 | # built application files 10 | *.apk 11 | *.ap_ 12 | 13 | # files for the dex VM 14 | *.dex 15 | 16 | # Java class files 17 | *.class 18 | 19 | # generated files 20 | bin/ 21 | gen/ 22 | 23 | # Local configuration file (sdk path, etc) 24 | *.properties 25 | 26 | ### Linux 27 | ######### 28 | !.gitignore 29 | *~ 30 | 31 | ### Windows 32 | ############ 33 | # Windows image file caches 34 | Thumbs.db 35 | ehthumbs.db 36 | 37 | # Folder config file 38 | Desktop.ini 39 | 40 | # Recycle Bin used on file shares 41 | $RECYCLE.BIN/ 42 | 43 | ### IntelliJ 44 | *.iml 45 | *.ipr 46 | *.iws 47 | .idea/ 48 | 49 | ### Gradle 50 | .gradle/ 51 | build/ 52 | out/ 53 | 54 | #Maven 55 | target 56 | release.properties 57 | pom.xml.* 58 | 59 | ### AppEngine 60 | google_generated/ 61 | datanucleus.log 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 baoyongzhang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ParcelableGenerator 2 | =================== 3 | [ ![Download](https://api.bintray.com/packages/baoyongzhang/maven/ParcelableGenerator/images/download.svg) ](https://bintray.com/baoyongzhang/maven/ParcelableGenerator/_latestVersion) 4 | ## 介绍 5 | ParcelableGenerator可以将任意对象转换为Parcelable类型,方便对象传输。 6 | 7 | 在Android中,对象的序列化一般有两种方式,一种是Serializable,一种是Parcelable。 8 | 9 | * Serializable 在Java中就存在,效率较低。 10 | * Parcelable 是Android中提供的,也是官方推荐的方式,效率比Serializable高很多。 11 | 12 | 虽然Parcelable效率高,但是使用起来比Serializable麻烦很多,很多人不使用Parcelable就是因为写法太麻烦,尤其是属性特别多的时候,我们要将每个属性Parcel.write()然后在Parcel.read()回来,相当繁琐,不如Serializable简单粗暴,直接有效。 13 | 14 | ParcelableGenerator可以解决Parcelable使用麻烦的问题,让使用Parcelable的简单性可以和使用Serializable相媲美。 15 | 16 | 17 | ## 使用方法 18 | 19 | 例如我们有一个User类,用来保存用户的一些信息,我们需要使用@Parcelable修饰该类,注意@Parcelable修饰的类必须有公有无参构造方法。 20 | 21 | ```java 22 | import com.baoyz.pg.Parcelable; 23 | 24 | @Parcelable 25 | public class User { 26 | 27 | private String name; 28 | private int age; 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | 38 | public int getAge() { 39 | return age; 40 | } 41 | 42 | public void setAge(int age) { 43 | this.age = age; 44 | } 45 | 46 | } 47 | ``` 48 | 49 | 我们要将一个User对象通过Intent传递给一个Activity叫做ShowUserActivity。我们需要调用Intent.putExtra()方法将对象传入,这时候直接传递肯定是不行的,我们需要调用PG.createParcelable()方法将对象转换为Parcelable在传入Intent中。 50 | 51 | ```java 52 | import com.baoyz.pg.PG; 53 | 54 | // 模拟创建对象,并设置属性值 55 | User user = new User(); 56 | user.setName("zhangsan"); 57 | user.setAge(18); 58 | 59 | Intent intent = new Intent(this, ShowUserActivity.class); 60 | // 调用PG将对象转换成Parcelable,传入Intent中 61 | intent.putExtra("user", PG.convertParcelable(user)); 62 | startActivity(intent); 63 | ``` 64 | 65 | 在ShowUserActivity中获取User对象,无需写任何转换的代码,直接getIntent().getParcelableExtra()赋值给原对象类型变量即可。 66 | 67 | ```java 68 | public class ShowUserActivity extends Activity { 69 | 70 | @Override 71 | protected void onCreate(Bundle savedInstanceState) { 72 | super.onCreate(savedInstanceState); 73 | 74 | // 直接获取原对象类型 75 | User user = getIntent().getParcelableExtra("user"); 76 | 77 | // 获取属性值 78 | user.getName(); 79 | user.getAge(); 80 | 81 | } 82 | 83 | } 84 | ``` 85 | 86 | #### 对于继承: `@Parcelable` 自动作用于被继承的类, 子类需修饰, 但父类无需修饰. 比如: 87 | 88 | ```java 89 | public class Base { 90 | private String str; 91 | 92 | public String getStr() { 93 | return str; 94 | } 95 | 96 | public void setStr(String str) { 97 | this.str = str; 98 | } 99 | } 100 | ``` 101 | 102 | 注意, 上面的 `Base` 是没有修饰的 POD. 103 | 104 | ```java 105 | @Parcelable 106 | public class Child extends Base { 107 | private int i; 108 | 109 | public int getI() { 110 | return i; 111 | } 112 | 113 | public voiid setI(int i) { 114 | this.i = i; 115 | } 116 | } 117 | ``` 118 | 119 | 注意, 子类 `Child` 是被 `@Parcelable` 修饰的. 120 | 121 | 此时, 我们有如下代码: 122 | 123 | ```java 124 | Intent intent = new Intent(this, MainActivity.class); 125 | Child child = new Child(); 126 | child.setStr("child"); // 基类成员 127 | child.setI(1234); // 子类成员 128 | intent.putExtra("bean", PG.convertParcelable(child)); 129 | ``` 130 | 基类 `str` 和子类 `i` 两个字段均可被 Parcel. 131 | #### 对于组合: 需要被组合的对象需 `@Parcelable` 修饰. 例如: 132 | ```java 133 | @Parcelable 134 | public class X { 135 | private String str; 136 | 137 | public String getStr() { 138 | return str; 139 | } 140 | 141 | public void setStr(String str) { 142 | this.str = str; 143 | } 144 | } 145 | ``` 146 | 147 | ```java 148 | @Parcelable 149 | public class Y { 150 | private X x; 151 | 152 | public X getX() { 153 | return x; 154 | } 155 | 156 | public void setX(X x) { 157 | this.x = x; 158 | } 159 | } 160 | ``` 161 | 162 | 注意, `X` 和 `Y` 都需要 `@Parcelable` 修饰. 163 | 164 |
165 | 166 | ## 更新介绍 167 | 168 | #### Version 2.0 169 | 170 | * 修复BUG,使用基本数据类型包装类会出现问题等。 171 | * 增加 @ParcelIgnore 注解,修饰在Model的Field上面,可以忽略该字段不进行序列化。 172 | * 使用更加方便,当Model中的属性是其他对象,或者List中包含其他对象,该对象的类用 @Parcelable 声明之后无需加转换代码。 173 | 174 | ```java 175 | // 当传递对象的属性包含其他对象,或者是List,而该对象或List中的对象不支持序列化,那么直接传递将会出现null 176 | // 解决办法,将不支持序列化的类用@Parcelable修饰 177 | // 例如一个教室对象 178 | Classroom room = new Classroom(); 179 | // 教室中包含一个老师,Teacher类用@Parcelable修饰 180 | Teacher teacher = new Teacher("teacherName"); 181 | // 将老师对象直接赋值给教室 182 | room.setTeacher(teacher); 183 | // 再例如,教室中包含很多学生,使用List保存,Student类用@Parcelable修饰 184 | List students = new ArrayList(); 185 | // 直接创建Student对象添加到List中 186 | students.add(new Student("stu1")); 187 | students.add(new Student("stu2")); 188 | students.add(new Student("stu3")); 189 | room.setStudents(students); 190 | // 传递教室对象,调用转换方法,此时内部会自动将Teacher、和List中的Student对象转为Parcelable类型并传递 191 | intent.putExtra("classroom", PG.convertParcelable(room)); 192 | ``` 193 | 194 | #### Version 1.1 195 | 196 | * Sample程序表达更加清楚。 197 | * 修改方法名createParcelable()为convertParcelable(),原方法@Deprecated 不影响原有代码。 198 | * 增加PG.convert(Object)方法,与createParcelable()功能类似,只是返回值不同,convertParcelable()返回Parcelable类型,convert()返回类型与传入的对象类型一致,只是该对象已经支持序列化。 199 | 200 | #### Version 1.0 201 | 202 | * 将任意对象转换为Parcelable类型 203 | 204 | 205 | ## 在你的项目中使用 206 | 207 | ### Gradle 208 | 209 | ``` 210 | dependencies { 211 | provided 'com.baoyz.pg:compiler:2.1.1' 212 | compile 'com.baoyz.pg:core:2.1.1' 213 | } 214 | ``` 215 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | maven { 7 | url "https://jcenter.bintray.com" 8 | } 9 | } 10 | dependencies { 11 | classpath 'com.android.tools.build:gradle:1.2.3' 12 | 13 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1' 14 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' 15 | // NOTE: Do not place your application dependencies here; they belong 16 | // in the individual module build.gradle files 17 | } 18 | } 19 | 20 | allprojects { 21 | repositories { 22 | jcenter() 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /compiler/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /compiler/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | dependencies { 4 | compile fileTree(dir: 'libs', include: ['*.jar']) 5 | compile project(':core') 6 | } 7 | 8 | apply plugin: 'com.jfrog.bintray' 9 | apply plugin: 'maven-publish' 10 | apply plugin: 'maven' 11 | version = "2.1.1" 12 | 13 | def siteUrl = 'https://github.com/baoyongzhang/ParcelableGenerator' 14 | def gitUrl = 'https://github.com/baoyongzhang/ParcelableGenerator.git' 15 | group = "com.baoyz.pg" 16 | publishing { 17 | publications { 18 | mavenJava(MavenPublication) { 19 | 20 | artifact sourcesJar 21 | artifact javadocJar 22 | } 23 | } 24 | } 25 | task sourcesJar(type: Jar, dependsOn: classes) { 26 | classifier = 'sources' 27 | from sourceSets.main.allSource 28 | } 29 | task javadocJar(type: Jar, dependsOn: javadoc) { 30 | classifier = 'javadoc' 31 | from javadoc.destinationDir 32 | } 33 | install { 34 | repositories.mavenInstaller { 35 | pom { 36 | project { 37 | packaging 'jar' 38 | name 'ParcelableGenerator' //项目描述 39 | url siteUrl 40 | licenses { 41 | license { 42 | name 'The MIT License (MIT)' 43 | url 'http://baoyz.com/licenses/LICENSE.txt' 44 | } 45 | } 46 | developers { 47 | developer { 48 | id 'baoyongzhang' //填写的一些基本信息 49 | name 'baoyongzhang' 50 | email 'baoyz94@gmail.com' 51 | } 52 | } 53 | scm { 54 | connection gitUrl 55 | developerConnection gitUrl 56 | url siteUrl 57 | } 58 | } 59 | } 60 | } 61 | } 62 | artifacts { 63 | archives javadocJar 64 | archives sourcesJar 65 | } 66 | publishing { 67 | publications { 68 | mavenJava(MavenPublication) { 69 | if (plugins.hasPlugin('war')) { 70 | from components.web 71 | } else { 72 | from components.java 73 | } 74 | 75 | artifact sourcesJar 76 | artifact javadocJar 77 | } 78 | } 79 | } 80 | Properties properties = new Properties() 81 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 82 | bintray { 83 | user = properties.getProperty("bintray.user") 84 | key = properties.getProperty("bintray.apikey") 85 | configurations = ['archives'] 86 | pkg { 87 | repo = "maven" 88 | name = "ParcelableGenerator" //发布到JCenter上的项目名字 89 | websiteUrl = siteUrl 90 | vcsUrl = gitUrl 91 | licenses = ["MIT"] 92 | publish = true 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /compiler/src/main/java/com/baoyz/pg/PGProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015 baoyongzhang 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.baoyz.pg; 25 | 26 | import java.io.Writer; 27 | import java.util.Set; 28 | 29 | import javax.annotation.processing.AbstractProcessor; 30 | import javax.annotation.processing.Filer; 31 | import javax.annotation.processing.ProcessingEnvironment; 32 | import javax.annotation.processing.RoundEnvironment; 33 | import javax.annotation.processing.SupportedAnnotationTypes; 34 | import javax.annotation.processing.SupportedSourceVersion; 35 | import javax.lang.model.SourceVersion; 36 | import javax.lang.model.element.Element; 37 | import javax.lang.model.element.TypeElement; 38 | import javax.tools.JavaFileObject; 39 | 40 | /** 41 | * ParcelableGenerator 42 | * Created by baoyz on 15/6/24. 43 | */ 44 | 45 | @SupportedSourceVersion(SourceVersion.RELEASE_6) 46 | @SupportedAnnotationTypes("com.baoyz.pg.Parcelable") 47 | public class PGProcessor extends AbstractProcessor{ 48 | 49 | private Filer filer; 50 | 51 | @Override 52 | public synchronized void init(ProcessingEnvironment env) { 53 | super.init(env); 54 | 55 | filer = env.getFiler(); 56 | } 57 | 58 | @Override 59 | public boolean process(Set annotations, 60 | RoundEnvironment roundEnv) { 61 | try { 62 | Set set = roundEnv 63 | .getElementsAnnotatedWith(Parcelable.class); 64 | for (Element element : set) { 65 | try { 66 | TypeElement enclosingElement = (TypeElement) element; 67 | ProxyInfo pi = new ProxyInfo(enclosingElement 68 | .getQualifiedName().toString()); 69 | writeLog(pi.getFullName()); 70 | JavaFileObject jfo = filer.createSourceFile( 71 | pi.getFullName(), enclosingElement); 72 | Writer writer = jfo.openWriter(); 73 | writeLog(pi.createCode()); 74 | writer.write(pi.createCode()); 75 | writer.flush(); 76 | writer.close(); 77 | writeLog("ok"); 78 | } catch (Exception e) { 79 | e.printStackTrace(); 80 | writeLog(e.getMessage()); 81 | } 82 | } 83 | } catch (Exception e) { 84 | e.printStackTrace(); 85 | writeLog(e.getMessage()); 86 | } 87 | return true; 88 | } 89 | 90 | private void writeLog(String str) { 91 | // try { 92 | // FileWriter fw = new FileWriter(new File("D:/process.txt"), true); 93 | // fw.write(str + "\n"); 94 | // fw.close(); 95 | // } catch (IOException e) { 96 | // e.printStackTrace(); 97 | // } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /compiler/src/main/java/com/baoyz/pg/ProxyInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015 baoyongzhang 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.baoyz.pg; 25 | 26 | /** 27 | * ParcelableGenerator 28 | * Created by baoyz on 15/6/24. 29 | */ 30 | public class ProxyInfo { 31 | 32 | private static final String SUFFIX = "$$Parcelable"; 33 | 34 | private String packageName; 35 | private String proxyName; 36 | private String className; 37 | 38 | public ProxyInfo(String qualifiedName) { 39 | super(); 40 | packageName = qualifiedName 41 | .substring(0, qualifiedName.lastIndexOf(".")); 42 | className = qualifiedName.substring(packageName.length() + 1); 43 | proxyName = className + SUFFIX; 44 | } 45 | 46 | public String createCode() { 47 | return ProxyTemplate.createCode(packageName, proxyName, className); 48 | } 49 | 50 | public String getFullName() { 51 | return packageName + "." + proxyName; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /compiler/src/main/java/com/baoyz/pg/ProxyTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015 baoyongzhang 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.baoyz.pg; 25 | 26 | /** 27 | * ParcelableGenerator 28 | * Created by baoyz on 15/6/24. 29 | */ 30 | public class ProxyTemplate { 31 | 32 | private static final String PACKAGE = "%package"; 33 | private static final String PROXY = "%proxy"; 34 | private static final String CLASS = "%class"; 35 | 36 | public static String createCode(String packageName, String proxyName, 37 | String className) { 38 | return getTemplate().replace(PACKAGE, packageName) 39 | .replace(PROXY, proxyName).replace(CLASS, className); 40 | } 41 | 42 | public static String getTemplate() { 43 | return template; 44 | } 45 | 46 | private static String template = "package %package;" 47 | + "" 48 | + "import android.os.Parcel;" 49 | + "import android.os.Parcelable;" 50 | + "import com.baoyz.pg.PGUtils;" 51 | + "" 52 | + "public class %proxy extends %class implements Parcelable {" 53 | + "" 54 | + " public %proxy(%class user) {" 55 | + " PGUtils.clone(user, this);" 56 | + " }" 57 | + "" 58 | + " public %proxy(Parcel source) {" 59 | + " PGUtils.read(this, source);" 60 | + " }" 61 | + "" 62 | + " @Override" 63 | + " public int describeContents() {" 64 | + " return 0;" 65 | + " }" 66 | + "" 67 | + " @Override" 68 | + " public void writeToParcel(Parcel dest, int flags) {" 69 | + " PGUtils.write(this, dest);" 70 | + " }" 71 | + "" 72 | + " public static final Parcelable.Creator<%proxy> CREATOR = new Parcelable.Creator<%proxy>() {" 73 | + " @Override" 74 | + " public %proxy createFromParcel(Parcel source) {" 75 | + " return new %proxy(source);" + " }" + "" + " @Override" 76 | + " public %proxy[] newArray(int size) {" 77 | + " return new %proxy[size];" + " }" + " };" + "}"; 78 | } 79 | -------------------------------------------------------------------------------- /compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor: -------------------------------------------------------------------------------- 1 | com.baoyz.pg.PGProcessor -------------------------------------------------------------------------------- /core/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /core/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'idea' 3 | 4 | configurations { 5 | provided 6 | } 7 | 8 | sourceSets.main.compileClasspath += [configurations.provided] 9 | 10 | idea { 11 | module{ 12 | scopes.PROVIDED.plus += [configurations.provided] 13 | } 14 | } 15 | dependencies { 16 | compile fileTree(dir: 'libs', include: ['*.jar']) 17 | provided 'com.google.android:android:+' 18 | } 19 | 20 | 21 | apply plugin: 'com.jfrog.bintray' 22 | apply plugin: 'maven-publish' 23 | apply plugin: 'maven' 24 | version = "2.1.1" 25 | 26 | def siteUrl = 'https://github.com/baoyongzhang/ParcelableGenerator' 27 | def gitUrl = 'https://github.com/baoyongzhang/ParcelableGenerator.git' 28 | group = "com.baoyz.pg" 29 | publishing { 30 | publications { 31 | mavenJava(MavenPublication) { 32 | 33 | artifact sourcesJar 34 | artifact javadocJar 35 | } 36 | } 37 | } 38 | task sourcesJar(type: Jar, dependsOn: classes) { 39 | classifier = 'sources' 40 | from sourceSets.main.allSource 41 | } 42 | task javadocJar(type: Jar, dependsOn: javadoc) { 43 | classifier = 'javadoc' 44 | from javadoc.destinationDir 45 | } 46 | install { 47 | repositories.mavenInstaller { 48 | pom { 49 | project { 50 | packaging 'jar' 51 | name 'ParcelableGenerator' 52 | url siteUrl 53 | licenses { 54 | license { 55 | name 'The MIT License (MIT)' 56 | url 'http://baoyz.com/licenses/LICENSE.txt' 57 | } 58 | } 59 | developers { 60 | developer { 61 | id 'baoyongzhang' 62 | name 'baoyongzhang' 63 | email 'baoyz94@gmail.com' 64 | } 65 | } 66 | scm { 67 | connection gitUrl 68 | developerConnection gitUrl 69 | url siteUrl 70 | } 71 | } 72 | } 73 | } 74 | } 75 | artifacts { 76 | archives javadocJar 77 | archives sourcesJar 78 | } 79 | publishing { 80 | publications { 81 | mavenJava(MavenPublication) { 82 | if (plugins.hasPlugin('war')) { 83 | from components.web 84 | } else { 85 | from components.java 86 | } 87 | 88 | artifact sourcesJar 89 | artifact javadocJar 90 | } 91 | } 92 | } 93 | Properties properties = new Properties() 94 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 95 | bintray { 96 | user = properties.getProperty("bintray.user") 97 | key = properties.getProperty("bintray.apikey") 98 | configurations = ['archives'] 99 | pkg { 100 | repo = "maven" 101 | name = "ParcelableGenerator" 102 | websiteUrl = siteUrl 103 | vcsUrl = gitUrl 104 | licenses = ["MIT"] 105 | publish = true 106 | } 107 | } -------------------------------------------------------------------------------- /core/src/main/java/com/baoyz/pg/PG.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015 baoyongzhang 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.baoyz.pg; 25 | 26 | import android.os.Parcelable; 27 | 28 | import java.lang.reflect.Constructor; 29 | 30 | /** 31 | * ParcelableGenerator 32 | * Created by baoyz on 15/6/24. 33 | */ 34 | public class PG { 35 | 36 | /** 37 | * 38 | * {@link #convertParcelable(Object obj)}. 39 | * 40 | * @param obj 41 | * @return 42 | */ 43 | @Deprecated 44 | public static Parcelable createParcelable(Object obj) { 45 | return convertParcelable(obj); 46 | } 47 | 48 | /** 49 | * 50 | * Object2Parcelable 51 | * 52 | * @param obj 53 | * @return 54 | */ 55 | public static Parcelable convertParcelable(Object obj) { 56 | if (obj instanceof Parcelable) { 57 | return (Parcelable) obj; 58 | } 59 | ParcelProxyInfo pi = new ParcelProxyInfo(obj.getClass().getCanonicalName()); 60 | try { 61 | Class clazz = Class.forName(pi.getFullName()); 62 | Constructor constructor = clazz.getConstructor(obj.getClass()); 63 | return (Parcelable) constructor.newInstance(obj); 64 | } catch (Exception e) { 65 | } 66 | return null; 67 | } 68 | 69 | /** 70 | * Object2Parcelable 71 | * 72 | * @param obj 73 | * @return 74 | */ 75 | public static T convert(T obj) { 76 | return (T) convertParcelable(obj); 77 | } 78 | 79 | public static T unconvert(Parcelable parcel) { 80 | ParcelInfo info = new ParcelInfo(parcel); 81 | return info.getSource(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /core/src/main/java/com/baoyz/pg/PGUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015 baoyongzhang 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.baoyz.pg; 25 | 26 | import android.os.Parcel; 27 | 28 | import java.lang.reflect.Field; 29 | import java.lang.reflect.Modifier; 30 | 31 | /** 32 | * ParcelableGenerator 33 | * Created by baoyz on 15/6/24. 34 | */ 35 | public class PGUtils { 36 | 37 | public static void write(Object obj, Parcel dest) { 38 | write(obj, obj.getClass().getSuperclass(), dest); 39 | } 40 | 41 | private static void write(Object obj, Class clazz, Parcel dest) { 42 | Field[] declaredFields = clazz.getDeclaredFields(); 43 | for (Field field : declaredFields) { 44 | log("write = " + field.getName()); 45 | PGUtils.writeValue(dest, field, obj); 46 | } 47 | // super class 48 | if (clazz.getSuperclass() != null && clazz != Object.class) { 49 | write(obj, clazz.getSuperclass(), dest); 50 | } 51 | } 52 | 53 | private static void writeValue(Parcel dest, Field field, Object target) { 54 | try { 55 | if (!checkSerializable(field)) { 56 | return; 57 | } 58 | field.setAccessible(true); 59 | if (field.getType().equals(int.class)) { 60 | dest.writeInt(field.getInt(target)); 61 | } else if (field.getType().equals(double.class)) { 62 | dest.writeDouble(field.getDouble(target)); 63 | } else if (field.getType().equals(float.class)) { 64 | dest.writeFloat(field.getFloat(target)); 65 | } else if (field.getType().equals(long.class)) { 66 | dest.writeLong(field.getLong(target)); 67 | } else if (field.getType().equals(boolean.class)) { 68 | dest.writeInt(field.getBoolean(target) ? 1 : 0); 69 | } else if (field.getType().equals(char.class)) { 70 | dest.writeInt(field.getChar(target)); 71 | } else if (field.getType().equals(byte.class)) { 72 | dest.writeByte(field.getByte(target)); 73 | } else if (field.getType().equals(short.class)) { 74 | dest.writeInt(field.getShort(target)); 75 | } else { 76 | Object value = field.get(target); 77 | ParcelUtil.writeValue(dest, value); 78 | } 79 | } catch (Exception e) { 80 | e.printStackTrace(); 81 | } 82 | } 83 | 84 | private static boolean checkSerializable(Field field) { 85 | return !Modifier.isTransient(field.getModifiers()) 86 | && !field.isAnnotationPresent(ParcelIgnore.class); 87 | // && (field.getType().isPrimitive() 88 | // || Serializable.class.isAssignableFrom(field.getType()) || 89 | // Parcelable.class 90 | // .isAssignableFrom(field.getType())); 91 | // return true; 92 | } 93 | 94 | public static void read(Object obj, Parcel source) { 95 | read(obj, obj.getClass().getSuperclass(), source); 96 | } 97 | 98 | private static void read(Object obj, Class clazz, Parcel source) { 99 | Field[] declaredFields = clazz.getDeclaredFields(); 100 | for (Field field : declaredFields) { 101 | log("read = " + field.getName()); 102 | PGUtils.readValue(source, field, obj); 103 | } 104 | // super class 105 | if (clazz.getSuperclass() != null && clazz != Object.class) { 106 | read(obj, clazz.getSuperclass(), source); 107 | } 108 | } 109 | 110 | private static void readValue(Parcel source, Field field, Object target) { 111 | try { 112 | if (!checkSerializable(field)) { 113 | return; 114 | } 115 | field.setAccessible(true); 116 | if (field.getType().equals(int.class)) { 117 | field.setInt(target, source.readInt()); 118 | } else if (field.getType().equals(double.class)) { 119 | field.setDouble(target, source.readDouble()); 120 | } else if (field.getType().equals(float.class)) { 121 | field.setFloat(target, source.readFloat()); 122 | } else if (field.getType().equals(long.class)) { 123 | field.setLong(target, source.readLong()); 124 | } else if (field.getType().equals(boolean.class)) { 125 | field.setBoolean(target, source.readInt() != 0); 126 | } else if (field.getType().equals(char.class)) { 127 | field.setChar(target, (char) source.readInt()); 128 | } else if (field.getType().equals(byte.class)) { 129 | field.setByte(target, source.readByte()); 130 | } else if (field.getType().equals(short.class)) { 131 | field.setShort(target, (short) source.readInt()); 132 | } else { 133 | field.set(target, 134 | source.readValue(target.getClass().getClassLoader())); 135 | } 136 | } catch (Exception e) { 137 | e.printStackTrace(); 138 | } 139 | } 140 | 141 | public static void clone(Object source, Object dest) { 142 | clone(source.getClass(), source, dest); 143 | } 144 | 145 | private static void clone(Class clazz, Object source, Object dest) { 146 | Field[] declaredFields = clazz.getDeclaredFields(); 147 | try { 148 | for (Field field : declaredFields) { 149 | field.setAccessible(true); 150 | if (Modifier.isFinal(field.getModifiers())) { 151 | continue; 152 | } 153 | field.set(dest, field.get(source)); 154 | } 155 | } catch (Exception e) { 156 | e.printStackTrace(); 157 | } 158 | if (clazz.getSuperclass() != null 159 | && clazz.getSuperclass() != Object.class) { 160 | clone(clazz.getSuperclass(), source, dest); 161 | } 162 | } 163 | 164 | private static void log(String log) { 165 | // Log.i("byz", log); 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /core/src/main/java/com/baoyz/pg/ParcelIgnore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015 baoyongzhang 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.baoyz.pg; 25 | 26 | import java.lang.annotation.Retention; 27 | import java.lang.annotation.Target; 28 | 29 | import static java.lang.annotation.ElementType.FIELD; 30 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 31 | 32 | /** 33 | * ParcelableGenerator 34 | * Created by baoyz on 15/6/24. 35 | */ 36 | @Retention(RUNTIME) @Target(FIELD) 37 | public @interface ParcelIgnore { 38 | } 39 | -------------------------------------------------------------------------------- /core/src/main/java/com/baoyz/pg/ParcelInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015 baoyongzhang 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.baoyz.pg; 25 | 26 | import android.os.Parcelable; 27 | 28 | /** 29 | * ParcelableGenerator 30 | * Created by baoyz on 15/6/24. 31 | */ 32 | public class ParcelInfo { 33 | 34 | private static final String SUFFIX = "$$Parcelable"; 35 | 36 | private String className; 37 | private Parcelable parcel; 38 | 39 | public ParcelInfo(Parcelable parcel) { 40 | super(); 41 | this.parcel = parcel; 42 | String qualifiedName = parcel.getClass().getCanonicalName(); 43 | if (qualifiedName.endsWith(SUFFIX)) { 44 | className = qualifiedName.substring(0, qualifiedName.length() 45 | - SUFFIX.length()); 46 | } 47 | } 48 | 49 | public T getSource() { 50 | try { 51 | Object obj = Class.forName(className).newInstance(); 52 | PGUtils.clone(parcel, obj); 53 | return (T) obj; 54 | } catch (Exception e) { 55 | e.printStackTrace(); 56 | } 57 | return (T) parcel; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /core/src/main/java/com/baoyz/pg/ParcelProxyInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015 baoyongzhang 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.baoyz.pg; 25 | 26 | /** 27 | * ParcelableGenerator 28 | * Created by baoyz on 15/6/24. 29 | */ 30 | public class ParcelProxyInfo { 31 | 32 | private static final String SUFFIX = "$$Parcelable"; 33 | 34 | private String packageName; 35 | private String proxyName; 36 | private String className; 37 | 38 | public ParcelProxyInfo(String qualifiedName) { 39 | super(); 40 | packageName = qualifiedName 41 | .substring(0, qualifiedName.lastIndexOf(".")); 42 | className = qualifiedName.substring(packageName.length() + 1); 43 | proxyName = className + SUFFIX; 44 | } 45 | 46 | public String getFullName() { 47 | return packageName + "." + proxyName; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /core/src/main/java/com/baoyz/pg/ParcelUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015 baoyongzhang 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.baoyz.pg; 25 | 26 | import android.os.Bundle; 27 | import android.os.IBinder; 28 | import android.os.Parcel; 29 | import android.os.Parcelable; 30 | import android.text.TextUtils; 31 | import android.util.SparseArray; 32 | 33 | import java.io.Serializable; 34 | import java.util.List; 35 | import java.util.Map; 36 | import java.util.Set; 37 | 38 | /** 39 | * ParcelableGenerator 40 | * Created by baoyz on 15/6/24. 41 | */ 42 | public class ParcelUtil { 43 | 44 | private static final int VAL_NULL = -1; 45 | private static final int VAL_STRING = 0; 46 | private static final int VAL_INTEGER = 1; 47 | private static final int VAL_MAP = 2; 48 | private static final int VAL_BUNDLE = 3; 49 | private static final int VAL_PARCELABLE = 4; 50 | private static final int VAL_SHORT = 5; 51 | private static final int VAL_LONG = 6; 52 | private static final int VAL_FLOAT = 7; 53 | private static final int VAL_DOUBLE = 8; 54 | private static final int VAL_BOOLEAN = 9; 55 | private static final int VAL_CHARSEQUENCE = 10; 56 | private static final int VAL_LIST = 11; 57 | private static final int VAL_SPARSEARRAY = 12; 58 | private static final int VAL_BYTEARRAY = 13; 59 | private static final int VAL_STRINGARRAY = 14; 60 | private static final int VAL_IBINDER = 15; 61 | private static final int VAL_PARCELABLEARRAY = 16; 62 | private static final int VAL_OBJECTARRAY = 17; 63 | private static final int VAL_INTARRAY = 18; 64 | private static final int VAL_LONGARRAY = 19; 65 | private static final int VAL_BYTE = 20; 66 | private static final int VAL_SERIALIZABLE = 21; 67 | private static final int VAL_SPARSEBOOLEANARRAY = 22; 68 | private static final int VAL_BOOLEANARRAY = 23; 69 | private static final int VAL_CHARSEQUENCEARRAY = 24; 70 | 71 | public static void writeValue(Parcel dest, Object v) { 72 | try { 73 | writeValueInternal(dest, v); 74 | } catch (Exception e) { 75 | writeValueInternal(dest, PG.convert(v)); 76 | } 77 | } 78 | 79 | private static void writeValueInternal(Parcel dest, Object v) { 80 | if (v == null) { 81 | dest.writeInt(VAL_NULL); 82 | } else if (v instanceof String) { 83 | dest.writeInt(VAL_STRING); 84 | dest.writeString((String) v); 85 | } else if (v instanceof Integer) { 86 | dest.writeInt(VAL_INTEGER); 87 | dest.writeInt((Integer) v); 88 | } else if (v instanceof Map) { 89 | dest.writeInt(VAL_MAP); 90 | writeMap(dest, (Map) v); 91 | } else if (v instanceof Bundle) { 92 | // Must be before Parcelable 93 | dest.writeInt(VAL_BUNDLE); 94 | dest.writeBundle((Bundle) v); 95 | } else if (v instanceof Parcelable) { 96 | dest.writeInt(VAL_PARCELABLE); 97 | dest.writeParcelable((Parcelable) v, 0); 98 | } else if (v instanceof Short) { 99 | dest.writeInt(VAL_SHORT); 100 | dest.writeInt(((Short) v).intValue()); 101 | } else if (v instanceof Long) { 102 | dest.writeInt(VAL_LONG); 103 | dest.writeLong((Long) v); 104 | } else if (v instanceof Float) { 105 | dest.writeInt(VAL_FLOAT); 106 | dest.writeFloat((Float) v); 107 | } else if (v instanceof Double) { 108 | dest.writeInt(VAL_DOUBLE); 109 | dest.writeDouble((Double) v); 110 | } else if (v instanceof Boolean) { 111 | dest.writeInt(VAL_BOOLEAN); 112 | dest.writeInt((Boolean) v ? 1 : 0); 113 | } else if (v instanceof CharSequence) { 114 | // Must be after String 115 | dest.writeInt(VAL_CHARSEQUENCE); 116 | TextUtils.writeToParcel((CharSequence) v, dest, 0); 117 | } else if (v instanceof List) { 118 | dest.writeInt(VAL_LIST); 119 | writeList(dest, (List) v); 120 | } else if (v instanceof SparseArray) { 121 | dest.writeInt(VAL_SPARSEARRAY); 122 | writeSparseArray(dest, (SparseArray) v); 123 | } else if (v instanceof boolean[]) { 124 | dest.writeInt(VAL_BOOLEANARRAY); 125 | dest.writeBooleanArray((boolean[]) v); 126 | } else if (v instanceof byte[]) { 127 | dest.writeInt(VAL_BYTEARRAY); 128 | dest.writeByteArray((byte[]) v); 129 | } else if (v instanceof String[]) { 130 | dest.writeInt(VAL_STRINGARRAY); 131 | dest.writeStringArray((String[]) v); 132 | } else if (v instanceof CharSequence[]) { 133 | // Must be after String[] and before Object[] 134 | dest.writeInt(VAL_CHARSEQUENCEARRAY); 135 | CharSequence[] val = (CharSequence[]) v; 136 | int N = val.length; 137 | dest.writeInt(N); 138 | for (int i = 0; i < N; i++) { 139 | TextUtils.writeToParcel((CharSequence) val[i], dest, 0); 140 | } 141 | } else if (v instanceof IBinder) { 142 | dest.writeInt(VAL_IBINDER); 143 | dest.writeStrongBinder((IBinder) v); 144 | } else if (v instanceof Parcelable[]) { 145 | dest.writeInt(VAL_PARCELABLEARRAY); 146 | dest.writeParcelableArray((Parcelable[]) v, 0); 147 | } else if (v instanceof Object[]) { 148 | dest.writeInt(VAL_OBJECTARRAY); 149 | writeArray(dest, (Object[]) v); 150 | } else if (v instanceof int[]) { 151 | dest.writeInt(VAL_INTARRAY); 152 | dest.writeIntArray((int[]) v); 153 | } else if (v instanceof long[]) { 154 | dest.writeInt(VAL_LONGARRAY); 155 | dest.writeLongArray((long[]) v); 156 | } else if (v instanceof Byte) { 157 | dest.writeInt(VAL_BYTE); 158 | dest.writeInt((Byte) v); 159 | } else if (v instanceof Serializable) { 160 | // Must be last 161 | dest.writeInt(VAL_SERIALIZABLE); 162 | dest.writeSerializable((Serializable) v); 163 | } else { 164 | throw new RuntimeException("Parcel: unable to marshal value " + v); 165 | } 166 | } 167 | 168 | private static void writeMap(Parcel dest, Map val) { 169 | if (val == null) { 170 | dest.writeInt(-1); 171 | return; 172 | } 173 | Set> entries = val.entrySet(); 174 | dest.writeInt(entries.size()); 175 | for (Map.Entry e : entries) { 176 | writeValue(dest, e.getKey()); 177 | writeValue(dest, e.getValue()); 178 | } 179 | } 180 | 181 | private static void writeList(Parcel dest, List val) { 182 | if (val == null) { 183 | dest.writeInt(-1); 184 | return; 185 | } 186 | int N = val.size(); 187 | int i = 0; 188 | dest.writeInt(N); 189 | while (i < N) { 190 | writeValue(dest, val.get(i)); 191 | i++; 192 | } 193 | } 194 | 195 | private static void writeSparseArray(Parcel dest, SparseArray val) { 196 | if (val == null) { 197 | dest.writeInt(-1); 198 | return; 199 | } 200 | int N = val.size(); 201 | dest.writeInt(N); 202 | int i = 0; 203 | while (i < N) { 204 | dest.writeInt(val.keyAt(i)); 205 | writeValue(dest, val.valueAt(i)); 206 | i++; 207 | } 208 | } 209 | 210 | private static void writeArray(Parcel dest, Object[] val) { 211 | if (val == null) { 212 | dest.writeInt(-1); 213 | return; 214 | } 215 | int N = val.length; 216 | int i = 0; 217 | dest.writeInt(N); 218 | while (i < N) { 219 | writeValue(dest, val[i]); 220 | i++; 221 | } 222 | } 223 | } 224 | -------------------------------------------------------------------------------- /core/src/main/java/com/baoyz/pg/Parcelable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015 baoyongzhang 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package com.baoyz.pg; 25 | 26 | import java.lang.annotation.Retention; 27 | import java.lang.annotation.Target; 28 | 29 | import static java.lang.annotation.ElementType.TYPE; 30 | import static java.lang.annotation.RetentionPolicy.CLASS; 31 | 32 | /** 33 | * ParcelableGenerator 34 | * Created by baoyz on 15/6/24. 35 | */ 36 | @Retention(CLASS) @Target(TYPE) 37 | public @interface Parcelable { 38 | } 39 | -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /demo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "22.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.baoyz.parcelablegenerator" 9 | minSdkVersion 8 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:appcompat-v7:22.1.1' 25 | provided project(':compiler') 26 | compile project(':core') 27 | } 28 | -------------------------------------------------------------------------------- /demo/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/baoyz/Developer/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /demo/src/androidTest/java/com/baoyz/parcelablegenerator/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.baoyz.parcelablegenerator; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 22 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baoyz/parcelablegenerator/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.baoyz.parcelablegenerator; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.Menu; 7 | import android.view.MenuItem; 8 | import android.view.View; 9 | 10 | import com.baoyz.parcelablegenerator.model.Classroom; 11 | import com.baoyz.parcelablegenerator.model.Student; 12 | import com.baoyz.parcelablegenerator.model.Teacher; 13 | import com.baoyz.parcelablegenerator.model.User; 14 | import com.baoyz.pg.PG; 15 | 16 | import java.util.ArrayList; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | public class MainActivity extends AppCompatActivity { 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_main); 26 | } 27 | 28 | public void showUser(View v) { 29 | 30 | // create model 31 | User user = new User(); 32 | user.setName("zhangsan"); 33 | user.setAge(18); 34 | user.setBalance(100.85); 35 | user.setId(11111l); 36 | user.setVip(true); 37 | user.setAlias(Arrays.asList("alias1", "alias2", "alias3")); 38 | 39 | Intent intent = new Intent(this, ShowUserActivity.class); 40 | // model convert to parceable 41 | intent.putExtra("user", PG.convertParcelable(user)); 42 | startActivity(intent); 43 | } 44 | 45 | public void showClassroom(View v) { 46 | // classroom object 47 | Classroom room = new Classroom("classroomName"); 48 | Teacher teacher = new Teacher("teacherName", "course"); 49 | // set teacher object 50 | room.setTeacher(teacher); 51 | // create students list 52 | List students = new ArrayList(); 53 | students.add(new Student("stu1", 18)); 54 | students.add(new Student("stu2", 19)); 55 | students.add(new Student("stu3", 20)); 56 | // set students list 57 | room.setStudents(students); 58 | 59 | Intent intent = new Intent(this, ShowClassroomActivity.class); 60 | // classroom object convert to parceable object 61 | intent.putExtra("classroom", PG.convertParcelable(room)); 62 | startActivity(intent); 63 | } 64 | 65 | @Override 66 | public boolean onCreateOptionsMenu(Menu menu) { 67 | // Inflate the menu; this adds items to the action bar if it is present. 68 | getMenuInflater().inflate(R.menu.menu_main, menu); 69 | return true; 70 | } 71 | 72 | @Override 73 | public boolean onOptionsItemSelected(MenuItem item) { 74 | // Handle action bar item clicks here. The action bar will 75 | // automatically handle clicks on the Home/Up button, so long 76 | // as you specify a parent activity in AndroidManifest.xml. 77 | int id = item.getItemId(); 78 | 79 | //noinspection SimplifiableIfStatement 80 | if (id == R.id.action_settings) { 81 | return true; 82 | } 83 | 84 | return super.onOptionsItemSelected(item); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baoyz/parcelablegenerator/ShowClassroomActivity.java: -------------------------------------------------------------------------------- 1 | package com.baoyz.parcelablegenerator; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.widget.TextView; 6 | 7 | import com.baoyz.parcelablegenerator.model.Classroom; 8 | 9 | public class ShowClassroomActivity extends Activity { 10 | 11 | @Override 12 | protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_show); 15 | TextView tv = (TextView) findViewById(R.id.textView); 16 | 17 | // get model object 18 | Classroom room = getIntent().getParcelableExtra("classroom"); 19 | 20 | // use model 21 | tv.setText(room.toString()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baoyz/parcelablegenerator/ShowUserActivity.java: -------------------------------------------------------------------------------- 1 | package com.baoyz.parcelablegenerator; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.widget.TextView; 6 | 7 | import com.baoyz.parcelablegenerator.model.User; 8 | 9 | public class ShowUserActivity extends Activity { 10 | 11 | @Override 12 | protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_show); 15 | TextView tv = (TextView) findViewById(R.id.textView); 16 | 17 | // get model object 18 | User user = getIntent().getParcelableExtra("user"); 19 | 20 | // use model 21 | tv.setText(user.toString()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baoyz/parcelablegenerator/model/Classroom.java: -------------------------------------------------------------------------------- 1 | package com.baoyz.parcelablegenerator.model; 2 | 3 | import com.baoyz.pg.Parcelable; 4 | 5 | import java.util.List; 6 | 7 | @Parcelable 8 | public class Classroom { 9 | 10 | private String name; 11 | private Teacher teacher; 12 | private List students; 13 | 14 | public Classroom() { 15 | } 16 | 17 | public Classroom(String name) { 18 | super(); 19 | this.name = name; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | public Teacher getTeacher() { 31 | return teacher; 32 | } 33 | 34 | public void setTeacher(Teacher teacher) { 35 | this.teacher = teacher; 36 | } 37 | 38 | public List getStudents() { 39 | return students; 40 | } 41 | 42 | public void setStudents(List students) { 43 | this.students = students; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return "Classroom [name=" + name + ", teacher=" + teacher 49 | + ", students=" + students + "]"; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baoyz/parcelablegenerator/model/Person.java: -------------------------------------------------------------------------------- 1 | package com.baoyz.parcelablegenerator.model; 2 | 3 | public class Person { 4 | 5 | private String name; 6 | private int age; 7 | 8 | public String getName() { 9 | return name; 10 | } 11 | 12 | public void setName(String name) { 13 | this.name = name; 14 | } 15 | 16 | public int getAge() { 17 | return age; 18 | } 19 | 20 | public void setAge(int age) { 21 | this.age = age; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baoyz/parcelablegenerator/model/Student.java: -------------------------------------------------------------------------------- 1 | package com.baoyz.parcelablegenerator.model; 2 | 3 | import com.baoyz.pg.Parcelable; 4 | 5 | @Parcelable 6 | public class Student { 7 | 8 | private String name; 9 | private int age; 10 | 11 | // required 12 | public Student() { 13 | } 14 | 15 | public Student(String name, int age) { 16 | super(); 17 | this.name = name; 18 | this.age = age; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public int getAge() { 30 | return age; 31 | } 32 | 33 | public void setAge(int age) { 34 | this.age = age; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "Student [name=" + name + ", age=" + age + "]"; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baoyz/parcelablegenerator/model/Teacher.java: -------------------------------------------------------------------------------- 1 | package com.baoyz.parcelablegenerator.model; 2 | 3 | import com.baoyz.pg.Parcelable; 4 | 5 | @Parcelable 6 | public class Teacher { 7 | 8 | private String name; 9 | private String course; 10 | 11 | // required 12 | public Teacher() { 13 | } 14 | 15 | public Teacher(String name, String course) { 16 | super(); 17 | this.name = name; 18 | this.course = course; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public String getCourse() { 30 | return course; 31 | } 32 | 33 | public void setCourse(String course) { 34 | this.course = course; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "Teacher [name=" + name + ", course=" + course + "]"; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /demo/src/main/java/com/baoyz/parcelablegenerator/model/User.java: -------------------------------------------------------------------------------- 1 | package com.baoyz.parcelablegenerator.model; 2 | 3 | import com.baoyz.pg.Parcelable; 4 | 5 | import java.util.List; 6 | 7 | @Parcelable 8 | public class User extends Person { 9 | 10 | private long id; 11 | private boolean vip; 12 | private double balance; 13 | private Long id2; 14 | private Boolean vip2; 15 | private Double balance2; 16 | private List alias; 17 | 18 | public long getId() { 19 | return id; 20 | } 21 | 22 | public void setId(long id) { 23 | this.id2 = id; 24 | this.id = id; 25 | } 26 | 27 | public boolean isVip() { 28 | return vip; 29 | } 30 | 31 | public void setVip(boolean vip) { 32 | this.vip2 = vip; 33 | this.vip = vip; 34 | } 35 | 36 | public double getBalance() { 37 | return balance; 38 | } 39 | 40 | public void setBalance(double balance) { 41 | this.balance2 = balance; 42 | this.balance = balance; 43 | } 44 | 45 | public List getAlias() { 46 | return alias; 47 | } 48 | 49 | public void setAlias(List alias) { 50 | this.alias = alias; 51 | } 52 | 53 | public Long getId2() { 54 | return id2; 55 | } 56 | 57 | public void setId2(Long id2) { 58 | this.id2 = id2; 59 | } 60 | 61 | public Boolean getVip2() { 62 | return vip2; 63 | } 64 | 65 | public void setVip2(Boolean vip2) { 66 | this.vip2 = vip2; 67 | } 68 | 69 | public Double getBalance2() { 70 | return balance2; 71 | } 72 | 73 | public void setBalance2(Double balance2) { 74 | this.balance2 = balance2; 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | return "User [name=" + getName() + ", age=" + getAge() + ", id=" + id 80 | + ", vip=" + vip + ", balance=" + balance + ", id2=" + id2 81 | + ", vip2=" + vip2 + ", balance2=" + balance2 + ", alias=" 82 | + alias + "]"; 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 |