├── .gitignore ├── LICENSE ├── README.md ├── api ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── jenzz │ └── pojobuilder │ └── api │ ├── Builder.java │ └── Ignore.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── processor ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── jenzz │ └── pojobuilder │ └── processor │ ├── BuilderAnnotatedClass.java │ ├── BuilderProcessor.java │ ├── ClassNameGenerator.java │ ├── CodeGenerator.java │ ├── ElementValidator.java │ ├── Messager.java │ ├── Utils.java │ ├── expections │ ├── AbstractClassException.java │ ├── MissingNoArgsConstructorException.java │ ├── PrivateFieldException.java │ ├── RuleException.java │ └── UnnamedPackageException.java │ └── rules │ ├── NonAbstractClassRule.java │ ├── NonPrivateFieldsRule.java │ ├── NonPrivateNoArgsConstructorRule.java │ └── Rule.java ├── sample ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── jenzz │ │ └── pojobuilder │ │ └── sample │ │ ├── MainActivity.java │ │ ├── example1 │ │ ├── DifferentPojo.java │ │ └── SamplePojo.java │ │ └── example2 │ │ ├── Address.java │ │ └── User.java │ └── res │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ └── values │ └── strings.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Gradle 2 | .gradle 3 | local.properties 4 | build 5 | 6 | # Android Studio 7 | .idea 8 | *.iml 9 | 10 | # Mac OSX 11 | .DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jens Driller 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Java - POJO Builder 2 | ============================ 3 | * A Java code generator for the builder pattern using annotation processing 4 | 5 | Usage 6 | ----- 7 | Just annotate your POJOs with the `@Builder` annotation, like so: 8 | 9 | ```java 10 | @Builder 11 | public class User { 12 | String name; 13 | int age; 14 | Address address; 15 | } 16 | ``` 17 | 18 | ```java 19 | @Builder 20 | public class Address { 21 | String street; 22 | String postcode; 23 | String city; 24 | } 25 | ``` 26 | 27 | When you compile your project, a new Builder class will be generated for each annotated POJO, like so: 28 | 29 | 30 | ```java 31 | public final class UserBuilder { 32 | 33 | private String name; 34 | private int age; 35 | private Address address; 36 | 37 | public static UserBuilder user() { 38 | return new UserBuilder(); 39 | } 40 | 41 | public static UserBuilder user(User from) { 42 | UserBuilder builder = new UserBuilder(); 43 | builder.name = from.name; 44 | builder.age = from.age; 45 | builder.address = from.address; 46 | return builder; 47 | } 48 | 49 | public UserBuilder name(String name) { 50 | this.name = name; 51 | return this; 52 | } 53 | 54 | public UserBuilder age(int age) { 55 | this.age = age; 56 | return this; 57 | } 58 | 59 | public UserBuilder address(Address address) { 60 | this.address = address; 61 | return this; 62 | } 63 | 64 | public UserBuilder address(AddressBuilder addressBuilder) { 65 | this.address = addressBuilder.build(); 66 | return this; 67 | } 68 | 69 | public User build() { 70 | User user = new User(); 71 | user.name = name; 72 | user.age = age; 73 | user.address = address; 74 | return user; 75 | } 76 | } 77 | ``` 78 | 79 | Using static imports, you can then create those objects as simple as: 80 | 81 | ```java 82 | User user = user() 83 | .name("Bob The Builder") 84 | .age(25) 85 | .address( 86 | address() 87 | .street("10 Hight Street") 88 | .postcode("WC2") 89 | .city("London")) 90 | .build(); 91 | ``` 92 | 93 | Fields annotated with `@Ignore` will be ignored. 94 | 95 | Example 96 | ------- 97 | Check out the [sample project](https://github.com/jenzz/Java-PojoBuilder/tree/master/sample) for an example implementation. 98 | 99 | The sample project is currently an Android module, but it should work with a pure Java project just as well. 100 | If somebody knows how to setup an annotation processor for a Java module using Gradle, please let me know. 101 | The [apt plugin](https://bitbucket.org/hvisser/android-apt) currently supports only Android modules. 102 | 103 | Download 104 | -------- 105 | 106 | Grab it via Gradle: 107 | 108 | ```groovy 109 | compile 'com.jenzz.pojobuilder:api:1.0' 110 | apt 'com.jenzz.pojobuilder:processor:1.0' 111 | ``` 112 | 113 | You only need to include the **api** module (contains just the annotations) as a real dependency. 114 | 115 | The annotation **processor** module can be a compile time only dependency. To do that I highly recommend using Hugo Visser's great [apt plugin](https://bitbucket.org/hvisser/android-apt). 116 | 117 | License 118 | ------- 119 | This project is licensed under the [MIT License](https://raw.githubusercontent.com/jenzz/Java-PojoBuilder/master/LICENSE). -------------------------------------------------------------------------------- /api/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'bintray-release' 3 | 4 | sourceCompatibility = JavaVersion.VERSION_1_7 5 | targetCompatibility = JavaVersion.VERSION_1_7 6 | 7 | publish { 8 | artifactId = 'api' 9 | userOrg = rootProject.ext.userOrg 10 | groupId = rootProject.ext.groupId 11 | uploadName = rootProject.ext.uploadName 12 | version = rootProject.ext.version 13 | description = rootProject.ext.description 14 | website = rootProject.ext.website 15 | licences = rootProject.ext.licences 16 | } -------------------------------------------------------------------------------- /api/src/main/java/com/jenzz/pojobuilder/api/Builder.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.api; 2 | 3 | import java.lang.annotation.Target; 4 | 5 | import static java.lang.annotation.ElementType.TYPE; 6 | 7 | @Target(TYPE) 8 | public @interface Builder {} 9 | -------------------------------------------------------------------------------- /api/src/main/java/com/jenzz/pojobuilder/api/Ignore.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.api; 2 | 3 | import java.lang.annotation.Target; 4 | 5 | import static java.lang.annotation.ElementType.FIELD; 6 | 7 | @Target(FIELD) 8 | public @interface Ignore {} 9 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | dependencies { 6 | classpath 'com.android.tools.build:gradle:1.1.3' 7 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' 8 | classpath 'com.novoda:bintray-release:0.2.7' 9 | } 10 | } 11 | 12 | allprojects { 13 | repositories { 14 | jcenter() 15 | } 16 | } 17 | 18 | ext { 19 | userOrg = 'jenzz' 20 | groupId = 'com.jenzz.pojobuilder' 21 | uploadName = 'Java-PojoBuilder' 22 | version = '1.0' 23 | description = 'A Java code generator for the builder pattern using annotation processing' 24 | website = 'https://github.com/jenzz/Java-PojoBuilder' 25 | licences = ['MIT'] 26 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenzz/Java-PojoBuilder/406c048584043bc93328d2c379a335cf3ee39798/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /processor/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'bintray-release' 3 | 4 | dependencies { 5 | compile project(':api') 6 | compile 'com.google.auto.service:auto-service:1.0-rc2' 7 | compile 'com.squareup:javapoet:1.0.0' 8 | } 9 | 10 | publish { 11 | artifactId = 'processor' 12 | userOrg = rootProject.ext.userOrg 13 | groupId = rootProject.ext.groupId 14 | uploadName = rootProject.ext.uploadName 15 | version = rootProject.ext.version 16 | description = rootProject.ext.description 17 | website = rootProject.ext.website 18 | licences = rootProject.ext.licences 19 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/BuilderAnnotatedClass.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor; 2 | 3 | import com.jenzz.pojobuilder.api.Ignore; 4 | import com.squareup.javapoet.ClassName; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import javax.lang.model.element.Element; 8 | import javax.lang.model.element.TypeElement; 9 | 10 | import static com.squareup.javapoet.ClassName.get; 11 | import static javax.lang.model.util.ElementFilter.fieldsIn; 12 | 13 | class BuilderAnnotatedClass { 14 | 15 | private final String packageName; 16 | private final TypeElement classElement; 17 | 18 | BuilderAnnotatedClass(String packageName, TypeElement classElement) { 19 | this.packageName = packageName; 20 | this.classElement = classElement; 21 | } 22 | 23 | String packageName() { return packageName; } 24 | 25 | TypeElement classElement() { 26 | return classElement; 27 | } 28 | 29 | String simpleName() { 30 | return classElement.getSimpleName().toString(); 31 | } 32 | 33 | ClassName className() { 34 | return get(classElement); 35 | } 36 | 37 | List nonIgnoredFields() { 38 | List list = new ArrayList<>(); 39 | for (Element element : fieldsIn(classElement.getEnclosedElements())) { 40 | boolean isIgnored = element.getAnnotation(Ignore.class) != null; 41 | if (!isIgnored) { 42 | list.add(element); 43 | } 44 | } 45 | return list; 46 | } 47 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/BuilderProcessor.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor; 2 | 3 | import com.google.auto.service.AutoService; 4 | import com.jenzz.pojobuilder.api.Builder; 5 | import com.jenzz.pojobuilder.api.Ignore; 6 | import com.jenzz.pojobuilder.processor.expections.RuleException; 7 | import com.jenzz.pojobuilder.processor.expections.UnnamedPackageException; 8 | import com.jenzz.pojobuilder.processor.rules.NonAbstractClassRule; 9 | import com.jenzz.pojobuilder.processor.rules.NonPrivateFieldsRule; 10 | import com.jenzz.pojobuilder.processor.rules.NonPrivateNoArgsConstructorRule; 11 | import com.jenzz.pojobuilder.processor.rules.Rule; 12 | import com.squareup.javapoet.JavaFile; 13 | import com.squareup.javapoet.TypeSpec; 14 | import java.io.IOException; 15 | import java.util.HashSet; 16 | import java.util.Set; 17 | import javax.annotation.processing.AbstractProcessor; 18 | import javax.annotation.processing.ProcessingEnvironment; 19 | import javax.annotation.processing.Processor; 20 | import javax.annotation.processing.RoundEnvironment; 21 | import javax.lang.model.SourceVersion; 22 | import javax.lang.model.element.Element; 23 | import javax.lang.model.element.TypeElement; 24 | 25 | import static com.jenzz.pojobuilder.processor.Utils.packageName; 26 | import static com.squareup.javapoet.JavaFile.builder; 27 | import static javax.lang.model.SourceVersion.latestSupported; 28 | 29 | @AutoService(Processor.class) 30 | public class BuilderProcessor extends AbstractProcessor { 31 | 32 | public static final String ANNOTATION = "@" + Builder.class.getSimpleName(); 33 | 34 | private static final Rule[] RULES = 35 | new Rule[] { 36 | new NonAbstractClassRule(), 37 | new NonPrivateNoArgsConstructorRule(), 38 | new NonPrivateFieldsRule() 39 | }; 40 | 41 | private final ElementValidator elementValidator = new ElementValidator(RULES); 42 | 43 | @Override 44 | public synchronized void init(ProcessingEnvironment processingEnv) { 45 | super.init(processingEnv); 46 | Messager.init(processingEnv); 47 | } 48 | 49 | @Override 50 | public Set getSupportedAnnotationTypes() { 51 | return new HashSet() {{ 52 | add(Builder.class.getCanonicalName()); 53 | add(Ignore.class.getCanonicalName()); 54 | }}; 55 | } 56 | 57 | @Override 58 | public SourceVersion getSupportedSourceVersion() { 59 | return latestSupported(); 60 | } 61 | 62 | @Override 63 | public boolean process(Set annotations, RoundEnvironment roundEnv) { 64 | for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(Builder.class)) { 65 | 66 | // annotation is only allowed on classes, so we can safely cast here 67 | TypeElement annotatedClass = (TypeElement) annotatedElement; 68 | try { 69 | elementValidator.validate(annotatedClass); 70 | } catch (RuleException e) { 71 | Messager.error(annotatedClass, e.getMessage()); 72 | return true; 73 | } 74 | 75 | try { 76 | generateCode(annotatedClass); 77 | } catch (UnnamedPackageException | IOException e) { 78 | Messager.error(annotatedElement, "Couldn't generate Builder for %s: %s", annotatedClass, 79 | e.getMessage()); 80 | } 81 | } 82 | 83 | return true; 84 | } 85 | 86 | private void generateCode(TypeElement annotatedClass) 87 | throws UnnamedPackageException, IOException { 88 | String packageName = packageName(processingEnv.getElementUtils(), annotatedClass); 89 | 90 | BuilderAnnotatedClass builderAnnotatedClass = 91 | new BuilderAnnotatedClass(packageName, annotatedClass); 92 | CodeGenerator codeGenerator = 93 | new CodeGenerator(builderAnnotatedClass, new ClassNameGenerator(packageName)); 94 | TypeSpec generatedClass = codeGenerator.brewJava(); 95 | 96 | JavaFile javaFile = builder(packageName, generatedClass).build(); 97 | javaFile.writeTo(processingEnv.getFiler()); 98 | } 99 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/ClassNameGenerator.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor; 2 | 3 | import com.squareup.javapoet.ClassName; 4 | import javax.lang.model.element.Element; 5 | 6 | import static com.squareup.javapoet.ClassName.get; 7 | 8 | class ClassNameGenerator { 9 | 10 | public static final String CLASS_SUFFIX = "Builder"; 11 | 12 | private final String packageName; 13 | 14 | ClassNameGenerator(String packageName) { 15 | this.packageName = packageName; 16 | } 17 | 18 | String generateBuilderClassString(BuilderAnnotatedClass clazz) { 19 | return generateBuilderClassString(clazz.simpleName()); 20 | } 21 | 22 | String generateBuilderClassString(Element clazz) { 23 | return generateBuilderClassString(clazz.getSimpleName().toString()); 24 | } 25 | 26 | String generateBuilderClassString(String clazz) { 27 | return clazz + CLASS_SUFFIX; 28 | } 29 | 30 | ClassName generateBuilderClassName(BuilderAnnotatedClass clazz) { 31 | return generateBuilderClassName(clazz.simpleName()); 32 | } 33 | 34 | ClassName generateBuilderClassName(Element clazz) { 35 | return generateBuilderClassName(clazz.getSimpleName().toString()); 36 | } 37 | 38 | ClassName generateBuilderClassName(String clazz) { 39 | return get(packageName, generateBuilderClassString(clazz)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/CodeGenerator.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor; 2 | 3 | import com.jenzz.pojobuilder.api.Builder; 4 | import com.squareup.javapoet.ClassName; 5 | import com.squareup.javapoet.FieldSpec; 6 | import com.squareup.javapoet.MethodSpec; 7 | import com.squareup.javapoet.TypeSpec; 8 | import javax.lang.model.element.Element; 9 | import javax.lang.model.type.DeclaredType; 10 | import javax.lang.model.type.TypeMirror; 11 | 12 | import static com.jenzz.pojobuilder.processor.Utils.decapitalize; 13 | import static com.squareup.javapoet.MethodSpec.methodBuilder; 14 | import static com.squareup.javapoet.TypeName.get; 15 | import static com.squareup.javapoet.TypeSpec.classBuilder; 16 | import static javax.lang.model.element.Modifier.FINAL; 17 | import static javax.lang.model.element.Modifier.PRIVATE; 18 | import static javax.lang.model.element.Modifier.PUBLIC; 19 | import static javax.lang.model.element.Modifier.STATIC; 20 | import static javax.lang.model.type.TypeKind.DECLARED; 21 | 22 | @SuppressWarnings("ConstantConditions") 23 | final class CodeGenerator { 24 | 25 | private final BuilderAnnotatedClass builderAnnotatedClass; 26 | private final ClassNameGenerator classNameGenerator; 27 | private final ClassName generatedClassName; 28 | 29 | CodeGenerator(BuilderAnnotatedClass builderAnnotatedClass, 30 | ClassNameGenerator classNameGenerator) { 31 | this.builderAnnotatedClass = builderAnnotatedClass; 32 | this.classNameGenerator = classNameGenerator; 33 | this.generatedClassName = classNameGenerator.generateBuilderClassName(builderAnnotatedClass); 34 | } 35 | 36 | TypeSpec brewJava() { 37 | String className = classNameGenerator.generateBuilderClassString(builderAnnotatedClass); 38 | TypeSpec.Builder builder = classBuilder(className) 39 | .addModifiers(PUBLIC, FINAL) 40 | .addMethod(constructor()) 41 | .addMethod(fromConstructor()); 42 | 43 | for (Element field : builderAnnotatedClass.nonIgnoredFields()) { 44 | builder.addField(field(field)); 45 | builder.addMethod(method(field)); 46 | if (hasBuilderAnnotation(field)) { 47 | builder.addMethod(builderMethod(field)); 48 | } 49 | } 50 | 51 | return builder.addMethod(build()).build(); 52 | } 53 | 54 | private boolean hasBuilderAnnotation(Element field) { 55 | boolean hasBuilderAnnotation = false; 56 | TypeMirror fieldType = field.asType(); 57 | if (fieldType.getKind() == DECLARED) { 58 | Element clazz = ((DeclaredType) fieldType).asElement(); 59 | hasBuilderAnnotation = clazz.getAnnotation(Builder.class) != null; 60 | } 61 | return hasBuilderAnnotation; 62 | } 63 | 64 | private MethodSpec constructor() { 65 | return methodBuilder(decapitalize(builderAnnotatedClass.simpleName())) 66 | .addModifiers(PUBLIC, STATIC) 67 | .returns(generatedClassName) 68 | .addStatement("return new $T()", generatedClassName) 69 | .build(); 70 | } 71 | 72 | private MethodSpec fromConstructor() { 73 | MethodSpec.Builder builder = 74 | methodBuilder(decapitalize(builderAnnotatedClass.simpleName())) 75 | .addModifiers(PUBLIC, STATIC) 76 | .returns(generatedClassName) 77 | .addParameter(builderAnnotatedClass.className(), "from") 78 | .addStatement("$T builder = new $T()", generatedClassName, generatedClassName); 79 | 80 | for (Element field : builderAnnotatedClass.nonIgnoredFields()) { 81 | String fieldName = field.getSimpleName().toString(); 82 | builder.addStatement("builder.$L = from.$L", fieldName, fieldName); 83 | } 84 | 85 | return builder.addStatement("return builder").build(); 86 | } 87 | 88 | private MethodSpec build() { 89 | String instanceName = decapitalize(builderAnnotatedClass.simpleName()); 90 | MethodSpec.Builder builder = methodBuilder("build") 91 | .addModifiers(PUBLIC) 92 | .returns(builderAnnotatedClass.className()) 93 | .addStatement("$T $L = new $T()", builderAnnotatedClass.classElement(), 94 | instanceName, builderAnnotatedClass.classElement()); 95 | 96 | for (Element field : builderAnnotatedClass.nonIgnoredFields()) { 97 | String fieldName = field.getSimpleName().toString(); 98 | builder.addStatement("$L.$L = $L", instanceName, fieldName, fieldName); 99 | } 100 | 101 | return builder.addStatement("return $L", instanceName).build(); 102 | } 103 | 104 | private FieldSpec field(Element field) { 105 | return FieldSpec.builder(get(field.asType()), field.getSimpleName().toString()) 106 | .addModifiers(PRIVATE) 107 | .build(); 108 | } 109 | 110 | private MethodSpec method(Element field) { 111 | String methodName = field.getSimpleName().toString(); 112 | return methodBuilder(methodName) 113 | .addModifiers(PUBLIC) 114 | .returns(generatedClassName) 115 | .addParameter(get(field.asType()), methodName) 116 | .addStatement("this.$L = $L", methodName, methodName) 117 | .addStatement("return this") 118 | .build(); 119 | } 120 | 121 | private MethodSpec builderMethod(Element field) { 122 | String methodName = field.getSimpleName().toString(); 123 | Element clazz = ((DeclaredType) field.asType()).asElement(); 124 | String clazzString = classNameGenerator.generateBuilderClassString(clazz); 125 | ClassName clazzName = classNameGenerator.generateBuilderClassName(clazz); 126 | return methodBuilder(methodName) 127 | .addModifiers(PUBLIC) 128 | .returns(generatedClassName) 129 | .addParameter(clazzName, decapitalize(clazzString)) 130 | .addStatement("this.$L = $L.build()", methodName, decapitalize(clazzString)) 131 | .addStatement("return this") 132 | .build(); 133 | } 134 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/ElementValidator.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor; 2 | 3 | import com.jenzz.pojobuilder.processor.expections.RuleException; 4 | import com.jenzz.pojobuilder.processor.rules.Rule; 5 | import javax.lang.model.element.Element; 6 | 7 | class ElementValidator { 8 | 9 | private final Rule[] rules; 10 | 11 | public ElementValidator(Rule... rules) { 12 | this.rules = rules; 13 | } 14 | 15 | public void validate(Element element) throws 16 | RuleException { 17 | for (Rule rule : rules) 18 | rule.validate(element); 19 | } 20 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/Messager.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor; 2 | 3 | import javax.annotation.processing.ProcessingEnvironment; 4 | import javax.lang.model.element.Element; 5 | 6 | import static javax.tools.Diagnostic.Kind.ERROR; 7 | import static javax.tools.Diagnostic.Kind.NOTE; 8 | import static javax.tools.Diagnostic.Kind.WARNING; 9 | 10 | final class Messager { 11 | 12 | private static javax.annotation.processing.Messager messager; 13 | 14 | static void init(ProcessingEnvironment processingEnvironment) { 15 | messager = processingEnvironment.getMessager(); 16 | } 17 | 18 | static void note(Element e, String msg, Object... args) { 19 | checkInitialized(); 20 | messager.printMessage(NOTE, String.format(msg, args), e); 21 | } 22 | 23 | static void warn(Element e, String msg, Object... args) { 24 | checkInitialized(); 25 | messager.printMessage(WARNING, String.format(msg, args), e); 26 | } 27 | 28 | static void error(Element e, String msg, Object... args) { 29 | checkInitialized(); 30 | messager.printMessage(ERROR, String.format(msg, args), e); 31 | } 32 | 33 | private static void checkInitialized() { 34 | if (messager == null) { 35 | throw new IllegalStateException("Messager not ready. Have you called init()?"); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/Utils.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor; 2 | 3 | import com.jenzz.pojobuilder.processor.expections.UnnamedPackageException; 4 | import javax.lang.model.element.Element; 5 | import javax.lang.model.element.PackageElement; 6 | import javax.lang.model.util.Elements; 7 | 8 | import static java.lang.Character.toLowerCase; 9 | 10 | final class Utils { 11 | 12 | private Utils() { 13 | // no instances 14 | } 15 | 16 | static String packageName(Elements elements, Element type) throws UnnamedPackageException { 17 | PackageElement pkg = elements.getPackageOf(type); 18 | if (pkg.isUnnamed()) { 19 | throw new UnnamedPackageException(type); 20 | } 21 | return pkg.getQualifiedName().toString(); 22 | } 23 | 24 | static String decapitalize(String text) { 25 | if (text == null || text.length() == 0) { 26 | return text; 27 | } 28 | return toLowerCase(text.charAt(0)) + text.substring(1); 29 | } 30 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/expections/AbstractClassException.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor.expections; 2 | 3 | public class AbstractClassException extends RuleException { 4 | 5 | public AbstractClassException(String message) { 6 | super(message); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/expections/MissingNoArgsConstructorException.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor.expections; 2 | 3 | public class MissingNoArgsConstructorException extends RuleException { 4 | 5 | public MissingNoArgsConstructorException(String message) { 6 | super(message); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/expections/PrivateFieldException.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor.expections; 2 | 3 | public class PrivateFieldException extends RuleException { 4 | 5 | public PrivateFieldException(String message) { 6 | super(message); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/expections/RuleException.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor.expections; 2 | 3 | public class RuleException extends Exception { 4 | 5 | public RuleException(String message) { 6 | super(message); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/expections/UnnamedPackageException.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor.expections; 2 | 3 | import javax.lang.model.element.Element; 4 | 5 | public class UnnamedPackageException extends Exception { 6 | 7 | public UnnamedPackageException(Element element) { 8 | super("The package of " + element.getSimpleName() + " is unnamed"); 9 | } 10 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/rules/NonAbstractClassRule.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor.rules; 2 | 3 | import com.jenzz.pojobuilder.processor.expections.AbstractClassException; 4 | import com.jenzz.pojobuilder.processor.expections.RuleException; 5 | import javax.lang.model.element.Element; 6 | 7 | import static com.jenzz.pojobuilder.processor.BuilderProcessor.ANNOTATION; 8 | import static javax.lang.model.element.Modifier.ABSTRACT; 9 | 10 | public class NonAbstractClassRule implements Rule { 11 | 12 | @Override 13 | public void validate(Element element) throws RuleException { 14 | if (element.getModifiers().contains(ABSTRACT)) throw exception(); 15 | } 16 | 17 | @Override 18 | public RuleException exception() { 19 | return new AbstractClassException( 20 | "Classes annotated with " + ANNOTATION + " must not be abstract."); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/rules/NonPrivateFieldsRule.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor.rules; 2 | 3 | import com.jenzz.pojobuilder.api.Ignore; 4 | import com.jenzz.pojobuilder.processor.expections.PrivateFieldException; 5 | import com.jenzz.pojobuilder.processor.expections.RuleException; 6 | import javax.lang.model.element.Element; 7 | import javax.lang.model.element.VariableElement; 8 | 9 | import static com.jenzz.pojobuilder.processor.BuilderProcessor.ANNOTATION; 10 | import static javax.lang.model.element.Modifier.PRIVATE; 11 | import static javax.lang.model.util.ElementFilter.fieldsIn; 12 | 13 | public class NonPrivateFieldsRule implements Rule { 14 | 15 | private VariableElement field; 16 | 17 | @Override 18 | public void validate(Element element) throws RuleException { 19 | for (VariableElement field : fieldsIn(element.getEnclosedElements())) { 20 | boolean isPrivate = field.getModifiers().contains(PRIVATE); 21 | boolean isIgnored = field.getAnnotation(Ignore.class) != null; 22 | if (isPrivate && !isIgnored) { 23 | this.field = field; 24 | throw exception(); 25 | } 26 | } 27 | } 28 | 29 | @Override 30 | public RuleException exception() { 31 | return new PrivateFieldException( 32 | "Class annotated with " + ANNOTATION + " has private field " + field.getSimpleName() + ". " 33 | + "Add @Ignore annotation or make field at least package-private."); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/rules/NonPrivateNoArgsConstructorRule.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor.rules; 2 | 3 | import com.jenzz.pojobuilder.processor.expections.MissingNoArgsConstructorException; 4 | import com.jenzz.pojobuilder.processor.expections.RuleException; 5 | import javax.lang.model.element.Element; 6 | import javax.lang.model.element.ExecutableElement; 7 | 8 | import static com.jenzz.pojobuilder.processor.BuilderProcessor.ANNOTATION; 9 | import static javax.lang.model.element.Modifier.PRIVATE; 10 | import static javax.lang.model.util.ElementFilter.constructorsIn; 11 | 12 | public class NonPrivateNoArgsConstructorRule implements Rule { 13 | 14 | @Override 15 | public void validate(Element element) throws RuleException { 16 | for (ExecutableElement constructor : constructorsIn(element.getEnclosedElements())) 17 | if (constructor.getParameters().isEmpty() && !constructor.getModifiers().contains(PRIVATE)) 18 | return; 19 | throw exception(); 20 | } 21 | 22 | @Override 23 | public RuleException exception() { 24 | return new MissingNoArgsConstructorException( 25 | "Class annotated with " + ANNOTATION + " must have a non-private no args constructor."); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /processor/src/main/java/com/jenzz/pojobuilder/processor/rules/Rule.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.processor.rules; 2 | 3 | import com.jenzz.pojobuilder.processor.expections.RuleException; 4 | import javax.lang.model.element.Element; 5 | 6 | public interface Rule { 7 | 8 | void validate(Element element) throws RuleException; 9 | 10 | RuleException exception(); 11 | } -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.neenbedankt.android-apt' 3 | 4 | android { 5 | compileSdkVersion 22 6 | buildToolsVersion "22.0.1" 7 | 8 | defaultConfig { 9 | applicationId 'com.jenzz.pojobuilder.sample' 10 | minSdkVersion 15 11 | targetSdkVersion 22 12 | versionCode 1 13 | versionName rootProject.ext.version 14 | } 15 | } 16 | 17 | dependencies { 18 | compile 'com.android.support:appcompat-v7:22.0.0' 19 | 20 | compile project(':api') 21 | apt project(':processor') 22 | } -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jenzz/pojobuilder/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.sample; 2 | 3 | import android.app.Activity; 4 | import com.jenzz.pojobuilder.sample.example1.DifferentPojo; 5 | import java.util.Collections; 6 | 7 | import static com.jenzz.pojobuilder.sample.example1.DifferentPojoBuilder.differentPojo; 8 | import static com.jenzz.pojobuilder.sample.example1.SamplePojoBuilder.samplePojo; 9 | import static com.jenzz.pojobuilder.sample.example1.StaticNestedPojoBuilder.staticNestedPojo; 10 | import static com.jenzz.pojobuilder.sample.example2.AddressBuilder.address; 11 | import static com.jenzz.pojobuilder.sample.example2.UserBuilder.user; 12 | 13 | public class MainActivity extends Activity { 14 | 15 | static { 16 | // example 1 17 | samplePojo() 18 | .aString("aString") 19 | .aInt(10) 20 | .aSet(Collections.emptySet()) 21 | .differentPojo( 22 | differentPojo() 23 | .aString("aString") 24 | .aLong(10L) 25 | .staticNestedPojo( 26 | staticNestedPojo() 27 | .aString("aString") 28 | .aCollection(Collections.emptyList())) 29 | ) 30 | .build(); 31 | 32 | // example 2 33 | user() 34 | .name("Bob The Builder") 35 | .age(25) 36 | .address( 37 | address() 38 | .street("10 Hight Street") 39 | .postcode("WC2") 40 | .city("London")) 41 | .build(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jenzz/pojobuilder/sample/example1/DifferentPojo.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.sample.example1; 2 | 3 | import com.jenzz.pojobuilder.api.Builder; 4 | import java.util.Collection; 5 | 6 | /** 7 | * Created by jenzz on 17/03/15. 8 | */ 9 | @Builder 10 | public class DifferentPojo { 11 | 12 | String aString; 13 | Long aLong; 14 | StaticNestedPojo staticNestedPojo; 15 | 16 | @Builder 17 | public static class StaticNestedPojo { 18 | 19 | String aString; 20 | Collection aCollection; 21 | } 22 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/jenzz/pojobuilder/sample/example1/SamplePojo.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.sample.example1; 2 | 3 | import com.jenzz.pojobuilder.api.Builder; 4 | import com.jenzz.pojobuilder.api.Ignore; 5 | import java.util.List; 6 | import java.util.Map; 7 | import java.util.Set; 8 | 9 | @Builder 10 | public class SamplePojo { 11 | 12 | // primitives 13 | byte aByte; 14 | short aShort; 15 | int aInt; 16 | long aLong; 17 | float aFloat; 18 | double aDouble; 19 | boolean aBoolean; 20 | char aChar; 21 | char[] aCharArray; 22 | 23 | // object types 24 | String aString; 25 | List aList; 26 | Map aMap; 27 | Set aSet; 28 | DifferentPojo differentPojo; 29 | 30 | // ignored field 31 | @Ignore String ignoredString; 32 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/jenzz/pojobuilder/sample/example2/Address.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.sample.example2; 2 | 3 | import com.jenzz.pojobuilder.api.Builder; 4 | 5 | @Builder 6 | public class Address { 7 | 8 | String street; 9 | String postcode; 10 | String city; 11 | } 12 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jenzz/pojobuilder/sample/example2/User.java: -------------------------------------------------------------------------------- 1 | package com.jenzz.pojobuilder.sample.example2; 2 | 3 | import com.jenzz.pojobuilder.api.Builder; 4 | 5 | @Builder 6 | public class User { 7 | 8 | String name; 9 | int age; 10 | 11 | Address address; 12 | } 13 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenzz/Java-PojoBuilder/406c048584043bc93328d2c379a335cf3ee39798/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenzz/Java-PojoBuilder/406c048584043bc93328d2c379a335cf3ee39798/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenzz/Java-PojoBuilder/406c048584043bc93328d2c379a335cf3ee39798/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenzz/Java-PojoBuilder/406c048584043bc93328d2c379a335cf3ee39798/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Java-PojoBuilder 4 | 5 | 6 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':api', ':processor', ':sample' --------------------------------------------------------------------------------