├── .checkstyle ├── .gitattributes ├── .github └── workflows │ ├── ci.yml │ └── publish.yml ├── .gitignore ├── .test └── .checkstyle ├── CONTRIBUTING.md ├── LICENSE.txt ├── README.md ├── build.gradle ├── config └── checkstyle │ ├── checkstyle.xml │ └── suppressions.xml ├── generated └── main │ └── java │ └── org │ └── inferred │ └── freebuilder │ └── processor │ ├── BuildableType_Builder.java │ ├── Datatype_Builder.java │ ├── property │ └── Property_Builder.java │ └── source │ └── TypeUsage_Builder.java ├── gradle.properties ├── gradle ├── java-compatibility.gradle ├── publication.gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── jar-footprint.txt └── src ├── it ├── gwt │ └── src │ │ ├── main │ │ └── java │ │ │ └── org │ │ │ └── inferred │ │ │ └── freebuilder │ │ │ └── client │ │ │ └── rpc │ │ │ ├── MapGwtType.java │ │ │ ├── NestedListGwtType.java │ │ │ ├── OptionalGwtType.java │ │ │ └── StringListGwtType.java │ │ └── test │ │ ├── java │ │ └── org │ │ │ └── inferred │ │ │ └── freebuilder │ │ │ ├── GwtTests.java │ │ │ ├── client │ │ │ └── rpc │ │ │ │ ├── MapTest.java │ │ │ │ ├── MapTestService.java │ │ │ │ ├── MapTestServiceAsync.java │ │ │ │ ├── NestedListTest.java │ │ │ │ ├── NestedListTestService.java │ │ │ │ ├── NestedListTestServiceAsync.java │ │ │ │ ├── OptionalTest.java │ │ │ │ ├── OptionalTestService.java │ │ │ │ ├── OptionalTestServiceAsync.java │ │ │ │ ├── StringListTest.java │ │ │ │ ├── StringListTestService.java │ │ │ │ └── StringListTestServiceAsync.java │ │ │ └── server │ │ │ └── rpc │ │ │ ├── MapTestServiceImpl.java │ │ │ ├── NestedListTestServiceImpl.java │ │ │ ├── OptionalTestServiceImpl.java │ │ │ └── StringListTestServiceImpl.java │ │ └── resources │ │ └── org │ │ └── inferred │ │ └── freebuilder │ │ └── TestServer.gwt.xml ├── lambda │ └── src │ │ ├── main │ │ └── java │ │ │ └── org │ │ │ └── inferred │ │ │ └── freebuilder │ │ │ ├── BiMapPropertyType.java │ │ │ ├── DefaultedPropertiesType.java │ │ │ ├── ListMultimapPropertyType.java │ │ │ ├── ListPropertyType.java │ │ │ ├── MapPropertyType.java │ │ │ ├── MultisetPropertyType.java │ │ │ ├── OptionalPropertiesType.java │ │ │ ├── RequiredPropertiesType.java │ │ │ ├── SetMultimapPropertyType.java │ │ │ └── SetPropertyType.java │ │ └── test │ │ └── java │ │ └── org │ │ └── inferred │ │ └── freebuilder │ │ ├── BiMapPropertiesTest.java │ │ ├── DefaultedPropertiesTest.java │ │ ├── ListMultimapPropertiesTest.java │ │ ├── ListPropertiesTest.java │ │ ├── MapPropertiesTest.java │ │ ├── MultisetPropertiesTest.java │ │ ├── OptionalPropertiesTest.java │ │ ├── RequiredPropertiesTest.java │ │ ├── SetMultimapPropertiesTest.java │ │ └── SetPropertiesTest.java ├── no-guava-j8 │ ├── invoker.properties │ └── src │ │ ├── main │ │ └── java │ │ │ └── org │ │ │ └── inferred │ │ │ └── freebuilder │ │ │ ├── OptionalProperties.java │ │ │ └── OptionalProperty.java │ │ └── test │ │ └── java │ │ └── org │ │ └── inferred │ │ └── freebuilder │ │ ├── OptionalPropertiesTest.java │ │ └── OptionalPropertyTest.java ├── no-guava │ └── src │ │ ├── main │ │ └── java │ │ │ └── org │ │ │ └── inferred │ │ │ └── freebuilder │ │ │ ├── ListProperty.java │ │ │ ├── MapProperty.java │ │ │ ├── PrimitiveProperty.java │ │ │ └── SetProperty.java │ │ └── test │ │ └── java │ │ └── org │ │ └── inferred │ │ └── freebuilder │ │ ├── ListPropertyTest.java │ │ ├── MapPropertyTest.java │ │ ├── PrimitivePropertyTest.java │ │ └── SetPropertyTest.java └── vanilla │ └── src │ ├── main │ └── java │ │ └── org │ │ └── inferred │ │ └── freebuilder │ │ ├── DefaultedPropertiesType.java │ │ ├── JacksonAnyGetterType.java │ │ ├── MapGwtType.java │ │ ├── NestedListGwtType.java │ │ ├── NoFrillsDataInterface.java │ │ ├── NoFrillsDataType.java │ │ ├── OptionalGwtType.java │ │ ├── RequiredPropertiesType.java │ │ ├── StringListGwtType.java │ │ └── WildcardListType.java │ └── test │ └── java │ └── org │ └── inferred │ └── freebuilder │ ├── DefaultsOptimizationTest.java │ ├── JacksonAnyGetterTypeTest.java │ ├── MapGwtTypeTest.java │ ├── NestedListGwtTypeTest.java │ ├── NoFrillsDataInterfaceTest.java │ ├── NoFrillsDataTypeTest.java │ ├── OptionalGwtTypeTest.java │ ├── StringListGwtTypeTest.java │ └── WildcardListTypeTest.java ├── main └── java │ └── org │ └── inferred │ └── freebuilder │ ├── FreeBuilder.java │ ├── IgnoredByEquals.java │ ├── NotInToString.java │ └── processor │ ├── Analyser.java │ ├── BuildableType.java │ ├── BuilderFactory.java │ ├── BuilderMethods.java │ ├── Datatype.java │ ├── Declarations.java │ ├── GeneratedBuilder.java │ ├── GeneratedStub.java │ ├── GeneratedType.java │ ├── GwtSupport.java │ ├── JacksonSupport.java │ ├── NameAndVisibility.java │ ├── NamePicker.java │ ├── Processor.java │ ├── ToStringGenerator.java │ ├── excerpt │ ├── BuildableList.java │ ├── CheckedBiMap.java │ ├── CheckedList.java │ ├── CheckedListMultimap.java │ ├── CheckedMap.java │ ├── CheckedMultiset.java │ ├── CheckedNavigableSet.java │ ├── CheckedSet.java │ └── CheckedSetMultimap.java │ ├── model │ ├── MethodFinder.java │ ├── MethodIntrospector.java │ ├── ModelUtils.java │ └── javac │ │ └── JavacMethodIntrospector.java │ ├── naming │ ├── BeanConvention.java │ ├── NamingConvention.java │ ├── NamingConventions.java │ └── PrefixlessConvention.java │ ├── property │ ├── BiMapProperty.java │ ├── BuildableListProperty.java │ ├── BuildableProperty.java │ ├── DefaultProperty.java │ ├── Factories.java │ ├── ListMultimapProperty.java │ ├── ListProperty.java │ ├── MapProperty.java │ ├── MergeAction.java │ ├── MultisetProperty.java │ ├── NullableProperty.java │ ├── OptionalProperty.java │ ├── PrimitiveOptionalProperty.java │ ├── Property.java │ ├── PropertyCodeGenerator.java │ ├── SetMultimapProperty.java │ ├── SetProperty.java │ └── SortedSetProperty.java │ └── source │ ├── AnnotationSource.java │ ├── CompilationUnitBuilder.java │ ├── CompilerReflection.java │ ├── ElementAppender.java │ ├── Excerpt.java │ ├── Excerpts.java │ ├── FieldAccess.java │ ├── FilerUtils.java │ ├── FunctionalType.java │ ├── IdKey.java │ ├── ImportManager.java │ ├── IsInvalidTypeVisitor.java │ ├── LazyName.java │ ├── ObjectsExcerpts.java │ ├── PreconditionExcerpts.java │ ├── QualifiedName.java │ ├── QualifiedNameAppendable.java │ ├── Quotes.java │ ├── RoundEnvironments.java │ ├── RuntimeReflection.java │ ├── Scope.java │ ├── ScopeHandler.java │ ├── Shading.java │ ├── SourceBuilder.java │ ├── SourceParser.java │ ├── TemplateApplier.java │ ├── Type.java │ ├── TypeClass.java │ ├── TypeMirrorAppender.java │ ├── TypeUsage.java │ ├── ValueType.java │ ├── Variable.java │ └── feature │ ├── EnvironmentFeatureSet.java │ ├── Feature.java │ ├── FeatureSet.java │ ├── FeatureType.java │ ├── GuavaLibrary.java │ ├── JavaxPackage.java │ ├── Jsr305.java │ ├── SourceLevel.java │ └── StaticFeatureSet.java └── test └── java └── org └── inferred └── freebuilder └── processor ├── AbstractBuilderTest.java ├── AnalyserTest.java ├── BuildableTypeTest.java ├── BuilderFactoryTest.java ├── FeatureSets.java ├── GeneratedTypeSubject.java ├── JacksonIntegrationTest.java ├── JacksonSupportTest.java ├── MultimapSubject.java ├── NamePickerTest.java ├── NamingConvention.java ├── ProcessorTest.java ├── ToStringGeneratorTest.java ├── model ├── ClassTypeImpl.java ├── GenericElement.java ├── GenericElementParameter.java ├── GenericElementTest.java ├── GenericMirror.java ├── GenericTypeElementImpl.java ├── ModelUtilsTest.java ├── NameImpl.java ├── NoTypes.java ├── NullTypeImpl.java ├── PackageElementImpl.java ├── PrimitiveTypeImpl.java ├── TypeParameterElementImpl.java ├── TypeVariableImpl.java └── WildcardTypeImpl.java ├── property ├── ArrayPropertiesTest.java ├── BiMapMutateMethodTest.java ├── BiMapPropertyTest.java ├── BiMapSourceTest.java ├── BuildableListMutateMethodTest.java ├── BuildableListPropertyTest.java ├── BuildablePropertyTest.java ├── DefaultSourceTest.java ├── DefaultedMapperMethodTest.java ├── DefaultedPropertiesTest.java ├── DodgyIterable.java ├── ElementFactory.java ├── GenericTypeTest.java ├── GuavaOptionalSourceTest.java ├── JavaUtilOptionalSourceTest.java ├── ListMultimapMutateMethodTest.java ├── ListMultimapPropertyTest.java ├── ListMutateMethodTest.java ├── ListPropertyTest.java ├── ListSourceTest.java ├── MapMutateMethodTest.java ├── MapPropertyTest.java ├── MapSourceTest.java ├── MergeActionTest.java ├── MultisetMutateMethodTest.java ├── MultisetPropertyTest.java ├── NullableMapperMethodTest.java ├── NullablePropertyFactoryTest.java ├── NullablePropertyTest.java ├── NullableSourceTest.java ├── OptionalMapperMethodTest.java ├── OptionalPropertyTest.java ├── PrimitiveOptionalPropertyTest.java ├── PrimitiveOptionalSourceTest.java ├── RequiredMapperMethodTest.java ├── RequiredPropertiesTest.java ├── SetMultimapMutateMethodTest.java ├── SetMultimapPropertyTest.java ├── SetMutateMethodTest.java ├── SetPropertyTest.java ├── SetSourceTest.java ├── SetType.java ├── SortedSetMutateMethodTest.java └── SortedSetPropertyTest.java ├── source ├── FilerUtilsTest.java ├── FunctionalTypeTest.java ├── LazyNameTest.java ├── MethodFinderTest.java ├── ObjectsExcerptsTest.java ├── Partial.java ├── PreconditionExcerptsTests.java ├── ScopeHandlerTests.java ├── ScopeTest.java ├── SourceBuilderTest.java ├── SourceParserTest.java ├── TypeClassTest.java ├── TypeMirrorAppenderTest.java ├── ValueTypeTest.java ├── feature │ └── SourceLevelTest.java └── testing │ ├── BehaviorTester.java │ ├── BehaviorTesterTest.java │ ├── CompilationException.java │ ├── CompilationUnit.java │ ├── Diagnostics.java │ ├── InMemoryFile.java │ ├── InMemoryJavaFile.java │ ├── MessagerRule.java │ ├── Model.java │ ├── ModelRule.java │ ├── ModelRuleTest.java │ ├── ModelTest.java │ ├── MultipleDiagnosticSubjects.java │ ├── ParameterizedBehaviorTestFactory.java │ ├── ParameterizedBehaviorTestRunner.java │ ├── SingleBehaviorTester.java │ ├── Target.java │ ├── TempJavaFileManager.java │ ├── TestBuilder.java │ ├── TestBuilderTest.java │ └── TypeMirrors.java └── testtype ├── AbstractNonComparable.java ├── NonComparable.java └── OtherNonComparable.java /.checkstyle: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.bat eol=crlf 2 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ "*" ] 8 | 9 | jobs: 10 | tests: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Set up JDK 11 15 | uses: actions/setup-java@v2 16 | with: 17 | java-version: '11' 18 | distribution: 'temurin' 19 | - name: Set $JAVA_11_HOME 20 | run: echo "JAVA_11_HOME=$JAVA_HOME" >> $GITHUB_ENV 21 | - name: Set up JDK 8 22 | uses: actions/setup-java@v2 23 | with: 24 | java-version: '8' 25 | distribution: 'temurin' 26 | - name: Set $JAVA_8_HOME 27 | run: echo "JAVA_8_HOME=$JAVA_HOME" >> $GITHUB_ENV 28 | - name: Validate gradle wrapper 29 | uses: gradle/wrapper-validation-action@v1 30 | - name: Build 31 | uses: gradle/gradle-build-action@937999e9cc2425eddc7fd62d1053baf041147db7 32 | with: 33 | arguments: build 34 | 35 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v*' 7 | 8 | jobs: 9 | maven-central: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v2 13 | - name: Set up JDK 11 14 | uses: actions/setup-java@v2 15 | with: 16 | java-version: '11' 17 | distribution: 'temurin' 18 | - name: Set $JAVA_11_HOME 19 | run: echo "JAVA_11_HOME=$JAVA_HOME" >> $GITHUB_ENV 20 | - name: Set up JDK 8 21 | uses: actions/setup-java@v2 22 | with: 23 | java-version: '8' 24 | distribution: 'temurin' 25 | - name: Set $JAVA_8_HOME 26 | run: echo "JAVA_8_HOME=$JAVA_HOME" >> $GITHUB_ENV 27 | - name: Validate gradle wrapper 28 | uses: gradle/wrapper-validation-action@v1 29 | - name: Run checks 30 | uses: gradle/gradle-build-action@937999e9cc2425eddc7fd62d1053baf041147db7 31 | with: 32 | arguments: check 33 | - name: Publish to Maven Central 34 | uses: gradle/gradle-build-action@937999e9cc2425eddc7fd62d1053baf041147db7 35 | with: 36 | arguments: publishToSonatype closeAndReleaseSonatypeStagingRepository 37 | env: 38 | ORG_GRADLE_PROJECT_signingKey: ${{ secrets.GPG_SIGNING_KEY }} 39 | ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.GPG_SIGNING_PASSWORD }} 40 | ORG_GRADLE_PROJECT_sonatypeUsername: ${{ secrets.MAVEN_CENTRAL_USERNAME }} 41 | ORG_GRADLE_PROJECT_sonatypePassword: ${{ secrets.MAVEN_CENTRAL_PASSWORD }} 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dependency-reduced-pom.xml 2 | 3 | # Java class files 4 | *.class 5 | 6 | # generated files 7 | bin/ 8 | gen/ 9 | 10 | # Eclipse project files 11 | .classpath 12 | .project 13 | .settings 14 | .test 15 | 16 | # Intellij project files 17 | *.iml 18 | *.ipr 19 | *.iws 20 | .idea/ 21 | 22 | # Gradle 23 | .gradletasknamecache 24 | .gradle/ 25 | build/ 26 | bin/ 27 | 28 | -------------------------------------------------------------------------------- /.test/.checkstyle: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Want to contribute? Great! First, read this page (including the small print at the end). 2 | 3 | ### Before you contribute 4 | Before we can use your code, you must sign the 5 | [Google Individual Contributor License Agreement](https://developers.google.com/open-source/cla/individual?csw=1) 6 | (CLA), which you can do online. The CLA is necessary mainly because you own the 7 | copyright to your changes, even after your contribution becomes part of our 8 | codebase, so we need your permission to use and distribute your code. We also 9 | need to be sure of various other things—for instance that you'll tell us if you 10 | know that your code infringes on other people's patents. You don't have to sign 11 | the CLA until after you've submitted your code for review and a member has 12 | approved it, but you must do it before we can put your code into our codebase. 13 | Before you start working on a larger contribution, you should get in touch with 14 | us first through the issue tracker with your idea so that we can help out and 15 | possibly guide you. Coordinating up front makes it much easier to avoid 16 | frustration later on. 17 | 18 | ### Building and testing 19 | FreeBuilder is built using [Gradle](http://gradle.org/). The following commands are especially useful: 20 | 21 | * `./gradlew eclipse` — Creates two Eclipse projects, `freebuilder` and `freebuilder-test`. 22 | * `./gradlew check` — Runs all unit and integration tests. These are automatically run against every PR, and will need to pass before any contribution will be accepted. 23 | 24 | ### Code reviews 25 | All submissions, including submissions by project members, require review. We 26 | use Github pull requests for this purpose. 27 | 28 | ### The small print 29 | Contributions made by corporations are covered by a different agreement than 30 | the one above, the Software Grant and Corporate Contributor License Agreement. 31 | -------------------------------------------------------------------------------- /config/checkstyle/suppressions.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | autoService=com.google.auto.service:auto-service:1.0-rc2 2 | googleJavaFormat=com.google.googlejavaformat:google-java-format:1.2 3 | guava=com.google.guava:guava:16.0 4 | guavaTestlib=com.google.guava:guava-testlib:17.0 5 | gwtUser=com.google.gwt:gwt-user:2.8.0 6 | hamcrest=org.hamcrest:hamcrest-all:1.3 7 | jacksonVersion=2.9.3 8 | javassist=org.javassist:javassist:3.19.0-GA 9 | jsr305=com.google.code.findbugs:jsr305:3.0.0 10 | junit=junit:junit:4.12 11 | mockito=org.mockito:mockito-core:1.10.8 12 | reflections=org.reflections:reflections:0.9.11 13 | truth=com.google.truth:truth:0.24 14 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inferred/FreeBuilder/6a0ba1b7007ab2a281c71eeec406efd7923dc39d/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /jar-footprint.txt: -------------------------------------------------------------------------------- 1 | META-INF/ 2 | META-INF/MANIFEST.MF 3 | META-INF/services/ 4 | META-INF/services/javax.annotation.processing.Processor 5 | org/ 6 | org/inferred/ 7 | org/inferred/freebuilder/ 8 | org/inferred/freebuilder/FreeBuilder.class 9 | org/inferred/freebuilder/IgnoredByEquals.class 10 | org/inferred/freebuilder/processor 11 | org/inferred/freebuilder/NotInToString.class 12 | org/inferred/freebuilder/shaded 13 | -------------------------------------------------------------------------------- /src/it/gwt/src/main/java/org/inferred/freebuilder/client/rpc/MapGwtType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import java.io.Serializable; 19 | import java.util.Map; 20 | 21 | import org.inferred.freebuilder.FreeBuilder; 22 | 23 | import com.google.common.annotations.GwtCompatible; 24 | 25 | /** Simple GWT-compatible FreeBuilder type with a map of strings to doubles. */ 26 | @FreeBuilder 27 | @GwtCompatible(serializable = true) 28 | public interface MapGwtType extends Serializable { 29 | Map getDistances(); 30 | 31 | /** Builder of {@link MapGwtType} instances. */ 32 | class Builder extends MapGwtType_Builder { } 33 | } 34 | -------------------------------------------------------------------------------- /src/it/gwt/src/main/java/org/inferred/freebuilder/client/rpc/NestedListGwtType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import java.io.Serializable; 19 | 20 | import org.inferred.freebuilder.FreeBuilder; 21 | 22 | import com.google.common.annotations.GwtCompatible; 23 | 24 | /** Simple GWT-compatible FreeBuilder type with a nested buildable type containing a list. */ 25 | @FreeBuilder 26 | @GwtCompatible(serializable = true) 27 | public interface NestedListGwtType extends Serializable { 28 | StringListGwtType getItem(); 29 | 30 | /** Builder for {@link NestedListGwtType}. */ 31 | class Builder extends NestedListGwtType_Builder { } 32 | } 33 | -------------------------------------------------------------------------------- /src/it/gwt/src/main/java/org/inferred/freebuilder/client/rpc/OptionalGwtType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import java.io.Serializable; 19 | 20 | import org.inferred.freebuilder.FreeBuilder; 21 | 22 | import com.google.common.annotations.GwtCompatible; 23 | import com.google.common.base.Optional; 24 | 25 | /** Simple GWT-compatible FreeBuilder type with a single optional String property. */ 26 | @FreeBuilder 27 | @GwtCompatible(serializable = true) 28 | public interface OptionalGwtType extends Serializable { 29 | Optional getName(); 30 | 31 | /** Builder for {@link OptionalGwtType}. */ 32 | class Builder extends OptionalGwtType_Builder { } 33 | } 34 | -------------------------------------------------------------------------------- /src/it/gwt/src/main/java/org/inferred/freebuilder/client/rpc/StringListGwtType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import java.io.Serializable; 19 | import java.util.List; 20 | 21 | import org.inferred.freebuilder.FreeBuilder; 22 | 23 | import com.google.common.annotations.GwtCompatible; 24 | 25 | /** Simple GWT-compatible FreeBuilder type with a list of strings. */ 26 | @FreeBuilder 27 | @GwtCompatible(serializable = true) 28 | public interface StringListGwtType extends Serializable { 29 | List getNames(); 30 | 31 | /** Builder for {@link StringListGwtType}. */ 32 | class Builder extends StringListGwtType_Builder { } 33 | } 34 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/GwtTests.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import com.google.gwt.junit.tools.GWTTestSuite; 4 | 5 | import org.inferred.freebuilder.client.rpc.MapTest; 6 | import org.inferred.freebuilder.client.rpc.NestedListTest; 7 | import org.inferred.freebuilder.client.rpc.OptionalTest; 8 | import org.inferred.freebuilder.client.rpc.StringListTest; 9 | 10 | public class GwtTests extends GWTTestSuite { 11 | 12 | public GwtTests() { 13 | addTest(new MapTest()); 14 | addTest(new NestedListTest()); 15 | addTest(new OptionalTest()); 16 | addTest(new StringListTest()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/MapTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.core.client.GWT; 19 | import com.google.gwt.junit.client.GWTTestCase; 20 | import com.google.gwt.user.client.rpc.AsyncCallback; 21 | import com.google.gwt.user.client.rpc.ServiceDefTarget; 22 | 23 | public class MapTest extends GWTTestCase { 24 | 25 | private static final int RPC_TIMEOUT = 15000; 26 | private static final String MODULE_NAME = "org.inferred.freebuilder.TestServer"; 27 | private static final MapGwtType VALUE = new MapGwtType.Builder() 28 | .putDistances("small", 1.0) 29 | .putDistances("medium", 10.3) 30 | .putDistances("large", 112.5) 31 | .build(); 32 | 33 | private MapTestServiceAsync customFieldSerializerTestService; 34 | 35 | @Override 36 | public String getModuleName() { 37 | return MODULE_NAME; 38 | } 39 | 40 | @Override 41 | protected void gwtSetUp() throws Exception { 42 | super.gwtSetUp(); 43 | delayTestFinish(RPC_TIMEOUT); 44 | } 45 | 46 | public void testSerialization() { 47 | MapTestServiceAsync service = getServiceAsync(); 48 | service.echo(VALUE, new AsyncCallback() { 49 | @Override 50 | public void onFailure(Throwable caught) { 51 | throw new AssertionError(caught); 52 | } 53 | 54 | @Override 55 | public void onSuccess(MapGwtType result) { 56 | assertEquals(VALUE, result); 57 | finishTest(); 58 | } 59 | }); 60 | } 61 | 62 | private MapTestServiceAsync getServiceAsync() { 63 | if (customFieldSerializerTestService == null) { 64 | customFieldSerializerTestService = 65 | (MapTestServiceAsync) GWT.create(MapTestService.class); 66 | ((ServiceDefTarget) customFieldSerializerTestService) 67 | .setServiceEntryPoint(GWT.getModuleBaseURL() + "map"); 68 | } 69 | return customFieldSerializerTestService; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/MapTestService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.user.client.rpc.RemoteService; 19 | 20 | public interface MapTestService extends RemoteService { 21 | MapGwtType echo(MapGwtType mapGwtType); 22 | } 23 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/MapTestServiceAsync.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.user.client.rpc.AsyncCallback; 19 | 20 | public interface MapTestServiceAsync { 21 | void echo(MapGwtType mapGwtType, AsyncCallback callback); 22 | } 23 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/NestedListTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.core.client.GWT; 19 | import com.google.gwt.junit.client.GWTTestCase; 20 | import com.google.gwt.user.client.rpc.AsyncCallback; 21 | import com.google.gwt.user.client.rpc.ServiceDefTarget; 22 | 23 | public class NestedListTest extends GWTTestCase { 24 | 25 | private static final int RPC_TIMEOUT = 15000; 26 | private static final String MODULE_NAME = "org.inferred.freebuilder.TestServer"; 27 | private static final NestedListGwtType VALUE = new NestedListGwtType.Builder() 28 | .setItem(new StringListGwtType.Builder() 29 | .addNames("john", "alice", "sam")) 30 | .build(); 31 | 32 | private NestedListTestServiceAsync customFieldSerializerTestService; 33 | 34 | @Override 35 | public String getModuleName() { 36 | return MODULE_NAME; 37 | } 38 | 39 | @Override 40 | protected void gwtSetUp() throws Exception { 41 | super.gwtSetUp(); 42 | delayTestFinish(RPC_TIMEOUT); 43 | } 44 | 45 | public void testSerialization() { 46 | NestedListTestServiceAsync service = getServiceAsync(); 47 | service.echo(VALUE, new AsyncCallback() { 48 | @Override 49 | public void onFailure(Throwable caught) { 50 | throw new AssertionError(caught); 51 | } 52 | 53 | @Override 54 | public void onSuccess(NestedListGwtType result) { 55 | assertEquals(VALUE, result); 56 | finishTest(); 57 | } 58 | }); 59 | } 60 | 61 | private NestedListTestServiceAsync getServiceAsync() { 62 | if (customFieldSerializerTestService == null) { 63 | customFieldSerializerTestService = 64 | (NestedListTestServiceAsync) GWT.create(NestedListTestService.class); 65 | ((ServiceDefTarget) customFieldSerializerTestService) 66 | .setServiceEntryPoint(GWT.getModuleBaseURL() + "nested-list"); 67 | } 68 | return customFieldSerializerTestService; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/NestedListTestService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.user.client.rpc.RemoteService; 19 | 20 | public interface NestedListTestService extends RemoteService { 21 | NestedListGwtType echo(NestedListGwtType nestedListGwtType); 22 | } 23 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/NestedListTestServiceAsync.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.user.client.rpc.AsyncCallback; 19 | 20 | public interface NestedListTestServiceAsync { 21 | void echo(NestedListGwtType nestedListGwtType, AsyncCallback callback); 22 | } 23 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/OptionalTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.core.client.GWT; 19 | import com.google.gwt.junit.client.GWTTestCase; 20 | import com.google.gwt.user.client.rpc.AsyncCallback; 21 | import com.google.gwt.user.client.rpc.ServiceDefTarget; 22 | 23 | public class OptionalTest extends GWTTestCase { 24 | 25 | private static final int RPC_TIMEOUT = 15000; 26 | private static final String MODULE_NAME = "org.inferred.freebuilder.TestServer"; 27 | private static final OptionalGwtType VALUE = new OptionalGwtType.Builder() 28 | .setName("Brian") 29 | .build(); 30 | 31 | private OptionalTestServiceAsync customFieldSerializerTestService; 32 | 33 | @Override 34 | public String getModuleName() { 35 | return MODULE_NAME; 36 | } 37 | 38 | @Override 39 | protected void gwtSetUp() throws Exception { 40 | super.gwtSetUp(); 41 | delayTestFinish(RPC_TIMEOUT); 42 | } 43 | 44 | public void testSerialization() { 45 | OptionalTestServiceAsync service = getServiceAsync(); 46 | service.echo(VALUE, new AsyncCallback() { 47 | @Override 48 | public void onFailure(Throwable caught) { 49 | throw new AssertionError(caught); 50 | } 51 | 52 | @Override 53 | public void onSuccess(OptionalGwtType result) { 54 | assertEquals(VALUE, result); 55 | finishTest(); 56 | } 57 | }); 58 | } 59 | 60 | private OptionalTestServiceAsync getServiceAsync() { 61 | if (customFieldSerializerTestService == null) { 62 | customFieldSerializerTestService = 63 | (OptionalTestServiceAsync) GWT.create(OptionalTestService.class); 64 | ((ServiceDefTarget) customFieldSerializerTestService) 65 | .setServiceEntryPoint(GWT.getModuleBaseURL() + "optional"); 66 | } 67 | return customFieldSerializerTestService; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/OptionalTestService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.user.client.rpc.RemoteService; 19 | 20 | public interface OptionalTestService extends RemoteService { 21 | OptionalGwtType echo(OptionalGwtType optionalGwtType); 22 | } 23 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/OptionalTestServiceAsync.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.user.client.rpc.AsyncCallback; 19 | 20 | public interface OptionalTestServiceAsync { 21 | void echo(OptionalGwtType optionalGwtType, AsyncCallback callback); 22 | } 23 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/StringListTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.core.client.GWT; 19 | import com.google.gwt.junit.client.GWTTestCase; 20 | import com.google.gwt.user.client.rpc.AsyncCallback; 21 | import com.google.gwt.user.client.rpc.ServiceDefTarget; 22 | 23 | public class StringListTest extends GWTTestCase { 24 | 25 | private static final int RPC_TIMEOUT = 15000; 26 | private static final String MODULE_NAME = "org.inferred.freebuilder.TestServer"; 27 | private static final StringListGwtType VALUE = new StringListGwtType.Builder() 28 | .addNames("john", "alice", "sam") 29 | .build(); 30 | 31 | private StringListTestServiceAsync customFieldSerializerTestService; 32 | 33 | @Override 34 | public String getModuleName() { 35 | return MODULE_NAME; 36 | } 37 | 38 | @Override 39 | protected void gwtSetUp() throws Exception { 40 | super.gwtSetUp(); 41 | delayTestFinish(RPC_TIMEOUT); 42 | } 43 | 44 | public void testSerialization() { 45 | StringListTestServiceAsync service = getServiceAsync(); 46 | service.echo(VALUE, new AsyncCallback() { 47 | @Override 48 | public void onFailure(Throwable caught) { 49 | throw new AssertionError(caught); 50 | } 51 | 52 | @Override 53 | public void onSuccess(StringListGwtType result) { 54 | assertEquals(VALUE, result); 55 | finishTest(); 56 | } 57 | }); 58 | } 59 | 60 | private StringListTestServiceAsync getServiceAsync() { 61 | if (customFieldSerializerTestService == null) { 62 | customFieldSerializerTestService = 63 | (StringListTestServiceAsync) GWT.create(StringListTestService.class); 64 | ((ServiceDefTarget) customFieldSerializerTestService) 65 | .setServiceEntryPoint(GWT.getModuleBaseURL() + "string-list"); 66 | } 67 | return customFieldSerializerTestService; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/StringListTestService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.user.client.rpc.RemoteService; 19 | 20 | public interface StringListTestService extends RemoteService { 21 | StringListGwtType echo(StringListGwtType stringListGwtType); 22 | } 23 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/client/rpc/StringListTestServiceAsync.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.client.rpc; 17 | 18 | import com.google.gwt.user.client.rpc.AsyncCallback; 19 | 20 | public interface StringListTestServiceAsync { 21 | void echo(StringListGwtType stringListGwtType, AsyncCallback callback); 22 | } 23 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/server/rpc/MapTestServiceImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.server.rpc; 17 | 18 | import org.inferred.freebuilder.client.rpc.MapGwtType; 19 | import org.inferred.freebuilder.client.rpc.MapTestService; 20 | 21 | import com.google.gwt.user.server.rpc.RemoteServiceServlet; 22 | 23 | public class MapTestServiceImpl extends RemoteServiceServlet implements MapTestService { 24 | 25 | @Override 26 | public MapGwtType echo(MapGwtType mapGwtType) { 27 | return mapGwtType; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/server/rpc/NestedListTestServiceImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.server.rpc; 17 | 18 | import org.inferred.freebuilder.client.rpc.NestedListGwtType; 19 | import org.inferred.freebuilder.client.rpc.NestedListTestService; 20 | 21 | import com.google.gwt.user.server.rpc.RemoteServiceServlet; 22 | 23 | public class NestedListTestServiceImpl extends RemoteServiceServlet 24 | implements NestedListTestService { 25 | 26 | @Override 27 | public NestedListGwtType echo(NestedListGwtType mapGwtType) { 28 | return mapGwtType; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/server/rpc/OptionalTestServiceImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.server.rpc; 17 | 18 | import org.inferred.freebuilder.client.rpc.OptionalGwtType; 19 | import org.inferred.freebuilder.client.rpc.OptionalTestService; 20 | 21 | import com.google.gwt.user.server.rpc.RemoteServiceServlet; 22 | 23 | public class OptionalTestServiceImpl extends RemoteServiceServlet implements OptionalTestService { 24 | 25 | @Override 26 | public OptionalGwtType echo(OptionalGwtType mapGwtType) { 27 | return mapGwtType; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/java/org/inferred/freebuilder/server/rpc/StringListTestServiceImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package org.inferred.freebuilder.server.rpc; 17 | 18 | import org.inferred.freebuilder.client.rpc.StringListGwtType; 19 | import org.inferred.freebuilder.client.rpc.StringListTestService; 20 | 21 | import com.google.gwt.user.server.rpc.RemoteServiceServlet; 22 | 23 | public class StringListTestServiceImpl extends RemoteServiceServlet 24 | implements StringListTestService { 25 | 26 | @Override 27 | public StringListGwtType echo(StringListGwtType mapGwtType) { 28 | return mapGwtType; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/it/gwt/src/test/resources/org/inferred/freebuilder/TestServer.gwt.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/it/lambda/src/main/java/org/inferred/freebuilder/BiMapPropertyType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import com.google.common.collect.BiMap; 4 | 5 | @FreeBuilder 6 | public interface BiMapPropertyType { 7 | 8 | BiMap getNumbers(); 9 | 10 | class Builder extends BiMapPropertyType_Builder {} 11 | } 12 | -------------------------------------------------------------------------------- /src/it/lambda/src/main/java/org/inferred/freebuilder/DefaultedPropertiesType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | @FreeBuilder 4 | public interface DefaultedPropertiesType { 5 | String getFirstName(); 6 | String getSurname(); 7 | 8 | class Builder extends DefaultedPropertiesType_Builder { 9 | public Builder() { 10 | setFirstName("Joe"); 11 | setSurname("Bloggs"); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/it/lambda/src/main/java/org/inferred/freebuilder/ListMultimapPropertyType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import com.google.common.collect.ListMultimap; 4 | 5 | @FreeBuilder 6 | public interface ListMultimapPropertyType { 7 | ListMultimap getNumbers(); 8 | 9 | class Builder extends ListMultimapPropertyType_Builder {} 10 | } 11 | -------------------------------------------------------------------------------- /src/it/lambda/src/main/java/org/inferred/freebuilder/ListPropertyType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import java.util.List; 4 | 5 | @FreeBuilder 6 | public interface ListPropertyType { 7 | List getNames(); 8 | 9 | class Builder extends ListPropertyType_Builder {} 10 | } 11 | -------------------------------------------------------------------------------- /src/it/lambda/src/main/java/org/inferred/freebuilder/MapPropertyType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import java.util.Map; 4 | 5 | @FreeBuilder 6 | public interface MapPropertyType { 7 | Map getNumbers(); 8 | 9 | class Builder extends MapPropertyType_Builder {} 10 | } 11 | -------------------------------------------------------------------------------- /src/it/lambda/src/main/java/org/inferred/freebuilder/MultisetPropertyType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import com.google.common.collect.Multiset; 4 | 5 | @FreeBuilder 6 | public interface MultisetPropertyType { 7 | Multiset getNames(); 8 | 9 | class Builder extends MultisetPropertyType_Builder {} 10 | } 11 | -------------------------------------------------------------------------------- /src/it/lambda/src/main/java/org/inferred/freebuilder/OptionalPropertiesType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import com.google.common.annotations.GwtCompatible; 19 | 20 | import java.io.Serializable; 21 | import java.util.Optional; 22 | 23 | @FreeBuilder 24 | @GwtCompatible(serializable = true) 25 | public interface OptionalPropertiesType extends Serializable { 26 | Optional getFirstName(); 27 | Optional getSurname(); 28 | 29 | class Builder extends OptionalPropertiesType_Builder { } 30 | } 31 | -------------------------------------------------------------------------------- /src/it/lambda/src/main/java/org/inferred/freebuilder/RequiredPropertiesType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | @FreeBuilder 4 | public interface RequiredPropertiesType { 5 | String getFirstName(); 6 | String getSurname(); 7 | 8 | class Builder extends RequiredPropertiesType_Builder {} 9 | } 10 | -------------------------------------------------------------------------------- /src/it/lambda/src/main/java/org/inferred/freebuilder/SetMultimapPropertyType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import com.google.common.collect.SetMultimap; 4 | 5 | @FreeBuilder 6 | public interface SetMultimapPropertyType { 7 | SetMultimap getNumbers(); 8 | 9 | class Builder extends SetMultimapPropertyType_Builder {} 10 | } 11 | -------------------------------------------------------------------------------- /src/it/lambda/src/main/java/org/inferred/freebuilder/SetPropertyType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import java.util.Set; 4 | 5 | @FreeBuilder 6 | public interface SetPropertyType { 7 | Set getNames(); 8 | 9 | class Builder extends SetPropertyType_Builder {} 10 | } 11 | -------------------------------------------------------------------------------- /src/it/lambda/src/test/java/org/inferred/freebuilder/BiMapPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import com.google.common.collect.ImmutableBiMap; 6 | import org.junit.Rule; 7 | import org.junit.Test; 8 | import org.junit.rules.ExpectedException; 9 | 10 | public class BiMapPropertiesTest { 11 | 12 | @Rule 13 | public final ExpectedException thrown = ExpectedException.none(); 14 | 15 | @Test 16 | public void testMutate() { 17 | BiMapPropertyType value = new BiMapPropertyType.Builder() 18 | .putNumbers(1, "one") 19 | .putNumbers(2, "two") 20 | .mutateNumbers(numbers -> numbers.replaceAll((i, s) -> s.toUpperCase() + " (" + i + ")")) 21 | .build(); 22 | assertEquals(ImmutableBiMap.of(1, "ONE (1)", 2, "TWO (2)"), value.getNumbers()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/it/lambda/src/test/java/org/inferred/freebuilder/DefaultedPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Rule; 6 | import org.junit.Test; 7 | import org.junit.rules.ExpectedException; 8 | 9 | import java.util.function.UnaryOperator; 10 | 11 | public class DefaultedPropertiesTest { 12 | 13 | @Rule public final ExpectedException thrown = ExpectedException.none(); 14 | 15 | @Test 16 | public void testMap() { 17 | DefaultedPropertiesType value = new DefaultedPropertiesType.Builder() 18 | .setFirstName("joe") 19 | .setSurname("bloggs") 20 | .mapFirstName(CAPITALIZE) 21 | .mapSurname(CAPITALIZE) 22 | .build(); 23 | assertEquals("Joe", value.getFirstName()); 24 | assertEquals("Bloggs", value.getSurname()); 25 | } 26 | 27 | private static final UnaryOperator CAPITALIZE = s -> 28 | s.substring(0, 1).toUpperCase() + s.substring(1, s.length()); 29 | } 30 | -------------------------------------------------------------------------------- /src/it/lambda/src/test/java/org/inferred/freebuilder/ListMultimapPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import com.google.common.collect.ImmutableListMultimap; 6 | 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.rules.ExpectedException; 10 | 11 | public class ListMultimapPropertiesTest { 12 | 13 | @Rule public final ExpectedException thrown = ExpectedException.none(); 14 | 15 | @Test 16 | public void testMutate() { 17 | ListMultimapPropertyType value = new ListMultimapPropertyType.Builder() 18 | .putNumbers(1, "one") 19 | .putNumbers(1, "uno") 20 | .putNumbers(2, "two") 21 | .putNumbers(2, "dos") 22 | .mutateNumbers(numbers -> numbers.entries().forEach(entry -> entry.setValue( 23 | entry.getValue().toUpperCase() + " (" + entry.getKey() + ")"))) 24 | .build(); 25 | assertEquals(ImmutableListMultimap.of(1, "ONE (1)", 1, "UNO (1)", 2, "TWO (2)", 2, "DOS (2)"), 26 | value.getNumbers()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/it/lambda/src/test/java/org/inferred/freebuilder/ListPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import com.google.common.collect.ImmutableList; 6 | 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.rules.ExpectedException; 10 | 11 | public class ListPropertiesTest { 12 | 13 | @Rule public final ExpectedException thrown = ExpectedException.none(); 14 | 15 | @Test 16 | public void testMutate() { 17 | ListPropertyType value = new ListPropertyType.Builder() 18 | .addNames("Alan", "Bob", "Chris", "Diana", "Emma", "Fred") 19 | .mutateNames(names -> names.subList(2, 4).clear()) 20 | .build(); 21 | assertEquals(ImmutableList.of("Alan", "Bob", "Emma", "Fred"), value.getNames()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/it/lambda/src/test/java/org/inferred/freebuilder/MapPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import com.google.common.collect.ImmutableMap; 6 | 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.rules.ExpectedException; 10 | 11 | public class MapPropertiesTest { 12 | 13 | @Rule public final ExpectedException thrown = ExpectedException.none(); 14 | 15 | @Test 16 | public void testMutate() { 17 | MapPropertyType value = new MapPropertyType.Builder() 18 | .putNumbers(1, "one") 19 | .putNumbers(2, "two") 20 | .mutateNumbers(numbers -> numbers.replaceAll((i, s) -> s.toUpperCase() + " (" + i + ")")) 21 | .build(); 22 | assertEquals(ImmutableMap.of(1, "ONE (1)", 2, "TWO (2)"), value.getNumbers()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/it/lambda/src/test/java/org/inferred/freebuilder/MultisetPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import com.google.common.collect.ImmutableMultiset; 6 | 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.rules.ExpectedException; 10 | 11 | public class MultisetPropertiesTest { 12 | 13 | @Rule public final ExpectedException thrown = ExpectedException.none(); 14 | 15 | @Test 16 | public void testMutate() { 17 | MultisetPropertyType value = new MultisetPropertyType.Builder() 18 | .addNames("Alan", "Bob", "Chris", "Diana", "Emma", "Fred") 19 | .mutateNames(names -> names.removeIf(name -> name.matches("[CD].*"))) 20 | .build(); 21 | assertEquals(ImmutableMultiset.of("Alan", "Bob", "Emma", "Fred"), value.getNames()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/it/lambda/src/test/java/org/inferred/freebuilder/OptionalPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Rule; 6 | import org.junit.Test; 7 | import org.junit.rules.ExpectedException; 8 | 9 | import java.util.Optional; 10 | import java.util.function.UnaryOperator; 11 | 12 | public class OptionalPropertiesTest { 13 | 14 | @Rule public final ExpectedException thrown = ExpectedException.none(); 15 | 16 | @Test 17 | public void testMap() { 18 | OptionalPropertiesType value = new OptionalPropertiesType.Builder() 19 | .setFirstName("joe") 20 | .setSurname("bloggs") 21 | .mapFirstName(CAPITALIZE) 22 | .mapSurname(CAPITALIZE) 23 | .build(); 24 | assertEquals(Optional.of("Joe"), value.getFirstName()); 25 | assertEquals(Optional.of("Bloggs"), value.getSurname()); 26 | } 27 | 28 | private static final UnaryOperator CAPITALIZE = s -> 29 | s.substring(0, 1).toUpperCase() + s.substring(1, s.length()); 30 | } 31 | -------------------------------------------------------------------------------- /src/it/lambda/src/test/java/org/inferred/freebuilder/RequiredPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Rule; 6 | import org.junit.Test; 7 | import org.junit.rules.ExpectedException; 8 | 9 | import java.util.function.UnaryOperator; 10 | 11 | public class RequiredPropertiesTest { 12 | 13 | @Rule public final ExpectedException thrown = ExpectedException.none(); 14 | 15 | @Test 16 | public void testMap() { 17 | RequiredPropertiesType value = new RequiredPropertiesType.Builder() 18 | .setFirstName("joe") 19 | .setSurname("bloggs") 20 | .mapFirstName(CAPITALIZE) 21 | .mapSurname(CAPITALIZE) 22 | .build(); 23 | assertEquals("Joe", value.getFirstName()); 24 | assertEquals("Bloggs", value.getSurname()); 25 | } 26 | 27 | private static final UnaryOperator CAPITALIZE = s -> 28 | s.substring(0, 1).toUpperCase() + s.substring(1, s.length()); 29 | } 30 | -------------------------------------------------------------------------------- /src/it/lambda/src/test/java/org/inferred/freebuilder/SetMultimapPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import com.google.common.collect.ImmutableSetMultimap; 6 | 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.rules.ExpectedException; 10 | 11 | public class SetMultimapPropertiesTest { 12 | 13 | @Rule public final ExpectedException thrown = ExpectedException.none(); 14 | 15 | @Test 16 | public void testMutate() { 17 | SetMultimapPropertyType value = new SetMultimapPropertyType.Builder() 18 | .putNumbers(1, "one") 19 | .putNumbers(1, "uno") 20 | .putNumbers(2, "two") 21 | .putNumbers(2, "dos") 22 | .mutateNumbers(numbers -> numbers.removeAll(2)) 23 | .build(); 24 | assertEquals(ImmutableSetMultimap.of(1, "one", 1, "uno"), value.getNumbers()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/it/lambda/src/test/java/org/inferred/freebuilder/SetPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import com.google.common.collect.ImmutableSet; 6 | 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.rules.ExpectedException; 10 | 11 | public class SetPropertiesTest { 12 | 13 | @Rule public final ExpectedException thrown = ExpectedException.none(); 14 | 15 | @Test 16 | public void testMutate() { 17 | SetPropertyType value = new SetPropertyType.Builder() 18 | .addNames("Alan", "Bob", "Chris", "Diana", "Emma", "Fred") 19 | .mutateNames(names -> names.removeIf(name -> name.matches("[CD].*"))) 20 | .build(); 21 | assertEquals(ImmutableSet.of("Alan", "Bob", "Emma", "Fred"), value.getNames()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/it/no-guava-j8/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.goals = clean test 2 | -------------------------------------------------------------------------------- /src/it/no-guava-j8/src/main/java/org/inferred/freebuilder/OptionalProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed under the Apache License, Version 2.0 (the "License"); 3 | * you may not use this file except in compliance with the License. 4 | * You may obtain a copy of the License at 5 | * 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * 8 | * Unless required by applicable law or agreed to in writing, software 9 | * distributed under the License is distributed on an "AS IS" BASIS, 10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the License for the specific language governing permissions and 12 | * limitations under the License. 13 | */ 14 | package org.inferred.freebuilder; 15 | 16 | import java.io.Serializable; 17 | import java.util.Optional; 18 | 19 | @FreeBuilder 20 | public interface OptionalProperties extends Serializable { 21 | Optional getFirstName(); 22 | Optional getMiddleName(); 23 | Optional getSurname(); 24 | 25 | class Builder extends OptionalProperties_Builder { } 26 | } 27 | -------------------------------------------------------------------------------- /src/it/no-guava-j8/src/main/java/org/inferred/freebuilder/OptionalProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed under the Apache License, Version 2.0 (the "License"); 3 | * you may not use this file except in compliance with the License. 4 | * You may obtain a copy of the License at 5 | * 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * 8 | * Unless required by applicable law or agreed to in writing, software 9 | * distributed under the License is distributed on an "AS IS" BASIS, 10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the License for the specific language governing permissions and 12 | * limitations under the License. 13 | */ 14 | package org.inferred.freebuilder; 15 | 16 | import java.io.Serializable; 17 | import java.util.Optional; 18 | 19 | @FreeBuilder 20 | public interface OptionalProperty extends Serializable { 21 | Optional getName(); 22 | 23 | class Builder extends OptionalProperty_Builder { } 24 | } 25 | -------------------------------------------------------------------------------- /src/it/no-guava-j8/src/test/java/org/inferred/freebuilder/OptionalPropertiesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.junit.runners.JUnit4; 23 | 24 | import java.util.Optional; 25 | 26 | @RunWith(JUnit4.class) 27 | public class OptionalPropertiesTest { 28 | 29 | private static final String NAME_1 = "name1"; 30 | private static final String NAME_2 = "name2"; 31 | private static final String NAME_3 = "name3"; 32 | 33 | @Test 34 | public void testSetters() { 35 | OptionalProperties.Builder builder = new OptionalProperties.Builder(); 36 | assertEquals(Optional.empty(), builder.build().getFirstName()); 37 | assertEquals(Optional.empty(), builder.build().getMiddleName()); 38 | assertEquals(Optional.empty(), builder.build().getSurname()); 39 | assertEquals("OptionalProperties{}", builder.build().toString()); 40 | builder.setFirstName(NAME_1); 41 | assertEquals( 42 | "OptionalProperties{firstName=name1}", 43 | builder.build().toString()); 44 | builder.setMiddleName(NAME_2); 45 | assertEquals( 46 | "OptionalProperties{firstName=name1, middleName=name2}", 47 | builder.build().toString()); 48 | builder.setSurname(NAME_3); 49 | assertEquals(Optional.of(NAME_1), builder.build().getFirstName()); 50 | assertEquals(Optional.of(NAME_2), builder.build().getMiddleName()); 51 | assertEquals(Optional.of(NAME_3), builder.build().getSurname()); 52 | assertEquals( 53 | "OptionalProperties{firstName=name1, middleName=name2, surname=name3}", 54 | builder.build().toString()); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/it/no-guava-j8/src/test/java/org/inferred/freebuilder/OptionalPropertyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.junit.runners.JUnit4; 23 | 24 | import java.util.Optional; 25 | 26 | /** FreeBuilder test using {@link ListProperty}. */ 27 | @RunWith(JUnit4.class) 28 | public class OptionalPropertyTest { 29 | 30 | private static final String NAME_1 = "name1"; 31 | private static final String NAME_2 = "name2"; 32 | private static final String NAME_3 = "name3"; 33 | 34 | @Test 35 | public void testAllMethodInteractions() { 36 | OptionalProperty.Builder builder = new OptionalProperty.Builder(); 37 | assertEquals(Optional.empty(), builder.build().getName()); 38 | builder.setName(NAME_1); 39 | assertEquals(Optional.of(NAME_1), builder.build().getName()); 40 | builder.mapName(n -> n.substring(0, 4) + "2"); 41 | assertEquals(Optional.of(NAME_2), builder.build().getName()); 42 | builder.clearName(); 43 | assertEquals(Optional.empty(), builder.build().getName()); 44 | builder.setName(NAME_3); 45 | assertEquals(Optional.of(NAME_3), builder.build().getName()); 46 | builder.clear(); 47 | assertEquals(Optional.empty(), builder.build().getName()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/it/no-guava/src/main/java/org/inferred/freebuilder/ListProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import java.io.Serializable; 19 | import java.util.List; 20 | 21 | @FreeBuilder 22 | public interface ListProperty extends Serializable { 23 | List getNames(); 24 | 25 | class Builder extends ListProperty_Builder { } 26 | } 27 | -------------------------------------------------------------------------------- /src/it/no-guava/src/main/java/org/inferred/freebuilder/MapProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import java.io.Serializable; 19 | import java.util.Map; 20 | 21 | @FreeBuilder 22 | public interface MapProperty extends Serializable { 23 | Map getDistances(); 24 | 25 | class Builder extends MapProperty_Builder { } 26 | } 27 | -------------------------------------------------------------------------------- /src/it/no-guava/src/main/java/org/inferred/freebuilder/PrimitiveProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | @FreeBuilder 19 | public interface PrimitiveProperty { 20 | int getProperty(); 21 | 22 | class Builder extends PrimitiveProperty_Builder {} 23 | } 24 | -------------------------------------------------------------------------------- /src/it/no-guava/src/main/java/org/inferred/freebuilder/SetProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import java.io.Serializable; 19 | import java.util.Set; 20 | 21 | @FreeBuilder 22 | public interface SetProperty extends Serializable { 23 | Set getNames(); 24 | 25 | class Builder extends SetProperty_Builder { } 26 | } 27 | -------------------------------------------------------------------------------- /src/it/no-guava/src/test/java/org/inferred/freebuilder/ListPropertyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static java.util.Arrays.asList; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | 22 | import org.junit.Test; 23 | import org.junit.runner.RunWith; 24 | import org.junit.runners.JUnit4; 25 | 26 | /** FreeBuilder test using {@link ListProperty}. */ 27 | @RunWith(JUnit4.class) 28 | public class ListPropertyTest { 29 | 30 | private static final String NAME_1 = "name1"; 31 | private static final String NAME_2 = "name2"; 32 | private static final String NAME_3 = "name3"; 33 | 34 | @Test 35 | public void testAllMethodInteractions() { 36 | ListProperty.Builder builder = new ListProperty.Builder(); 37 | builder.addNames(NAME_1); 38 | assertEquals(asList(NAME_1), builder.build().getNames()); 39 | builder.addNames(NAME_2, NAME_3); 40 | assertEquals(asList(NAME_1, NAME_2, NAME_3), builder.build().getNames()); 41 | builder.clearNames(); 42 | assertEquals(asList(), builder.build().getNames()); 43 | builder.addAllNames(asList(NAME_2, NAME_1)); 44 | assertEquals(asList(NAME_2, NAME_1), builder.build().getNames()); 45 | builder.clear(); 46 | assertEquals(asList(), builder.build().getNames()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/it/no-guava/src/test/java/org/inferred/freebuilder/PrimitivePropertyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.junit.runners.JUnit4; 23 | 24 | /** 25 | * Integration test for {@link org.inferred.freebuilder.FreeBuilder 26 | * FreeBuilder}, using the {@link PrimitiveProperty}. 27 | */ 28 | @RunWith(JUnit4.class) 29 | public class PrimitivePropertyTest { 30 | 31 | @Test 32 | public void test() { 33 | PrimitiveProperty b = new PrimitiveProperty.Builder() 34 | .setProperty(11) 35 | .build(); 36 | assertEquals(11, b.getProperty()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/it/no-guava/src/test/java/org/inferred/freebuilder/SetPropertyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.junit.runners.JUnit4; 23 | 24 | import java.util.HashSet; 25 | import java.util.Set; 26 | 27 | /** FreeBuilder test using {@link ListProperty}. */ 28 | @RunWith(JUnit4.class) 29 | public class SetPropertyTest { 30 | 31 | private static final String NAME_1 = "name1"; 32 | private static final String NAME_2 = "name2"; 33 | private static final String NAME_3 = "name3"; 34 | 35 | @Test 36 | public void testAllMethodInteractions() { 37 | SetProperty.Builder builder = new SetProperty.Builder(); 38 | builder.addNames(NAME_1); 39 | assertEquals(newHashSet(NAME_1), builder.build().getNames()); 40 | builder.addNames(NAME_1); 41 | assertEquals(newHashSet(NAME_1), builder.build().getNames()); 42 | builder.addNames(NAME_2, NAME_3); 43 | assertEquals(newHashSet(NAME_1, NAME_2, NAME_3), builder.build().getNames()); 44 | builder.clearNames(); 45 | assertEquals(newHashSet(), builder.build().getNames()); 46 | builder.addAllNames(newHashSet(NAME_2, NAME_1)); 47 | assertEquals(newHashSet(NAME_2, NAME_1), builder.build().getNames()); 48 | builder.clear(); 49 | assertEquals(newHashSet(), builder.build().getNames()); 50 | } 51 | 52 | @SafeVarargs 53 | private static Set newHashSet(E... items) { 54 | Set list = new HashSet<>(); 55 | for (E item : items) { 56 | list.add(item); 57 | } 58 | return list; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/it/vanilla/src/main/java/org/inferred/freebuilder/DefaultedPropertiesType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | @FreeBuilder 4 | public interface DefaultedPropertiesType { 5 | String getFirstName(); 6 | String getSurname(); 7 | 8 | class Builder extends DefaultedPropertiesType_Builder { 9 | public Builder() { 10 | setFirstName("Joe"); 11 | setSurname("Bloggs"); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/it/vanilla/src/main/java/org/inferred/freebuilder/JacksonAnyGetterType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import com.fasterxml.jackson.annotation.JsonAnyGetter; 4 | import com.fasterxml.jackson.annotation.JsonProperty; 5 | import com.fasterxml.jackson.databind.JsonNode; 6 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 7 | 8 | import java.util.Map; 9 | 10 | @FreeBuilder 11 | @JsonDeserialize(builder = JacksonAnyGetterType.Builder.class) 12 | public interface JacksonAnyGetterType { 13 | @JsonProperty("simple_property") 14 | String getSimpleProperty(); 15 | @JsonAnyGetter 16 | Map getUnknownProperties(); 17 | 18 | class Builder extends JacksonAnyGetterType_Builder {} 19 | } 20 | -------------------------------------------------------------------------------- /src/it/vanilla/src/main/java/org/inferred/freebuilder/MapGwtType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import com.google.common.annotations.GwtCompatible; 19 | 20 | import java.io.Serializable; 21 | import java.util.Map; 22 | 23 | /** Simple GWT-compatible FreeBuilder type with a map of strings to doubles. */ 24 | @FreeBuilder 25 | @GwtCompatible(serializable = true) 26 | public interface MapGwtType extends Serializable { 27 | Map getDistances(); 28 | 29 | /** Builder for {@link StringListGwtType}. */ 30 | class Builder extends MapGwtType_Builder { } 31 | } 32 | -------------------------------------------------------------------------------- /src/it/vanilla/src/main/java/org/inferred/freebuilder/NestedListGwtType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import com.google.common.annotations.GwtCompatible; 19 | 20 | import java.io.Serializable; 21 | 22 | /** Simple GWT-compatible FreeBuilder type with a nested buildable type containing a list. */ 23 | @FreeBuilder 24 | @GwtCompatible(serializable = true) 25 | public interface NestedListGwtType extends Serializable { 26 | StringListGwtType getItem(); 27 | 28 | /** Builder for {@link NestedListGwtType}. */ 29 | class Builder extends NestedListGwtType_Builder { } 30 | } 31 | -------------------------------------------------------------------------------- /src/it/vanilla/src/main/java/org/inferred/freebuilder/NoFrillsDataInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | /** A trivial {@link FreeBuilder @FreeBuilder} interface with no customization. */ 19 | @FreeBuilder 20 | public interface NoFrillsDataInterface { 21 | int getPropertyA(); 22 | boolean isPropertyB(); 23 | 24 | /** Builder for {@link NoFrillsDataInterface}. */ 25 | class Builder extends NoFrillsDataInterface_Builder {} 26 | } 27 | -------------------------------------------------------------------------------- /src/it/vanilla/src/main/java/org/inferred/freebuilder/NoFrillsDataType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | /** A trivial {@link FreeBuilder @FreeBuilder} type with no customization. */ 19 | @FreeBuilder 20 | public abstract class NoFrillsDataType { 21 | public abstract int getPropertyA(); 22 | public abstract boolean isPropertyB(); 23 | 24 | /** Builder for {@link NoFrillsDataType}. */ 25 | public static class Builder extends NoFrillsDataType_Builder {} 26 | public static Builder builder() { 27 | return new Builder(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/it/vanilla/src/main/java/org/inferred/freebuilder/OptionalGwtType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import com.google.common.annotations.GwtCompatible; 19 | import com.google.common.base.Optional; 20 | 21 | import java.io.Serializable; 22 | 23 | /** Simple GWT-compatible FreeBuilder type with a single optional String property. */ 24 | @FreeBuilder 25 | @GwtCompatible(serializable = true) 26 | public interface OptionalGwtType extends Serializable { 27 | Optional getName(); 28 | 29 | /** Builder for {@link OptionalGwtType}. */ 30 | class Builder extends OptionalGwtType_Builder { } 31 | } 32 | -------------------------------------------------------------------------------- /src/it/vanilla/src/main/java/org/inferred/freebuilder/RequiredPropertiesType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | @FreeBuilder 4 | public interface RequiredPropertiesType { 5 | String getFirstName(); 6 | String getSurname(); 7 | 8 | class Builder extends RequiredPropertiesType_Builder {} 9 | } 10 | -------------------------------------------------------------------------------- /src/it/vanilla/src/main/java/org/inferred/freebuilder/StringListGwtType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import com.google.common.annotations.GwtCompatible; 19 | 20 | import java.io.Serializable; 21 | import java.util.List; 22 | 23 | /** Simple GWT-compatible FreeBuilder type with a list of strings. */ 24 | @FreeBuilder 25 | @GwtCompatible(serializable = true) 26 | public interface StringListGwtType extends Serializable { 27 | List getNames(); 28 | 29 | /** Builder for {@link StringListGwtType}. */ 30 | class Builder extends StringListGwtType_Builder { } 31 | } 32 | -------------------------------------------------------------------------------- /src/it/vanilla/src/main/java/org/inferred/freebuilder/WildcardListType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import java.util.List; 19 | 20 | @FreeBuilder 21 | public interface WildcardListType { 22 | List numbers(); 23 | 24 | class Builder extends WildcardListType_Builder { } 25 | } 26 | -------------------------------------------------------------------------------- /src/it/vanilla/src/test/java/org/inferred/freebuilder/DefaultsOptimizationTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import com.google.common.collect.ImmutableSet; 6 | 7 | import org.junit.Test; 8 | 9 | import java.lang.reflect.Field; 10 | import java.lang.reflect.Modifier; 11 | import java.util.HashSet; 12 | import java.util.Set; 13 | 14 | public class DefaultsOptimizationTest { 15 | 16 | @Test 17 | public void testNoDefaultsNoOptimisation() { 18 | assertEquals( 19 | ImmutableSet.of("firstName", "surname", "_unsetProperties"), 20 | fieldsOn(RequiredPropertiesType.Builder.class.getSuperclass())); 21 | } 22 | 23 | @Test 24 | public void testDefaultsOptimisation() { 25 | assertEquals( 26 | ImmutableSet.of("firstName", "surname"), 27 | fieldsOn(DefaultedPropertiesType.Builder.class.getSuperclass())); 28 | } 29 | 30 | private static Set fieldsOn(Class cls) { 31 | Set generatedFields = new HashSet<>(); 32 | for (Field field : cls.getDeclaredFields()) { 33 | if (!Modifier.isStatic(field.getModifiers())) { 34 | generatedFields.add(field.getName()); 35 | } 36 | } 37 | return generatedFields; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/it/vanilla/src/test/java/org/inferred/freebuilder/JacksonAnyGetterTypeTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import com.fasterxml.jackson.databind.node.IntNode; 6 | import com.fasterxml.jackson.databind.node.TextNode; 7 | import org.junit.Test; 8 | 9 | import java.io.IOException; 10 | 11 | import static org.junit.Assert.assertEquals; 12 | import static org.junit.Assert.assertNotNull; 13 | import static org.junit.Assert.assertTrue; 14 | 15 | public class JacksonAnyGetterTypeTest { 16 | 17 | private final ObjectMapper objectMapper = new ObjectMapper(); 18 | 19 | @Test 20 | public void testDeserializeJsonWithAnyGetter() throws IOException { 21 | JacksonAnyGetterType parsed = objectMapper.readValue( 22 | "{\"simple_property\": \"simpleValue\", \"other_property\": 3}", 23 | JacksonAnyGetterType.class); 24 | 25 | assertEquals("simpleValue", parsed.getSimpleProperty()); 26 | assertNotNull(parsed.getUnknownProperties()); 27 | assertEquals(1, parsed.getUnknownProperties().size()); 28 | assertEquals(new IntNode(3), parsed.getUnknownProperties().get("other_property")); 29 | } 30 | 31 | @Test 32 | public void testSerializeJsonWithAnyGetter() throws JsonProcessingException { 33 | JacksonAnyGetterType getterType = new JacksonAnyGetterType.Builder() 34 | .setSimpleProperty("checkValue") 35 | .putUnknownProperties("propertyOne", new TextNode("abc")) 36 | .putUnknownProperties("propertyTwo", new IntNode(2)).build(); 37 | 38 | String json = objectMapper.writeValueAsString(getterType); 39 | assertTrue("should contain simple_property", 40 | json.contains("\"simple_property\":\"checkValue\"")); 41 | assertTrue("should contain propertyOne", json.contains("\"propertyOne\":\"abc\"")); 42 | assertTrue("should contain propertyTwo", json.contains("\"propertyTwo\":2")); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/it/vanilla/src/test/java/org/inferred/freebuilder/MapGwtTypeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import com.google.common.collect.ImmutableMap; 21 | 22 | import java.util.Map; 23 | 24 | import org.junit.Test; 25 | import org.junit.runner.RunWith; 26 | import org.junit.runners.JUnit4; 27 | 28 | /** FreeBuilder test using {@link MapGwtType}. */ 29 | @RunWith(JUnit4.class) 30 | public class MapGwtTypeTest { 31 | 32 | private static final String NAME_1 = "name1"; 33 | private static final String NAME_2 = "name2"; 34 | private static final String NAME_3 = "name3"; 35 | private static final double DISTANCE_1 = 8.7; 36 | private static final double DISTANCE_2 = 13.1; 37 | private static final double DISTANCE_3 = 27.0; 38 | 39 | @Test 40 | public void testAllMethodInteractions() { 41 | MapGwtType.Builder builder = new MapGwtType.Builder(); 42 | Map distancesView = builder.getDistances(); 43 | assertEquals(ImmutableMap.of(), distancesView); 44 | assertEquals(ImmutableMap.of(), builder.build().getDistances()); 45 | builder.putDistances(NAME_1, DISTANCE_1); 46 | assertEquals(ImmutableMap.of(NAME_1, DISTANCE_1), distancesView); 47 | assertEquals(ImmutableMap.of(NAME_1, DISTANCE_1), builder.build().getDistances()); 48 | builder.putAllDistances(ImmutableMap.of(NAME_2, DISTANCE_2, NAME_3, DISTANCE_3)); 49 | assertEquals(ImmutableMap.of(NAME_1, DISTANCE_1, NAME_2, DISTANCE_2, NAME_3, DISTANCE_3), 50 | distancesView); 51 | assertEquals(ImmutableMap.of(NAME_1, DISTANCE_1, NAME_2, DISTANCE_2, NAME_3, DISTANCE_3), 52 | builder.build().getDistances()); 53 | builder.removeDistances(NAME_2); 54 | assertEquals(ImmutableMap.of(NAME_1, DISTANCE_1, NAME_3, DISTANCE_3), distancesView); 55 | assertEquals(ImmutableMap.of(NAME_1, DISTANCE_1, NAME_3, DISTANCE_3), 56 | builder.build().getDistances()); 57 | builder.clearDistances(); 58 | assertEquals(ImmutableMap.of(), distancesView); 59 | assertEquals(ImmutableMap.of(), builder.build().getDistances()); 60 | builder.putDistances(NAME_1, DISTANCE_1); 61 | assertEquals(ImmutableMap.of(NAME_1, DISTANCE_1), distancesView); 62 | assertEquals(ImmutableMap.of(NAME_1, DISTANCE_1), builder.build().getDistances()); 63 | builder.clear(); 64 | assertEquals(ImmutableMap.of(), distancesView); 65 | assertEquals(ImmutableMap.of(), builder.build().getDistances()); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/it/vanilla/src/test/java/org/inferred/freebuilder/NestedListGwtTypeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static java.util.Arrays.asList; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | 22 | import org.junit.Test; 23 | import org.junit.runner.RunWith; 24 | import org.junit.runners.JUnit4; 25 | 26 | /** FreeBuilder test using {@link NestedListGwtType}. */ 27 | @RunWith(JUnit4.class) 28 | public class NestedListGwtTypeTest { 29 | 30 | private static final String NAME_1 = "name1"; 31 | private static final String NAME_2 = "name2"; 32 | private static final String NAME_3 = "name3"; 33 | 34 | @Test 35 | public void testAllMethodInteractions() { 36 | NestedListGwtType.Builder builder = new NestedListGwtType.Builder(); 37 | // getItemBuilder() 38 | builder.getItemBuilder().addNames(NAME_1); 39 | assertEquals(asList(NAME_1), builder.build().getItem().getNames()); 40 | // setItem(Value) 41 | builder.setItem(new StringListGwtType.Builder() 42 | .addNames(NAME_2, NAME_3) 43 | .build()); 44 | assertEquals(asList(NAME_2, NAME_3), builder.build().getItem().getNames()); 45 | // setItem(Builder) 46 | builder.setItem(new StringListGwtType.Builder() 47 | .addNames(NAME_1, NAME_3)); 48 | assertEquals(asList(NAME_1, NAME_3), builder.build().getItem().getNames()); 49 | // Top-level clear() 50 | builder.clear(); 51 | assertEquals(asList(), builder.build().getItem().getNames()); 52 | // mergeFrom(Value) 53 | builder.mergeFrom(new NestedListGwtType.Builder() 54 | .setItem(new StringListGwtType.Builder() 55 | .addNames(NAME_2)) 56 | .build()); 57 | assertEquals(asList(NAME_2), builder.build().getItem().getNames()); 58 | // mergeFrom(Builder) 59 | builder.mergeFrom(new NestedListGwtType.Builder() 60 | .setItem(new StringListGwtType.Builder() 61 | .addNames(NAME_3))); 62 | assertEquals(asList(NAME_2, NAME_3), builder.build().getItem().getNames()); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/it/vanilla/src/test/java/org/inferred/freebuilder/NoFrillsDataInterfaceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | import static org.junit.Assert.assertTrue; 20 | 21 | import org.junit.Test; 22 | import org.junit.runner.RunWith; 23 | import org.junit.runners.JUnit4; 24 | 25 | /** 26 | * Integration test for {@link org.inferred.freebuilder.FreeBuilder 27 | * FreeBuilder}, using the {@link NoFrillsDataInterface}. 28 | */ 29 | @RunWith(JUnit4.class) 30 | public class NoFrillsDataInterfaceTest { 31 | 32 | @Test 33 | public void test() { 34 | NoFrillsDataInterface b = new NoFrillsDataInterface.Builder() 35 | .setPropertyA(11) 36 | .setPropertyB(true) 37 | .build(); 38 | assertEquals(11, b.getPropertyA()); 39 | assertTrue(b.isPropertyB()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/it/vanilla/src/test/java/org/inferred/freebuilder/NoFrillsDataTypeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | import static org.junit.Assert.assertTrue; 20 | 21 | import org.junit.Test; 22 | import org.junit.runner.RunWith; 23 | import org.junit.runners.JUnit4; 24 | 25 | /** 26 | * Integration test for {@link org.inferred.freebuilder.FreeBuilder 27 | * FreeBuilder}, using the {@link NoFrillsDataType}. 28 | */ 29 | @RunWith(JUnit4.class) 30 | public class NoFrillsDataTypeTest { 31 | 32 | @Test 33 | public void test() { 34 | NoFrillsDataType value = NoFrillsDataType.builder() 35 | .setPropertyA(11) 36 | .setPropertyB(true) 37 | .build(); 38 | assertEquals(11, value.getPropertyA()); 39 | assertTrue(value.isPropertyB()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/it/vanilla/src/test/java/org/inferred/freebuilder/OptionalGwtTypeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import com.google.common.base.Optional; 21 | 22 | import org.junit.Test; 23 | import org.junit.runner.RunWith; 24 | import org.junit.runners.JUnit4; 25 | 26 | /** FreeBuilder test using {@link OptionalGwtType}. */ 27 | @RunWith(JUnit4.class) 28 | public class OptionalGwtTypeTest { 29 | 30 | private static final String NAME_1 = "name1"; 31 | private static final String NAME_2 = "name2"; 32 | private static final String NAME_3 = "name3"; 33 | 34 | @Test 35 | public void testAllMethodInteractions() { 36 | OptionalGwtType.Builder builder = new OptionalGwtType.Builder(); 37 | builder.setName(NAME_1); 38 | assertEquals(Optional.of(NAME_1), builder.getName()); 39 | assertEquals(Optional.of(NAME_1), builder.build().getName()); 40 | builder.clearName(); 41 | assertEquals(Optional.absent(), builder.getName()); 42 | assertEquals(Optional.absent(), builder.build().getName()); 43 | builder.setNullableName(NAME_3); 44 | assertEquals(Optional.of(NAME_3), builder.getName()); 45 | assertEquals(Optional.of(NAME_3), builder.build().getName()); 46 | builder.setName(Optional.absent()); 47 | assertEquals(Optional.absent(), builder.getName()); 48 | assertEquals(Optional.absent(), builder.build().getName()); 49 | builder.setNullableName(null); 50 | assertEquals(Optional.absent(), builder.getName()); 51 | assertEquals(Optional.absent(), builder.build().getName()); 52 | builder.setName(Optional.of(NAME_2)); 53 | assertEquals(Optional.of(NAME_2), builder.getName()); 54 | assertEquals(Optional.of(NAME_2), builder.build().getName()); 55 | builder.clear(); 56 | assertEquals(Optional.absent(), builder.getName()); 57 | assertEquals(Optional.absent(), builder.build().getName()); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/it/vanilla/src/test/java/org/inferred/freebuilder/StringListGwtTypeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static java.util.Arrays.asList; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | 22 | import org.junit.Test; 23 | import org.junit.runner.RunWith; 24 | import org.junit.runners.JUnit4; 25 | 26 | /** FreeBuilder test using {@link StringListGwtType}. */ 27 | @RunWith(JUnit4.class) 28 | public class StringListGwtTypeTest { 29 | 30 | private static final String NAME_1 = "name1"; 31 | private static final String NAME_2 = "name2"; 32 | private static final String NAME_3 = "name3"; 33 | 34 | @Test 35 | public void testAllMethodInteractions() { 36 | StringListGwtType.Builder builder = new StringListGwtType.Builder(); 37 | builder.addNames(NAME_1); 38 | assertEquals(asList(NAME_1), builder.build().getNames()); 39 | builder.addNames(NAME_2, NAME_3); 40 | assertEquals(asList(NAME_1, NAME_2, NAME_3), builder.build().getNames()); 41 | builder.clearNames(); 42 | assertEquals(asList(), builder.build().getNames()); 43 | builder.addAllNames(asList(NAME_2, NAME_1)); 44 | assertEquals(asList(NAME_2, NAME_1), builder.build().getNames()); 45 | builder.clear(); 46 | assertEquals(asList(), builder.build().getNames()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/it/vanilla/src/test/java/org/inferred/freebuilder/WildcardListTypeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import java.util.Arrays; 21 | 22 | import org.junit.Test; 23 | import org.junit.runner.RunWith; 24 | import org.junit.runners.JUnit4; 25 | 26 | /** FreeBuilder test using {@link WildcardListType}. */ 27 | @RunWith(JUnit4.class) 28 | public class WildcardListTypeTest { 29 | 30 | private static final String NAME_1 = "name1"; 31 | private static final String NAME_2 = "name2"; 32 | private static final String NAME_3 = "name3"; 33 | 34 | @Test 35 | public void testBuild() { 36 | WildcardListType value = new WildcardListType.Builder() 37 | .addNumbers(1) 38 | .addNumbers(2.8) 39 | .build(); 40 | assertEquals(Arrays.asList(1, 2.8), value.numbers()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/FreeBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Annotates a type that has an auto-generated builder. 25 | * 26 | *

