├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── RELEASING.md ├── apache2.header ├── dataenum-integration-test ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── spotify │ │ └── dataenim │ │ └── apicompat │ │ ├── MostThings_dataenum.java │ │ └── subpackage │ │ └── Other_dataenum.java │ └── test │ └── java │ └── com │ └── spotify │ └── dataenum │ └── integration │ ├── OuterIntegrationTest.java │ ├── Redacted.java │ ├── RedactedTesting_dataenum.java │ └── Testing_dataenum.java ├── dataenum-processor ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── spotify │ │ │ └── dataenum │ │ │ └── processor │ │ │ ├── AccessSelector.java │ │ │ ├── DataEnumProcessor.java │ │ │ ├── ProcessingContext.java │ │ │ ├── data │ │ │ ├── OutputSpec.java │ │ │ ├── OutputValue.java │ │ │ ├── Parameter.java │ │ │ ├── Spec.java │ │ │ └── Value.java │ │ │ ├── generator │ │ │ ├── data │ │ │ │ ├── OutputSpecFactory.java │ │ │ │ └── OutputValueFactory.java │ │ │ ├── match │ │ │ │ ├── MapMethods.java │ │ │ │ ├── MatchMethods.java │ │ │ │ └── TypeVariableUtils.java │ │ │ ├── method │ │ │ │ └── MethodMethods.java │ │ │ ├── spec │ │ │ │ └── SpecTypeFactory.java │ │ │ └── value │ │ │ │ ├── ValueMethods.java │ │ │ │ └── ValueTypeFactory.java │ │ │ ├── parser │ │ │ ├── MembersParser.java │ │ │ ├── MethodParser.java │ │ │ ├── ParserException.java │ │ │ ├── SpecParser.java │ │ │ └── ValueParser.java │ │ │ └── util │ │ │ └── Iterables.java │ └── resources │ │ └── META-INF │ │ ├── gradle │ │ └── incremental.annotation.processors │ │ └── services │ │ └── javax.annotation.processing.Processor │ └── test │ ├── java │ └── com │ │ └── spotify │ │ └── dataenum │ │ └── processor │ │ ├── AccessSelectorTest.java │ │ ├── IntegrationTest.java │ │ └── generator │ │ └── data │ │ └── OutputSpecFactoryTest.java │ └── resources │ ├── ArrayFields.java │ ├── ArrayFields_dataenum.java │ ├── BadValueSpecs_dataenum.java │ ├── ConflictingFieldNames.java │ ├── ConflictingFieldNames_dataenum.java │ ├── DuplicateCases_dataenum.java │ ├── EfficientEquals.java │ ├── EfficientEquals_dataenum.java │ ├── Empty.java │ ├── EmptyValue.java │ ├── EmptyValue_dataenum.java │ ├── Empty_dataenum.java │ ├── EnumAsInner.java │ ├── EnumAsInner_dataenum.java │ ├── GenericDeclarationOnValues_dataenum.java │ ├── GenericValues.java │ ├── GenericValues_dataenum.java │ ├── InnerGenericValue.java │ ├── InnerGenericValue_dataenum.java │ ├── MethodsAndValues.java │ ├── MethodsAndValues_dataenum.java │ ├── MultipleValues.java │ ├── MultipleValues_dataenum.java │ ├── MyEnum.java │ ├── NonInterfaceSpec_dataenum.java │ ├── NullableValue.java │ ├── NullableValue_dataenum.java │ ├── PrimitiveValue.java │ ├── PrimitiveValue_dataenum.java │ ├── PublicSpec_dataenum.java │ ├── RecursiveGenericValue.java │ ├── RecursiveGenericValue_dataenum.java │ ├── RecursiveValue.java │ ├── RecursiveValue_dataenum.java │ ├── Redacted.java │ ├── Redacted_dataenum.java │ ├── ReferencesOther.java │ ├── ReferencesOther_dataenum.java │ ├── SuperInterfaces.java │ ├── SuperInterfaces_dataenum.java │ ├── VarargValue_dataenum.java │ ├── annotation │ ├── Annotation.java │ ├── Annotation_dataenum.java │ └── MyAnnotation.java │ ├── hide │ ├── ctors │ │ ├── PrivateConstructors.java │ │ └── PrivateConstructors_dataenum.java │ └── package-info.java │ ├── javadoc │ └── Javadoc_dataenum.java │ └── just │ └── some │ └── pkg │ ├── InPackage.java │ └── InPackage_dataenum.java ├── dataenum ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── spotify │ │ └── dataenum │ │ ├── Access.java │ │ ├── ConstructorAccess.java │ │ ├── DataEnum.java │ │ ├── DataenumUtils.java │ │ ├── Redacted.java │ │ ├── Static.java │ │ ├── dataenum_case.java │ │ └── function │ │ ├── Cases.java │ │ ├── Consumer.java │ │ ├── Function.java │ │ └── Supplier.java │ └── test │ └── java │ └── com │ └── spotify │ └── dataenum │ ├── DataenumUtilsTest.java │ └── function │ └── CasesTest.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml 3 | target/ 4 | .java-version 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | jdk: 4 | - openjdk11 5 | 6 | script: 7 | - mvn -Pcoverage clean verify 8 | 9 | after_success: 10 | - bash <(curl -s https://codecov.io/bash) 11 | 12 | #notifications: 13 | # email: false 14 | -------------------------------------------------------------------------------- /RELEASING.md: -------------------------------------------------------------------------------- 1 | Releasing 2 | ======== 3 | 4 | 1. Ensure you are set up for deploying to Maven Central. 5 | 1. Ensure you're using JDK 8. 6 | 1. Run `mvn release:prepare release:perform` and follow the instructions. 7 | 1. Enter the release version when prompted or press Enter for default (Please double check the version if you do so). 8 | 1. Enter the next development version when prompted or press Enter for default (Please double check the version if you do so). 9 | 1. Add release notes to the newly created tag on https://github.com/spotify/dataenum/releases. 10 | 1. Once the new release has propagated to Maven Central, update the `last.version` property in the 11 | root pom.xml to indicate the right version. Failing to do so means opening up for this: 12 | 1. `last.version` points to version 14. 13 | 1. version 15 is released, adding the method `foo(Integer i)`. 14 | 1. version 16 is released, changing signature of `foo` to `foo(Boolean b)`. This is an API-breaking 15 | change that goes undetected, since japicmp is only checking against version 14, where `foo` 16 | didn't exist. 17 | -------------------------------------------------------------------------------- /apache2.header: -------------------------------------------------------------------------------- 1 | -\-\- 2 | DataEnum 3 | -- 4 | Copyright (c) 2017 Spotify AB 5 | -- 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | -/-/- 18 | -------------------------------------------------------------------------------- /dataenum-integration-test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | dataenum-parent 5 | com.spotify.dataenum 6 | 1.5.1-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | dataenum-integration-test 11 | Dataenum Integration Tests 12 | 13 | 14 | 15 | com.spotify.dataenum 16 | dataenum 17 | ${project.version} 18 | 19 | 20 | com.spotify.dataenum 21 | dataenum-processor 22 | ${project.version} 23 | provided 24 | 25 | 26 | 27 | junit 28 | junit 29 | test 30 | 31 | 32 | org.assertj 33 | assertj-core 34 | test 35 | 36 | 37 | 38 | 39 | 40 | 41 | com.coveo 42 | fmt-maven-plugin 43 | 44 | 45 | com.github.siom79.japicmp 46 | japicmp-maven-plugin 47 | 48 | 49 | com.github.spotbugs 50 | spotbugs-maven-plugin 51 | 52 | 53 | verify 54 | check 55 | 56 | 57 | 58 | 59 | 60 | com.mebigfatguy.fb-contrib 61 | fb-contrib 62 | 7.4.0 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | coverage 73 | 74 | 75 | 76 | org.jacoco 77 | jacoco-maven-plugin 78 | 79 | 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /dataenum-integration-test/src/main/java/com/spotify/dataenim/apicompat/MostThings_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenim.apicompat; 21 | 22 | import com.spotify.dataenim.apicompat.subpackage.Other_dataenum; 23 | import com.spotify.dataenum.DataEnum; 24 | import com.spotify.dataenum.dataenum_case; 25 | import java.util.List; 26 | import java.util.Set; 27 | import javax.annotation.Nullable; 28 | 29 | /** Starting point for dataenum API compatibility tests. */ 30 | @DataEnum 31 | public interface MostThings_dataenum { 32 | dataenum_case SimpleTypes(String s, Integer i, int unboxed); 33 | 34 | dataenum_case Collections(Set set, List list); 35 | 36 | dataenum_case TypeParameter(T thing); 37 | 38 | dataenum_case ReferencesOther(Other_dataenum other); 39 | 40 | dataenum_case NullableParameter(@Nullable String maybeNull); 41 | } 42 | -------------------------------------------------------------------------------- /dataenum-integration-test/src/main/java/com/spotify/dataenim/apicompat/subpackage/Other_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenim.apicompat.subpackage; 21 | 22 | import com.spotify.dataenum.DataEnum; 23 | import com.spotify.dataenum.dataenum_case; 24 | 25 | @DataEnum 26 | public interface Other_dataenum { 27 | dataenum_case Another(); 28 | } 29 | -------------------------------------------------------------------------------- /dataenum-integration-test/src/test/java/com/spotify/dataenum/integration/OuterIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.integration; 21 | 22 | import static com.spotify.dataenum.function.Cases.illegal; 23 | import static com.spotify.dataenum.function.Cases.todo; 24 | import static org.assertj.core.api.Assertions.assertThat; 25 | import static org.assertj.core.api.Assertions.assertThatThrownBy; 26 | 27 | import com.spotify.dataenum.integration.Testing.RedactedValue; 28 | import com.spotify.dataenum.integration.Testing.Three; 29 | import com.spotify.dataenum.integration.Testing.Two; 30 | import org.junit.Before; 31 | import org.junit.Test; 32 | 33 | public class OuterIntegrationTest { 34 | 35 | private Testing t; 36 | 37 | @Before 38 | public void setUp() throws Exception { 39 | t = Testing.one(1); 40 | } 41 | 42 | @Test 43 | public void shouldAllowMapping() throws Exception { 44 | // should compile and not show warnings 45 | String s = t.map(one -> String.valueOf(one.i()), Two::s, Three::s, RedactedValue::shouldShow); 46 | 47 | // use s to ensure this test class has zero warnings 48 | assertThat(s).isEqualTo("1"); 49 | } 50 | 51 | @Test 52 | public void shouldAllowConvenientTodoForMap() throws Exception { 53 | assertThatThrownBy(() -> t.map(one -> todo(), Two::s, Three::s, RedactedValue::shouldShow)) 54 | .isInstanceOf(UnsupportedOperationException.class) 55 | .hasMessageContaining("TODO"); 56 | } 57 | 58 | @Test 59 | public void shouldAllowConvenientTodoForMatch() throws Exception { 60 | assertThatThrownBy(() -> t.match(one -> todo(), two -> {}, three -> {}, value -> {})) 61 | .isInstanceOf(UnsupportedOperationException.class) 62 | .hasMessageContaining("TODO"); 63 | } 64 | 65 | @Test 66 | public void shouldAllowConvenientIllegalStateForMap() throws Exception { 67 | assertThatThrownBy( 68 | () -> 69 | t.map( 70 | one -> illegal("nono: " + one.i()), 71 | Two::s, 72 | Three::s, 73 | RedactedValue::shouldShow)) 74 | .isInstanceOf(IllegalStateException.class) 75 | .hasMessage("nono: 1"); 76 | } 77 | 78 | @Test 79 | public void shouldAllowConvenientIllegalStateForMatch() throws Exception { 80 | assertThatThrownBy( 81 | () -> 82 | t.match( 83 | one -> illegal("nope, should be: " + (one.i() + 1)), 84 | two -> {}, 85 | three -> {}, 86 | value -> {})) 87 | .isInstanceOf(IllegalStateException.class) 88 | .hasMessage("nope, should be: 2"); 89 | } 90 | 91 | @Test 92 | public void shouldRedactAnnotatedFieldsInToString() { 93 | assertThat(Testing.redactedValue("i want to see this", 866).toString()) 94 | .contains("i want to see this") 95 | .doesNotContain("866"); 96 | } 97 | 98 | @Test 99 | public void shouldSupportAnyRedactedAnnotation() { 100 | assertThat(RedactedTesting.redactMe("hide this").toString()).doesNotContain("hide this"); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /dataenum-integration-test/src/test/java/com/spotify/dataenum/integration/Redacted.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.integration; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | /** 28 | * Proprietary 'Redacted' annotation to ensure that you can use any - this helps avoiding conflicts 29 | * with auto-value-redacted, for instance. 30 | */ 31 | @Retention(RetentionPolicy.SOURCE) 32 | @Target(ElementType.PARAMETER) 33 | public @interface Redacted {} 34 | -------------------------------------------------------------------------------- /dataenum-integration-test/src/test/java/com/spotify/dataenum/integration/RedactedTesting_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.integration; 21 | 22 | import com.spotify.dataenum.DataEnum; 23 | import com.spotify.dataenum.dataenum_case; 24 | 25 | @DataEnum 26 | interface RedactedTesting_dataenum { 27 | 28 | dataenum_case RedactMe(@Redacted String foo); 29 | } 30 | -------------------------------------------------------------------------------- /dataenum-integration-test/src/test/java/com/spotify/dataenum/integration/Testing_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.integration; 21 | 22 | import com.spotify.dataenum.DataEnum; 23 | import com.spotify.dataenum.Redacted; 24 | import com.spotify.dataenum.dataenum_case; 25 | 26 | /** For use in integration-type tests. */ 27 | @DataEnum 28 | interface Testing_dataenum { 29 | dataenum_case One(int i); 30 | 31 | dataenum_case Two(String s); 32 | 33 | dataenum_case Three(String s); 34 | 35 | dataenum_case RedactedValue(String shouldShow, @Redacted Integer redacted); 36 | } 37 | -------------------------------------------------------------------------------- /dataenum-processor/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | 5 | com.spotify.dataenum 6 | dataenum-parent 7 | 1.5.1-SNAPSHOT 8 | 9 | 10 | Dataenum Annotation Processor 11 | dataenum-processor 12 | 13 | 14 | 15 | com.spotify.dataenum 16 | dataenum 17 | ${project.version} 18 | 19 | 20 | com.squareup 21 | javapoet 22 | 23 | 24 | javax.annotation 25 | javax.annotation-api 26 | 1.3.2 27 | 28 | 29 | 30 | com.google.testing.compile 31 | compile-testing 32 | test 33 | 34 | 35 | junit 36 | junit 37 | test 38 | 39 | 40 | org.assertj 41 | assertj-core 42 | test 43 | 44 | 45 | 46 | 47 | 48 | 49 | maven-compiler-plugin 50 | 51 | 52 | default-compile 53 | 54 | -proc:none 55 | 56 | 57 | 58 | 59 | 60 | com.coveo 61 | fmt-maven-plugin 62 | 63 | 64 | 65 | 66 | 67 | 68 | coverage 69 | 70 | 71 | 72 | org.jacoco 73 | jacoco-maven-plugin 74 | 0.8.4 75 | 76 | 77 | **/AutoValue_* 78 | 79 | 80 | 81 | 82 | 83 | prepare-agent 84 | 85 | 86 | 87 | report 88 | prepare-package 89 | 90 | report 91 | 92 | 93 | 94 | check 95 | 96 | check 97 | 98 | 99 | 100 | 101 | BUNDLE 102 | 103 | 104 | INSTRUCTION 105 | COVEREDRATIO 106 | 0.90 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | java9 120 | 121 | 9 122 | 123 | 124 | 125 | 126 | maven-compiler-plugin 127 | 128 | 129 | default-compile 130 | 131 | -proc:none 132 | 8 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/AccessSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor; 21 | 22 | import com.spotify.dataenum.Access; 23 | import com.spotify.dataenum.ConstructorAccess; 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | import java.util.Optional; 27 | import java.util.Set; 28 | import javax.lang.model.element.Element; 29 | import javax.lang.model.element.Modifier; 30 | import javax.lang.model.element.PackageElement; 31 | 32 | /** 33 | * Determines the most appropriate access level based on package-level annotations. Uses a 34 | * simplistic algorithm that should perform well given a small number of annotated packages, which 35 | * seems like a reasonable assumption to make. 36 | */ 37 | class AccessSelector { 38 | 39 | // sorted so that the longest names are first; this means the first hit is going to be the best 40 | // one 41 | private final List packages; 42 | 43 | AccessSelector(Set visibilityAnnotatedPackages) { 44 | packages = parseAnnotatedPackages(visibilityAnnotatedPackages); 45 | } 46 | 47 | private List parseAnnotatedPackages( 48 | Set visibilityAnnotatedPackages) { 49 | ArrayList result = new ArrayList<>(visibilityAnnotatedPackages.size()); 50 | 51 | for (Element element : visibilityAnnotatedPackages) { 52 | if (!(element instanceof PackageElement)) { 53 | throw new IllegalArgumentException( 54 | "received a access annotated element that is not a package: " + element); 55 | } 56 | 57 | PackageElement packageElement = (PackageElement) element; 58 | 59 | result.add( 60 | new PackageAndAccess( 61 | packageElement.getQualifiedName().toString(), 62 | element.getAnnotation(ConstructorAccess.class).value())); 63 | } 64 | 65 | result.sort((o1, o2) -> o2.packageName.length() - o1.packageName.length()); 66 | 67 | return result; 68 | } 69 | 70 | public Optional accessModifierFor(String packageName) { 71 | switch (accessFor(packageName)) { 72 | case PACKAGE_PRIVATE: 73 | return Optional.empty(); 74 | case PRIVATE: 75 | return Optional.of(Modifier.PRIVATE); 76 | case PROTECTED: 77 | return Optional.of(Modifier.PROTECTED); 78 | case PUBLIC: 79 | return Optional.of(Modifier.PUBLIC); 80 | } 81 | 82 | throw new AssertionError("cannot get here"); 83 | } 84 | 85 | private Access accessFor(String packageName) { 86 | for (PackageAndAccess packageAndAccess : packages) { 87 | if (isParentOf(packageAndAccess.packageName, packageName)) { 88 | return packageAndAccess.access; 89 | } 90 | } 91 | 92 | return Access.PACKAGE_PRIVATE; 93 | } 94 | 95 | private boolean isParentOf(String maybeParentPackage, String packageName) { 96 | // check the easy case first 97 | if (!packageName.startsWith(maybeParentPackage)) { 98 | return false; 99 | } 100 | 101 | // even if they start with the same string, there might be a mismatch in the last package name 102 | // in the possible parent. For instance, "com.spot" is not a parent package of "com.spotify". 103 | String[] parentParts = maybeParentPackage.split("\\."); 104 | String[] packageParts = packageName.split("\\."); 105 | 106 | String lastParentPackagePart = parentParts[parentParts.length - 1]; 107 | 108 | return packageParts[parentParts.length - 1].equals(lastParentPackagePart); 109 | } 110 | 111 | private static class PackageAndAccess { 112 | 113 | private final String packageName; 114 | private final Access access; 115 | 116 | private PackageAndAccess(String packageName, Access access) { 117 | this.packageName = packageName; 118 | this.access = access; 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/DataEnumProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor; 21 | 22 | import com.spotify.dataenum.ConstructorAccess; 23 | import com.spotify.dataenum.DataEnum; 24 | import com.spotify.dataenum.DataenumUtils; 25 | import com.spotify.dataenum.processor.data.OutputSpec; 26 | import com.spotify.dataenum.processor.data.Parameter; 27 | import com.spotify.dataenum.processor.data.Spec; 28 | import com.spotify.dataenum.processor.data.Value; 29 | import com.spotify.dataenum.processor.generator.data.OutputSpecFactory; 30 | import com.spotify.dataenum.processor.generator.spec.SpecTypeFactory; 31 | import com.spotify.dataenum.processor.parser.ParserException; 32 | import com.spotify.dataenum.processor.parser.SpecParser; 33 | import com.squareup.javapoet.ArrayTypeName; 34 | import com.squareup.javapoet.JavaFile; 35 | import com.squareup.javapoet.TypeSpec; 36 | import com.sun.source.util.Trees; 37 | import java.io.IOException; 38 | import java.util.Collections; 39 | import java.util.Set; 40 | import javax.annotation.processing.*; 41 | import javax.lang.model.SourceVersion; 42 | import javax.lang.model.element.Element; 43 | import javax.lang.model.element.TypeElement; 44 | import javax.tools.Diagnostic; 45 | 46 | public class DataEnumProcessor extends AbstractProcessor { 47 | private Trees trees; 48 | 49 | @Override 50 | public synchronized void init(ProcessingEnvironment processingEnv) { 51 | super.init(processingEnv); 52 | this.trees = Trees.instance(processingEnv); 53 | } 54 | 55 | @Override 56 | public boolean process(Set set, RoundEnvironment roundEnvironment) { 57 | Filer filer = processingEnv.getFiler(); 58 | Messager messager = processingEnv.getMessager(); 59 | 60 | AccessSelector accessSelector = 61 | new AccessSelector(roundEnvironment.getElementsAnnotatedWith(ConstructorAccess.class)); 62 | 63 | for (Element element : roundEnvironment.getElementsAnnotatedWith(DataEnum.class)) { 64 | try { 65 | 66 | Spec spec = SpecParser.parse(element, new ProcessingContext(processingEnv)); 67 | if (spec == null) { 68 | continue; 69 | } 70 | 71 | OutputSpec outputSpec = OutputSpecFactory.create(spec); 72 | TypeSpec outputTypeSpec = 73 | SpecTypeFactory.create( 74 | outputSpec, 75 | accessSelector.accessModifierFor(outputSpec.outputClass().packageName()), 76 | element); 77 | 78 | JavaFile.Builder javaFileBuilder = 79 | JavaFile.builder(outputSpec.outputClass().packageName(), outputTypeSpec); 80 | if (needsCheckNotNull(spec)) { 81 | javaFileBuilder.addStaticImport(DataenumUtils.class, "checkNotNull"); 82 | } 83 | if (needsNullSafeEquals(spec)) { 84 | javaFileBuilder.addStaticImport(DataenumUtils.class, "equal"); 85 | } 86 | 87 | JavaFile javaFile = javaFileBuilder.build(); 88 | javaFile.writeTo(filer); 89 | 90 | } catch (IOException | ParserException e) { 91 | messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage()); 92 | } 93 | } 94 | 95 | return false; 96 | } 97 | 98 | @Override 99 | public SourceVersion getSupportedSourceVersion() { 100 | return SourceVersion.latestSupported(); 101 | } 102 | 103 | @Override 104 | public Set getSupportedAnnotationTypes() { 105 | return Collections.singleton(DataEnum.class.getCanonicalName()); 106 | } 107 | 108 | private static boolean needsCheckNotNull(Spec enumDef) { 109 | for (Value value : enumDef.values()) { 110 | for (Parameter parameter : value.parameters()) { 111 | if (!parameter.type().isPrimitive() && !parameter.canBeNull()) { 112 | return true; 113 | } 114 | } 115 | } 116 | return false; 117 | } 118 | 119 | private static boolean needsNullSafeEquals(Spec enumDef) { 120 | for (Value value : enumDef.values()) { 121 | for (Parameter parameter : value.parameters()) { 122 | if (!parameter.type().isPrimitive() 123 | && !(parameter.type() instanceof ArrayTypeName) 124 | && parameter.canBeNull()) { 125 | return true; 126 | } 127 | } 128 | } 129 | return false; 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/ProcessingContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | 21 | package com.spotify.dataenum.processor; 22 | 23 | import com.spotify.dataenum.dataenum_case; 24 | import com.sun.source.util.Trees; 25 | import javax.annotation.processing.ProcessingEnvironment; 26 | import javax.lang.model.type.TypeMirror; 27 | import javax.lang.model.util.Elements; 28 | import javax.lang.model.util.Types; 29 | 30 | public final class ProcessingContext { 31 | public final ProcessingEnvironment env; 32 | public final Trees trees; 33 | public final TypeMirror dataenum_class_element; 34 | 35 | public ProcessingContext(ProcessingEnvironment env) { 36 | this.env = env; 37 | this.trees = Trees.instance(env); 38 | 39 | Types types = env.getTypeUtils(); 40 | Elements elements = env.getElementUtils(); 41 | this.dataenum_class_element = 42 | elements.getTypeElement(dataenum_case.class.getCanonicalName()).asType(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/data/OutputSpec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.data; 21 | 22 | import com.spotify.dataenum.processor.util.Iterables; 23 | import com.squareup.javapoet.ClassName; 24 | import com.squareup.javapoet.ParameterizedTypeName; 25 | import com.squareup.javapoet.TypeName; 26 | import com.squareup.javapoet.TypeVariableName; 27 | 28 | /** Output-version of spec. Contains derived information that wasn't available during parsing. */ 29 | public class OutputSpec extends Spec { 30 | private final ClassName outputClass; 31 | private final Iterable outputValues; 32 | 33 | public OutputSpec(Spec input, ClassName outputClass, Iterable outputValues) { 34 | super( 35 | input.specClass(), 36 | input.typeVariables(), 37 | input.superInterfaces(), 38 | input.values(), 39 | input.methods()); 40 | this.outputClass = outputClass; 41 | this.outputValues = outputValues; 42 | } 43 | 44 | public ClassName outputClass() { 45 | return outputClass; 46 | } 47 | 48 | public TypeName parameterizedOutputClass() { 49 | if (!hasTypeVariables()) { 50 | return outputClass(); 51 | } 52 | 53 | TypeName[] typeNames = Iterables.toArray(typeVariables(), TypeVariableName.class); 54 | return ParameterizedTypeName.get(outputClass(), typeNames); 55 | } 56 | 57 | public Iterable outputValues() { 58 | return outputValues; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/data/OutputValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.data; 21 | 22 | import com.spotify.dataenum.processor.util.Iterables; 23 | import com.squareup.javapoet.AnnotationSpec; 24 | import com.squareup.javapoet.ClassName; 25 | import com.squareup.javapoet.ParameterizedTypeName; 26 | import com.squareup.javapoet.TypeName; 27 | import com.squareup.javapoet.TypeVariableName; 28 | import javax.annotation.Nullable; 29 | 30 | /** Output-version of value. Contains derived information that wasn't available during parsing. */ 31 | public class OutputValue { 32 | private final ClassName outputClass; 33 | private final Iterable typeVariables; 34 | private final String name; 35 | @Nullable private final String javadoc; 36 | private final Iterable parameters; 37 | private final Iterable annotations; 38 | 39 | public OutputValue( 40 | ClassName outputClass, 41 | String name, 42 | String javadoc, 43 | Iterable parameters, 44 | Iterable typeVariables, 45 | Iterable annotations) { 46 | this.outputClass = outputClass; 47 | this.name = name; 48 | this.javadoc = javadoc; 49 | this.parameters = parameters; 50 | this.typeVariables = typeVariables; 51 | this.annotations = annotations; 52 | } 53 | 54 | public ClassName outputClass() { 55 | return outputClass; 56 | } 57 | 58 | public TypeName parameterizedOutputClass() { 59 | if (!hasTypeVariables()) { 60 | return outputClass(); 61 | } 62 | 63 | TypeName[] typeNames = Iterables.toArray(typeVariables(), TypeVariableName.class); 64 | return ParameterizedTypeName.get(outputClass(), typeNames); 65 | } 66 | 67 | public Iterable typeVariables() { 68 | return typeVariables; 69 | } 70 | 71 | public boolean hasTypeVariables() { 72 | return !Iterables.isEmpty(typeVariables()); 73 | } 74 | 75 | public String name() { 76 | return name; 77 | } 78 | 79 | @Nullable 80 | public String javadoc() { 81 | return javadoc; 82 | } 83 | 84 | public Iterable parameters() { 85 | return parameters; 86 | } 87 | 88 | public boolean hasParameters() { 89 | return !Iterables.isEmpty(parameters()); 90 | } 91 | 92 | public Iterable annotations() { 93 | return annotations; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/data/Parameter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.data; 21 | 22 | import com.squareup.javapoet.TypeName; 23 | 24 | public class Parameter { 25 | private final String name; 26 | private final TypeName type; 27 | private final boolean nullable; 28 | private final boolean redacted; 29 | private final boolean isEnum; 30 | 31 | public Parameter(String name, TypeName type, boolean nullable, boolean redacted, boolean isEnum) { 32 | this.name = name; 33 | this.type = type; 34 | this.nullable = nullable; 35 | this.redacted = redacted; 36 | this.isEnum = isEnum; 37 | } 38 | 39 | public String name() { 40 | return name; 41 | } 42 | 43 | public TypeName type() { 44 | return type; 45 | } 46 | 47 | public boolean canBeNull() { 48 | return nullable; 49 | } 50 | 51 | public boolean redacted() { 52 | return redacted; 53 | } 54 | 55 | public boolean isEnum() { 56 | return isEnum; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/data/Spec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.data; 21 | 22 | import com.spotify.dataenum.processor.util.Iterables; 23 | import com.squareup.javapoet.ClassName; 24 | import com.squareup.javapoet.MethodSpec; 25 | import com.squareup.javapoet.TypeVariableName; 26 | 27 | /** 28 | * Represents the data represented in a 'MyValue_spec' class. 29 | * 30 | *

Contains a reference to the source _spec and its type variable, as well as all the possible 31 | * values. 32 | */ 33 | public class Spec { 34 | private final ClassName specClass; 35 | private final Iterable typeVariables; 36 | private final Iterable interfaces; 37 | private final Iterable values; 38 | private final Iterable methods; 39 | 40 | public Spec( 41 | ClassName specClass, 42 | Iterable typeVariables, 43 | Iterable interfaces, 44 | Iterable values, 45 | Iterable methods) { 46 | this.specClass = specClass; 47 | this.typeVariables = typeVariables; 48 | this.interfaces = interfaces; 49 | this.values = values; 50 | this.methods = methods; 51 | } 52 | 53 | public ClassName specClass() { 54 | return specClass; 55 | } 56 | 57 | public Iterable typeVariables() { 58 | return typeVariables; 59 | } 60 | 61 | public Iterable superInterfaces() { 62 | return interfaces; 63 | } 64 | 65 | public boolean hasTypeVariables() { 66 | return !Iterables.isEmpty(typeVariables()); 67 | } 68 | 69 | public Iterable values() { 70 | return values; 71 | } 72 | 73 | public Iterable methods() { 74 | return methods; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/data/Value.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.data; 21 | 22 | import com.spotify.dataenum.processor.util.Iterables; 23 | import com.squareup.javapoet.AnnotationSpec; 24 | import javax.annotation.Nullable; 25 | 26 | /** Represents one of the possible values of a dataenum spec. */ 27 | public class Value { 28 | private final String simpleName; 29 | @Nullable private final String javadoc; 30 | private final Iterable parameters; 31 | private final Iterable annotations; 32 | 33 | public Value( 34 | String simpleName, 35 | String javadoc, 36 | Iterable parameters, 37 | Iterable annotations) { 38 | this.simpleName = simpleName; 39 | this.javadoc = javadoc; 40 | this.parameters = parameters; 41 | this.annotations = annotations; 42 | } 43 | 44 | public String name() { 45 | return simpleName; 46 | } 47 | 48 | @Nullable 49 | public String javadoc() { 50 | return javadoc; 51 | } 52 | 53 | public Iterable parameters() { 54 | return parameters; 55 | } 56 | 57 | public boolean hasParameters() { 58 | return !Iterables.isEmpty(parameters()); 59 | } 60 | 61 | public Iterable annotations() { 62 | return annotations; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/generator/data/OutputSpecFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.generator.data; 21 | 22 | import com.spotify.dataenum.processor.data.OutputSpec; 23 | import com.spotify.dataenum.processor.data.OutputValue; 24 | import com.spotify.dataenum.processor.data.Spec; 25 | import com.spotify.dataenum.processor.data.Value; 26 | import com.spotify.dataenum.processor.parser.ParserException; 27 | import com.squareup.javapoet.ClassName; 28 | import java.util.ArrayList; 29 | import java.util.List; 30 | 31 | public final class OutputSpecFactory { 32 | 33 | private OutputSpecFactory() {} 34 | 35 | private static final String SUFFIX = "_dataenum"; 36 | 37 | public static OutputSpec create(Spec spec) throws ParserException { 38 | ClassName specClass = spec.specClass(); 39 | 40 | ClassName outputClass = toOutputClassName(specClass); 41 | 42 | List values = new ArrayList<>(); 43 | for (Value value : spec.values()) { 44 | values.add(OutputValueFactory.create(value, outputClass, spec)); 45 | } 46 | 47 | return new OutputSpec(spec, outputClass, values); 48 | } 49 | 50 | static boolean isSpecClassName(ClassName specClassName) { 51 | return specClassName.simpleName().endsWith(SUFFIX); 52 | } 53 | 54 | static ClassName toOutputClassName(ClassName specClassName) throws ParserException { 55 | String packageName = specClassName.packageName(); 56 | String name = specClassName.simpleName(); 57 | 58 | if (!isSpecClassName(specClassName)) { 59 | throw new ParserException( 60 | String.format( 61 | "Bad name for DataEnum interface! Name must end with '%s', found: %s", SUFFIX, name)); 62 | } 63 | 64 | String nameWithoutSuffix = name.substring(0, name.length() - SUFFIX.length()); 65 | return ClassName.get(packageName, nameWithoutSuffix); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/generator/data/OutputValueFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.generator.data; 21 | 22 | import static com.spotify.dataenum.processor.generator.data.OutputSpecFactory.isSpecClassName; 23 | import static com.spotify.dataenum.processor.generator.data.OutputSpecFactory.toOutputClassName; 24 | 25 | import com.spotify.dataenum.processor.data.OutputValue; 26 | import com.spotify.dataenum.processor.data.Parameter; 27 | import com.spotify.dataenum.processor.data.Spec; 28 | import com.spotify.dataenum.processor.data.Value; 29 | import com.spotify.dataenum.processor.parser.ParserException; 30 | import com.squareup.javapoet.ArrayTypeName; 31 | import com.squareup.javapoet.ClassName; 32 | import com.squareup.javapoet.ParameterizedTypeName; 33 | import com.squareup.javapoet.TypeName; 34 | import com.squareup.javapoet.TypeVariableName; 35 | import java.util.ArrayList; 36 | import java.util.LinkedHashSet; 37 | import java.util.List; 38 | import java.util.Set; 39 | 40 | final class OutputValueFactory { 41 | 42 | private OutputValueFactory() {} 43 | 44 | static OutputValue create(Value value, ClassName specOutputClass, Spec spec) 45 | throws ParserException { 46 | ClassName outputClass = specOutputClass.nestedClass(value.name()); 47 | Iterable typeVariables = getTypeVariables(value, spec.typeVariables()); 48 | 49 | List parameters = new ArrayList<>(); 50 | for (Parameter parameter : value.parameters()) { 51 | parameters.add(parameterWithoutDataEnumSuffix(parameter)); 52 | } 53 | 54 | return new OutputValue( 55 | outputClass, value.name(), value.javadoc(), parameters, typeVariables, value.annotations()); 56 | } 57 | 58 | private static Parameter parameterWithoutDataEnumSuffix(Parameter parameter) 59 | throws ParserException { 60 | return new Parameter( 61 | parameter.name(), 62 | typeWithoutDataEnumSuffixes(parameter.type()), 63 | parameter.canBeNull(), 64 | parameter.redacted(), 65 | parameter.isEnum()); 66 | } 67 | 68 | private static TypeName typeWithoutDataEnumSuffixes(TypeName typeName) throws ParserException { 69 | if (typeName instanceof ParameterizedTypeName) { 70 | ParameterizedTypeName paramTypeName = (ParameterizedTypeName) typeName; 71 | 72 | ClassName outputClassName; 73 | if (isSpecClassName(paramTypeName.rawType)) { 74 | outputClassName = toOutputClassName(paramTypeName.rawType); 75 | } else { 76 | outputClassName = paramTypeName.rawType; 77 | } 78 | 79 | TypeName[] typeArgumentsArr = new TypeName[paramTypeName.typeArguments.size()]; 80 | for (int i = 0; i < typeArgumentsArr.length; i++) { 81 | typeArgumentsArr[i] = typeWithoutDataEnumSuffixes(paramTypeName.typeArguments.get(i)); 82 | } 83 | 84 | return ParameterizedTypeName.get(outputClassName, typeArgumentsArr); 85 | } 86 | 87 | if (typeName instanceof ClassName) { 88 | ClassName className = (ClassName) typeName; 89 | if (isSpecClassName(className)) { 90 | return toOutputClassName(className); 91 | } 92 | return className; 93 | } 94 | 95 | return typeName; 96 | } 97 | 98 | private static Iterable getTypeVariables( 99 | Value value, Iterable typeVariables) { 100 | Set output = new LinkedHashSet<>(); 101 | for (TypeVariableName typeVariable : typeVariables) { 102 | for (Parameter parameter : value.parameters()) { 103 | if (typeNeedsTypeVariable(parameter.type(), typeVariable)) { 104 | output.add(typeVariable); 105 | } 106 | } 107 | } 108 | return output; 109 | } 110 | 111 | private static boolean typeNeedsTypeVariable(TypeName type, TypeVariableName typeVariable) { 112 | if (typeVariable.equals(type)) { 113 | return true; 114 | } 115 | 116 | if (type instanceof ParameterizedTypeName) { 117 | ParameterizedTypeName parameterized = ((ParameterizedTypeName) type); 118 | for (TypeName typeArgument : parameterized.typeArguments) { 119 | if (typeVariable.equals(typeArgument)) { 120 | return true; 121 | } 122 | if (typeNeedsTypeVariable(typeArgument, typeVariable)) { 123 | return true; 124 | } 125 | } 126 | } 127 | 128 | if (type instanceof ArrayTypeName) { 129 | ArrayTypeName arrayType = (ArrayTypeName) type; 130 | if (typeVariable.equals(arrayType.componentType)) { 131 | return true; 132 | } 133 | } 134 | return false; 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/generator/match/MapMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.generator.match; 21 | 22 | import com.spotify.dataenum.function.Function; 23 | import com.spotify.dataenum.processor.data.OutputValue; 24 | import com.spotify.dataenum.processor.parser.ParserException; 25 | import com.squareup.javapoet.ClassName; 26 | import com.squareup.javapoet.MethodSpec; 27 | import com.squareup.javapoet.ParameterSpec; 28 | import com.squareup.javapoet.ParameterizedTypeName; 29 | import com.squareup.javapoet.TypeName; 30 | import com.squareup.javapoet.TypeVariableName; 31 | import javax.annotation.Nonnull; 32 | import javax.lang.model.element.Modifier; 33 | 34 | public class MapMethods { 35 | 36 | private static final TypeVariableName FOLD_RETURN_TYPE = TypeVariableName.get("R_"); 37 | private final Iterable values; 38 | 39 | public MapMethods(Iterable values) { 40 | this.values = values; 41 | } 42 | 43 | private MethodSpec.Builder createFoldSignature(Iterable availableTypeVariables) 44 | throws ParserException { 45 | MethodSpec.Builder builder = 46 | MethodSpec.methodBuilder("map") 47 | .addTypeVariable(FOLD_RETURN_TYPE) 48 | .addModifiers(Modifier.PUBLIC) 49 | .returns(FOLD_RETURN_TYPE); 50 | 51 | for (OutputValue arg : values) { 52 | TypeName visitor = 53 | ParameterizedTypeName.get( 54 | ClassName.get(Function.class), 55 | TypeVariableUtils.withoutMissingTypeVariables( 56 | arg.parameterizedOutputClass(), availableTypeVariables), 57 | FOLD_RETURN_TYPE); 58 | 59 | builder.addParameter( 60 | ParameterSpec.builder(visitor, asCamelCase(arg.name())) 61 | .addAnnotation(Nonnull.class) 62 | .build()); 63 | } 64 | 65 | return builder; 66 | } 67 | 68 | public MethodSpec createAbstractFoldMethod() throws ParserException { 69 | return createFoldSignature(null).addModifiers(Modifier.ABSTRACT).build(); 70 | } 71 | 72 | public MethodSpec createFoldMethod(OutputValue value) throws ParserException { 73 | MethodSpec.Builder builder = 74 | createFoldSignature(value.typeVariables()) 75 | .addAnnotation(Override.class) 76 | .addModifiers(Modifier.FINAL); 77 | 78 | builder.addStatement("return $L.apply(this)", asCamelCase(value.name())); 79 | 80 | return builder.build(); 81 | } 82 | 83 | private static String asCamelCase(String text) { 84 | return Character.toLowerCase(text.charAt(0)) + text.substring(1); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/generator/match/MatchMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.generator.match; 21 | 22 | import static com.spotify.dataenum.processor.generator.match.TypeVariableUtils.withoutMissingTypeVariables; 23 | 24 | import com.spotify.dataenum.function.Consumer; 25 | import com.spotify.dataenum.processor.data.OutputValue; 26 | import com.spotify.dataenum.processor.parser.ParserException; 27 | import com.squareup.javapoet.ClassName; 28 | import com.squareup.javapoet.MethodSpec; 29 | import com.squareup.javapoet.ParameterSpec; 30 | import com.squareup.javapoet.ParameterizedTypeName; 31 | import com.squareup.javapoet.TypeName; 32 | import com.squareup.javapoet.TypeVariableName; 33 | import javax.annotation.Nonnull; 34 | import javax.lang.model.element.Modifier; 35 | 36 | public class MatchMethods { 37 | 38 | private final Iterable values; 39 | 40 | public MatchMethods(Iterable values) { 41 | this.values = values; 42 | } 43 | 44 | private MethodSpec.Builder createFoldVoidSignature( 45 | Iterable availableTypeVariables) throws ParserException { 46 | MethodSpec.Builder builder = 47 | MethodSpec.methodBuilder("match").addModifiers(Modifier.PUBLIC).returns(void.class); 48 | 49 | for (OutputValue arg : values) { 50 | TypeName visitor = 51 | ParameterizedTypeName.get( 52 | ClassName.get(Consumer.class), 53 | withoutMissingTypeVariables(arg.parameterizedOutputClass(), availableTypeVariables)); 54 | 55 | builder.addParameter( 56 | ParameterSpec.builder(visitor, asCamelCase(arg.name())) 57 | .addAnnotation(Nonnull.class) 58 | .build()); 59 | } 60 | 61 | return builder; 62 | } 63 | 64 | public MethodSpec createAbstractFoldVoidMethod() throws ParserException { 65 | return createFoldVoidSignature(null).addModifiers(Modifier.ABSTRACT).build(); 66 | } 67 | 68 | public MethodSpec createFoldVoidMethod(OutputValue value) throws ParserException { 69 | MethodSpec.Builder builder = 70 | createFoldVoidSignature(value.typeVariables()) 71 | .addAnnotation(Override.class) 72 | .addModifiers(Modifier.FINAL); 73 | 74 | builder.addStatement("$L.accept(this)", asCamelCase(value.name())); 75 | 76 | return builder.build(); 77 | } 78 | 79 | private static String asCamelCase(String text) { 80 | return Character.toLowerCase(text.charAt(0)) + text.substring(1); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/generator/match/TypeVariableUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.generator.match; 21 | 22 | import com.spotify.dataenum.processor.parser.ParserException; 23 | import com.spotify.dataenum.processor.util.Iterables; 24 | import com.squareup.javapoet.ParameterizedTypeName; 25 | import com.squareup.javapoet.TypeName; 26 | import com.squareup.javapoet.TypeVariableName; 27 | import java.util.ArrayList; 28 | import java.util.List; 29 | 30 | class TypeVariableUtils { 31 | static TypeName withoutMissingTypeVariables( 32 | TypeName typeName, Iterable availableTypeVariables) throws ParserException { 33 | if (!(typeName instanceof ParameterizedTypeName) || availableTypeVariables == null) { 34 | return typeName; 35 | } 36 | 37 | ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName; 38 | 39 | List adjustedArguments = new ArrayList<>(); 40 | for (TypeName argument : parameterizedTypeName.typeArguments) { 41 | if (argument instanceof ParameterizedTypeName) { 42 | // Recursive call 43 | adjustedArguments.add(withoutMissingTypeVariables(argument, availableTypeVariables)); 44 | } else if (argument instanceof TypeVariableName) { 45 | TypeVariableName variable = (TypeVariableName) argument; 46 | if (Iterables.contains(availableTypeVariables, variable)) { 47 | adjustedArguments.add(variable); 48 | } else { 49 | if (variable.bounds.size() == 0) { 50 | adjustedArguments.add(TypeName.OBJECT); 51 | } else if (variable.bounds.size() == 1) { 52 | adjustedArguments.add(variable.bounds.get(0)); 53 | } else { 54 | throw new ParserException("More than one generic type bound is not supported"); 55 | } 56 | } 57 | } else { 58 | adjustedArguments.add(argument); 59 | } 60 | } 61 | 62 | TypeName[] adjustedArgumentsArr = adjustedArguments.toArray(new TypeName[] {}); 63 | return ParameterizedTypeName.get(parameterizedTypeName.rawType, adjustedArgumentsArr); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/generator/method/MethodMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | 21 | package com.spotify.dataenum.processor.generator.method; 22 | 23 | import com.squareup.javapoet.*; 24 | import com.sun.source.tree.MethodTree; 25 | import com.sun.source.util.TreePathScanner; 26 | import com.sun.source.util.Trees; 27 | import java.util.concurrent.atomic.AtomicReference; 28 | import java.util.function.Consumer; 29 | import java.util.function.Function; 30 | import java.util.stream.Collectors; 31 | import java.util.stream.Stream; 32 | import javax.lang.model.element.AnnotationMirror; 33 | import javax.lang.model.element.ElementKind; 34 | import javax.lang.model.element.ExecutableElement; 35 | import javax.lang.model.element.Modifier; 36 | 37 | public final class MethodMethods { 38 | private MethodMethods() {} 39 | 40 | public static MethodSpec.Builder builderFrom( 41 | ExecutableElement el, 42 | Function, Stream> adjustModifiers, 43 | Function, Stream> 44 | adjustAnnotations) { 45 | return MethodSpec.methodBuilder(el.getSimpleName().toString()) 46 | .addModifiers(adjustModifiers.apply(el.getModifiers().stream()).collect(Collectors.toSet())) 47 | .addAnnotations( 48 | adjustAnnotations 49 | .apply(el.getAnnotationMirrors().stream()) 50 | .map(AnnotationSpec::get) 51 | .collect(Collectors.toList())) 52 | .addTypeVariables( 53 | el.getTypeParameters().stream().map(TypeVariableName::get).collect(Collectors.toList())) 54 | .returns(TypeName.get(el.getReturnType())) 55 | .addParameters( 56 | el.getParameters().stream().map(ParameterSpec::get).collect(Collectors.toList())) 57 | .varargs(el.isVarArgs()) 58 | .addExceptions( 59 | el.getThrownTypes().stream().map(TypeName::get).collect(Collectors.toList())); 60 | } 61 | 62 | public static CodeBlock codeBlockFrom(ExecutableElement el, Trees trees) { 63 | final MethodTree methodTree = MethodLookup.lookupTree(el, trees); 64 | return methodTree 65 | .getBody() 66 | .getStatements() 67 | .stream() 68 | .map(x -> CodeBlock.of(x.toString())) 69 | .reduce(CodeBlock.of(""), (a, b) -> a.toBuilder().add(b).build()); 70 | } 71 | 72 | private static class MethodLookup extends TreePathScanner { 73 | 74 | private final Consumer onMethod; 75 | 76 | private MethodLookup(Consumer onMethod) { 77 | this.onMethod = onMethod; 78 | } 79 | 80 | public static MethodTree lookupTree(ExecutableElement methodElement, Trees trees) { 81 | assert methodElement.getKind() == ElementKind.METHOD; 82 | 83 | AtomicReference methodRef = new AtomicReference<>(); 84 | new MethodLookup(methodRef::set).scan(trees.getPath(methodElement), null); 85 | MethodTree method = methodRef.get(); 86 | assert method != null; 87 | 88 | return method; 89 | } 90 | 91 | @Override 92 | public Void visitMethod(MethodTree methodTree, Void v) { 93 | this.onMethod.accept(methodTree); 94 | return super.visitMethod(methodTree, v); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/generator/spec/SpecTypeFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.generator.spec; 21 | 22 | import static com.spotify.dataenum.processor.util.Iterables.fromOptional; 23 | 24 | import com.spotify.dataenum.processor.DataEnumProcessor; 25 | import com.spotify.dataenum.processor.data.OutputSpec; 26 | import com.spotify.dataenum.processor.data.OutputValue; 27 | import com.spotify.dataenum.processor.generator.match.MapMethods; 28 | import com.spotify.dataenum.processor.generator.match.MatchMethods; 29 | import com.spotify.dataenum.processor.generator.value.ValueMethods; 30 | import com.spotify.dataenum.processor.generator.value.ValueTypeFactory; 31 | import com.spotify.dataenum.processor.parser.ParserException; 32 | import com.spotify.dataenum.processor.util.Iterables; 33 | import com.squareup.javapoet.AnnotationSpec; 34 | import com.squareup.javapoet.CodeBlock; 35 | import com.squareup.javapoet.MethodSpec; 36 | import com.squareup.javapoet.TypeSpec; 37 | import java.util.ArrayList; 38 | import java.util.List; 39 | import java.util.Optional; 40 | import javax.annotation.Generated; 41 | import javax.lang.model.element.Element; 42 | import javax.lang.model.element.Modifier; 43 | 44 | public final class SpecTypeFactory { 45 | 46 | private SpecTypeFactory() {} 47 | 48 | public static TypeSpec create( 49 | OutputSpec spec, Optional constructorAccessModifier, Element element) 50 | throws ParserException { 51 | List valueTypes = new ArrayList<>(); 52 | List factoryMethods = new ArrayList<>(); 53 | List isMethods = new ArrayList<>(); 54 | List asMethods = new ArrayList<>(); 55 | 56 | MatchMethods matchMethods = new MatchMethods(spec.outputValues()); 57 | MapMethods mapMethods = new MapMethods(spec.outputValues()); 58 | 59 | for (OutputValue value : spec.outputValues()) { 60 | valueTypes.add( 61 | ValueTypeFactory.create( 62 | value, spec, matchMethods, mapMethods, constructorAccessModifier)); 63 | 64 | ValueMethods valueMethods = new ValueMethods(value); 65 | factoryMethods.add(valueMethods.createFactoryMethod(spec)); 66 | isMethods.add(valueMethods.createIsMethod()); 67 | asMethods.add(valueMethods.createAsMethod(spec)); 68 | } 69 | 70 | TypeSpec.Builder enumBuilder = 71 | TypeSpec.classBuilder(spec.outputClass()) 72 | .addOriginatingElement(element) 73 | .addJavadoc("Generated from {@link $T}\n", spec.specClass()) 74 | .addAnnotation( 75 | AnnotationSpec.builder(Generated.class) 76 | .addMember( 77 | "value", CodeBlock.of("$S", DataEnumProcessor.class.getCanonicalName())) 78 | .build()) 79 | .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) 80 | .addTypeVariables(spec.typeVariables()) 81 | .addSuperinterfaces(spec.superInterfaces()); 82 | 83 | // add constructor with correct access 84 | enumBuilder.addMethod( 85 | MethodSpec.constructorBuilder() 86 | .addModifiers(fromOptional(constructorAccessModifier)) 87 | .build()); 88 | 89 | enumBuilder.addTypes(valueTypes); 90 | enumBuilder.addMethods(factoryMethods); 91 | enumBuilder.addMethods(isMethods); 92 | enumBuilder.addMethods(asMethods); 93 | 94 | if (!Iterables.isEmpty(spec.outputValues())) { 95 | enumBuilder.addMethod(matchMethods.createAbstractFoldVoidMethod()); 96 | enumBuilder.addMethod(mapMethods.createAbstractFoldMethod()); 97 | } 98 | 99 | enumBuilder.addMethods(spec.methods()); 100 | 101 | return enumBuilder.build(); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/generator/value/ValueMethods.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.generator.value; 21 | 22 | import com.spotify.dataenum.processor.data.OutputSpec; 23 | import com.spotify.dataenum.processor.data.OutputValue; 24 | import com.spotify.dataenum.processor.data.Parameter; 25 | import com.squareup.javapoet.AnnotationSpec; 26 | import com.squareup.javapoet.MethodSpec; 27 | import com.squareup.javapoet.MethodSpec.Builder; 28 | import com.squareup.javapoet.ParameterSpec; 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | import javax.annotation.Nonnull; 32 | import javax.annotation.Nullable; 33 | import javax.lang.model.element.Modifier; 34 | 35 | public class ValueMethods { 36 | 37 | private final OutputValue value; 38 | 39 | public ValueMethods(OutputValue value) { 40 | this.value = value; 41 | } 42 | 43 | public MethodSpec createFactoryMethod(OutputSpec spec) { 44 | MethodSpec.Builder factory = 45 | MethodSpec.methodBuilder(asCamelCase(value.name())) 46 | .addTypeVariables(spec.typeVariables()) 47 | .addModifiers(Modifier.PUBLIC, Modifier.STATIC) 48 | .returns(spec.parameterizedOutputClass()); 49 | 50 | if (value.javadoc() != null) { 51 | factory.addJavadoc(value.javadoc() + "\n\n"); 52 | } 53 | 54 | factory.addJavadoc( 55 | "@return a {@link $T} (see {@link $T#$L} for source)\n", 56 | value.outputClass(), 57 | spec.specClass(), 58 | value.name()); 59 | 60 | for (AnnotationSpec annotationSpec : value.annotations()) { 61 | factory.addAnnotation(annotationSpec); 62 | } 63 | 64 | StringBuilder newString = new StringBuilder(); 65 | List newArgs = new ArrayList<>(); 66 | 67 | newString.append("return new $T("); 68 | newArgs.add(value.parameterizedOutputClass()); 69 | 70 | boolean first = true; 71 | 72 | for (Parameter parameter : value.parameters()) { 73 | ParameterSpec.Builder builder = ParameterSpec.builder(parameter.type(), parameter.name()); 74 | if (!parameter.type().isPrimitive()) { 75 | if (parameter.canBeNull()) { 76 | builder.addAnnotation(Nullable.class); 77 | } else { 78 | builder.addAnnotation(Nonnull.class); 79 | } 80 | } 81 | 82 | factory.addParameter(builder.build()); 83 | 84 | if (first) { 85 | newString.append("$L"); 86 | first = false; 87 | } else { 88 | newString.append(", $L"); 89 | } 90 | 91 | newArgs.add(parameter.name()); 92 | } 93 | 94 | newString.append(")"); 95 | 96 | if (spec.hasTypeVariables()) { 97 | newString.append(".as$L()"); 98 | newArgs.add(spec.outputClass().simpleName()); 99 | } 100 | 101 | factory.addStatement(newString.toString(), newArgs.toArray()); 102 | 103 | return factory.build(); 104 | } 105 | 106 | public MethodSpec createIsMethod() { 107 | return MethodSpec.methodBuilder("is" + value.name()) 108 | .returns(boolean.class) 109 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL) 110 | .addStatement("return (this instanceof $T)", value.outputClass()) 111 | .build(); 112 | } 113 | 114 | public MethodSpec createAsMethod(OutputSpec spec) { 115 | Builder builder = 116 | MethodSpec.methodBuilder("as" + value.name()) 117 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL) 118 | .returns(value.parameterizedOutputClass()) 119 | .addStatement("return ($T) this", value.parameterizedOutputClass()); 120 | 121 | if (!ValueTypeFactory.extractMissingTypeVariablesForValue(value, spec).isEmpty() 122 | && value.hasTypeVariables()) { 123 | builder.addAnnotation(ValueTypeFactory.SUPPRESS_UNCHECKED_WARNINGS); 124 | } 125 | 126 | return builder.build(); 127 | } 128 | 129 | private static String asCamelCase(String text) { 130 | return Character.toLowerCase(text.charAt(0)) + text.substring(1); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/parser/MethodParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | 21 | package com.spotify.dataenum.processor.parser; 22 | 23 | import com.spotify.dataenum.Static; 24 | import com.spotify.dataenum.processor.generator.method.MethodMethods; 25 | import com.squareup.javapoet.ClassName; 26 | import com.squareup.javapoet.MethodSpec; 27 | import com.squareup.javapoet.TypeName; 28 | import com.sun.source.util.Trees; 29 | import java.util.function.Function; 30 | import java.util.stream.Stream; 31 | import javax.lang.model.element.AnnotationMirror; 32 | import javax.lang.model.element.ExecutableElement; 33 | import javax.lang.model.element.Modifier; 34 | 35 | public final class MethodParser { 36 | 37 | private static final TypeName STATIC_ANNOTATION = ClassName.get(Static.class); 38 | 39 | public static MethodSpec parse(ExecutableElement el, Trees trees) { 40 | // filter off DEFAULT modifier 41 | Function, Stream> adjustModifiers = 42 | mods -> mods.filter(x -> x != Modifier.DEFAULT); 43 | // populate STATIC modifier if required 44 | if (el.getAnnotation(Static.class) != null) { 45 | adjustModifiers = 46 | adjustModifiers.andThen(mods -> Stream.concat(Stream.of(Modifier.STATIC), mods)); 47 | } 48 | 49 | // make sure internal @Static annotation doesn't sneak out 50 | Function, Stream> 51 | adjustAnnotations = 52 | annos -> annos.filter(x -> !"@com.spotify.dataenum.Static".equals(x.toString())); 53 | 54 | return MethodMethods.builderFrom(el, adjustModifiers, adjustAnnotations) 55 | .addCode(MethodMethods.codeBlockFrom(el, trees)) 56 | .build(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/parser/ParserException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.parser; 21 | 22 | public class ParserException extends Exception { 23 | public ParserException(String s) { 24 | super(s); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/parser/SpecParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.parser; 21 | 22 | import com.spotify.dataenum.processor.ProcessingContext; 23 | import com.spotify.dataenum.processor.data.Spec; 24 | import com.spotify.dataenum.processor.data.Value; 25 | import com.squareup.javapoet.ClassName; 26 | import com.squareup.javapoet.MethodSpec; 27 | import com.squareup.javapoet.TypeVariableName; 28 | import com.sun.tools.javac.util.Pair; 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | import java.util.stream.Collectors; 32 | import javax.annotation.processing.Messager; 33 | import javax.lang.model.element.Element; 34 | import javax.lang.model.element.ElementKind; 35 | import javax.lang.model.element.TypeElement; 36 | import javax.lang.model.element.TypeParameterElement; 37 | import javax.lang.model.type.DeclaredType; 38 | import javax.tools.Diagnostic; 39 | 40 | public final class SpecParser { 41 | 42 | private SpecParser() {} 43 | 44 | public static Spec parse(Element element, ProcessingContext ctx) { 45 | Messager messager = ctx.env.getMessager(); 46 | 47 | if (element.getKind() != ElementKind.INTERFACE) { 48 | messager.printMessage( 49 | Diagnostic.Kind.ERROR, "@DataEnum can only be used on interfaces.", element); 50 | return null; 51 | } 52 | 53 | TypeElement dataEnum = (TypeElement) element; 54 | 55 | List typeVariableNames = new ArrayList<>(); 56 | for (TypeParameterElement typeParameterElement : dataEnum.getTypeParameters()) { 57 | typeVariableNames.add(TypeVariableName.get(typeParameterElement)); 58 | } 59 | 60 | final Pair, List> valuesAndMethods = MembersParser.parse(dataEnum, ctx); 61 | if (valuesAndMethods == null) { 62 | return null; 63 | } 64 | 65 | List interfaces = 66 | dataEnum 67 | .getInterfaces() 68 | .stream() 69 | .map(x -> ClassName.get((TypeElement) ((DeclaredType) x).asElement())) 70 | .collect(Collectors.toList()); 71 | 72 | ClassName enumInterface = ClassName.get(dataEnum); 73 | 74 | return new Spec( 75 | enumInterface, typeVariableNames, interfaces, valuesAndMethods.fst, valuesAndMethods.snd); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/parser/ValueParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.parser; 21 | 22 | import com.spotify.dataenum.function.Function; 23 | import com.spotify.dataenum.processor.ProcessingContext; 24 | import com.spotify.dataenum.processor.data.Parameter; 25 | import com.spotify.dataenum.processor.data.Value; 26 | import com.squareup.javapoet.AnnotationSpec; 27 | import com.squareup.javapoet.AnnotationSpec.Builder; 28 | import com.squareup.javapoet.ClassName; 29 | import com.squareup.javapoet.TypeName; 30 | import java.util.ArrayList; 31 | import java.util.List; 32 | import java.util.Map.Entry; 33 | import javax.annotation.processing.Messager; 34 | import javax.lang.model.element.AnnotationMirror; 35 | import javax.lang.model.element.AnnotationValue; 36 | import javax.lang.model.element.Element; 37 | import javax.lang.model.element.ElementKind; 38 | import javax.lang.model.element.ExecutableElement; 39 | import javax.lang.model.element.VariableElement; 40 | import javax.lang.model.type.TypeMirror; 41 | import javax.tools.Diagnostic; 42 | import javax.tools.Diagnostic.Kind; 43 | 44 | final class ValueParser { 45 | 46 | private ValueParser() {} 47 | 48 | static Value parse(Element element, ProcessingContext ctx) { 49 | Messager messager = ctx.env.getMessager(); 50 | 51 | ExecutableElement methodElement = (ExecutableElement) element; 52 | 53 | if (methodElement.getTypeParameters().size() != 0) { 54 | messager.printMessage( 55 | Diagnostic.Kind.ERROR, 56 | String.format( 57 | "Type parameters must be specified on the top-level interface, found: %s", element), 58 | element); 59 | return null; 60 | } 61 | 62 | if (methodElement.isVarArgs()) { 63 | messager.printMessage( 64 | Diagnostic.Kind.ERROR, 65 | String.format("Vararg parameters not permitted: %s", element), 66 | element); 67 | return null; 68 | } 69 | 70 | List parameters = new ArrayList<>(); 71 | for (VariableElement parameterElement : methodElement.getParameters()) { 72 | String parameterName = parameterElement.getSimpleName().toString(); 73 | TypeName parameterType = TypeName.get(parameterElement.asType()); 74 | 75 | boolean nullable = isAnnotationPresent(parameterElement, ValueParser::isNullableAnnotation); 76 | boolean redacted = isAnnotationPresent(parameterElement, ValueParser::isRedactedAnnotation); 77 | Element parameterTypeElement = ctx.env.getTypeUtils().asElement(parameterElement.asType()); 78 | boolean isEnum = 79 | parameterTypeElement != null && parameterTypeElement.getKind() == ElementKind.ENUM; 80 | 81 | parameters.add(new Parameter(parameterName, parameterType, nullable, redacted, isEnum)); 82 | } 83 | 84 | String javadoc = ctx.env.getElementUtils().getDocComment(element); 85 | 86 | if (javadoc != null) { 87 | javadoc = javadoc.trim(); 88 | } 89 | 90 | String valueSimpleName = methodElement.getSimpleName().toString(); 91 | return new Value( 92 | valueSimpleName, javadoc, parameters, parseMethodAnnotations(methodElement, messager)); 93 | } 94 | 95 | private static Iterable parseMethodAnnotations( 96 | ExecutableElement methodElement, Messager messager) { 97 | ArrayList annotations = new ArrayList<>(); 98 | 99 | for (AnnotationMirror annotationMirror : methodElement.getAnnotationMirrors()) { 100 | TypeName annotationTypeName = 101 | ClassName.get(annotationMirror.getAnnotationType().asElement().asType()); 102 | 103 | if (!(annotationTypeName instanceof ClassName)) { 104 | messager.printMessage( 105 | Kind.ERROR, 106 | "Annotation is not a class; this shouldn't happen", 107 | methodElement, 108 | annotationMirror); 109 | continue; 110 | } 111 | 112 | Builder builder = AnnotationSpec.builder(((ClassName) annotationTypeName)); 113 | 114 | for (Entry entry : 115 | annotationMirror.getElementValues().entrySet()) { 116 | 117 | builder.addMember(entry.getKey().getSimpleName().toString(), entry.getValue().toString()); 118 | } 119 | 120 | annotations.add(builder.build()); 121 | } 122 | 123 | return annotations; 124 | } 125 | 126 | private static boolean isAnnotationPresent( 127 | VariableElement parameterElement, Function criterion) { 128 | for (AnnotationMirror annotation : parameterElement.getAnnotationMirrors()) { 129 | if (criterion.apply(annotation)) { 130 | return true; 131 | } 132 | } 133 | return false; 134 | } 135 | 136 | public static boolean isValueSpecMarker(TypeMirror returnType, ProcessingContext ctx) { 137 | 138 | return ctx.env.getTypeUtils().isSameType(returnType, ctx.dataenum_class_element); 139 | } 140 | 141 | private static boolean isNullableAnnotation(AnnotationMirror annotation) { 142 | return "Nullable".contentEquals(annotation.getAnnotationType().asElement().getSimpleName()); 143 | } 144 | 145 | private static boolean isRedactedAnnotation(AnnotationMirror annotationMirror) { 146 | return "Redacted" 147 | .contentEquals(annotationMirror.getAnnotationType().asElement().getSimpleName()); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/java/com/spotify/dataenum/processor/util/Iterables.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.util; 21 | 22 | import java.lang.reflect.Array; 23 | import java.util.ArrayList; 24 | import java.util.Collections; 25 | import java.util.List; 26 | import java.util.Optional; 27 | 28 | public class Iterables { 29 | public static boolean contains(Iterable iterable, T value) { 30 | for (T t : iterable) { 31 | if (t.equals(value)) return true; 32 | } 33 | return false; 34 | } 35 | 36 | public static boolean isEmpty(Iterable iterable) { 37 | return !iterable.iterator().hasNext(); 38 | } 39 | 40 | @SuppressWarnings("unchecked") 41 | public static T[] toArray(Iterable iterable, Class tClass) { 42 | List out = new ArrayList<>(); 43 | for (T value : iterable) { 44 | out.add(value); 45 | } 46 | return out.toArray((T[]) Array.newInstance(tClass, out.size())); 47 | } 48 | 49 | public static int sizeOf(Iterable iterable) { 50 | int size = 0; 51 | 52 | for (T entry : iterable) { 53 | size++; 54 | } 55 | 56 | return size; 57 | } 58 | 59 | public static Iterable fromOptional(Optional input) { 60 | return input.map(Collections::singletonList).orElse(Collections.emptyList()); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/resources/META-INF/gradle/incremental.annotation.processors: -------------------------------------------------------------------------------- 1 | com.spotify.dataenum.processor.DataEnumProcessor,isolating 2 | -------------------------------------------------------------------------------- /dataenum-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor: -------------------------------------------------------------------------------- 1 | com.spotify.dataenum.processor.DataEnumProcessor 2 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/java/com/spotify/dataenum/processor/generator/data/OutputSpecFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.processor.generator.data; 21 | 22 | import static org.assertj.core.api.Assertions.assertThatThrownBy; 23 | import static org.hamcrest.core.Is.is; 24 | import static org.junit.Assert.assertThat; 25 | 26 | import com.spotify.dataenum.processor.parser.ParserException; 27 | import com.squareup.javapoet.ClassName; 28 | import org.junit.Test; 29 | 30 | public class OutputSpecFactoryTest { 31 | 32 | @Test 33 | public void shouldRemoveDataEnumInterfaceSuffix() throws Exception { 34 | assertThat( 35 | OutputSpecFactory.toOutputClassName(ClassName.get("com.spotify", "My_dataenum")), 36 | is(ClassName.get("com.spotify", "My"))); 37 | } 38 | 39 | @Test 40 | public void shouldThrowForNonDataenumClassName() throws Exception { 41 | assertThatThrownBy( 42 | () -> OutputSpecFactory.toOutputClassName(ClassName.get("com.spotify", "My"))) 43 | .isInstanceOf(ParserException.class) 44 | .hasMessageContaining("Bad name"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/ArrayFields.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | 21 | import static com.spotify.dataenum.DataenumUtils.checkNotNull; 22 | 23 | import com.spotify.dataenum.function.Consumer; 24 | import com.spotify.dataenum.function.Function; 25 | import java.lang.Object; 26 | import java.lang.Override; 27 | import java.lang.String; 28 | import java.lang.StringBuilder; 29 | import java.util.Arrays; 30 | import javax.annotation.Generated; 31 | import javax.annotation.Nonnull; 32 | import javax.annotation.Nullable; 33 | 34 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 35 | public abstract class ArrayFields { 36 | ArrayFields() { 37 | } 38 | 39 | public static ArrayFields value(@Nullable byte[] builder, @Nonnull char[] result, @Nonnull Object[] param3) { 40 | return new Value(builder, result, param3); 41 | } 42 | 43 | public final boolean isValue() { 44 | return (this instanceof Value); 45 | } 46 | 47 | public final Value asValue() { 48 | return (Value) this; 49 | } 50 | 51 | public abstract void match(@Nonnull Consumer value); 52 | 53 | public abstract R_ map(@Nonnull Function value); 54 | 55 | public static final class Value extends ArrayFields { 56 | private final byte[] builder; 57 | 58 | private final char[] result; 59 | 60 | private final Object[] param3; 61 | 62 | Value(byte[] builder, char[] result, Object[] param3) { 63 | this.builder = builder; 64 | this.result = checkNotNull(result); 65 | this.param3 = checkNotNull(param3); 66 | } 67 | 68 | @Nullable 69 | public final byte[] builder() { 70 | return builder; 71 | } 72 | 73 | @Nonnull 74 | public final char[] result() { 75 | return result; 76 | } 77 | 78 | @Nonnull 79 | public final Object[] param3() { 80 | return param3; 81 | } 82 | 83 | @Override 84 | public boolean equals(Object other) { 85 | if (other == this) return true; 86 | if (!(other instanceof Value)) return false; 87 | Value o = (Value) other; 88 | return Arrays.equals(o.builder, builder) 89 | && Arrays.equals(o.result, result) 90 | && Arrays.equals(o.param3, param3); 91 | } 92 | 93 | @Override 94 | public int hashCode() { 95 | int result = 0; 96 | result = result * 31 + Arrays.hashCode(this.builder); 97 | result = result * 31 + Arrays.hashCode(this.result); 98 | return result * 31 + Arrays.hashCode(this.param3); 99 | } 100 | 101 | @Override 102 | public String toString() { 103 | StringBuilder builder = new StringBuilder(); 104 | builder.append("Value{builder=").append(Arrays.toString(this.builder)); 105 | builder.append(", result=").append(Arrays.toString(this.result)); 106 | builder.append(", param3=").append(Arrays.toString(this.param3)); 107 | return builder.append('}').toString(); 108 | } 109 | 110 | @Override 111 | public final void match(@Nonnull Consumer value) { 112 | value.accept(this); 113 | } 114 | 115 | @Override 116 | public final R_ map(@Nonnull Function value) { 117 | return value.apply(this); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/ArrayFields_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | import javax.annotation.Nullable; 23 | 24 | @DataEnum 25 | interface ArrayFields_dataenum { 26 | dataenum_case Value(@Nullable byte[] builder, 27 | char[] result, 28 | Object[] param3); 29 | } 30 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/BadValueSpecs_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | 22 | // should lead to two compiler errors: for not having dataenum_case as the return type, and for having an interface in there 23 | 24 | @DataEnum 25 | interface BadValueSpecs_dataenum { 26 | void Value1(int param1, 27 | boolean param2, 28 | float param3, 29 | double param4); 30 | 31 | interface Value2 {} 32 | } 33 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/ConflictingFieldNames.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | 21 | import static com.spotify.dataenum.DataenumUtils.checkNotNull; 22 | 23 | import com.spotify.dataenum.function.Consumer; 24 | import com.spotify.dataenum.function.Function; 25 | import java.lang.Double; 26 | import java.lang.Integer; 27 | import java.lang.Object; 28 | import java.lang.Override; 29 | import java.lang.String; 30 | import java.lang.StringBuilder; 31 | import javax.annotation.Generated; 32 | import javax.annotation.Nonnull; 33 | 34 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 35 | public abstract class ConflictingFieldNames { 36 | ConflictingFieldNames() { 37 | } 38 | 39 | public static ConflictingFieldNames value(int result, @Nonnull String builder, @Nonnull MyEnum param3, double param4) { 40 | return new Value(result, builder, param3, param4); 41 | } 42 | 43 | public final boolean isValue() { 44 | return (this instanceof Value); 45 | } 46 | 47 | public final Value asValue() { 48 | return (Value) this; 49 | } 50 | 51 | public abstract void match(@Nonnull Consumer value); 52 | 53 | public abstract R_ map(@Nonnull Function value); 54 | 55 | public static final class Value extends ConflictingFieldNames { 56 | private final int result; 57 | 58 | private final String builder; 59 | 60 | private final MyEnum param3; 61 | 62 | private final double param4; 63 | 64 | Value(int result, String builder, MyEnum param3, double param4) { 65 | this.result = result; 66 | this.builder = checkNotNull(builder); 67 | this.param3 = checkNotNull(param3); 68 | this.param4 = param4; 69 | } 70 | 71 | public final int result() { 72 | return result; 73 | } 74 | 75 | @Nonnull 76 | public final String builder() { 77 | return builder; 78 | } 79 | 80 | @Nonnull 81 | public final MyEnum param3() { 82 | return param3; 83 | } 84 | 85 | public final double param4() { 86 | return param4; 87 | } 88 | 89 | @Override 90 | public boolean equals(Object other) { 91 | if (other == this) return true; 92 | if (!(other instanceof Value)) return false; 93 | Value o = (Value) other; 94 | return o.result == result 95 | && o.param3 == param3 96 | && o.param4 == param4 97 | && o.builder.equals(this.builder); 98 | } 99 | 100 | @Override 101 | public int hashCode() { 102 | int result = 0; 103 | result = result * 31 + Integer.valueOf(this.result).hashCode(); 104 | result = result * 31 + this.builder.hashCode(); 105 | result = result * 31 + this.param3.hashCode(); 106 | return result * 31 + Double.valueOf(this.param4).hashCode(); 107 | } 108 | 109 | @Override 110 | public String toString() { 111 | StringBuilder builder = new StringBuilder(); 112 | builder.append("Value{result=").append(this.result); 113 | builder.append(", builder=").append(this.builder); 114 | builder.append(", param3=").append(this.param3); 115 | builder.append(", param4=").append(this.param4); 116 | return builder.append('}').toString(); 117 | } 118 | 119 | @Override 120 | public final void match(@Nonnull Consumer value) { 121 | value.accept(this); 122 | } 123 | 124 | @Override 125 | public final R_ map(@Nonnull Function value) { 126 | return value.apply(this); 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/ConflictingFieldNames_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | @DataEnum 24 | interface ConflictingFieldNames_dataenum { 25 | dataenum_case Value(int result, 26 | String builder, 27 | MyEnum param3, 28 | double param4); 29 | } 30 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/DuplicateCases_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | // should lead to two compiler errors for two times having duplicate case names. 24 | 25 | @DataEnum 26 | interface DuplicateCases_dataenum { 27 | dataenum_case Value(int param); 28 | dataenum_case Value(String param); 29 | 30 | dataenum_case caseIsImportant(int param); 31 | dataenum_case caseISimportant(String param); 32 | } 33 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/EfficientEquals.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | 21 | import static com.spotify.dataenum.DataenumUtils.checkNotNull; 22 | 23 | import com.spotify.dataenum.function.Consumer; 24 | import com.spotify.dataenum.function.Function; 25 | import java.lang.Double; 26 | import java.lang.Integer; 27 | import java.lang.Object; 28 | import java.lang.Override; 29 | import java.lang.String; 30 | import java.lang.StringBuilder; 31 | import javax.annotation.Generated; 32 | import javax.annotation.Nonnull; 33 | 34 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 35 | public abstract class EfficientEquals { 36 | EfficientEquals() { 37 | } 38 | 39 | public static EfficientEquals value(int param1, @Nonnull String param2, @Nonnull MyEnum param3, double param4) { 40 | return new Value(param1, param2, param3, param4); 41 | } 42 | 43 | public final boolean isValue() { 44 | return (this instanceof Value); 45 | } 46 | 47 | public final Value asValue() { 48 | return (Value) this; 49 | } 50 | 51 | public abstract void match(@Nonnull Consumer value); 52 | 53 | public abstract R_ map(@Nonnull Function value); 54 | 55 | public static final class Value extends EfficientEquals { 56 | private final int param1; 57 | 58 | private final String param2; 59 | 60 | private final MyEnum param3; 61 | 62 | private final double param4; 63 | 64 | Value(int param1, String param2, MyEnum param3, double param4) { 65 | this.param1 = param1; 66 | this.param2 = checkNotNull(param2); 67 | this.param3 = checkNotNull(param3); 68 | this.param4 = param4; 69 | } 70 | 71 | public final int param1() { 72 | return param1; 73 | } 74 | 75 | @Nonnull 76 | public final String param2() { 77 | return param2; 78 | } 79 | 80 | @Nonnull 81 | public final MyEnum param3() { 82 | return param3; 83 | } 84 | 85 | public final double param4() { 86 | return param4; 87 | } 88 | 89 | @Override 90 | public boolean equals(Object other) { 91 | if (other == this) return true; 92 | if (!(other instanceof Value)) return false; 93 | Value o = (Value) other; 94 | return o.param1 == param1 95 | && o.param3 == param3 96 | && o.param4 == param4 97 | && o.param2.equals(this.param2); 98 | } 99 | 100 | @Override 101 | public int hashCode() { 102 | int result = 0; 103 | result = result * 31 + Integer.valueOf(this.param1).hashCode(); 104 | result = result * 31 + this.param2.hashCode(); 105 | result = result * 31 + this.param3.hashCode(); 106 | return result * 31 + Double.valueOf(this.param4).hashCode(); 107 | } 108 | 109 | @Override 110 | public String toString() { 111 | StringBuilder builder = new StringBuilder(); 112 | builder.append("Value{param1=").append(this.param1); 113 | builder.append(", param2=").append(this.param2); 114 | builder.append(", param3=").append(this.param3); 115 | builder.append(", param4=").append(this.param4); 116 | return builder.append('}').toString(); 117 | } 118 | 119 | @Override 120 | public final void match(@Nonnull Consumer value) { 121 | value.accept(this); 122 | } 123 | 124 | @Override 125 | public final R_ map(@Nonnull Function value) { 126 | return value.apply(this); 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/EfficientEquals_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | @DataEnum 24 | interface EfficientEquals_dataenum { 25 | dataenum_case Value(int param1, 26 | String param2, 27 | MyEnum param3, 28 | double param4); 29 | } 30 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/Empty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import javax.annotation.Generated; 21 | 22 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 23 | public abstract class Empty { 24 | Empty() { 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/EmptyValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.function.Consumer; 21 | import com.spotify.dataenum.function.Function; 22 | import java.lang.Object; 23 | import java.lang.Override; 24 | import java.lang.String; 25 | import javax.annotation.Generated; 26 | import javax.annotation.Nonnull; 27 | 28 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 29 | public abstract class EmptyValue { 30 | EmptyValue() { 31 | } 32 | 33 | public static EmptyValue value() { 34 | return new Value(); 35 | } 36 | 37 | public final boolean isValue() { 38 | return (this instanceof Value); 39 | } 40 | 41 | public final Value asValue() { 42 | return (Value) this; 43 | } 44 | 45 | public abstract void match(@Nonnull Consumer value); 46 | 47 | public abstract R_ map(@Nonnull Function value); 48 | 49 | public static final class Value extends EmptyValue { 50 | Value() { 51 | } 52 | 53 | @Override 54 | public boolean equals(Object other) { 55 | return other instanceof Value; 56 | } 57 | 58 | @Override 59 | public int hashCode() { 60 | return 0; 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | return "Value{}"; 66 | } 67 | 68 | @Override 69 | public final void match(@Nonnull Consumer value) { 70 | value.accept(this); 71 | } 72 | 73 | @Override 74 | public final R_ map(@Nonnull Function value) { 75 | return value.apply(this); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/EmptyValue_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | @DataEnum 24 | interface EmptyValue_dataenum { 25 | dataenum_case Value(); 26 | } 27 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/Empty_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | 22 | @DataEnum 23 | interface Empty_dataenum { 24 | } 25 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/EnumAsInner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.function.Consumer; 21 | import com.spotify.dataenum.function.Function; 22 | import java.lang.Object; 23 | import java.lang.Override; 24 | import java.lang.String; 25 | import javax.annotation.Generated; 26 | import javax.annotation.Nonnull; 27 | 28 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 29 | public abstract class EnumAsInner { 30 | EnumAsInner() { 31 | } 32 | 33 | public static EnumAsInner value() { 34 | return new Value(); 35 | } 36 | 37 | public final boolean isValue() { 38 | return (this instanceof Value); 39 | } 40 | 41 | public final Value asValue() { 42 | return (Value) this; 43 | } 44 | 45 | public abstract void match(@Nonnull Consumer value); 46 | 47 | public abstract R_ map(@Nonnull Function value); 48 | 49 | public static final class Value extends EnumAsInner { 50 | Value() { 51 | } 52 | 53 | @Override 54 | public boolean equals(Object other) { 55 | return other instanceof Value; 56 | } 57 | 58 | @Override 59 | public int hashCode() { 60 | return 0; 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | return "Value{}"; 66 | } 67 | 68 | @Override 69 | public final void match(@Nonnull Consumer value) { 70 | value.accept(this); 71 | } 72 | 73 | @Override 74 | public final R_ map(@Nonnull Function value) { 75 | return value.apply(this); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/EnumAsInner_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | 24 | class MyOuterClass { 25 | @DataEnum 26 | interface EnumAsInner_dataenum { 27 | dataenum_case Value(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/GenericDeclarationOnValues_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | @DataEnum 24 | interface GenericDeclarationOnValues_dataenum { 25 | dataenum_case Just(T param); 26 | dataenum_case None(); 27 | } 28 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/GenericValues_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | import java.util.Set; 23 | 24 | @DataEnum 25 | interface GenericValues_dataenum { 26 | dataenum_case Left(L other); 27 | dataenum_case Right(R error); 28 | dataenum_case Neither(String s); 29 | dataenum_case Both(L one, R two); 30 | dataenum_case Wrapped(Set> setOfSetOfL); 31 | } 32 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/InnerGenericValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import static com.spotify.dataenum.DataenumUtils.checkNotNull; 21 | 22 | 23 | import com.spotify.dataenum.function.Consumer; 24 | import com.spotify.dataenum.function.Function; 25 | import java.lang.Object; 26 | import java.lang.Override; 27 | import java.lang.String; 28 | import java.lang.StringBuilder; 29 | import java.lang.SuppressWarnings; 30 | import java.util.List; 31 | import javax.annotation.Generated; 32 | import javax.annotation.Nonnull; 33 | 34 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 35 | public abstract class InnerGenericValue { 36 | InnerGenericValue() { 37 | } 38 | 39 | public static InnerGenericValue many(@Nonnull List values) { 40 | return new Many(values).asInnerGenericValue(); 41 | } 42 | 43 | public static InnerGenericValue one(@Nonnull T value) { 44 | return new One(value).asInnerGenericValue(); 45 | } 46 | 47 | public static InnerGenericValue none() { 48 | return new None().asInnerGenericValue(); 49 | } 50 | 51 | public final boolean isMany() { 52 | return (this instanceof Many); 53 | } 54 | 55 | public final boolean isOne() { 56 | return (this instanceof One); 57 | } 58 | 59 | public final boolean isNone() { 60 | return (this instanceof None); 61 | } 62 | 63 | public final Many asMany() { 64 | return (Many) this; 65 | } 66 | 67 | public final One asOne() { 68 | return (One) this; 69 | } 70 | 71 | public final None asNone() { 72 | return (None) this; 73 | } 74 | 75 | public abstract void match(@Nonnull Consumer> many, @Nonnull Consumer> one, 76 | @Nonnull Consumer none); 77 | 78 | public abstract R_ map(@Nonnull Function, R_> many, 79 | @Nonnull Function, R_> one, @Nonnull Function none); 80 | 81 | public static final class Many extends InnerGenericValue { 82 | private final List values; 83 | 84 | Many(List values) { 85 | this.values = checkNotNull(values); 86 | } 87 | 88 | @Nonnull 89 | public final List values() { 90 | return values; 91 | } 92 | 93 | @Override 94 | public boolean equals(Object other) { 95 | if (other == this) return true; 96 | if (!(other instanceof Many)) return false; 97 | Many o = (Many) other; 98 | return o.values.equals(this.values); 99 | } 100 | 101 | @Override 102 | public int hashCode() { 103 | int result = 0; 104 | return result * 31 + this.values.hashCode(); 105 | } 106 | 107 | @Override 108 | public String toString() { 109 | StringBuilder builder = new StringBuilder(); 110 | builder.append("Many{values=").append(this.values); 111 | return builder.append('}').toString(); 112 | } 113 | 114 | @Override 115 | public final void match(@Nonnull Consumer> many, @Nonnull Consumer> one, 116 | @Nonnull Consumer none) { 117 | many.accept(this); 118 | } 119 | 120 | @Override 121 | public final R_ map(@Nonnull Function, R_> many, 122 | @Nonnull Function, R_> one, @Nonnull Function none) { 123 | return many.apply(this); 124 | } 125 | 126 | public final InnerGenericValue asInnerGenericValue() { 127 | return (InnerGenericValue) this; 128 | } 129 | } 130 | 131 | public static final class One extends InnerGenericValue { 132 | private final T value; 133 | 134 | One(T value) { 135 | this.value = checkNotNull(value); 136 | } 137 | 138 | @Nonnull 139 | public final T value() { 140 | return value; 141 | } 142 | 143 | @Override 144 | public boolean equals(Object other) { 145 | if (other == this) return true; 146 | if (!(other instanceof One)) return false; 147 | One o = (One) other; 148 | return o.value.equals(this.value); 149 | } 150 | 151 | @Override 152 | public int hashCode() { 153 | int result = 0; 154 | return result * 31 + this.value.hashCode(); 155 | } 156 | 157 | @Override 158 | public String toString() { 159 | StringBuilder builder = new StringBuilder(); 160 | builder.append("One{value=").append(this.value); 161 | return builder.append('}').toString(); 162 | } 163 | 164 | @Override 165 | public final void match(@Nonnull Consumer> many, @Nonnull Consumer> one, 166 | @Nonnull Consumer none) { 167 | one.accept(this); 168 | } 169 | 170 | @Override 171 | public final R_ map(@Nonnull Function, R_> many, 172 | @Nonnull Function, R_> one, @Nonnull Function none) { 173 | return one.apply(this); 174 | } 175 | 176 | public final InnerGenericValue asInnerGenericValue() { 177 | return (InnerGenericValue) this; 178 | } 179 | } 180 | 181 | public static final class None extends InnerGenericValue { 182 | None() { 183 | } 184 | 185 | @Override 186 | public boolean equals(Object other) { 187 | return other instanceof None; 188 | } 189 | 190 | @Override 191 | public int hashCode() { 192 | return 0; 193 | } 194 | 195 | @Override 196 | public String toString() { 197 | return "None{}"; 198 | } 199 | 200 | @Override 201 | public final void match(@Nonnull Consumer> many, 202 | @Nonnull Consumer> one, @Nonnull Consumer none) { 203 | none.accept(this); 204 | } 205 | 206 | @Override 207 | public final R_ map(@Nonnull Function, R_> many, 208 | @Nonnull Function, R_> one, @Nonnull Function none) { 209 | return none.apply(this); 210 | } 211 | 212 | @SuppressWarnings("unchecked") 213 | public final InnerGenericValue asInnerGenericValue() { 214 | return (InnerGenericValue) this; 215 | } 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/InnerGenericValue_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | import java.util.List; 24 | 25 | @DataEnum 26 | interface InnerGenericValue_dataenum { 27 | dataenum_case Many(List values); 28 | 29 | dataenum_case One(T value); 30 | 31 | dataenum_case None(); 32 | } 33 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/MethodsAndValues.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import static com.spotify.dataenum.DataenumUtils.checkNotNull; 21 | 22 | import com.spotify.dataenum.function.Consumer; 23 | import com.spotify.dataenum.function.Function; 24 | import java.lang.Object; 25 | import java.lang.Override; 26 | import java.lang.String; 27 | import java.lang.StringBuilder; 28 | import javax.annotation.Generated; 29 | import javax.annotation.Nonnull; 30 | 31 | /** 32 | * Generated from {@link MethodsAndValues_dataenum} 33 | */ 34 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 35 | public abstract class MethodsAndValues { 36 | MethodsAndValues() { 37 | } 38 | 39 | /** 40 | * @return a {@link Val1} (see {@link MethodsAndValues_dataenum#Val1} for source) 41 | */ 42 | public static MethodsAndValues val1(@Nonnull String x) { 43 | return new Val1(x); 44 | } 45 | 46 | /** 47 | * @return a {@link Val2} (see {@link MethodsAndValues_dataenum#Val2} for source) 48 | */ 49 | public static MethodsAndValues val2(@Nonnull String x) { 50 | return new Val2(x); 51 | } 52 | 53 | public final boolean isVal1() { 54 | return (this instanceof Val1); 55 | } 56 | 57 | public final boolean isVal2() { 58 | return (this instanceof Val2); 59 | } 60 | 61 | public final Val1 asVal1() { 62 | return (Val1) this; 63 | } 64 | 65 | public final Val2 asVal2() { 66 | return (Val2) this; 67 | } 68 | 69 | public abstract void match(@Nonnull Consumer val1, @Nonnull Consumer val2); 70 | 71 | public abstract R_ map(@Nonnull Function val1, @Nonnull Function val2); 72 | 73 | public String classMethodNoExtraArgs(MethodsAndValues x) { 74 | return x.map((v1)->v1.x(), (v2)->v2.x());} 75 | 76 | public String classMethodSomeExtraArgs(MethodsAndValues x, String suffix) { 77 | return x.map((v1)->v1.x() + suffix, (v2)->v2.x() + suffix);} 78 | 79 | public String classMethodTypeParams(MethodsAndValues x, T suffix) { 80 | return x.map((v1)->v1.x() + suffix.toString(), (v2)->v2.x() + suffix.toString());} 81 | 82 | public static String staticMethodNoExtraArgs(MethodsAndValues x) { 83 | return x.map((v1)->v1.x(), (v2)->v2.x());} 84 | 85 | public static String staticMethodSomeExtraArgs(MethodsAndValues x, String suffix) { 86 | return x.map((v1)->v1.x() + suffix, (v2)->v2.x() + suffix);} 87 | 88 | public static String staticMethodTypeParams(MethodsAndValues x, T suffix) { 89 | return x.map((v1)->v1.x() + suffix.toString(), (v2)->v2.x() + suffix.toString());} 90 | 91 | public static final class Val1 extends MethodsAndValues { 92 | private final String x; 93 | 94 | Val1(String x) { 95 | this.x = checkNotNull(x); 96 | } 97 | 98 | @Nonnull 99 | public final String x() { 100 | return x; 101 | } 102 | 103 | @Override 104 | public boolean equals(Object other) { 105 | if (other == this) return true; 106 | if (!(other instanceof Val1)) return false; 107 | Val1 o = (Val1) other; 108 | return o.x.equals(this.x); 109 | } 110 | 111 | @Override 112 | public int hashCode() { 113 | int result = 0; 114 | return result * 31 + this.x.hashCode(); 115 | } 116 | 117 | @Override 118 | public String toString() { 119 | StringBuilder builder = new StringBuilder(); 120 | builder.append("Val1{x=").append(this.x); 121 | return builder.append('}').toString(); 122 | } 123 | 124 | @Override 125 | public final void match(@Nonnull Consumer val1, @Nonnull Consumer val2) { 126 | val1.accept(this); 127 | } 128 | 129 | @Override 130 | public final R_ map(@Nonnull Function val1, @Nonnull Function val2) { 131 | return val1.apply(this); 132 | } 133 | } 134 | 135 | public static final class Val2 extends MethodsAndValues { 136 | private final String x; 137 | 138 | Val2(String x) { 139 | this.x = checkNotNull(x); 140 | } 141 | 142 | @Nonnull 143 | public final String x() { 144 | return x; 145 | } 146 | 147 | @Override 148 | public boolean equals(Object other) { 149 | if (other == this) return true; 150 | if (!(other instanceof Val2)) return false; 151 | Val2 o = (Val2) other; 152 | return o.x.equals(this.x); 153 | } 154 | 155 | @Override 156 | public int hashCode() { 157 | int result = 0; 158 | return result * 31 + this.x.hashCode(); 159 | } 160 | 161 | @Override 162 | public String toString() { 163 | StringBuilder builder = new StringBuilder(); 164 | builder.append("Val2{x=").append(this.x); 165 | return builder.append('}').toString(); 166 | } 167 | 168 | @Override 169 | public final void match(@Nonnull Consumer val1, @Nonnull Consumer val2) { 170 | val2.accept(this); 171 | } 172 | 173 | @Override 174 | public final R_ map(@Nonnull Function val1, @Nonnull Function val2) { 175 | return val2.apply(this); 176 | } 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/MethodsAndValues_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | 21 | import com.spotify.dataenum.DataEnum; 22 | import com.spotify.dataenum.Static; 23 | import com.spotify.dataenum.dataenum_case; 24 | 25 | import java.util.Set; 26 | 27 | @DataEnum 28 | interface MethodsAndValues_dataenum { 29 | dataenum_case Val1(String x); 30 | 31 | dataenum_case Val2(String x); 32 | 33 | default String classMethodNoExtraArgs(MethodsAndValues x) { 34 | return x.map( 35 | v1 -> v1.x(), 36 | v2 -> v2.x() 37 | ); 38 | } 39 | 40 | default String classMethodSomeExtraArgs(MethodsAndValues x, String suffix) { 41 | return x.map( 42 | v1 -> v1.x() + suffix, 43 | v2 -> v2.x() + suffix 44 | ); 45 | } 46 | 47 | default String classMethodTypeParams(MethodsAndValues x, T suffix) { 48 | return x.map( 49 | v1 -> v1.x() + suffix.toString(), 50 | v2 -> v2.x() + suffix.toString() 51 | ); 52 | } 53 | 54 | @Static 55 | default String staticMethodNoExtraArgs(MethodsAndValues x) { 56 | return x.map( 57 | v1 -> v1.x(), 58 | v2 -> v2.x() 59 | ); 60 | } 61 | 62 | @Static 63 | default String staticMethodSomeExtraArgs(MethodsAndValues x, String suffix) { 64 | return x.map( 65 | v1 -> v1.x() + suffix, 66 | v2 -> v2.x() + suffix 67 | ); 68 | } 69 | 70 | @Static 71 | default String staticMethodTypeParams(MethodsAndValues x, T suffix) { 72 | return x.map( 73 | v1 -> v1.x() + suffix.toString(), 74 | v2 -> v2.x() + suffix.toString() 75 | ); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/MultipleValues.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.function.Consumer; 21 | import com.spotify.dataenum.function.Function; 22 | import java.lang.Boolean; 23 | import java.lang.Integer; 24 | import java.lang.Object; 25 | import java.lang.Override; 26 | import java.lang.String; 27 | import java.lang.StringBuilder; 28 | import javax.annotation.Generated; 29 | import javax.annotation.Nonnull; 30 | 31 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 32 | public abstract class MultipleValues { 33 | MultipleValues() { 34 | } 35 | 36 | public static MultipleValues value1(int param1, boolean param2) { 37 | return new Value1(param1, param2); 38 | } 39 | 40 | public static MultipleValues value2(int param1, boolean param2) { 41 | return new Value2(param1, param2); 42 | } 43 | 44 | public final boolean isValue1() { 45 | return (this instanceof Value1); 46 | } 47 | 48 | public final boolean isValue2() { 49 | return (this instanceof Value2); 50 | } 51 | 52 | public final Value1 asValue1() { 53 | return (Value1) this; 54 | } 55 | 56 | public final Value2 asValue2() { 57 | return (Value2) this; 58 | } 59 | 60 | public abstract void match(@Nonnull Consumer value1, @Nonnull Consumer value2); 61 | 62 | public abstract R_ map(@Nonnull Function value1, 63 | @Nonnull Function value2); 64 | 65 | public static final class Value1 extends MultipleValues { 66 | private final int param1; 67 | 68 | private final boolean param2; 69 | 70 | Value1(int param1, boolean param2) { 71 | this.param1 = param1; 72 | this.param2 = param2; 73 | } 74 | 75 | public final int param1() { 76 | return param1; 77 | } 78 | 79 | public final boolean param2() { 80 | return param2; 81 | } 82 | 83 | @Override 84 | public boolean equals(Object other) { 85 | if (other == this) return true; 86 | if (!(other instanceof Value1)) return false; 87 | Value1 o = (Value1) other; 88 | return o.param1 == param1 89 | && o.param2 == param2; 90 | } 91 | 92 | @Override 93 | public int hashCode() { 94 | int result = 0; 95 | result = result * 31 + Integer.valueOf(this.param1).hashCode(); 96 | return result * 31 + Boolean.valueOf(this.param2).hashCode(); 97 | } 98 | 99 | @Override 100 | public String toString() { 101 | StringBuilder builder = new StringBuilder(); 102 | builder.append("Value1{param1=").append(this.param1); 103 | builder.append(", param2=").append(this.param2); 104 | return builder.append('}').toString(); 105 | } 106 | 107 | @Override 108 | public final void match(@Nonnull Consumer value1, @Nonnull Consumer value2) { 109 | value1.accept(this); 110 | } 111 | 112 | @Override 113 | public final R_ map(@Nonnull Function value1, 114 | @Nonnull Function value2) { 115 | return value1.apply(this); 116 | } 117 | } 118 | 119 | public static final class Value2 extends MultipleValues { 120 | private final int param1; 121 | 122 | private final boolean param2; 123 | 124 | Value2(int param1, boolean param2) { 125 | this.param1 = param1; 126 | this.param2 = param2; 127 | } 128 | 129 | public final int param1() { 130 | return param1; 131 | } 132 | 133 | public final boolean param2() { 134 | return param2; 135 | } 136 | 137 | @Override 138 | public boolean equals(Object other) { 139 | if (other == this) return true; 140 | if (!(other instanceof Value2)) return false; 141 | Value2 o = (Value2) other; 142 | return o.param1 == param1 143 | && o.param2 == param2; 144 | } 145 | 146 | @Override 147 | public int hashCode() { 148 | int result = 0; 149 | result = result * 31 + Integer.valueOf(this.param1).hashCode(); 150 | return result * 31 + Boolean.valueOf(this.param2).hashCode(); 151 | } 152 | 153 | @Override 154 | public String toString() { 155 | StringBuilder builder = new StringBuilder(); 156 | builder.append("Value2{param1=").append(this.param1); 157 | builder.append(", param2=").append(this.param2); 158 | return builder.append('}').toString(); 159 | } 160 | 161 | @Override 162 | public final void match(@Nonnull Consumer value1, @Nonnull Consumer value2) { 163 | value2.accept(this); 164 | } 165 | 166 | @Override 167 | public final R_ map(@Nonnull Function value1, 168 | @Nonnull Function value2) { 169 | return value2.apply(this); 170 | } 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/MultipleValues_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | @DataEnum 24 | interface MultipleValues_dataenum { 25 | dataenum_case Value1(int param1, boolean param2); 26 | dataenum_case Value2(int param1, boolean param2); 27 | } 28 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/MyEnum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | 21 | 22 | enum MyEnum { 23 | A, B 24 | } 25 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/NonInterfaceSpec_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | 22 | // should lead to a compiler error for not being an interface 23 | 24 | @DataEnum 25 | class NonInterfaceSpec_dataenum { 26 | } 27 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/NullableValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import static com.spotify.dataenum.DataenumUtils.checkNotNull; 21 | import static com.spotify.dataenum.DataenumUtils.equal; 22 | 23 | import com.spotify.dataenum.function.Consumer; 24 | import com.spotify.dataenum.function.Function; 25 | import java.lang.Object; 26 | import java.lang.Override; 27 | import java.lang.String; 28 | import java.lang.StringBuilder; 29 | import javax.annotation.Generated; 30 | import javax.annotation.Nonnull; 31 | import javax.annotation.Nullable; 32 | 33 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 34 | public abstract class NullableValue { 35 | NullableValue() { 36 | } 37 | 38 | public static NullableValue value(@Nonnull Object param1, @Nullable Object param2, 39 | @Nullable Object param3, @Nonnull Object param4, @Nonnull Object param5) { 40 | return new Value(param1, param2, param3, param4, param5); 41 | } 42 | 43 | public final boolean isValue() { 44 | return (this instanceof Value); 45 | } 46 | 47 | public final Value asValue() { 48 | return (Value) this; 49 | } 50 | 51 | public abstract void match(@Nonnull Consumer value); 52 | 53 | public abstract R_ map(@Nonnull Function value); 54 | 55 | public static final class Value extends NullableValue { 56 | private final Object param1; 57 | 58 | private final Object param2; 59 | 60 | private final Object param3; 61 | 62 | private final Object param4; 63 | 64 | private final Object param5; 65 | 66 | Value(Object param1, Object param2, Object param3, Object param4, Object param5) { 67 | this.param1 = checkNotNull(param1); 68 | this.param2 = param2; 69 | this.param3 = param3; 70 | this.param4 = checkNotNull(param4); 71 | this.param5 = checkNotNull(param5); 72 | } 73 | 74 | @Nonnull 75 | public final Object param1() { 76 | return param1; 77 | } 78 | 79 | @Nullable 80 | public final Object param2() { 81 | return param2; 82 | } 83 | 84 | @Nullable 85 | public final Object param3() { 86 | return param3; 87 | } 88 | 89 | @Nonnull 90 | public final Object param4() { 91 | return param4; 92 | } 93 | 94 | @Nonnull 95 | public final Object param5() { 96 | return param5; 97 | } 98 | 99 | @Override 100 | public boolean equals(Object other) { 101 | if (other == this) return true; 102 | if (!(other instanceof Value)) return false; 103 | Value o = (Value) other; 104 | return o.param1.equals(this.param1) 105 | && equal(o.param2, this.param2) 106 | && equal(o.param3, this.param3) 107 | && o.param4.equals(this.param4) 108 | && o.param5.equals(this.param5); 109 | } 110 | 111 | @Override 112 | public int hashCode() { 113 | int result = 0; 114 | result = result * 31 + this.param1.hashCode(); 115 | result = result * 31 + (this.param2 != null ? this.param2.hashCode() : 0); 116 | result = result * 31 + (this.param3 != null ? this.param3.hashCode() : 0); 117 | result = result * 31 + this.param4.hashCode(); 118 | return result * 31 + this.param5.hashCode(); 119 | } 120 | 121 | @Override 122 | public String toString() { 123 | StringBuilder builder = new StringBuilder(); 124 | builder.append("Value{param1=").append(this.param1); 125 | builder.append(", param2=").append(this.param2); 126 | builder.append(", param3=").append(this.param3); 127 | builder.append(", param4=").append(this.param4); 128 | builder.append(", param5=").append(this.param5); 129 | return builder.append('}').toString(); 130 | } 131 | 132 | @Override 133 | public final void match(@Nonnull Consumer value) { 134 | value.accept(this); 135 | } 136 | 137 | @Override 138 | public final R_ map(@Nonnull Function value) { 139 | return value.apply(this); 140 | } 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/NullableValue_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | @interface Nullable {} 24 | @interface NotNull {} 25 | 26 | @DataEnum 27 | interface NullableValue_dataenum { 28 | dataenum_case Value(Object param1, 29 | @Nullable Object param2, 30 | @javax.annotation.Nullable Object param3, 31 | @NotNull Object param4, 32 | @javax.annotation.Nonnull Object param5); 33 | } 34 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/PrimitiveValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.function.Consumer; 21 | import com.spotify.dataenum.function.Function; 22 | import java.lang.Boolean; 23 | import java.lang.Double; 24 | import java.lang.Float; 25 | import java.lang.Integer; 26 | import java.lang.Object; 27 | import java.lang.Override; 28 | import java.lang.String; 29 | import java.lang.StringBuilder; 30 | import javax.annotation.Generated; 31 | import javax.annotation.Nonnull; 32 | 33 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 34 | public abstract class PrimitiveValue { 35 | PrimitiveValue() { 36 | } 37 | 38 | public static PrimitiveValue value(int param1, boolean param2, float param3, double param4) { 39 | return new Value(param1, param2, param3, param4); 40 | } 41 | 42 | public final boolean isValue() { 43 | return (this instanceof Value); 44 | } 45 | 46 | public final Value asValue() { 47 | return (Value) this; 48 | } 49 | 50 | public abstract void match(@Nonnull Consumer value); 51 | 52 | public abstract R_ map(@Nonnull Function value); 53 | 54 | public static final class Value extends PrimitiveValue { 55 | private final int param1; 56 | 57 | private final boolean param2; 58 | 59 | private final float param3; 60 | 61 | private final double param4; 62 | 63 | Value(int param1, boolean param2, float param3, double param4) { 64 | this.param1 = param1; 65 | this.param2 = param2; 66 | this.param3 = param3; 67 | this.param4 = param4; 68 | } 69 | 70 | public final int param1() { 71 | return param1; 72 | } 73 | 74 | public final boolean param2() { 75 | return param2; 76 | } 77 | 78 | public final float param3() { 79 | return param3; 80 | } 81 | 82 | public final double param4() { 83 | return param4; 84 | } 85 | 86 | @Override 87 | public boolean equals(Object other) { 88 | if (other == this) return true; 89 | if (!(other instanceof Value)) return false; 90 | Value o = (Value) other; 91 | return o.param1 == param1 92 | && o.param2 == param2 93 | && o.param3 == param3 94 | && o.param4 == param4; 95 | } 96 | 97 | @Override 98 | public int hashCode() { 99 | int result = 0; 100 | result = result * 31 + Integer.valueOf(this.param1).hashCode(); 101 | result = result * 31 + Boolean.valueOf(this.param2).hashCode(); 102 | result = result * 31 + Float.valueOf(this.param3).hashCode(); 103 | return result * 31 + Double.valueOf(this.param4).hashCode(); 104 | } 105 | 106 | @Override 107 | public String toString() { 108 | StringBuilder builder = new StringBuilder(); 109 | builder.append("Value{param1=").append(this.param1); 110 | builder.append(", param2=").append(this.param2); 111 | builder.append(", param3=").append(this.param3); 112 | builder.append(", param4=").append(this.param4); 113 | return builder.append('}').toString(); 114 | } 115 | 116 | @Override 117 | public final void match(@Nonnull Consumer value) { 118 | value.accept(this); 119 | } 120 | 121 | @Override 122 | public final R_ map(@Nonnull Function value) { 123 | return value.apply(this); 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/PrimitiveValue_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | @DataEnum 24 | interface PrimitiveValue_dataenum { 25 | dataenum_case Value(int param1, 26 | boolean param2, 27 | float param3, 28 | double param4); 29 | } 30 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/PublicSpec_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | 22 | // should lead to a compiler warning for having a public input 23 | 24 | @DataEnum 25 | public interface PublicSpec_dataenum { 26 | } 27 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/RecursiveGenericValue_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | @DataEnum 24 | interface RecursiveGenericValue_dataenum { 25 | dataenum_case Branch(RecursiveGenericValue_dataenum left, RecursiveGenericValue_dataenum right); 26 | 27 | dataenum_case Left(L value); 28 | 29 | dataenum_case Right(R value); 30 | } 31 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/RecursiveValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import static com.spotify.dataenum.DataenumUtils.checkNotNull; 21 | 22 | import com.spotify.dataenum.function.Consumer; 23 | import com.spotify.dataenum.function.Function; 24 | import java.lang.Object; 25 | import java.lang.Override; 26 | import java.lang.String; 27 | import java.lang.StringBuilder; 28 | import java.util.Set; 29 | import javax.annotation.Generated; 30 | import javax.annotation.Nonnull; 31 | 32 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 33 | public abstract class RecursiveValue { 34 | RecursiveValue() { 35 | } 36 | 37 | public static RecursiveValue value(@Nonnull RecursiveValue child) { 38 | return new Value(child); 39 | } 40 | 41 | public static RecursiveValue typeParamValue(@Nonnull Set children) { 42 | return new TypeParamValue(children); 43 | } 44 | 45 | public final boolean isValue() { 46 | return (this instanceof Value); 47 | } 48 | 49 | public final boolean isTypeParamValue() { 50 | return (this instanceof TypeParamValue); 51 | } 52 | 53 | public final Value asValue() { 54 | return (Value) this; 55 | } 56 | 57 | public final TypeParamValue asTypeParamValue() { 58 | return (TypeParamValue) this; 59 | } 60 | 61 | public abstract void match(@Nonnull Consumer value, 62 | @Nonnull Consumer typeParamValue); 63 | 64 | public abstract R_ map(@Nonnull Function value, 65 | @Nonnull Function typeParamValue); 66 | 67 | public static final class Value extends RecursiveValue { 68 | private final RecursiveValue child; 69 | 70 | Value(RecursiveValue child) { 71 | this.child = checkNotNull(child); 72 | } 73 | 74 | @Nonnull 75 | public final RecursiveValue child() { 76 | return child; 77 | } 78 | 79 | @Override 80 | public boolean equals(Object other) { 81 | if (other == this) return true; 82 | if (!(other instanceof Value)) return false; 83 | Value o = (Value) other; 84 | return o.child.equals(this.child); 85 | } 86 | 87 | @Override 88 | public int hashCode() { 89 | int result = 0; 90 | return result * 31 + this.child.hashCode(); 91 | } 92 | 93 | @Override 94 | public String toString() { 95 | StringBuilder builder = new StringBuilder(); 96 | builder.append("Value{child=").append(this.child); 97 | return builder.append('}').toString(); 98 | } 99 | 100 | @Override 101 | public final void match(@Nonnull Consumer value, 102 | @Nonnull Consumer typeParamValue) { 103 | value.accept(this); 104 | } 105 | 106 | @Override 107 | public final R_ map(@Nonnull Function value, 108 | @Nonnull Function typeParamValue) { 109 | return value.apply(this); 110 | } 111 | } 112 | 113 | public static final class TypeParamValue extends RecursiveValue { 114 | private final Set children; 115 | 116 | TypeParamValue(Set children) { 117 | this.children = checkNotNull(children); 118 | } 119 | 120 | @Nonnull 121 | public final Set children() { 122 | return children; 123 | } 124 | 125 | @Override 126 | public boolean equals(Object other) { 127 | if (other == this) return true; 128 | if (!(other instanceof TypeParamValue)) return false; 129 | TypeParamValue o = (TypeParamValue) other; 130 | return o.children.equals(this.children); 131 | } 132 | 133 | @Override 134 | public int hashCode() { 135 | int result = 0; 136 | return result * 31 + this.children.hashCode(); 137 | } 138 | 139 | @Override 140 | public String toString() { 141 | StringBuilder builder = new StringBuilder(); 142 | builder.append("TypeParamValue{children=").append(this.children); 143 | return builder.append('}').toString(); 144 | } 145 | 146 | @Override 147 | public final void match(@Nonnull Consumer value, 148 | @Nonnull Consumer typeParamValue) { 149 | typeParamValue.accept(this); 150 | } 151 | 152 | @Override 153 | public final R_ map(@Nonnull Function value, 154 | @Nonnull Function typeParamValue) { 155 | return typeParamValue.apply(this); 156 | } 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/RecursiveValue_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | import java.util.Set; 23 | 24 | @DataEnum 25 | interface RecursiveValue_dataenum { 26 | dataenum_case Value(RecursiveValue_dataenum child); 27 | dataenum_case TypeParamValue(Set children); 28 | } 29 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/Redacted.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import static com.spotify.dataenum.DataenumUtils.checkNotNull; 21 | 22 | import com.spotify.dataenum.function.Consumer; 23 | import com.spotify.dataenum.function.Function; 24 | import java.lang.Boolean; 25 | import java.lang.Object; 26 | import java.lang.Override; 27 | import java.lang.String; 28 | import java.lang.StringBuilder; 29 | import java.util.Arrays; 30 | import javax.annotation.Generated; 31 | import javax.annotation.Nonnull; 32 | 33 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 34 | public abstract class Redacted { 35 | Redacted() { 36 | } 37 | 38 | public static Redacted value(@Nonnull int[] param1, boolean param2) { 39 | return new Value(param1, param2); 40 | } 41 | 42 | public final boolean isValue() { 43 | return (this instanceof Value); 44 | } 45 | 46 | public final Value asValue() { 47 | return (Value) this; 48 | } 49 | 50 | public abstract void match(@Nonnull Consumer value); 51 | 52 | public abstract R_ map(@Nonnull Function value); 53 | 54 | public static final class Value extends Redacted { 55 | private final int[] param1; 56 | 57 | private final boolean param2; 58 | 59 | Value(int[] param1, boolean param2) { 60 | this.param1 = checkNotNull(param1); 61 | this.param2 = param2; 62 | } 63 | 64 | @Nonnull 65 | public final int[] param1() { 66 | return param1; 67 | } 68 | 69 | public final boolean param2() { 70 | return param2; 71 | } 72 | 73 | @Override 74 | public boolean equals(Object other) { 75 | if (other == this) return true; 76 | if (!(other instanceof Value)) return false; 77 | Value o = (Value) other; 78 | return o.param2 == param2 79 | && Arrays.equals(o.param1, param1); 80 | } 81 | 82 | @Override 83 | public int hashCode() { 84 | int result = 0; 85 | result = result * 31 + Arrays.hashCode(this.param1); 86 | return result * 31 + Boolean.valueOf(this.param2).hashCode(); 87 | } 88 | 89 | @Override 90 | public String toString() { 91 | StringBuilder builder = new StringBuilder(); 92 | builder.append("Value{param1=").append("***"); 93 | builder.append(", param2=").append("***"); 94 | return builder.append('}').toString(); 95 | } 96 | 97 | @Override 98 | public final void match(@Nonnull Consumer value) { 99 | value.accept(this); 100 | } 101 | 102 | @Override 103 | public final R_ map(@Nonnull Function value) { 104 | return value.apply(this); 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/Redacted_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.Redacted; 22 | import com.spotify.dataenum.dataenum_case; 23 | 24 | @DataEnum 25 | interface Redacted_dataenum { 26 | dataenum_case Value(@Redacted int[] param1, @Redacted boolean param2); 27 | } 28 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/ReferencesOther_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | import java.util.Set; 23 | import just.some.pkg.InPackage_dataenum; 24 | 25 | @DataEnum 26 | interface ReferencesOther_dataenum { 27 | dataenum_case Another(InPackage_dataenum other); 28 | dataenum_case TypeParamAnother(Set others); 29 | dataenum_case TypeParamParamAnother(Set> manyOthers); 30 | } 31 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/SuperInterfaces.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import static com.spotify.dataenum.DataenumUtils.checkNotNull; 21 | 22 | import com.spotify.dataenum.function.Consumer; 23 | import com.spotify.dataenum.function.Function; 24 | import java.io.Serializable; 25 | import java.lang.Integer; 26 | import java.lang.Object; 27 | import java.lang.Override; 28 | import java.lang.String; 29 | import java.lang.StringBuilder; 30 | import javax.annotation.Generated; 31 | import javax.annotation.Nonnull; 32 | 33 | /** 34 | * Generated from {@link SuperInterfaces_dataenum} 35 | */ 36 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 37 | public abstract class SuperInterfaces implements Serializable { 38 | SuperInterfaces() { 39 | } 40 | 41 | /** 42 | * @return a {@link Value1} (see {@link SuperInterfaces_dataenum#Value1} for source) 43 | */ 44 | public static SuperInterfaces value1(@Nonnull String msg) { 45 | return new Value1(msg); 46 | } 47 | 48 | /** 49 | * @return a {@link Value2} (see {@link SuperInterfaces_dataenum#Value2} for source) 50 | */ 51 | public static SuperInterfaces value2(@Nonnull String msg, int code) { 52 | return new Value2(msg, code); 53 | } 54 | 55 | public final boolean isValue1() { 56 | return (this instanceof Value1); 57 | } 58 | 59 | public final boolean isValue2() { 60 | return (this instanceof Value2); 61 | } 62 | 63 | public final Value1 asValue1() { 64 | return (Value1) this; 65 | } 66 | 67 | public final Value2 asValue2() { 68 | return (Value2) this; 69 | } 70 | 71 | public abstract void match(@Nonnull Consumer value1, @Nonnull Consumer value2); 72 | 73 | public abstract R_ map(@Nonnull Function value1, 74 | @Nonnull Function value2); 75 | 76 | public static final class Value1 extends SuperInterfaces { 77 | private final String msg; 78 | 79 | Value1(String msg) { 80 | this.msg = checkNotNull(msg); 81 | } 82 | 83 | @Nonnull 84 | public final String msg() { 85 | return msg; 86 | } 87 | 88 | @Override 89 | public boolean equals(Object other) { 90 | if (other == this) return true; 91 | if (!(other instanceof Value1)) return false; 92 | Value1 o = (Value1) other; 93 | return o.msg.equals(this.msg); 94 | } 95 | 96 | @Override 97 | public int hashCode() { 98 | int result = 0; 99 | return result * 31 + this.msg.hashCode(); 100 | } 101 | 102 | @Override 103 | public String toString() { 104 | StringBuilder builder = new StringBuilder(); 105 | builder.append("Value1{msg=").append(this.msg); 106 | return builder.append('}').toString(); 107 | } 108 | 109 | @Override 110 | public final void match(@Nonnull Consumer value1, @Nonnull Consumer value2) { 111 | value1.accept(this); 112 | } 113 | 114 | @Override 115 | public final R_ map(@Nonnull Function value1, 116 | @Nonnull Function value2) { 117 | return value1.apply(this); 118 | } 119 | } 120 | 121 | public static final class Value2 extends SuperInterfaces { 122 | private final String msg; 123 | 124 | private final int code; 125 | 126 | Value2(String msg, int code) { 127 | this.msg = checkNotNull(msg); 128 | this.code = code; 129 | } 130 | 131 | @Nonnull 132 | public final String msg() { 133 | return msg; 134 | } 135 | 136 | public final int code() { 137 | return code; 138 | } 139 | 140 | @Override 141 | public boolean equals(Object other) { 142 | if (other == this) return true; 143 | if (!(other instanceof Value2)) return false; 144 | Value2 o = (Value2) other; 145 | return o.code == code 146 | && o.msg.equals(this.msg); 147 | } 148 | 149 | @Override 150 | public int hashCode() { 151 | int result = 0; 152 | result = result * 31 + this.msg.hashCode(); 153 | return result * 31 + Integer.valueOf(this.code).hashCode(); 154 | } 155 | 156 | @Override 157 | public String toString() { 158 | StringBuilder builder = new StringBuilder(); 159 | builder.append("Value2{msg=").append(this.msg); 160 | builder.append(", code=").append(this.code); 161 | return builder.append('}').toString(); 162 | } 163 | 164 | @Override 165 | public final void match(@Nonnull Consumer value1, @Nonnull Consumer value2) { 166 | value2.accept(this); 167 | } 168 | 169 | @Override 170 | public final R_ map(@Nonnull Function value1, 171 | @Nonnull Function value2) { 172 | return value2.apply(this); 173 | } 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/SuperInterfaces_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | import java.io.Serializable; 23 | 24 | @DataEnum 25 | interface SuperInterfaces_dataenum extends Serializable { 26 | dataenum_case Value1(String msg); 27 | dataenum_case Value2(String msg, int code); 28 | } 29 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/VarargValue_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | import com.spotify.dataenum.DataEnum; 21 | import com.spotify.dataenum.dataenum_case; 22 | 23 | @DataEnum 24 | interface VarargValue_dataenum { 25 | dataenum_case Value(int... values); 26 | } 27 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/annotation/Annotation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package annotation; 21 | 22 | import com.spotify.dataenum.function.Consumer; 23 | import com.spotify.dataenum.function.Function; 24 | import java.lang.Deprecated; 25 | import java.lang.Object; 26 | import java.lang.Override; 27 | import java.lang.String; 28 | import java.lang.SuppressWarnings; 29 | import javax.annotation.Generated; 30 | import javax.annotation.Nonnull; 31 | 32 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 33 | public abstract class Annotation { 34 | Annotation() { 35 | } 36 | 37 | @SuppressWarnings({"floopity", "floop"}) 38 | @Deprecated 39 | @MyAnnotation(foo = "hi", fie = 15) 40 | public static Annotation annotatedWithParams() { 41 | return new AnnotatedWithParams(); 42 | } 43 | 44 | @MyAnnotation(foo = "hi") 45 | public static Annotation annotatedWithDefault() { 46 | return new AnnotatedWithDefault(); 47 | } 48 | 49 | public final boolean isAnnotatedWithParams() { 50 | return (this instanceof AnnotatedWithParams); 51 | } 52 | 53 | public final boolean isAnnotatedWithDefault() { 54 | return (this instanceof AnnotatedWithDefault); 55 | } 56 | 57 | public final AnnotatedWithParams asAnnotatedWithParams() { 58 | return (AnnotatedWithParams) this; 59 | } 60 | 61 | public final AnnotatedWithDefault asAnnotatedWithDefault() { 62 | return (AnnotatedWithDefault) this; 63 | } 64 | 65 | public abstract void match(@Nonnull Consumer annotatedWithParams, 66 | @Nonnull Consumer annotatedWithDefault); 67 | 68 | public abstract R_ map(@Nonnull Function annotatedWithParams, 69 | @Nonnull Function annotatedWithDefault); 70 | 71 | public static final class AnnotatedWithParams extends Annotation { 72 | AnnotatedWithParams() { 73 | } 74 | 75 | @Override 76 | public boolean equals(Object other) { 77 | return other instanceof AnnotatedWithParams; 78 | } 79 | 80 | @Override 81 | public int hashCode() { 82 | return 0; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "AnnotatedWithParams{}"; 88 | } 89 | 90 | @Override 91 | public final void match(@Nonnull Consumer annotatedWithParams, 92 | @Nonnull Consumer annotatedWithDefault) { 93 | annotatedWithParams.accept(this); 94 | } 95 | 96 | @Override 97 | public final R_ map(@Nonnull Function annotatedWithParams, 98 | @Nonnull Function annotatedWithDefault) { 99 | return annotatedWithParams.apply(this); 100 | } 101 | } 102 | 103 | public static final class AnnotatedWithDefault extends Annotation { 104 | AnnotatedWithDefault() { 105 | } 106 | 107 | @Override 108 | public boolean equals(Object other) { 109 | return other instanceof AnnotatedWithDefault; 110 | } 111 | 112 | @Override 113 | public int hashCode() { 114 | return 0; 115 | } 116 | 117 | @Override 118 | public String toString() { 119 | return "AnnotatedWithDefault{}"; 120 | } 121 | 122 | @Override 123 | public final void match(@Nonnull Consumer annotatedWithParams, 124 | @Nonnull Consumer annotatedWithDefault) { 125 | annotatedWithDefault.accept(this); 126 | } 127 | 128 | @Override 129 | public final R_ map(@Nonnull Function annotatedWithParams, 130 | @Nonnull Function annotatedWithDefault) { 131 | return annotatedWithDefault.apply(this); 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/annotation/Annotation_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package annotation; 21 | 22 | import com.spotify.dataenum.DataEnum; 23 | import com.spotify.dataenum.dataenum_case; 24 | 25 | @DataEnum 26 | interface Annotation_dataenum { 27 | @SuppressWarnings({"floopity", "floop"}) 28 | @Deprecated 29 | @MyAnnotation(foo = "hi", fie = 15) 30 | dataenum_case AnnotatedWithParams(); 31 | 32 | @MyAnnotation(foo = "hi") 33 | dataenum_case AnnotatedWithDefault(); 34 | } 35 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/annotation/MyAnnotation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package annotation; 21 | 22 | import static java.lang.annotation.ElementType.METHOD; 23 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 24 | 25 | import java.lang.annotation.Retention; 26 | import java.lang.annotation.Target; 27 | 28 | @Retention(RUNTIME) 29 | @Target(METHOD) 30 | public @interface MyAnnotation { 31 | String foo(); 32 | int fie() default 99; 33 | } 34 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/hide/ctors/PrivateConstructors.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package hide.ctors; 21 | 22 | import com.spotify.dataenum.function.Consumer; 23 | import com.spotify.dataenum.function.Function; 24 | import java.lang.Object; 25 | import java.lang.Override; 26 | import java.lang.String; 27 | import javax.annotation.Generated; 28 | import javax.annotation.Nonnull; 29 | 30 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 31 | public abstract class PrivateConstructors { 32 | private PrivateConstructors() { 33 | } 34 | 35 | public static PrivateConstructors value() { 36 | return new Value(); 37 | } 38 | 39 | public final boolean isValue() { 40 | return (this instanceof Value); 41 | } 42 | 43 | public final Value asValue() { 44 | return (Value) this; 45 | } 46 | 47 | public abstract void match(@Nonnull Consumer value); 48 | 49 | public abstract R_ map(@Nonnull Function value); 50 | 51 | public static final class Value extends PrivateConstructors { 52 | private Value() { 53 | } 54 | 55 | @Override 56 | public boolean equals(Object other) { 57 | return other instanceof Value; 58 | } 59 | 60 | @Override 61 | public int hashCode() { 62 | return 0; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return "Value{}"; 68 | } 69 | 70 | @Override 71 | public final void match(@Nonnull Consumer value) { 72 | value.accept(this); 73 | } 74 | 75 | @Override 76 | public final R_ map(@Nonnull Function value) { 77 | return value.apply(this); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/hide/ctors/PrivateConstructors_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package hide.ctors; 21 | 22 | import com.spotify.dataenum.DataEnum; 23 | import com.spotify.dataenum.dataenum_case; 24 | 25 | @DataEnum 26 | interface PrivateConstructors_dataenum { 27 | dataenum_case Value(); 28 | } 29 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/hide/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | @ConstructorAccess(PRIVATE) 21 | package hide; 22 | 23 | import static com.spotify.dataenum.Access.PRIVATE; 24 | 25 | import com.spotify.dataenum.ConstructorAccess; 26 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/javadoc/Javadoc_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package javadoc; 21 | 22 | import com.spotify.dataenum.DataEnum; 23 | import com.spotify.dataenum.dataenum_case; 24 | 25 | @DataEnum 26 | interface Javadoc_dataenum { 27 | 28 | /** 29 | * Some documentation about this case. 30 | */ 31 | dataenum_case Documented(); 32 | 33 | dataenum_case Value(); 34 | } 35 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/just/some/pkg/InPackage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package just.some.pkg; 21 | 22 | import com.spotify.dataenum.function.Consumer; 23 | import com.spotify.dataenum.function.Function; 24 | import java.lang.Boolean; 25 | import java.lang.Integer; 26 | import java.lang.Object; 27 | import java.lang.Override; 28 | import java.lang.String; 29 | import java.lang.StringBuilder; 30 | import javax.annotation.Generated; 31 | import javax.annotation.Nonnull; 32 | 33 | @Generated("com.spotify.dataenum.processor.DataEnumProcessor") 34 | public abstract class InPackage { 35 | InPackage() { 36 | } 37 | 38 | public static InPackage value1(int param1, boolean param2) { 39 | return new Value1(param1, param2); 40 | } 41 | 42 | public final boolean isValue1() { 43 | return (this instanceof Value1); 44 | } 45 | 46 | public final Value1 asValue1() { 47 | return (Value1) this; 48 | } 49 | 50 | public abstract void match(@Nonnull Consumer value1); 51 | 52 | public abstract R_ map(@Nonnull Function value1); 53 | 54 | public static final class Value1 extends InPackage { 55 | private final int param1; 56 | 57 | private final boolean param2; 58 | 59 | Value1(int param1, boolean param2) { 60 | this.param1 = param1; 61 | this.param2 = param2; 62 | } 63 | 64 | public final int param1() { 65 | return param1; 66 | } 67 | 68 | public final boolean param2() { 69 | return param2; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object other) { 74 | if (other == this) return true; 75 | if (!(other instanceof Value1)) return false; 76 | Value1 o = (Value1) other; 77 | return o.param1 == param1 78 | && o.param2 == param2; 79 | } 80 | 81 | @Override 82 | public int hashCode() { 83 | int result = 0; 84 | result = result * 31 + Integer.valueOf(this.param1).hashCode(); 85 | return result * 31 + Boolean.valueOf(this.param2).hashCode(); 86 | } 87 | 88 | @Override 89 | public String toString() { 90 | StringBuilder builder = new StringBuilder(); 91 | builder.append("Value1{param1=").append(this.param1); 92 | builder.append(", param2=").append(this.param2); 93 | return builder.append('}').toString(); 94 | } 95 | 96 | @Override 97 | public final void match(@Nonnull Consumer value1) { 98 | value1.accept(this); 99 | } 100 | 101 | @Override 102 | public final R_ map(@Nonnull Function value1) { 103 | return value1.apply(this); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /dataenum-processor/src/test/resources/just/some/pkg/InPackage_dataenum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package just.some.pkg; 21 | 22 | import com.spotify.dataenum.DataEnum; 23 | import com.spotify.dataenum.dataenum_case; 24 | 25 | @DataEnum 26 | public interface InPackage_dataenum { 27 | dataenum_case Value1(int param1, boolean param2); 28 | } 29 | -------------------------------------------------------------------------------- /dataenum/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | 5 | com.spotify.dataenum 6 | dataenum-parent 7 | 1.5.1-SNAPSHOT 8 | 9 | 10 | Dataenum Runtime Dependencies 11 | dataenum 12 | 13 | 14 | 15 | com.google.code.findbugs 16 | jsr305 17 | 18 | 19 | 20 | junit 21 | junit 22 | test 23 | 24 | 25 | org.hamcrest 26 | hamcrest-junit 27 | test 28 | 29 | 30 | 31 | 32 | 33 | 34 | maven-compiler-plugin 35 | 36 | 37 | default-compile 38 | 39 | 1.7 40 | 1.7 41 | 42 | 43 | 44 | 45 | 46 | com.coveo 47 | fmt-maven-plugin 48 | 49 | 50 | com.github.siom79.japicmp 51 | japicmp-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | 58 | coverage 59 | 60 | 61 | 62 | org.jacoco 63 | jacoco-maven-plugin 64 | 0.8.4 65 | 66 | 67 | **/AutoValue_* 68 | **/dataenum_case* 69 | 70 | 71 | 72 | 73 | 74 | prepare-agent 75 | 76 | 77 | 78 | report 79 | prepare-package 80 | 81 | report 82 | 83 | 84 | 85 | check 86 | 87 | check 88 | 89 | 90 | 91 | 92 | BUNDLE 93 | 94 | 95 | INSTRUCTION 96 | COVEREDRATIO 97 | 0.31 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | java9 111 | 112 | 9 113 | 114 | 115 | 116 | 117 | maven-compiler-plugin 118 | 119 | 123 | 7 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /dataenum/src/main/java/com/spotify/dataenum/Access.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum; 21 | 22 | /** Defines the possible values for Java element access levels. */ 23 | public enum Access { 24 | // We cannot use javax.lang.model.element.Modifier, since it include non-access-related 25 | // modifiers and doesn't include PACKAGE_PRIVATE 26 | PUBLIC, 27 | PROTECTED, 28 | PRIVATE, 29 | PACKAGE_PRIVATE; 30 | } 31 | -------------------------------------------------------------------------------- /dataenum/src/main/java/com/spotify/dataenum/ConstructorAccess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | /** 28 | * Allows configuration of which access level should be used for generated constructors. When 29 | * generating code in package X, the dataenum-processor will use the value from the nearest 30 | * package-info.java file with this annotation to determine which value to use. If the traversal 31 | * reaches the root (the default package) without finding such a package-info.java file, {@link 32 | * Access#PACKAGE_PRIVATE} will be used. 33 | * 34 | *

There are two useful choices to make: PRIVATE or PACKAGE_PRIVATE. PRIVATE is best in the sense 35 | * of providing the clearest API: the only way to instantiate DataEnum cases is through the static 36 | * factory method in the top-level type. However, using private constructors leads to the compiler 37 | * generating synthetic constructor methods, and for Android, you want to keep the method count 38 | * down. As that is an important use case for DataEnum, we chose PACKAGE_PRIVATE as the default. 39 | */ 40 | @Retention(RetentionPolicy.SOURCE) 41 | @Target(ElementType.PACKAGE) 42 | public @interface ConstructorAccess { 43 | Access value(); 44 | } 45 | -------------------------------------------------------------------------------- /dataenum/src/main/java/com/spotify/dataenum/DataEnum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | /** 28 | * Annotation used for specifying and generating tagged union style enums. 29 | * 30 | *

To be used together with the dataenum-processor annotation processor. 31 | * 32 | *

This annotation must be used on a package private interface, and the interface name must end 33 | * with '_dataenum'. For each value of the enum, declare a method with {@link dataenum_case} as the 34 | * return type. The name of the method will be used as the name of the value type, and the arguments 35 | * of the method will be the data associated with the value. 36 | * 37 | *

The value names are taken verbatim from the interface, so you will want to use CamelCase for 38 | * the value names. Non-primitive types will be non-nullable by default, but this can be overriden 39 | * using a @Nullable annotation. 40 | * 41 | *

A new class will be generated without the `_dataenum` suffix, and that will be your data 42 | * enumeration class. It will act as a factory and a base class for your enumeration values. The 43 | * interface that has the @DataEnum annotation is only used as an input for code generation, 44 | * therefore all classes with the `_dataenum` suffix can be excluded from artifacts. 45 | * 46 | *

47 |  * @DataEnum
48 |  * interface MyData_dataenum {
49 |  *   dataenum_case Foo(int justANumber, String stringThatCannotBeNull);
50 |  *   dataenum_case Bar(@Nullable String thisStringCanBeNull);
51 |  *   dataenum_case Baz(); // no data associated with this value
52 |  * }
53 |  * 
54 | */ 55 | @Retention(RetentionPolicy.SOURCE) 56 | @Target(ElementType.TYPE) 57 | public @interface DataEnum {} 58 | -------------------------------------------------------------------------------- /dataenum/src/main/java/com/spotify/dataenum/DataenumUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum; 21 | 22 | import javax.annotation.Nonnull; 23 | import javax.annotation.Nullable; 24 | 25 | /** Utilities copied from Guava in order to avoid depending directly on it. */ 26 | public final class DataenumUtils { 27 | private DataenumUtils() {} 28 | 29 | @Nonnull 30 | public static T checkNotNull(T value) { 31 | if (value == null) { 32 | throw new NullPointerException(); 33 | } 34 | return value; 35 | } 36 | 37 | public static boolean equal(@Nullable Object a, @Nullable Object b) { 38 | return a == b || (a != null && a.equals(b)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /dataenum/src/main/java/com/spotify/dataenum/Redacted.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | /** 28 | * Marks a dataenum field for redaction, meaning that the generated toString() method for that case 29 | * will not output the contents of that field. 30 | */ 31 | @Retention(RetentionPolicy.SOURCE) 32 | @Target(ElementType.PARAMETER) 33 | public @interface Redacted {} 34 | -------------------------------------------------------------------------------- /dataenum/src/main/java/com/spotify/dataenum/Static.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | /** 28 | * Marks a `default` method specified on a spec as `static`, which would mean that in an adt 29 | * implementation this method is going to be a static method. 30 | */ 31 | @Retention(RetentionPolicy.SOURCE) 32 | @Target(ElementType.METHOD) 33 | public @interface Static {} 34 | -------------------------------------------------------------------------------- /dataenum/src/main/java/com/spotify/dataenum/dataenum_case.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum; 21 | 22 | /** 23 | * Marker for cases in dataenum specifications. 24 | * 25 | *

Used as a dummy return type for each case of a dataenum specification. 26 | */ 27 | public enum dataenum_case {} 28 | -------------------------------------------------------------------------------- /dataenum/src/main/java/com/spotify/dataenum/function/Cases.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.function; 21 | 22 | /** Contains utility methods for creating case-specific functions. */ 23 | public final class Cases { 24 | 25 | private Cases() { 26 | // prevent instantiation 27 | } 28 | 29 | public static R illegal(String message) { 30 | throw new IllegalStateException(message); 31 | } 32 | 33 | public static R todo() { 34 | throw new UnsupportedOperationException("TODO: not implemented"); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /dataenum/src/main/java/com/spotify/dataenum/function/Consumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.function; 21 | 22 | import javax.annotation.Nonnull; 23 | 24 | /** Interface for consuming values. */ 25 | public interface Consumer { 26 | void accept(@Nonnull T value); 27 | } 28 | -------------------------------------------------------------------------------- /dataenum/src/main/java/com/spotify/dataenum/function/Function.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.function; 21 | 22 | import javax.annotation.Nonnull; 23 | 24 | /** Interface for simple functions. */ 25 | public interface Function { 26 | @Nonnull 27 | R apply(@Nonnull T value); 28 | } 29 | -------------------------------------------------------------------------------- /dataenum/src/main/java/com/spotify/dataenum/function/Supplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.function; 21 | 22 | import javax.annotation.Nonnull; 23 | 24 | /** Interface for producing values. */ 25 | public interface Supplier { 26 | @Nonnull 27 | T get(); 28 | } 29 | -------------------------------------------------------------------------------- /dataenum/src/test/java/com/spotify/dataenum/DataenumUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum; 21 | 22 | import static org.hamcrest.MatcherAssert.assertThat; 23 | import static org.hamcrest.Matchers.is; 24 | 25 | import java.util.Collections; 26 | import java.util.List; 27 | import org.junit.Rule; 28 | import org.junit.Test; 29 | import org.junit.rules.ExpectedException; 30 | 31 | public class DataenumUtilsTest { 32 | 33 | @Rule public ExpectedException thrown = ExpectedException.none(); 34 | 35 | @Test 36 | public void checkNotNullShouldThrowForNull() throws Exception { 37 | 38 | thrown.expect(NullPointerException.class); 39 | DataenumUtils.checkNotNull(null); 40 | } 41 | 42 | @Test 43 | public void checkNotNullShouldReturnInputOnNonNull() throws Exception { 44 | Object expected = new Object(); 45 | 46 | assertThat(DataenumUtils.checkNotNull(expected), is(expected)); 47 | } 48 | 49 | @Test 50 | public void equalsShouldSupportReferenceIdentity() throws Exception { 51 | Object o = new Object(); 52 | 53 | assertThat(DataenumUtils.equal(o, o), is(true)); 54 | } 55 | 56 | @Test 57 | public void equalsShouldSupportNulls() throws Exception { 58 | assertThat(DataenumUtils.equal(null, new Object()), is(false)); 59 | assertThat(DataenumUtils.equal(new Object(), null), is(false)); 60 | assertThat(DataenumUtils.equal(null, null), is(true)); 61 | } 62 | 63 | @Test 64 | public void equalsShouldFallbackToEquals() throws Exception { 65 | List one = Collections.singletonList(1); 66 | List two = Collections.singletonList(1); 67 | 68 | assertThat(DataenumUtils.equal(one, two), is(true)); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /dataenum/src/test/java/com/spotify/dataenum/function/CasesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * -\-\- 3 | * DataEnum 4 | * -- 5 | * Copyright (c) 2017 Spotify AB 6 | * -- 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * -/-/- 19 | */ 20 | package com.spotify.dataenum.function; 21 | 22 | import org.junit.Rule; 23 | import org.junit.Test; 24 | import org.junit.rules.ExpectedException; 25 | 26 | public class CasesTest { 27 | 28 | @Rule public ExpectedException thrown = ExpectedException.none(); 29 | 30 | @Test 31 | public void shouldThrowIllegalStateExceptionForIllegal() throws Exception { 32 | 33 | thrown.expect(IllegalStateException.class); 34 | thrown.expectMessage("don't do this dude"); 35 | Cases.illegal("don't do this dude"); 36 | } 37 | 38 | @Test 39 | public void shouldThrowUnsupportedOperationForTodo() throws Exception { 40 | 41 | thrown.expect(UnsupportedOperationException.class); 42 | Cases.todo(); 43 | } 44 | } 45 | --------------------------------------------------------------------------------