Quick start

27 | * 28 | *

Create your value type (e.g. {@code Person}) as an interface or abstract class, containing 29 | * an abstract accessor method for each desired field. This accessor must be non-void, 30 | * parameterless, and start with 'get' or 'is'. Add the {@code @FreeBuilder} annotation to your 31 | * class, and it will automatically generate an implementing class and a package-visible builder API 32 | * ({@code Person_Builder}), which you must subclass. For instance: 33 | * 34 | *

@FreeBuilder
35 |  * public interface Person {
36 |  *   /** Returns the person's full (English) name. */
37 |  *   String getName();
38 |  *   /** Returns the person's age in years, rounded down. */
39 |  *   int getAge();
40 |  *   /** Builder of {@link Person} instances. */
41 |  *   class Builder extends Person_Builder { }
42 |  * }
43 | * 44 | *

You can now use the {@code Builder} class: 45 | * 46 | *

Person person = new Person.Builder()
47 |  *     .setName("Phil")
48 |  *     .setAge(31)
49 |  *     .build();
50 |  * System.out.println(person);  // Person{name=Phil, age=31}
51 | * 52 | * @see 53 | * Full documentation at freebuilder.inferred.org 54 | */ 55 | @Target(ElementType.TYPE) 56 | @Retention(RetentionPolicy.SOURCE) 57 | public @interface FreeBuilder {} 58 | 59 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/IgnoredByEquals.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | import java.util.Comparator; 8 | import java.util.Map; 9 | import java.util.Set; 10 | 11 | /** 12 | * {@link FreeBuilder} will not check properties annotated {@code @IgnoredByEquals} when comparing 13 | * objects in its generated {@link Object#equals(Object)}. 14 | * 15 | *

To maintain the contract of {@link Object#hashCode()}), the value of these properties will 16 | * also be ignored in the generated implementation of that method. 17 | * 18 | *

Warning: Dropping properties from equality checks makes it very easy to accidentally 19 | * write broken unit tests (and hard to write good ones). If you find yourself wanting to use 20 | * this annotation, consider first whether you actually want a different collection type 21 | * (typicaly a {@link Map} rather than a {@link Set}, for instance), or whether you can use an 22 | * explicit field-ignoring {@link Comparator} in the parts of the code that need it. 23 | */ 24 | @Target(ElementType.METHOD) 25 | @Retention(RetentionPolicy.SOURCE) 26 | public @interface IgnoredByEquals { 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/NotInToString.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * {@link FreeBuilder} will not include properties annotated {@code @NotInToString} in the output of 10 | * its generated {@link Object#toString()} implementation. 11 | */ 12 | @Target(ElementType.METHOD) 13 | @Retention(RetentionPolicy.SOURCE) 14 | public @interface NotInToString { 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/Declarations.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor; 2 | 3 | import org.inferred.freebuilder.processor.BuilderFactory.TypeInference; 4 | import org.inferred.freebuilder.processor.source.Scope; 5 | import org.inferred.freebuilder.processor.source.Scope.Level; 6 | import org.inferred.freebuilder.processor.source.SourceBuilder; 7 | import org.inferred.freebuilder.processor.source.Variable; 8 | 9 | import java.util.Optional; 10 | 11 | public class Declarations { 12 | 13 | private static final String UPCAST_COMMENT = 14 | "// Upcast to access private fields; otherwise, oddly, we get an access violation."; 15 | 16 | private enum Declaration implements Scope.Key { 17 | UPCAST, FRESH_BUILDER; 18 | 19 | @Override 20 | public Level level() { 21 | return Level.METHOD; 22 | } 23 | } 24 | 25 | /** 26 | * Upcasts a Builder instance to the generated superclass, to allow access to private fields. 27 | * 28 | *

Reuses an existing upcast instance if one was already declared in this scope. 29 | * 30 | * @param code the {@link SourceBuilder} to add the declaration to 31 | * @param datatype metadata about the user type the builder is being generated for 32 | * @param builder the Builder instance to upcast 33 | * @returns a variable holding the upcasted instance 34 | */ 35 | public static Variable upcastToGeneratedBuilder( 36 | SourceBuilder code, Datatype datatype, String builder) { 37 | return code.scope().computeIfAbsent(Declaration.UPCAST, () -> { 38 | Variable base = new Variable("base"); 39 | code.addLine(UPCAST_COMMENT) 40 | .addLine("%s %s = %s;", datatype.getGeneratedBuilder(), base, builder); 41 | return base; 42 | }); 43 | } 44 | 45 | /** 46 | * Declares a fresh Builder to copy default property values from. 47 | * 48 | *

Reuses an existing fresh Builder instance if one was already declared in this scope. 49 | * 50 | * @returns a variable holding a fresh Builder, if a no-args factory method is available to 51 | * create one with 52 | */ 53 | public static Optional freshBuilder(SourceBuilder code, Datatype datatype) { 54 | if (!datatype.getBuilderFactory().isPresent()) { 55 | return Optional.empty(); 56 | } 57 | return Optional.of(code.scope().computeIfAbsent(Declaration.FRESH_BUILDER, () -> { 58 | Variable defaults = new Variable("defaults"); 59 | code.addLine("%s %s = %s;", 60 | datatype.getGeneratedBuilder(), 61 | defaults, 62 | datatype.getBuilderFactory().get() 63 | .newBuilder(datatype.getBuilder(), TypeInference.INFERRED_TYPES)); 64 | return defaults; 65 | })); 66 | } 67 | 68 | private Declarations() {} 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/GeneratedStub.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor; 2 | 3 | import org.inferred.freebuilder.processor.source.Excerpts; 4 | import org.inferred.freebuilder.processor.source.QualifiedName; 5 | import org.inferred.freebuilder.processor.source.SourceBuilder; 6 | import org.inferred.freebuilder.processor.source.TypeClass; 7 | 8 | class GeneratedStub extends GeneratedType { 9 | 10 | private final QualifiedName datatype; 11 | private final TypeClass stub; 12 | 13 | GeneratedStub(QualifiedName datatype, TypeClass stub) { 14 | this.datatype = datatype; 15 | this.stub = stub; 16 | } 17 | 18 | @Override 19 | public void addTo(SourceBuilder code) { 20 | code.addLine("// Autogenerated code. Do not modify.") 21 | .addLine("package %s;", stub.getQualifiedName().getPackage()) 22 | .addLine("") 23 | .addLine("/**") 24 | .addLine(" * Placeholder. Create {@code %s.Builder} and subclass this type.", datatype) 25 | .addLine(" */") 26 | .add(Excerpts.generated(Processor.class)) 27 | .addLine("abstract class %s {}", stub.declaration()); 28 | } 29 | 30 | @Override 31 | protected void addFields(FieldReceiver fields) { 32 | fields.add("datatype", datatype); 33 | fields.add("stub", stub); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/GeneratedType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor; 2 | 3 | import org.inferred.freebuilder.processor.source.Excerpt; 4 | import org.inferred.freebuilder.processor.source.ValueType; 5 | 6 | abstract class GeneratedType extends ValueType implements Excerpt { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/NameAndVisibility.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor; 2 | 3 | import org.inferred.freebuilder.processor.Datatype.Visibility; 4 | 5 | import java.util.Objects; 6 | 7 | public class NameAndVisibility { 8 | 9 | public static NameAndVisibility of(String name, Visibility visibility) { 10 | return new NameAndVisibility(name, visibility); 11 | } 12 | 13 | private final String name; 14 | private final Visibility visibility; 15 | 16 | private NameAndVisibility(String name, Visibility visibility) { 17 | this.name = name; 18 | this.visibility = visibility; 19 | } 20 | 21 | public String name() { 22 | return name; 23 | } 24 | 25 | public Visibility visibility() { 26 | return visibility; 27 | } 28 | 29 | @Override 30 | public int hashCode() { 31 | return Objects.hash(name, visibility); 32 | } 33 | 34 | @Override 35 | public boolean equals(Object obj) { 36 | if (this == obj) { 37 | return true; 38 | } 39 | if (!(obj instanceof NameAndVisibility)) { 40 | return false; 41 | } 42 | NameAndVisibility other = (NameAndVisibility) obj; 43 | return Objects.equals(name, other.name) && visibility == other.visibility; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return "NameAndVisibility{name=" + name + ", visibility=" + visibility + "}"; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/excerpt/CheckedSet.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.excerpt; 2 | 3 | import org.inferred.freebuilder.processor.source.Excerpt; 4 | import org.inferred.freebuilder.processor.source.LazyName; 5 | import org.inferred.freebuilder.processor.source.SourceBuilder; 6 | import org.inferred.freebuilder.processor.source.ValueType; 7 | 8 | import java.util.AbstractSet; 9 | import java.util.Iterator; 10 | import java.util.Set; 11 | import java.util.function.Consumer; 12 | 13 | /** 14 | * Excerpts defining a set implementation that delegates to a provided add method to perform 15 | * element validation and insertion into a backing set. 16 | */ 17 | public class CheckedSet extends ValueType implements Excerpt { 18 | 19 | public static final LazyName TYPE = LazyName.of("CheckedSet", new CheckedSet()); 20 | 21 | private CheckedSet() {} 22 | 23 | @Override 24 | public void addTo(SourceBuilder code) { 25 | code.addLine("") 26 | .addLine("/**") 27 | .addLine(" * A set implementation that delegates to a provided add method") 28 | .addLine(" * to perform element validation and insertion into a backing set.") 29 | .addLine(" */") 30 | .addLine("private static class %s extends %s {", TYPE, AbstractSet.class) 31 | .addLine("") 32 | .addLine(" private final %s set;", Set.class) 33 | .addLine(" private final %s add;", Consumer.class) 34 | .addLine("") 35 | .addLine(" %s(%s set, %s add) {", TYPE, Set.class, Consumer.class) 36 | .addLine(" this.set = set;") 37 | .addLine(" this.add = add;") 38 | .addLine(" }") 39 | .addLine("") 40 | .addLine("") 41 | .addLine(" @Override public %s iterator() {", Iterator.class) 42 | .addLine(" return set.iterator();") 43 | .addLine(" }") 44 | .addLine("") 45 | .addLine(" @Override public int size() {") 46 | .addLine(" return set.size();") 47 | .addLine(" }") 48 | .addLine("") 49 | .addLine(" @Override public boolean contains(Object e) {") 50 | .addLine(" return set.contains(e);") 51 | .addLine(" }") 52 | .addLine("") 53 | .addLine(" @Override public boolean add(E e) {") 54 | .addLine(" if (!set.contains(e)) {") 55 | .addLine(" add.accept(e);") 56 | .addLine(" return true;") 57 | .addLine(" } else {") 58 | .addLine(" return false;") 59 | .addLine(" }") 60 | .addLine(" }") 61 | .addLine("") 62 | .addLine(" @Override public boolean remove(Object e) {") 63 | .addLine(" return set.remove(e);") 64 | .addLine(" }") 65 | .addLine("}"); 66 | } 67 | 68 | @Override 69 | protected void addFields(FieldReceiver fields) {} 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/naming/NamingConvention.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.naming; 17 | 18 | import org.inferred.freebuilder.processor.property.Property; 19 | 20 | import java.util.Optional; 21 | 22 | import javax.lang.model.element.ExecutableElement; 23 | import javax.lang.model.element.TypeElement; 24 | 25 | public interface NamingConvention { 26 | /** 27 | * Verifies {@code method} is an abstract getter following this naming convention. Any 28 | * deviations will be logged as an error. 29 | */ 30 | Optional getPropertyNames(TypeElement valueType, ExecutableElement method); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/property/Factories.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.property; 2 | 3 | import com.google.common.collect.ImmutableList; 4 | 5 | import java.util.List; 6 | 7 | public class Factories { 8 | 9 | /** 10 | * Factories of {@link PropertyCodeGenerator} instances. Note: order is important; the default 11 | * factory should always be last. 12 | */ 13 | public static final List PROPERTY_FACTORIES = ImmutableList.of( 14 | new NullableProperty.Factory(), // Must be first, as no other factory supports nulls 15 | new BuildableListProperty.Factory(), // Must be before ListProperty 16 | new ListProperty.Factory(), 17 | new SetProperty.Factory(), 18 | new SortedSetProperty.Factory(), 19 | new MapProperty.Factory(), 20 | new BiMapProperty.Factory(), 21 | new MultisetProperty.Factory(), 22 | new ListMultimapProperty.Factory(), 23 | new SetMultimapProperty.Factory(), 24 | new PrimitiveOptionalProperty.Factory(), 25 | new OptionalProperty.Factory(), 26 | new BuildableProperty.Factory(), 27 | new DefaultProperty.Factory()); // Must be last, as it will always return a CodeGenerator 28 | 29 | private Factories() { } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/property/MergeAction.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.property; 2 | 3 | import static com.google.common.collect.Iterables.getLast; 4 | 5 | import com.google.common.collect.ImmutableList; 6 | import com.google.common.collect.SetMultimap; 7 | import com.google.common.collect.TreeMultimap; 8 | 9 | import org.inferred.freebuilder.processor.source.SourceBuilder; 10 | import org.inferred.freebuilder.processor.source.ValueType; 11 | 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | /** 16 | * Readable action fragments for documenting mergeFrom behaviors. 17 | */ 18 | public class MergeAction extends ValueType { 19 | 20 | public static MergeAction appendingToCollections() { 21 | return new MergeAction("appending to", "collections", false); 22 | } 23 | 24 | public static MergeAction skippingDefaults() { 25 | return new MergeAction("skipping", "defaults", false); 26 | } 27 | 28 | public static MergeAction skippingEmptyOptionals() { 29 | return new MergeAction("skipping", "empty optionals", false); 30 | } 31 | 32 | public static MergeAction skippingUnsetProperties() { 33 | return new MergeAction("skipping", "unset properties", true); 34 | } 35 | 36 | /** 37 | * Emits a sentence fragment combining all the merge actions. 38 | */ 39 | public static void addActionsTo( 40 | SourceBuilder code, 41 | Set mergeActions, 42 | boolean forBuilder) { 43 | SetMultimap nounsByVerb = TreeMultimap.create(); 44 | mergeActions.forEach(mergeAction -> { 45 | if (forBuilder || !mergeAction.builderOnly) { 46 | nounsByVerb.put(mergeAction.verb, mergeAction.noun); 47 | } 48 | }); 49 | List verbs = ImmutableList.copyOf(nounsByVerb.keySet()); 50 | String lastVerb = getLast(verbs, null); 51 | for (String verb : nounsByVerb.keySet()) { 52 | code.add(", %s%s", (verbs.size() > 1 && verb.equals(lastVerb)) ? "and " : "", verb); 53 | List nouns = ImmutableList.copyOf(nounsByVerb.get(verb)); 54 | for (int i = 0; i < nouns.size(); ++i) { 55 | String separator = (i == 0) ? "" : (i == nouns.size() - 1) ? " and" : ","; 56 | code.add("%s %s", separator, nouns.get(i)); 57 | } 58 | } 59 | } 60 | 61 | private final String verb; 62 | private final String noun; 63 | private final boolean builderOnly; 64 | 65 | private MergeAction(String verb, String noun, boolean builderOnly) { 66 | this.verb = verb; 67 | this.noun = noun; 68 | this.builderOnly = builderOnly; 69 | } 70 | 71 | @Override 72 | protected void addFields(FieldReceiver fields) { 73 | fields.add("verb", verb); 74 | fields.add("noun", noun); 75 | fields.add("builderOnly", builderOnly); 76 | } 77 | 78 | @Override 79 | public String toString() { 80 | return verb + " " + noun + (builderOnly ? " on builders" : ""); 81 | } 82 | } -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/ElementAppender.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source; 2 | 3 | import javax.lang.model.element.Element; 4 | import javax.lang.model.element.PackageElement; 5 | import javax.lang.model.element.TypeElement; 6 | import javax.lang.model.util.SimpleElementVisitor8; 7 | 8 | class ElementAppender extends SimpleElementVisitor8 { 9 | private static final ElementAppender INSTANCE = new ElementAppender(); 10 | 11 | public static void appendShortened(Element arg, QualifiedNameAppendable source) { 12 | INSTANCE.visit(arg, source); 13 | } 14 | 15 | private ElementAppender() { } 16 | 17 | @Override 18 | public Void visitPackage(PackageElement pkg, QualifiedNameAppendable a) { 19 | a.append(pkg.getQualifiedName()); 20 | return null; 21 | } 22 | 23 | @Override 24 | public Void visitType(TypeElement type, QualifiedNameAppendable a) { 25 | a.append(QualifiedName.of(type)); 26 | return null; 27 | } 28 | 29 | @Override 30 | protected Void defaultAction(Element e, QualifiedNameAppendable a) { 31 | a.append(e.toString()); 32 | return null; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/Excerpt.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source; 17 | 18 | /** 19 | * An object representing a source code excerpt, e.g. a type. 20 | */ 21 | @FunctionalInterface 22 | public interface Excerpt { 23 | void addTo(SourceBuilder source); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/Excerpts.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source; 2 | 3 | import org.inferred.freebuilder.processor.source.feature.JavaxPackage; 4 | 5 | public class Excerpts { 6 | 7 | public static final Excerpt EMPTY = code -> { }; 8 | 9 | public static Excerpt add(String fmt, Object... args) { 10 | return code -> code.add(fmt, args); 11 | } 12 | 13 | /** 14 | * Returns an excerpt of the {@link javax.annotation.Generated} annotation, if available, 15 | * with value set to the full name of the {@code generator} class as recommended. 16 | */ 17 | public static Excerpt generated(Class generator) { 18 | return code -> { 19 | code.feature(JavaxPackage.JAVAX).generated().ifPresent(generated -> { 20 | code.addLine("@%s(\"%s\")", generated, generator.getName()); 21 | }); 22 | }; 23 | } 24 | 25 | public static Excerpt join(String separator, Iterable excerpts) { 26 | return code -> appendJoined(code, separator, excerpts); 27 | } 28 | 29 | private static void appendJoined(SourceBuilder source, String separator, Iterable excerpts) { 30 | String itemPrefix = ""; 31 | for (Object object : excerpts) { 32 | source.add("%s%s", itemPrefix, object); 33 | itemPrefix = separator; 34 | } 35 | } 36 | 37 | private Excerpts() {} 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/FieldAccess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source; 17 | 18 | public class FieldAccess extends ValueType implements Excerpt { 19 | 20 | private final String fieldName; 21 | 22 | public FieldAccess(String fieldName) { 23 | this.fieldName = fieldName; 24 | } 25 | 26 | @Override 27 | public void addTo(SourceBuilder source) { 28 | IdKey key = new IdKey(fieldName); 29 | if (source.scope().canStore(key)) { 30 | // In method scope; we may need to qualify the field reference 31 | Object idOwner = source.scope().putIfAbsent(key, this); 32 | if (idOwner != null && !idOwner.equals(this)) { 33 | source.add("this."); 34 | } 35 | } 36 | source.add(fieldName); 37 | } 38 | 39 | public Excerpt on(Object obj) { 40 | return code -> code.add("%s.%s", obj, fieldName); 41 | } 42 | 43 | @Override 44 | protected void addFields(FieldReceiver fields) { 45 | fields.add("fieldName", fieldName); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/FilerUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source; 17 | 18 | import java.io.IOException; 19 | import java.io.Writer; 20 | 21 | import javax.annotation.processing.Filer; 22 | import javax.lang.model.element.Element; 23 | 24 | /** Static utility methods for working with {@link Filer}. */ 25 | public class FilerUtils { 26 | 27 | /** 28 | * Writes {@code unit} to the correct file. 29 | * 30 | *

This is complicated by an EJC bug that returns the wrong object from 31 | * {@link Writer#append(CharSequence)}. 32 | */ 33 | public static void writeCompilationUnit( 34 | Filer filer, 35 | SourceBuilder unit, 36 | Element originatingElement) throws IOException { 37 | String typename = unit.typename().toString(); 38 | String finalSource = unit.toString(); 39 | try (Writer writer = filer.createSourceFile(typename, originatingElement).openWriter()) { 40 | writer.append(finalSource); 41 | } 42 | } 43 | 44 | private FilerUtils() {} 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/IdKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source; 17 | 18 | import org.inferred.freebuilder.processor.source.Scope.Level; 19 | 20 | /** 21 | * Maps Java identifiers to their usage (e.g. a parameter, a variable) in the current method scope. 22 | */ 23 | class IdKey extends ValueType implements Scope.Key { 24 | 25 | private final String name; 26 | 27 | IdKey(String name) { 28 | this.name = name; 29 | } 30 | 31 | String name() { 32 | return name; 33 | } 34 | 35 | @Override 36 | public Level level() { 37 | return Level.METHOD; 38 | } 39 | 40 | @Override 41 | protected void addFields(FieldReceiver fields) { 42 | fields.add("name", name); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/ObjectsExcerpts.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source; 2 | 3 | import com.google.common.base.Preconditions; 4 | 5 | import java.util.Objects; 6 | 7 | import javax.lang.model.type.TypeKind; 8 | 9 | public class ObjectsExcerpts { 10 | 11 | /** 12 | * Returns an Excerpt equivalent to {@code Objects.equals(a, b)}. 13 | * 14 | *

Uses == for primitive types, as this avoids boxing. 15 | */ 16 | public static Excerpt equals(Object a, Object b, TypeKind kind) { 17 | return equalsExcerpt(true, a, b, kind); 18 | } 19 | 20 | /** 21 | * Returns an Excerpt equivalent to {@code !Objects.equals(a, b)}. 22 | * 23 | *

Uses != for primitive types, as this avoids boxing. 24 | */ 25 | public static Excerpt notEquals(Object a, Object b, TypeKind kind) { 26 | return equalsExcerpt(false, a, b, kind); 27 | } 28 | 29 | private static Excerpt equalsExcerpt(boolean areEqual, Object a, Object b, TypeKind kind) { 30 | switch (kind) { 31 | case FLOAT: 32 | return code -> code.add("%1$s.floatToIntBits(%2$s) %3$s %1$s.floatToIntBits(%4$s)", 33 | Float.class, a, areEqual ? "==" : "!=", b); 34 | 35 | case DOUBLE: 36 | return code -> code.add("%1$s.doubleToLongBits(%2$s) %3$s %1$s.doubleToLongBits(%4$s)", 37 | Double.class, a, areEqual ? "==" : "!=", b); 38 | 39 | case BOOLEAN: 40 | case BYTE: 41 | case SHORT: 42 | case INT: 43 | case LONG: 44 | case CHAR: 45 | case ARRAY: 46 | return code -> code.add("%s %s %s", a, areEqual ? "==" : "!=", b); 47 | 48 | default: 49 | Preconditions.checkState(!kind.isPrimitive(), "Unexpected primitive type " + kind); 50 | return code -> code.add("%s%s.equals(%s, %s)", areEqual ? "" : "!", Objects.class, a, b); 51 | } 52 | } 53 | 54 | private ObjectsExcerpts() {} 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/QualifiedNameAppendable.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source; 2 | 3 | interface QualifiedNameAppendable { 4 | void append(char c); 5 | void append(CharSequence csq); 6 | void append(CharSequence csq, int start, int end); 7 | void append(QualifiedName type); 8 | } -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/Quotes.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source; 2 | 3 | public class Quotes { 4 | 5 | /** Escapes each character in a string that has an escape sequence. */ 6 | public static CharSequence escapeJava(CharSequence s) { 7 | return quote(s, 0, s.length()); 8 | } 9 | 10 | public static CharSequence quote(CharSequence s, int start, int end) { 11 | StringBuilder buf = new StringBuilder(); 12 | for (int i = start; i < end; i++) { 13 | appendQuoted(buf, s.charAt(i)); 14 | } 15 | return buf; 16 | } 17 | 18 | private static void appendQuoted(StringBuilder buf, char ch) { 19 | switch (ch) { 20 | case '\b': 21 | buf.append("\\b"); 22 | return; 23 | case '\f': 24 | buf.append("\\f"); 25 | return; 26 | case '\n': 27 | buf.append("\\n"); 28 | return; 29 | case '\r': 30 | buf.append("\\r"); 31 | return; 32 | case '\t': 33 | buf.append("\\t"); 34 | return; 35 | case '\"': 36 | buf.append("\\\""); 37 | return; 38 | case '\\': 39 | buf.append("\\\\"); 40 | return; 41 | default: 42 | buf.append(ch); 43 | return; 44 | } 45 | } 46 | 47 | private Quotes() { 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/RoundEnvironments.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source; 17 | 18 | import com.google.common.collect.Sets; 19 | 20 | import java.lang.annotation.Annotation; 21 | import java.util.Set; 22 | 23 | import javax.annotation.processing.RoundEnvironment; 24 | import javax.lang.model.element.Element; 25 | 26 | /** Utility class for {@link RoundEnvironment}. */ 27 | public class RoundEnvironments { 28 | 29 | /** 30 | * Sanitizes the result of {@link RoundEnvironment#getElementsAnnotatedWith}, which otherwise 31 | * can contain elements annotated with annotations of ERROR type. 32 | * 33 | *

The canonical example is forgetting to import @Nullable. 34 | */ 35 | public static Set annotatedElementsIn( 36 | RoundEnvironment roundEnv, Class a) { 37 | return Sets.filter(roundEnv.getElementsAnnotatedWith(a), 38 | element -> element.getAnnotation(a) != null); 39 | } 40 | 41 | private RoundEnvironments() { } // COV_NF_LINE 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/RuntimeReflection.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source; 2 | 3 | import org.inferred.freebuilder.processor.source.ScopeHandler.TypeInfo; 4 | import org.inferred.freebuilder.processor.source.ScopeHandler.Visibility; 5 | 6 | import java.lang.reflect.Modifier; 7 | import java.util.Arrays; 8 | import java.util.Optional; 9 | import java.util.stream.Stream; 10 | 11 | class RuntimeReflection implements ScopeHandler.Reflection { 12 | 13 | private class RuntimeTypeInfo implements ScopeHandler.TypeInfo { 14 | 15 | private final Class cls; 16 | private final QualifiedName name; 17 | 18 | RuntimeTypeInfo(Class cls) { 19 | this.cls = cls; 20 | this.name = QualifiedName.of(cls); 21 | } 22 | 23 | @Override 24 | public QualifiedName name() { 25 | return name; 26 | } 27 | 28 | @Override 29 | public Visibility visibility() { 30 | int modifiers = cls.getModifiers(); 31 | if (Modifier.isPublic(modifiers)) { 32 | return Visibility.PUBLIC; 33 | } else if (Modifier.isProtected(modifiers)) { 34 | return Visibility.PROTECTED; 35 | } else if (Modifier.isPrivate(modifiers)) { 36 | return Visibility.PRIVATE; 37 | } else if (cls.getEnclosingClass() != null && cls.getEnclosingClass().isInterface()) { 38 | return Visibility.PUBLIC; 39 | } else { 40 | return Visibility.PROTECTED; 41 | } 42 | } 43 | 44 | @Override 45 | public Stream supertypes() { 46 | return Stream.concat(stream(cls.getSuperclass()), Arrays.stream(cls.getInterfaces())) 47 | .map(RuntimeTypeInfo::new); 48 | } 49 | 50 | @Override 51 | public Stream nestedTypes() { 52 | return Arrays.stream(cls.getDeclaredClasses()).map(RuntimeTypeInfo::new); 53 | } 54 | } 55 | 56 | private final ClassLoader classLoader; 57 | 58 | RuntimeReflection(ClassLoader classLoader) { 59 | this.classLoader = classLoader; 60 | } 61 | 62 | @Override 63 | public Optional find(String typename) { 64 | try { 65 | return Optional.of(classLoader.loadClass(typename)).map(RuntimeTypeInfo::new); 66 | } catch (ClassNotFoundException e) { 67 | return Optional.empty(); 68 | } 69 | } 70 | 71 | private static Stream stream(T value) { 72 | return (value != null) ? Stream.of(value) : Stream.of(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/Shading.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source; 17 | 18 | /** 19 | * Utility methods related to @FreeBuilder dependencies being relocated as part of shading. 20 | */ 21 | public class Shading { 22 | 23 | private static final String SHADE_PACKAGE = "org.inferred.freebuilder.shaded."; 24 | 25 | public static String unshadedName(String qualifiedName) { 26 | if (qualifiedName.startsWith(Shading.SHADE_PACKAGE)) { 27 | return qualifiedName.substring(Shading.SHADE_PACKAGE.length()); 28 | } else { 29 | return qualifiedName; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/TemplateApplier.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source; 2 | 3 | import static java.lang.Integer.parseInt; 4 | 5 | import java.util.MissingFormatArgumentException; 6 | import java.util.regex.Matcher; 7 | import java.util.regex.Pattern; 8 | 9 | public class TemplateApplier { 10 | 11 | public interface TextAppender { 12 | void append(CharSequence chars, int start, int end); 13 | } 14 | 15 | public interface ParamAppender { 16 | void append(Object param); 17 | } 18 | 19 | public static TemplateApplier withParams(Object[] params) { 20 | return new TemplateApplier(params); 21 | } 22 | 23 | private static final Pattern PARAM = Pattern.compile("%([%ns]|([1-9]\\d*)\\$s)"); 24 | private static final String LINE_SEPARATOR = System.getProperty("line.separator"); 25 | 26 | private final Object[] params; 27 | private TextAppender textAppender; 28 | private ParamAppender paramAppender; 29 | private int nextParam = 0; 30 | 31 | private TemplateApplier(Object[] params) { 32 | this.params = params; 33 | } 34 | 35 | public TemplateApplier onText(TextAppender textAppender) { 36 | this.textAppender = textAppender; 37 | return this; 38 | } 39 | 40 | public TemplateApplier onParam(ParamAppender paramAppender) { 41 | this.paramAppender = paramAppender; 42 | return this; 43 | } 44 | 45 | public TemplateApplier parse(CharSequence template) { 46 | int offset = 0; 47 | Matcher matcher = PARAM.matcher(template); 48 | while (matcher.find()) { 49 | if (offset != matcher.start()) { 50 | textAppender.append(template, offset, matcher.start()); 51 | } 52 | if (matcher.group(1).contentEquals("%")) { 53 | textAppender.append("%", 0, 1); 54 | } else if (matcher.group(1).contentEquals("n")) { 55 | textAppender.append(LINE_SEPARATOR, 0, LINE_SEPARATOR.length()); 56 | } else if (matcher.group(1).contentEquals("s")) { 57 | if (nextParam >= params.length) { 58 | throw new MissingFormatArgumentException(matcher.group()); 59 | } 60 | paramAppender.append(params[nextParam++]); 61 | } else { 62 | int index = parseInt(matcher.group(2)) - 1; 63 | if (index >= params.length) { 64 | throw new MissingFormatArgumentException(matcher.group()); 65 | } 66 | paramAppender.append(params[index]); 67 | } 68 | offset = matcher.end(); 69 | } 70 | if (offset != template.length()) { 71 | textAppender.append(template, offset, template.length()); 72 | } 73 | return this; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/TypeMirrorAppender.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source; 2 | 3 | import static org.inferred.freebuilder.processor.model.ModelUtils.asElement; 4 | 5 | import javax.lang.model.element.Modifier; 6 | import javax.lang.model.type.DeclaredType; 7 | import javax.lang.model.type.TypeKind; 8 | import javax.lang.model.type.TypeMirror; 9 | import javax.lang.model.type.WildcardType; 10 | import javax.lang.model.util.SimpleTypeVisitor8; 11 | 12 | class TypeMirrorAppender extends SimpleTypeVisitor8 { 13 | 14 | private static final TypeMirrorAppender INSTANCE = new TypeMirrorAppender(); 15 | 16 | public static void appendShortened(TypeMirror mirror, QualifiedNameAppendable a) { 17 | mirror.accept(INSTANCE, a); 18 | } 19 | 20 | private TypeMirrorAppender() { } 21 | 22 | @Override 23 | public Void visitDeclared(DeclaredType mirror, QualifiedNameAppendable a) { 24 | if (!isInnerClass(mirror)) { 25 | a.append(QualifiedName.of(asElement(mirror))); 26 | } else { 27 | mirror.getEnclosingType().accept(this, a); 28 | a.append('.'); 29 | a.append(mirror.asElement().getSimpleName()); 30 | } 31 | if (!mirror.getTypeArguments().isEmpty()) { 32 | String prefix = "<"; 33 | for (TypeMirror typeArgument : mirror.getTypeArguments()) { 34 | a.append(prefix); 35 | typeArgument.accept(this, a); 36 | prefix = ", "; 37 | } 38 | a.append(">"); 39 | } 40 | return null; 41 | } 42 | 43 | private static boolean isInnerClass(DeclaredType mirror) { 44 | if (mirror.getEnclosingType().getKind() == TypeKind.NONE) { 45 | return false; 46 | } 47 | // Work around a little Eclipse bug 48 | if (asElement(mirror).getModifiers().contains(Modifier.STATIC)) { 49 | return false; 50 | } 51 | return true; 52 | } 53 | 54 | @Override 55 | public Void visitWildcard(WildcardType t, QualifiedNameAppendable a) { 56 | a.append("?"); 57 | if (t.getSuperBound() != null) { 58 | a.append(" super "); 59 | t.getSuperBound().accept(this, a); 60 | } 61 | if (t.getExtendsBound() != null) { 62 | a.append(" extends "); 63 | t.getExtendsBound().accept(this, a); 64 | } 65 | return null; 66 | } 67 | 68 | @Override 69 | protected Void defaultAction(TypeMirror mirror, QualifiedNameAppendable a) { 70 | a.append(mirror.toString()); 71 | return null; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/TypeUsage.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source; 2 | 3 | import org.inferred.freebuilder.FreeBuilder; 4 | 5 | import java.util.Optional; 6 | 7 | @FreeBuilder 8 | interface TypeUsage { 9 | 10 | int start(); 11 | int end(); 12 | QualifiedName type(); 13 | Optional scope(); 14 | 15 | class Builder extends TypeUsage_Builder { } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/Variable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source; 17 | 18 | import org.inferred.freebuilder.processor.source.Scope.Level; 19 | 20 | public class Variable extends ValueType implements Excerpt, Scope.Key { 21 | 22 | private final String preferredName; 23 | 24 | public Variable(String preferredName) { 25 | this.preferredName = preferredName; 26 | } 27 | 28 | @Override 29 | public Level level() { 30 | return Level.METHOD; 31 | } 32 | 33 | @Override 34 | public boolean equals(Object obj) { 35 | return this == obj; 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | return super.hashCode(); 41 | } 42 | 43 | @Override 44 | public void addTo(SourceBuilder code) { 45 | IdKey name = code.scope().computeIfAbsent(this, () -> new IdKey(pickName(code))); 46 | code.add("%s", name.name()); 47 | } 48 | 49 | @Override 50 | protected void addFields(FieldReceiver fields) { 51 | fields.add("preferredName", preferredName); 52 | } 53 | 54 | private String pickName(SourceBuilder code) { 55 | if (registerName(code, preferredName)) { 56 | return preferredName; 57 | } 58 | if (registerName(code, "_" + preferredName)) { 59 | return "_" + preferredName; 60 | } 61 | int suffix = 2; 62 | while (!registerName(code, "_" + preferredName + suffix)) { 63 | suffix++; 64 | } 65 | return "_" + preferredName + suffix; 66 | } 67 | 68 | private boolean registerName(SourceBuilder code, String name) { 69 | return code.scope().putIfAbsent(new IdKey(name), this) == null; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/feature/EnvironmentFeatureSet.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source.feature; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import javax.annotation.processing.ProcessingEnvironment; 7 | 8 | /** 9 | * A set of {@link Feature} instances, determined dynamically by calling 10 | * {@link FeatureType#forEnvironment}. 11 | */ 12 | public class EnvironmentFeatureSet implements FeatureSet { 13 | 14 | private final ProcessingEnvironment env; 15 | private final Map, Feature> featuresByType = new HashMap<>(); 16 | 17 | /** Constructs a feature set using the given processing environment. */ 18 | public EnvironmentFeatureSet(ProcessingEnvironment env) { 19 | this.env = env; 20 | } 21 | 22 | @Override 23 | public > T get(FeatureType featureType) { 24 | @SuppressWarnings("unchecked") 25 | T feature = (T) featuresByType.computeIfAbsent(featureType, $ -> $.forEnvironment(env, this)); 26 | return feature; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/feature/Feature.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source.feature; 2 | 3 | import org.inferred.freebuilder.processor.source.SourceBuilder; 4 | 5 | /** 6 | * A feature encapsulates the availability of a type or source level feature that can be used in 7 | * the source written to a {@link SourceBuilder}, such as Java language-level features, or Guava 8 | * types and methods. 9 | * 10 | *

A feature will typically provide a {@link FeatureType} constant that can be passed to 11 | * {@code SourceBuilder#feature(FeatureType)} to determine the current status of a feature. 12 | * For instance, to determine whether {@code java.util.Objects} is available for use: 13 | * 14 | *

code.feature({@link SourceLevel#SOURCE_LEVEL
15 |  *     SOURCE_LEVEL}).{@link SourceLevel#javaUtilObjects() javaUtilObjects()}.isPresent()
16 | */ 17 | public interface Feature> {} 18 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/feature/FeatureSet.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source.feature; 2 | 3 | /** 4 | * A set of {@link Feature} instances, indexed by {@link FeatureType}. 5 | */ 6 | public interface FeatureSet { 7 | /** Returns an instance of {@code featureType}. */ 8 | > T get(FeatureType featureType); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/feature/FeatureType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source.feature; 2 | 3 | import org.inferred.freebuilder.processor.source.SourceBuilder; 4 | 5 | import javax.annotation.processing.ProcessingEnvironment; 6 | 7 | /** 8 | * Algorithm to select the correct instance of a given feature type for a processing environment, 9 | * and the default to use in tests when an explicit value has not been registered for that feature. 10 | * 11 | *

Each feature class should expose a single {@code FeatureType} constant for the user to pass 12 | * to {@link SourceBuilder#feature(FeatureType)}, e.g. {@link SourceLevel#SOURCE_LEVEL}. 13 | */ 14 | public abstract class FeatureType> { 15 | 16 | /** Returns the instance of {@code F} to use by default in tests. */ 17 | protected abstract F testDefault(FeatureSet features); 18 | 19 | /** Returns the instance of {@code F} to use in {@code env}. */ 20 | protected abstract F forEnvironment(ProcessingEnvironment env, FeatureSet features); 21 | 22 | @SuppressWarnings("unchecked") 23 | protected Class type() { 24 | return (Class) testDefault(new StaticFeatureSet()).getClass(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/feature/JavaxPackage.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source.feature; 2 | 3 | import org.inferred.freebuilder.processor.source.QualifiedName; 4 | import org.inferred.freebuilder.processor.source.SourceBuilder; 5 | 6 | import java.util.Optional; 7 | 8 | import javax.annotation.processing.ProcessingEnvironment; 9 | import javax.lang.model.util.Elements; 10 | 11 | /** 12 | * Types in the javax package, if available. Linked to the {@link SourceLevel} by default in tests. 13 | */ 14 | public enum JavaxPackage implements Feature { 15 | 16 | AVAILABLE("javax"), UNAVAILABLE("No javax"); 17 | 18 | /** 19 | * Constant to pass to {@link SourceBuilder#feature(FeatureType)} to get the current status of 20 | * {@link JavaxPackage}. 21 | */ 22 | public static final FeatureType JAVAX = 23 | new FeatureType() { 24 | 25 | @Override 26 | protected JavaxPackage testDefault(FeatureSet features) { 27 | return UNAVAILABLE; 28 | } 29 | 30 | @Override 31 | protected JavaxPackage forEnvironment(ProcessingEnvironment env, FeatureSet features) { 32 | return hasType(env.getElementUtils(), GENERATED) ? AVAILABLE : UNAVAILABLE; 33 | } 34 | }; 35 | 36 | private static final QualifiedName GENERATED = QualifiedName.of("javax.annotation", "Generated"); 37 | 38 | private final String humanReadableFormat; 39 | 40 | JavaxPackage(String humanReadableFormat) { 41 | this.humanReadableFormat = humanReadableFormat; 42 | } 43 | 44 | /** 45 | * Parameterized type for {@code java.util.function.Consumer}, if available. 46 | */ 47 | public Optional generated() { 48 | return ifAvailable(GENERATED); 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return humanReadableFormat; 54 | } 55 | 56 | private static boolean hasType(Elements elements, QualifiedName type) { 57 | return elements.getTypeElement(type.toString()) != null; 58 | } 59 | 60 | private Optional ifAvailable(T value) { 61 | return (this == AVAILABLE) ? Optional.of(value) : Optional.empty(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/feature/Jsr305.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source.feature; 2 | 3 | import org.inferred.freebuilder.processor.source.Excerpt; 4 | import org.inferred.freebuilder.processor.source.QualifiedName; 5 | import org.inferred.freebuilder.processor.source.SourceBuilder; 6 | 7 | import javax.annotation.processing.ProcessingEnvironment; 8 | import javax.lang.model.util.Elements; 9 | 10 | /** 11 | * Types from JSR 305, if available. Not available by default in tests. 12 | */ 13 | public enum Jsr305 implements Feature { 14 | 15 | AVAILABLE("JSR 305"), UNAVAILABLE("No JSR 305"); 16 | 17 | /** 18 | * Constant to pass to {@link SourceBuilder#feature(FeatureType)} to get the current status of 19 | * {@link Jsr305}. 20 | */ 21 | public static final FeatureType JSR305 = 22 | new FeatureType() { 23 | 24 | @Override 25 | protected Jsr305 testDefault(FeatureSet features) { 26 | return UNAVAILABLE; 27 | } 28 | 29 | @Override 30 | protected Jsr305 forEnvironment(ProcessingEnvironment env, FeatureSet features) { 31 | return hasType(env.getElementUtils(), NULLABLE) ? AVAILABLE : UNAVAILABLE; 32 | } 33 | }; 34 | 35 | /** 36 | * Excerpt that adds a JSR-303 Nullable annotation, if available. 37 | */ 38 | public static Excerpt nullable() { 39 | return new NullableExcerpt(); 40 | } 41 | 42 | private static class NullableExcerpt implements Excerpt { 43 | 44 | @Override 45 | public void addTo(SourceBuilder source) { 46 | switch (source.feature(JSR305)) { 47 | case AVAILABLE: 48 | source.add("@%s", NULLABLE); 49 | break; 50 | 51 | default: 52 | break; 53 | } 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "@Nullable"; 59 | } 60 | } 61 | 62 | private static final QualifiedName NULLABLE = QualifiedName.of("javax.annotation", "Nullable"); 63 | 64 | private final String humanReadableFormat; 65 | 66 | Jsr305(String humanReadableFormat) { 67 | this.humanReadableFormat = humanReadableFormat; 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return humanReadableFormat; 73 | } 74 | 75 | private static boolean hasType(Elements elements, QualifiedName type) { 76 | try { 77 | return elements.getTypeElement(type.toString()) != null; 78 | } catch (RuntimeException e) { 79 | // Work around Eclipse bug 80 | return false; 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/feature/SourceLevel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source.feature; 17 | 18 | import com.google.common.collect.ImmutableList; 19 | 20 | import org.inferred.freebuilder.processor.source.SourceBuilder; 21 | 22 | import java.util.List; 23 | 24 | import javax.annotation.processing.ProcessingEnvironment; 25 | import javax.lang.model.SourceVersion; 26 | 27 | /** 28 | * Compliance levels which are idiomatically supported by this processor. 29 | * 30 | *

{@link SourceVersion} is problematic to use, as the constants themselves will be missing 31 | * on compilers that do not support them (e.g. "RELEASE_8" is not available on javac v6 or v7). 32 | * Additionally, {@code sourceLevel.javaUtilObjects().isPresent()} is far more readable than 33 | * {@code sourceVersion.compareTo(SourceLevel.RELEASE_7) >= 0}. 34 | */ 35 | public enum SourceLevel implements Feature { 36 | 37 | JAVA_8("Java 8+", 8); 38 | 39 | /** 40 | * Constant to pass to {@link SourceBuilder#feature(FeatureType)} to get the current 41 | * {@link SourceLevel}. 42 | */ 43 | public static final FeatureType SOURCE_LEVEL = new FeatureType() { 44 | 45 | @Override 46 | protected SourceLevel testDefault(FeatureSet features) { 47 | return JAVA_8; 48 | } 49 | 50 | @Override 51 | protected SourceLevel forEnvironment(ProcessingEnvironment env, FeatureSet features) { 52 | return JAVA_8; 53 | } 54 | }; 55 | 56 | private final String humanReadableFormat; 57 | private final int version; 58 | 59 | SourceLevel(String humanReadableFormat, int version) { 60 | this.humanReadableFormat = humanReadableFormat; 61 | this.version = version; 62 | } 63 | 64 | public List javacArguments() { 65 | return ImmutableList.of("-source", Integer.toString(version)); 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | return humanReadableFormat; 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/inferred/freebuilder/processor/source/feature/StaticFeatureSet.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.source.feature; 2 | 3 | import static java.util.stream.Collectors.joining; 4 | 5 | import com.google.common.collect.ImmutableMap; 6 | 7 | /** 8 | * Stores a set of {@link Feature} instances, defaulting to {@link FeatureType#testDefault} when 9 | * asked for a type that was not explicitly registered. 10 | */ 11 | public class StaticFeatureSet implements FeatureSet { 12 | 13 | @SuppressWarnings("rawtypes") 14 | private final ImmutableMap, Feature> featuresByType; 15 | 16 | /** 17 | * Creates a feature set which will return {@code features} when {@link #get} is called for the 18 | * appropriate type. 19 | */ 20 | public StaticFeatureSet(Feature... features) { 21 | @SuppressWarnings("rawtypes") 22 | ImmutableMap.Builder, Feature> featuresBuilder = 23 | ImmutableMap.builder(); 24 | for (Feature feature : features) { 25 | featuresBuilder.put(feature.getClass(), feature); 26 | } 27 | this.featuresByType = featuresBuilder.build(); 28 | } 29 | 30 | /** 31 | * Returns the registered instance of {@code featureType}, or the value of 32 | * {@link FeatureType#testDefault} if no explicit instance was registered with this set. 33 | */ 34 | @Override 35 | public > T get(FeatureType featureType) { 36 | @SuppressWarnings("unchecked") 37 | T feature = (T) featuresByType.get(featureType.type()); 38 | if (feature != null) { 39 | return feature; 40 | } 41 | return featureType.testDefault(this); 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return featuresByType.values().stream().map(Object::toString).collect(joining(", ")); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/AbstractBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor; 17 | 18 | import org.inferred.freebuilder.FreeBuilder; 19 | import org.inferred.freebuilder.processor.source.SourceBuilder; 20 | import org.inferred.freebuilder.processor.source.feature.StaticFeatureSet; 21 | import org.inferred.freebuilder.processor.source.testing.BehaviorTester; 22 | import org.junit.Test; 23 | 24 | public class AbstractBuilderTest { 25 | private final BehaviorTester behaviorTester = BehaviorTester.create(new StaticFeatureSet()); 26 | 27 | private static final SourceBuilder TYPE_WITH_ABSTRACT_BUILDER = SourceBuilder 28 | .forTesting() 29 | .addLine("package com.example;") 30 | .addLine("@%s", FreeBuilder.class) 31 | .addLine("public abstract class TypeWithAbstractBuilder {") 32 | .addLine(" public abstract int getItem();") 33 | .addLine("") 34 | .addLine(" public abstract static class Builder") 35 | .addLine(" extends TypeWithAbstractBuilder_Builder {}") 36 | .addLine("}"); 37 | 38 | @Test 39 | public void testGenericWithConstraint() { 40 | behaviorTester 41 | .with(new Processor()) 42 | .with(TYPE_WITH_ABSTRACT_BUILDER) 43 | .compiles() 44 | .withNoWarnings(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/FeatureSets.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor; 2 | 3 | import static org.inferred.freebuilder.processor.source.feature.SourceLevel.JAVA_8; 4 | 5 | import com.google.common.collect.ImmutableList; 6 | 7 | import org.inferred.freebuilder.processor.source.feature.FeatureSet; 8 | import org.inferred.freebuilder.processor.source.feature.GuavaLibrary; 9 | import org.inferred.freebuilder.processor.source.feature.StaticFeatureSet; 10 | 11 | import java.util.List; 12 | 13 | public final class FeatureSets { 14 | 15 | /** For tests valid in any environment. */ 16 | public static final List ALL = ImmutableList.of( 17 | new StaticFeatureSet(JAVA_8), 18 | new StaticFeatureSet(JAVA_8, GuavaLibrary.AVAILABLE)); 19 | 20 | /** For tests using Guava types. */ 21 | public static final List WITH_GUAVA = ImmutableList.of( 22 | new StaticFeatureSet(JAVA_8, GuavaLibrary.AVAILABLE)); 23 | 24 | private FeatureSets() {} 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/GeneratedTypeSubject.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor; 2 | 3 | import static com.google.common.truth.Truth.THROW_ASSERTION_ERROR; 4 | 5 | import static java.util.stream.Collectors.joining; 6 | 7 | import com.google.common.truth.FailureStrategy; 8 | import com.google.common.truth.Subject; 9 | 10 | import org.inferred.freebuilder.processor.source.SourceBuilder; 11 | import org.inferred.freebuilder.processor.source.feature.Feature; 12 | import org.junit.ComparisonFailure; 13 | 14 | import java.util.Arrays; 15 | import java.util.HashSet; 16 | import java.util.Set; 17 | 18 | public class GeneratedTypeSubject extends Subject { 19 | 20 | public static GeneratedTypeSubject assertThat(GeneratedType subject) { 21 | return new GeneratedTypeSubject(THROW_ASSERTION_ERROR, subject); 22 | } 23 | 24 | private final Set> environmentFeatures = new HashSet<>(); 25 | 26 | private GeneratedTypeSubject(FailureStrategy failureStrategy, GeneratedType subject) { 27 | super(failureStrategy, subject); 28 | } 29 | 30 | public GeneratedTypeSubject given(Feature... features) { 31 | environmentFeatures.addAll(Arrays.asList(features)); 32 | return this; 33 | } 34 | 35 | public void generates(String... code) { 36 | String expected = Arrays.stream(code).collect(joining("\n", "", "\n")); 37 | String source = SourceBuilder 38 | .forTesting(environmentFeatures.toArray(new Feature[0])) 39 | .add(getSubject()) 40 | .toString(); 41 | if (!source.equals(expected)) { 42 | throw new ComparisonFailure("Generated code incorrect", expected, source); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/JacksonIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor; 2 | 3 | import com.fasterxml.jackson.annotation.JsonAlias; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 6 | 7 | import org.inferred.freebuilder.FreeBuilder; 8 | import org.inferred.freebuilder.processor.source.SourceBuilder; 9 | import org.inferred.freebuilder.processor.source.feature.FeatureSet; 10 | import org.inferred.freebuilder.processor.source.feature.StaticFeatureSet; 11 | import org.inferred.freebuilder.processor.source.testing.BehaviorTester; 12 | import org.inferred.freebuilder.processor.source.testing.ParameterizedBehaviorTestFactory.Shared; 13 | import org.inferred.freebuilder.processor.source.testing.TestBuilder; 14 | import org.junit.Test; 15 | 16 | public class JacksonIntegrationTest { 17 | 18 | @Shared public BehaviorTester behaviorTester; 19 | 20 | @Test 21 | public void testJsonAliasSupport() { 22 | FeatureSet featureSet = new StaticFeatureSet(); 23 | BehaviorTester.create(featureSet) 24 | .with(new Processor(featureSet)) 25 | .with(SourceBuilder.forTesting() 26 | .addLine("package com.example;") 27 | .addLine("@%s", FreeBuilder.class) 28 | .addLine("@%s(builder = DataType.Builder.class)", JsonDeserialize.class) 29 | .addLine("public abstract class DataType {") 30 | .addLine(" @%s({\"a\", \"theagame\"})", JsonAlias.class) 31 | .addLine(" public abstract int propertyA();") 32 | .addLine("") 33 | .addLine(" public static class Builder extends DataType_Builder {}") 34 | .addLine("}")) 35 | .with(testBuilder() 36 | .addLine("DataType expected = new DataType.Builder().propertyA(13).build();") 37 | .addLine("%1$s mapper = new %1$s();", ObjectMapper.class) 38 | .addLine("String canonicalJson = \"{ \\\"propertyA\\\": 13 }\";") 39 | .addLine("DataType canonical = mapper.readValue(canonicalJson, DataType.class);") 40 | .addLine("assertEquals(expected, canonical);") 41 | .addLine("String alternative1Json = \"{ \\\"a\\\": 13 }\";") 42 | .addLine("DataType alternative1 = mapper.readValue(canonicalJson, DataType.class);") 43 | .addLine("assertEquals(expected, alternative1);") 44 | .addLine("String alternative2Json = \"{ \\\"theagame\\\": 13 }\";") 45 | .addLine("DataType alternative2 = mapper.readValue(canonicalJson, DataType.class);") 46 | .addLine("assertEquals(expected, alternative2);") 47 | .build()) 48 | .runTest(); 49 | } 50 | 51 | private static TestBuilder testBuilder() { 52 | return new TestBuilder().addImport("com.example.DataType"); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/NamingConvention.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor; 17 | 18 | public enum NamingConvention { 19 | PREFIXLESS("prefixless") { 20 | @Override 21 | String accessor(String prefix, String fieldName) { 22 | return fieldName; 23 | } 24 | }, 25 | BEAN("bean") { 26 | @Override 27 | String accessor(String prefix, String fieldName) { 28 | return prefix + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); 29 | } 30 | }; 31 | 32 | private final String name; 33 | 34 | NamingConvention(String name) { 35 | this.name = name; 36 | } 37 | 38 | public String get() { 39 | return get("items"); 40 | } 41 | 42 | public String get(String fieldName) { 43 | return accessor("get", fieldName) + "()"; 44 | } 45 | 46 | public String set(String fieldName) { 47 | return accessor("set", fieldName); 48 | } 49 | 50 | public String is(String fieldName) { 51 | return accessor("is", fieldName) + "()"; 52 | } 53 | 54 | abstract String accessor(String prefix, String fieldName); 55 | 56 | @Override 57 | public String toString() { 58 | return name; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/model/GenericMirror.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.model; 17 | 18 | import static com.google.common.base.Preconditions.checkState; 19 | 20 | import com.google.common.collect.ImmutableList; 21 | 22 | import org.inferred.freebuilder.processor.source.Partial; 23 | 24 | import java.util.List; 25 | import java.util.concurrent.atomic.AtomicReference; 26 | 27 | import javax.lang.model.type.DeclaredType; 28 | import javax.lang.model.type.NoType; 29 | import javax.lang.model.type.TypeKind; 30 | import javax.lang.model.type.TypeMirror; 31 | import javax.lang.model.type.TypeVisitor; 32 | 33 | /** 34 | * Fake representation of a generic top-level type. 35 | */ 36 | public abstract class GenericMirror implements DeclaredType { 37 | 38 | static GenericMirror create( 39 | AtomicReference element, Iterable typeArguments) { 40 | return Partial.of(GenericMirror.class, element, typeArguments); 41 | } 42 | 43 | private final AtomicReference element; 44 | private final ImmutableList typeArguments; 45 | 46 | GenericMirror( 47 | AtomicReference element, Iterable typeArguments) { 48 | this.element = element; 49 | this.typeArguments = ImmutableList.copyOf(typeArguments); 50 | } 51 | 52 | @Override 53 | public TypeKind getKind() { 54 | return TypeKind.DECLARED; 55 | } 56 | 57 | @Override 58 | public R accept(TypeVisitor v, P p) { 59 | return v.visitDeclared(this, p); 60 | } 61 | 62 | @Override 63 | public GenericElement asElement() { 64 | GenericElement impl = element.get(); 65 | checkState(impl != null, 66 | "Cannot call asElement() on a GenericMirror referencing an unbuilt GenericType"); 67 | return impl; 68 | } 69 | 70 | @Override 71 | public NoType getEnclosingType() { 72 | return NoTypes.NONE; 73 | } 74 | 75 | @Override 76 | public List getTypeArguments() { 77 | return typeArguments; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/model/NameImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.model; 17 | 18 | import javax.lang.model.element.Name; 19 | 20 | public class NameImpl implements Name { 21 | 22 | private final String delegate; 23 | 24 | public NameImpl(String delegate) { 25 | this.delegate = delegate; 26 | } 27 | 28 | @Override 29 | public int length() { 30 | return delegate.length(); 31 | } 32 | 33 | @Override 34 | public char charAt(int index) { 35 | return delegate.charAt(index); 36 | } 37 | 38 | @Override 39 | public CharSequence subSequence(int start, int end) { 40 | return delegate.subSequence(start, end); 41 | } 42 | 43 | @Override 44 | public boolean contentEquals(CharSequence cs) { 45 | return delegate.contentEquals(cs); 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return delegate; 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | return delegate.hashCode(); 56 | } 57 | 58 | @Override 59 | public boolean equals(Object o) { 60 | return (o instanceof NameImpl) && delegate.equals(((NameImpl) o).delegate); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/model/NoTypes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.model; 17 | 18 | import org.inferred.freebuilder.processor.source.Partial; 19 | 20 | import javax.lang.model.type.NoType; 21 | import javax.lang.model.type.TypeKind; 22 | import javax.lang.model.type.TypeVisitor; 23 | 24 | /** 25 | * Fake implementation of {@link NoType} for unit tests. 26 | */ 27 | public abstract class NoTypes implements NoType { 28 | 29 | public static final NoType NONE = Partial.of(NoTypes.class, TypeKind.NONE); 30 | 31 | private final TypeKind kind; 32 | 33 | NoTypes(TypeKind kind) { 34 | this.kind = kind; 35 | } 36 | 37 | @Override 38 | public TypeKind getKind() { 39 | return kind; 40 | } 41 | 42 | @Override 43 | public R accept(TypeVisitor v, P p) { 44 | return v.visitNoType(this, p); 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return getKind().toString().toLowerCase(); 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/model/NullTypeImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.model; 17 | 18 | import org.inferred.freebuilder.processor.source.Partial; 19 | 20 | import javax.lang.model.type.NullType; 21 | import javax.lang.model.type.TypeKind; 22 | import javax.lang.model.type.TypeVisitor; 23 | 24 | public abstract class NullTypeImpl implements NullType { 25 | 26 | public static final NullType NULL = Partial.of(NullTypeImpl.class); 27 | 28 | NullTypeImpl() {} 29 | 30 | @Override 31 | public TypeKind getKind() { 32 | return TypeKind.NULL; 33 | } 34 | 35 | @Override 36 | public R accept(TypeVisitor v, P p) { 37 | return v.visitNull(this, p); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/model/PrimitiveTypeImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.model; 17 | 18 | import static com.google.common.base.Preconditions.checkState; 19 | 20 | import org.inferred.freebuilder.processor.source.Partial; 21 | 22 | import javax.lang.model.type.PrimitiveType; 23 | import javax.lang.model.type.TypeKind; 24 | import javax.lang.model.type.TypeVisitor; 25 | 26 | /** 27 | * Fake implementation of {@link PrimitiveType} for unit tests. 28 | */ 29 | public abstract class PrimitiveTypeImpl implements PrimitiveType { 30 | 31 | public static final PrimitiveType CHAR = Partial.of(PrimitiveTypeImpl.class, TypeKind.CHAR); 32 | public static final PrimitiveType INT = Partial.of(PrimitiveTypeImpl.class, TypeKind.INT); 33 | public static final PrimitiveType FLOAT = Partial.of(PrimitiveTypeImpl.class, TypeKind.FLOAT); 34 | public static final PrimitiveType DOUBLE = Partial.of(PrimitiveTypeImpl.class, TypeKind.DOUBLE); 35 | 36 | private final TypeKind kind; 37 | 38 | PrimitiveTypeImpl(TypeKind kind) { 39 | checkState(kind.isPrimitive()); 40 | this.kind = kind; 41 | } 42 | 43 | @Override 44 | public TypeKind getKind() { 45 | return kind; 46 | } 47 | 48 | @Override 49 | public R accept(TypeVisitor v, P p) { 50 | return v.visitPrimitive(this, p); 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return kind.toString().toLowerCase(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/model/TypeParameterElementImpl.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.model; 2 | 3 | import com.google.common.collect.ImmutableList; 4 | 5 | import org.inferred.freebuilder.processor.source.Partial; 6 | 7 | import java.util.List; 8 | 9 | import javax.lang.model.element.ElementKind; 10 | import javax.lang.model.element.ElementVisitor; 11 | import javax.lang.model.element.Name; 12 | import javax.lang.model.element.TypeParameterElement; 13 | import javax.lang.model.type.TypeMirror; 14 | import javax.lang.model.type.TypeVariable; 15 | 16 | public abstract class TypeParameterElementImpl implements TypeParameterElement { 17 | 18 | public static TypeParameterElementImpl newTypeParameterElement(String variableName) { 19 | return Partial.of(TypeParameterElementImpl.class, variableName); 20 | } 21 | 22 | private final String variableName; 23 | 24 | TypeParameterElementImpl(String variableName) { 25 | this.variableName = variableName; 26 | } 27 | 28 | @Override 29 | public TypeVariable asType() { 30 | return TypeVariableImpl.newTypeVariable(variableName); 31 | } 32 | 33 | @Override 34 | public ElementKind getKind() { 35 | return ElementKind.TYPE_PARAMETER; 36 | } 37 | 38 | @Override 39 | public Name getSimpleName() { 40 | return new NameImpl(variableName); 41 | } 42 | 43 | @Override 44 | public R accept(ElementVisitor v, P p) { 45 | return v.visitTypeParameter(this, p); 46 | } 47 | 48 | @Override 49 | public List getBounds() { 50 | return ImmutableList.of(); 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return variableName; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/model/TypeVariableImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.model; 17 | 18 | import static org.inferred.freebuilder.processor.model.ClassTypeImpl.newTopLevelClass; 19 | 20 | import com.google.common.collect.ImmutableList; 21 | 22 | import org.inferred.freebuilder.processor.source.Partial; 23 | 24 | import java.util.List; 25 | 26 | import javax.lang.model.element.TypeParameterElement; 27 | import javax.lang.model.type.TypeKind; 28 | import javax.lang.model.type.TypeMirror; 29 | import javax.lang.model.type.TypeVariable; 30 | import javax.lang.model.type.TypeVisitor; 31 | 32 | /** 33 | * Fake implementation of {@link TypeVariable} for unit tests. 34 | */ 35 | public abstract class TypeVariableImpl implements TypeVariable { 36 | 37 | public static TypeVariable newTypeVariable(String variableName) { 38 | return Partial.of(TypeVariableImpl.class, variableName); 39 | } 40 | 41 | private final String variableName; 42 | 43 | TypeVariableImpl(String variableName) { 44 | this.variableName = variableName; 45 | } 46 | 47 | @Override 48 | public TypeKind getKind() { 49 | return TypeKind.TYPEVAR; 50 | } 51 | 52 | @Override 53 | public R accept(TypeVisitor v, P p) { 54 | return v.visitTypeVariable(this, p); 55 | } 56 | 57 | @Override 58 | public TypeMirror getUpperBound() { 59 | return newTopLevelClass("java.lang.Object"); 60 | } 61 | 62 | @Override 63 | public TypeMirror getLowerBound() { 64 | return NullTypeImpl.NULL; 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return variableName; 70 | } 71 | 72 | @Override 73 | public TypeParameterElement asElement() { 74 | return Partial.of(ElementImpl.class, this); 75 | } 76 | 77 | abstract class ElementImpl implements TypeParameterElement { 78 | @Override 79 | public List getBounds() { 80 | return ImmutableList.of(); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/model/WildcardTypeImpl.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.model; 2 | 3 | import org.inferred.freebuilder.processor.source.Partial; 4 | import org.inferred.freebuilder.processor.source.ValueType; 5 | 6 | import javax.lang.model.type.TypeKind; 7 | import javax.lang.model.type.TypeMirror; 8 | import javax.lang.model.type.TypeVisitor; 9 | import javax.lang.model.type.WildcardType; 10 | 11 | public abstract class WildcardTypeImpl extends ValueType implements WildcardType { 12 | 13 | public static WildcardType wildcard() { 14 | return Partial.of(WildcardTypeImpl.class, null, null); 15 | } 16 | 17 | public static WildcardType wildcardSuper(TypeMirror superBound) { 18 | return Partial.of(WildcardTypeImpl.class, superBound, null); 19 | } 20 | 21 | public static WildcardType wildcardExtends(TypeMirror extendsBound) { 22 | return Partial.of(WildcardTypeImpl.class, null, extendsBound); 23 | } 24 | 25 | private final TypeMirror superBound; 26 | private final TypeMirror extendsBound; 27 | 28 | WildcardTypeImpl(TypeMirror superBound, TypeMirror extendsBound) { 29 | this.superBound = superBound; 30 | this.extendsBound = extendsBound; 31 | } 32 | 33 | @Override 34 | public TypeKind getKind() { 35 | return TypeKind.WILDCARD; 36 | } 37 | 38 | @Override 39 | public R accept(TypeVisitor v, P p) { 40 | return v.visitWildcard(this, p); 41 | } 42 | 43 | @Override 44 | public TypeMirror getExtendsBound() { 45 | return extendsBound; 46 | } 47 | 48 | @Override 49 | public TypeMirror getSuperBound() { 50 | return superBound; 51 | } 52 | 53 | @Override 54 | protected void addFields(FieldReceiver fields) { 55 | fields.add("superBound", superBound); 56 | fields.add("extendsBound", extendsBound); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/property/DodgyIterable.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.property; 2 | 3 | import com.google.common.collect.ImmutableList; 4 | 5 | import java.util.Iterator; 6 | 7 | /** Throws a {@link NullPointerException} the second time {@link #iterator()} is called. */ 8 | public class DodgyIterable implements Iterable { 9 | private ImmutableList values; 10 | 11 | @SafeVarargs 12 | public DodgyIterable(E... values) { 13 | this.values = ImmutableList.copyOf(values); 14 | } 15 | 16 | @Override 17 | public Iterator iterator() { 18 | try { 19 | return values.iterator(); 20 | } finally { 21 | values = null; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/property/SetType.java: -------------------------------------------------------------------------------- 1 | package org.inferred.freebuilder.processor.property; 2 | 3 | import static com.google.common.base.Preconditions.checkState; 4 | 5 | import static java.util.stream.Collectors.joining; 6 | 7 | import com.google.common.collect.ImmutableSet; 8 | import com.google.common.collect.ImmutableSortedSet; 9 | 10 | import java.util.Arrays; 11 | import java.util.Set; 12 | import java.util.SortedSet; 13 | import java.util.stream.IntStream; 14 | 15 | @SuppressWarnings("rawtypes") 16 | public enum SetType { 17 | SET(Set.class, ImmutableSet.class) { 18 | @Override 19 | public int[] inOrder(int... exampleIds) { 20 | return exampleIds; 21 | } 22 | 23 | @Override 24 | public String intsInOrder(int... examples) { 25 | return IntStream.of(examples).mapToObj(Integer::toString).collect(joining(", ")); 26 | } 27 | }, 28 | SORTED_SET(SortedSet.class, ImmutableSortedSet.class) { 29 | @Override 30 | public int[] inOrder(int... exampleIds) { 31 | int[] result = exampleIds.clone(); 32 | Arrays.sort(result); 33 | return result; 34 | } 35 | 36 | @Override 37 | public String intsInOrder(int... examples) { 38 | return IntStream.of(examples).sorted().mapToObj(Integer::toString).collect(joining(", ")); 39 | } 40 | }; 41 | 42 | private final Class setType; 43 | private final Class immutableSetType; 44 | 45 | SetType(Class setType, Class immutableSetType) { 46 | this.setType = setType; 47 | this.immutableSetType = immutableSetType; 48 | checkState(setType.isAssignableFrom(immutableSetType)); 49 | } 50 | 51 | public Class type() { 52 | return setType; 53 | } 54 | 55 | public Class immutableType() { 56 | return immutableSetType; 57 | } 58 | 59 | public boolean isSorted() { 60 | return SortedSet.class.isAssignableFrom(setType); 61 | } 62 | 63 | public abstract int[] inOrder(int... exampleIds); 64 | 65 | public abstract String intsInOrder(int... examples); 66 | 67 | @Override 68 | public String toString() { 69 | return setType.getSimpleName(); 70 | } 71 | } -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/source/feature/SourceLevelTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source.feature; 17 | 18 | import static org.inferred.freebuilder.processor.source.feature.SourceLevel.SOURCE_LEVEL; 19 | import static org.junit.Assert.assertEquals; 20 | import static org.mockito.Mockito.mock; 21 | import static org.mockito.Mockito.when; 22 | 23 | import org.junit.Test; 24 | import org.junit.runner.RunWith; 25 | import org.junit.runners.JUnit4; 26 | 27 | import javax.annotation.processing.ProcessingEnvironment; 28 | import javax.lang.model.SourceVersion; 29 | 30 | @RunWith(JUnit4.class) 31 | public class SourceLevelTest { 32 | 33 | @Test 34 | public void java8() { 35 | assertEquals(SourceLevel.JAVA_8, sourceLevelFrom(SourceVersion.RELEASE_8)); 36 | } 37 | 38 | private static SourceLevel sourceLevelFrom(SourceVersion version) { 39 | ProcessingEnvironment env = mock(ProcessingEnvironment.class); 40 | when(env.getSourceVersion()).thenReturn(version); 41 | return SOURCE_LEVEL.forEnvironment(env, null); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/source/testing/CompilationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source.testing; 17 | 18 | import com.google.common.collect.ImmutableList; 19 | 20 | import java.util.List; 21 | 22 | import javax.tools.Diagnostic; 23 | import javax.tools.JavaFileObject; 24 | 25 | /** 26 | * {@code CompilationException} is thrown when a 27 | * {@link javax.tools.JavaCompiler.CompilationTask CompilationTask} fails, to capture any emitted 28 | * {@link Diagnostic} instances. 29 | */ 30 | public class CompilationException extends RuntimeException { 31 | 32 | private final ImmutableList> diagnostics; 33 | 34 | public CompilationException(List> diagnostics) { 35 | this.diagnostics = ImmutableList.copyOf(diagnostics); 36 | } 37 | 38 | public CompilationException(CompilationException e) { 39 | this(e.diagnostics); 40 | } 41 | 42 | public List> getDiagnostics() { 43 | return diagnostics; 44 | } 45 | 46 | @Override 47 | public String getMessage() { 48 | StringBuilder fullMessage = new StringBuilder("Compilation failed"); 49 | int i = 1; 50 | for (Diagnostic diagnostic : diagnostics) { 51 | fullMessage.append("\n ").append(i++).append(") ").append(diagnostic); 52 | } 53 | return fullMessage.toString(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/source/testing/CompilationUnit.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source.testing; 17 | 18 | import org.inferred.freebuilder.processor.source.QualifiedName; 19 | import org.inferred.freebuilder.processor.source.SourceBuilder; 20 | 21 | import java.net.URI; 22 | import java.net.URISyntaxException; 23 | import java.util.Objects; 24 | 25 | import javax.tools.SimpleJavaFileObject; 26 | 27 | /** Simple in-memory implementation of {@link javax.tools.JavaFileObject JavaFileObject}. */ 28 | class CompilationUnit extends SimpleJavaFileObject { 29 | 30 | private final QualifiedName typename; 31 | private final String code; 32 | 33 | /** Returns a dummy URI for the given type name. */ 34 | static URI uriForClass(String typeName) { 35 | try { 36 | return new URI("mem:///" + typeName.replaceAll("\\.", "/") + ".java"); 37 | } catch (URISyntaxException e) { 38 | throw new RuntimeException(e); 39 | } 40 | } 41 | 42 | CompilationUnit(SourceBuilder unit) { 43 | super(uriForClass(unit.typename().toString()), Kind.SOURCE); 44 | this.typename = unit.typename(); 45 | this.code = unit.toString(); 46 | } 47 | 48 | @Override 49 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { 50 | return code; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return typename.toString(); 56 | } 57 | 58 | @Override 59 | public int hashCode() { 60 | return Objects.hash(typename, code); 61 | } 62 | 63 | @Override 64 | public boolean equals(Object obj) { 65 | if (!(obj instanceof CompilationUnit)) { 66 | return false; 67 | } 68 | CompilationUnit other = (CompilationUnit) obj; 69 | return typename.equals(other.typename) && code.equals(other.code); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/source/testing/Diagnostics.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source.testing; 17 | 18 | import com.google.common.base.Predicate; 19 | import com.google.common.base.Strings; 20 | 21 | import java.io.File; 22 | import java.util.EnumSet; 23 | import java.util.Locale; 24 | 25 | import javax.tools.Diagnostic; 26 | import javax.tools.Diagnostic.Kind; 27 | import javax.tools.JavaFileObject; 28 | 29 | /** 30 | * Static utilities for {@link Diagnostic} instances. 31 | */ 32 | public class Diagnostics { 33 | 34 | /** 35 | * Appends a human-readable form of {@code diagnostic} to {@code appendable}. 36 | */ 37 | public static void appendTo( 38 | StringBuilder appendable, 39 | Diagnostic diagnostic, 40 | int indentLevel) { 41 | String indent = "\n" + Strings.repeat(" ", indentLevel); 42 | appendable.append(diagnostic.getMessage(Locale.getDefault()).replace("\n", indent)); 43 | JavaFileObject source = diagnostic.getSource(); 44 | long line = diagnostic.getLineNumber(); 45 | if (source != null && line != Diagnostic.NOPOS) { 46 | File sourceFile = new File(source.getName()); 47 | appendable.append(" (").append(sourceFile.getName()).append(":").append(line).append(")"); 48 | } 49 | } 50 | 51 | /** 52 | * Predicate that returns true if its argument is of kind {@code kind} or any of 53 | * {@code otherKinds}. 54 | */ 55 | public static final Predicate> isKind(Kind kind, Kind... otherKinds) { 56 | final EnumSet allKinds = EnumSet.of(kind, otherKinds); 57 | return diagnostic -> allKinds.contains(diagnostic.getKind()); 58 | } 59 | 60 | private Diagnostics() {} 61 | } 62 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/source/testing/InMemoryJavaFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source.testing; 17 | 18 | import static java.util.stream.Collectors.joining; 19 | 20 | import org.inferred.freebuilder.processor.source.QualifiedName; 21 | 22 | import javax.lang.model.element.Modifier; 23 | import javax.lang.model.element.NestingKind; 24 | import javax.tools.JavaFileObject; 25 | 26 | public class InMemoryJavaFile extends InMemoryFile implements JavaFileObject { 27 | 28 | private final QualifiedName qualifiedName; 29 | private final Kind kind; 30 | 31 | public InMemoryJavaFile(QualifiedName qualifiedName, Kind kind) { 32 | super(name(qualifiedName, kind)); 33 | this.qualifiedName = qualifiedName; 34 | this.kind = kind; 35 | } 36 | 37 | @Override 38 | public Kind getKind() { 39 | return kind; 40 | } 41 | 42 | @Override 43 | public boolean isNameCompatible(String simpleName, Kind kind) { 44 | return qualifiedName.getSimpleName().equals(simpleName) && this.kind == kind; 45 | } 46 | 47 | @Override 48 | public NestingKind getNestingKind() { 49 | return qualifiedName.isTopLevel() ? NestingKind.TOP_LEVEL : NestingKind.MEMBER; 50 | } 51 | 52 | @Override 53 | public Modifier getAccessLevel() { 54 | return null; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "InMemoryJavaFile(name=" + getName() + ")"; 60 | } 61 | 62 | private static String name(QualifiedName qualifiedName, Kind kind) { 63 | StringBuilder name = new StringBuilder(); 64 | if (!qualifiedName.getPackage().isEmpty()) { 65 | name.append(qualifiedName.getPackage().replace('.', '/')).append('/'); 66 | } 67 | name.append(qualifiedName.getSimpleNames().stream().collect(joining("$"))); 68 | name.append(kind.extension); 69 | return name.toString(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/source/testing/ModelRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source.testing; 17 | 18 | import org.junit.rules.TestRule; 19 | import org.junit.runner.Description; 20 | import org.junit.runners.model.Statement; 21 | 22 | /** 23 | * {@link org.junit.rules.TestRule TestRule} for creating javax.lang.model instances in a unit 24 | * test. 25 | * 26 | *

The standard javax.lang.model.util objects, and {@link javax.lang.model.type.TypeMirror 27 | * TypeMirror} instances for existing classes, are readily available. More complex {@link 28 | * javax.lang.model.element.Element elements} can be constructed from Java source code snippets, 29 | * allowing top-level types and even code with errors in to be contained within a single test 30 | * method. 31 | * 32 | *

33 |  * {@link org.junit.runner.RunWith @RunWith}({@link org.junit.runners.JUnit4 JUnit4}.class)
34 |  * public class TypeUtilsTest {
35 |  *
36 |  *   {@link org.junit.Rule @Rule} public final ModelRule model = new ModelRule();
37 |  *
38 |  *   {@link org.junit.Test @Test}
39 |  *   public void aTest() {
40 |  *     TypeMirror intType = {@link #typeMirror model.typeMirror}(int.class);
41 |  *     TypeElement myType = {@link #newType model.newType}(
42 |  *         "package my.test.package;",
43 |  *         "public class MyType {",
44 |  *         "  public void aMethod(int anArg);",
45 |  *         "}");
46 |  *     ...
47 |  * 
48 | */ 49 | public class ModelRule extends Model implements TestRule { 50 | 51 | @Override 52 | public Statement apply(Statement base, Description description) { 53 | return new Statement() { 54 | @Override 55 | public void evaluate() throws Throwable { 56 | start(); 57 | try { 58 | base.evaluate(); 59 | } finally { 60 | destroy(); 61 | } 62 | } 63 | }; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/source/testing/ModelRuleTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source.testing; 17 | 18 | import static com.google.common.collect.Iterables.getOnlyElement; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | 22 | import static javax.lang.model.util.ElementFilter.methodsIn; 23 | 24 | import org.junit.Rule; 25 | import org.junit.Test; 26 | import org.junit.runner.RunWith; 27 | import org.junit.runners.JUnit4; 28 | 29 | import javax.lang.model.element.ElementKind; 30 | import javax.lang.model.element.NestingKind; 31 | import javax.lang.model.element.TypeElement; 32 | 33 | /** Tests for {@link ModelRule}. */ 34 | @RunWith(JUnit4.class) 35 | public class ModelRuleTest { 36 | 37 | @Rule public ModelRule model = new ModelRule(); 38 | public ModelRule brokenModel = new ModelRule(); 39 | 40 | @Test 41 | public void newType() { 42 | TypeElement type = model.newType( 43 | "package foo.bar;", 44 | "public class MyType {", 45 | " public void doNothing() { }", 46 | "}"); 47 | assertEquals(ElementKind.CLASS, type.getKind()); 48 | assertEquals(NestingKind.TOP_LEVEL, type.getNestingKind()); 49 | assertEquals("MyType", type.getSimpleName().toString()); 50 | assertEquals("foo.bar.MyType", type.toString()); 51 | assertEquals("doNothing", 52 | getOnlyElement(methodsIn(type.getEnclosedElements())).getSimpleName().toString()); 53 | } 54 | 55 | @Test(expected = IllegalStateException.class) 56 | public void testRuleAnnotationMissing() { 57 | brokenModel.typeUtils(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/source/testing/ParameterizedBehaviorTestFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source.testing; 17 | 18 | import static java.lang.annotation.ElementType.FIELD; 19 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 20 | 21 | import org.junit.rules.ExpectedException; 22 | import org.junit.runner.Runner; 23 | import org.junit.runners.Parameterized; 24 | import org.junit.runners.model.InitializationError; 25 | import org.junit.runners.parameterized.ParametersRunnerFactory; 26 | import org.junit.runners.parameterized.TestWithParameters; 27 | 28 | import java.lang.annotation.Retention; 29 | import java.lang.annotation.Target; 30 | 31 | /** 32 | * Optimized test runner factory for {@link Parameterized} tests using {@link BehaviorTester}. 33 | * 34 | *

Pre-runs all tests once with no compilation, to determine which can use the same compiler. 35 | * If two tests add the same processors and do not give conflicting java files, they will be 36 | * run back-to-back with the same compiler instance. This means all tests are run twice. 37 | * Any tests which do not compile the same code every time (e.g. random or time-based code) will 38 | * flake with this runner. 39 | * 40 | *

Tests that provide non-compilable test code must use 41 | * {@link ExpectedException} to catch the {@link CompilationException}, as this runner uses the 42 | * resulting pre-run error message to mark the test as unmergeable. (If we did not do this, 43 | * all tests using the same compiler would fail with the same compilation exception!) 44 | * 45 | *

Adds an "Introspection" test to the suite to show how much time the initial pre-run is taking. 46 | * If no tests can share a compiler, the "Introspection" test will fail. This may indicate your 47 | * processor does not have suitable equals and hashCode implementations. 48 | */ 49 | public class ParameterizedBehaviorTestFactory implements ParametersRunnerFactory { 50 | 51 | /** 52 | * Annotates a public {@link BehaviorTester} field that will potentially be shared between 53 | * multiple unit tests. 54 | */ 55 | @Retention(RUNTIME) 56 | @Target(FIELD) 57 | public @interface Shared {} 58 | 59 | @Override 60 | public Runner createRunnerForTestWithParameters(TestWithParameters test) 61 | throws InitializationError { 62 | return new ParameterizedBehaviorTestRunner(test); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/source/testing/Target.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source.testing; 17 | 18 | /** 19 | * Annotation used internally within the {@link Model} implementation to target elements. 20 | * 21 | *

Public as it is used to annotated arbitrary user-supplied code at runtime. Not visible in 22 | * Blaze, as it should not be used outside of this package. 23 | */ 24 | public @interface Target { } 25 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/source/testing/TestBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.source.testing; 17 | 18 | import static com.google.common.truth.Truth.assertThat; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.runners.MethodSorters.NAME_ASCENDING; 22 | 23 | import com.google.common.collect.LinkedHashMultiset; 24 | import com.google.common.collect.Multiset; 25 | 26 | import org.junit.FixMethodOrder; 27 | import org.junit.Test; 28 | import org.junit.runner.RunWith; 29 | import org.junit.runners.JUnit4; 30 | 31 | import javax.tools.JavaFileObject; 32 | 33 | /** Unit tests for {@link TestBuilder}. */ 34 | @RunWith(JUnit4.class) 35 | @FixMethodOrder(NAME_ASCENDING) 36 | public class TestBuilderTest { 37 | 38 | private final Multiset seenNames = LinkedHashMultiset.create(); 39 | 40 | @Test 41 | public void test1_UniqueNames() { 42 | JavaFileObject source1 = new TestBuilder().build().selectName(seenNames); 43 | assertThat(source1.toUri().toString()).endsWith( 44 | "/org/inferred/freebuilder/processor/source/testing/generatedcode/TestBuilderTest.java"); 45 | JavaFileObject source2 = new TestBuilder().build().selectName(seenNames); 46 | assertThat(source2.toUri().toString()).endsWith("TestBuilderTest__2.java"); 47 | assertEquals(2, seenNames.size()); 48 | } 49 | 50 | public static class InnerClass { } 51 | 52 | @Test 53 | public void test2_InnerClassNames() { 54 | String result = new TestBuilder() 55 | .addLine("%s", InnerClass.class) 56 | .build() 57 | .selectName(seenNames) 58 | .getCharContent(false) 59 | .toString(); 60 | assertThat(result).contains( 61 | "org.inferred.freebuilder.processor.source.testing.TestBuilderTest.InnerClass"); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/testtype/AbstractNonComparable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.testtype; 17 | 18 | import java.util.Comparator; 19 | 20 | public abstract class AbstractNonComparable { 21 | 22 | public abstract int id(); 23 | 24 | public static final class ReverseIdComparator implements Comparator { 25 | @Override 26 | public int compare(AbstractNonComparable o1, AbstractNonComparable o2) { 27 | return Integer.compare(o2.id(), o1.id()); 28 | } 29 | 30 | @Override 31 | public boolean equals(Object obj) { 32 | return (obj instanceof ReverseIdComparator); 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | return getClass().hashCode(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/testtype/NonComparable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.testtype; 17 | 18 | import static java.util.Objects.requireNonNull; 19 | 20 | import com.fasterxml.jackson.annotation.JsonCreator; 21 | import com.fasterxml.jackson.annotation.JsonProperty; 22 | 23 | import java.util.Objects; 24 | import java.util.concurrent.ConcurrentHashMap; 25 | import java.util.concurrent.ConcurrentMap; 26 | 27 | public class NonComparable extends AbstractNonComparable { 28 | 29 | private static final ConcurrentMap interned = 30 | new ConcurrentHashMap<>(); 31 | 32 | private final int id; 33 | private final String name; 34 | 35 | @JsonCreator 36 | public static NonComparable of(@JsonProperty("id") int id, @JsonProperty("name") String name) { 37 | return new NonComparable(id, name).intern(); 38 | } 39 | 40 | private NonComparable(int id, String name) { 41 | this.id = requireNonNull(id); 42 | this.name = requireNonNull(name); 43 | } 44 | 45 | public NonComparable(NonComparable other) { 46 | this(other.id, other.name); 47 | } 48 | 49 | public NonComparable intern() { 50 | return interned.computeIfAbsent(this, k -> k); 51 | } 52 | 53 | @Override 54 | @JsonProperty("id") 55 | public int id() { 56 | return id; 57 | } 58 | 59 | @JsonProperty("name") 60 | public String name() { 61 | return name; 62 | } 63 | 64 | @Override 65 | public int hashCode() { 66 | return Objects.hash(id, name); 67 | } 68 | 69 | @Override 70 | public boolean equals(Object obj) { 71 | if (!(obj instanceof NonComparable)) { 72 | return false; 73 | } 74 | NonComparable other = (NonComparable) obj; 75 | return (id == other.id) && (name.equals(other.name)); 76 | } 77 | 78 | @Override 79 | public String toString() { 80 | return "NonComparable [id=" + id + ", name=" + name + "]"; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/test/java/org/inferred/freebuilder/processor/testtype/OtherNonComparable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.inferred.freebuilder.processor.testtype; 17 | 18 | import static java.util.Objects.requireNonNull; 19 | 20 | import java.util.Objects; 21 | 22 | public class OtherNonComparable extends AbstractNonComparable { 23 | 24 | private final int id; 25 | private final String name; 26 | 27 | public OtherNonComparable(int id, String name) { 28 | this.id = requireNonNull(id); 29 | this.name = requireNonNull(name); 30 | } 31 | 32 | @Override 33 | public int id() { 34 | return id; 35 | } 36 | 37 | public String name() { 38 | return name; 39 | } 40 | 41 | @Override 42 | public int hashCode() { 43 | return Objects.hash(id, name); 44 | } 45 | 46 | @Override 47 | public boolean equals(Object obj) { 48 | if (!(obj instanceof OtherNonComparable)) { 49 | return false; 50 | } 51 | OtherNonComparable other = (OtherNonComparable) obj; 52 | return (id == other.id) && (name.equals(other.name)); 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return "OtherNonComparable [id=" + id + ", name=" + name + "]"; 58 | } 59 | } 60 | --------------------------------------------------------------------------------