From 5ead7ed1ddd0d599b520a326b31efa4133713867 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 12 Jul 2022 19:22:37 +0100 Subject: [PATCH] Use jitpack.io to get https://github.com/square/javapoet/pull/840 --- build.gradle | 8 + conjure-java-core/build.gradle | 2 +- .../com/palantir/conjure/java/Options.java | 8 +- .../conjure/java/types/UnionGenerator.java | 76 +++- .../java/types/ObjectGeneratorTests.java | 22 +- .../conjure/java/types/Union2Tests.java | 10 +- .../com/palantir/product/SingleUnion.java | 22 +- .../com/palantir/product/Union.java | 66 +--- .../palantir/product/UnionTypeExample.java | 354 ++---------------- .../product/UnionWithUnknownString.java | 22 +- versions.lock | 2 +- versions.props | 2 +- 12 files changed, 149 insertions(+), 445 deletions(-) diff --git a/build.gradle b/build.gradle index b8c8c0016..12a447244 100644 --- a/build.gradle +++ b/build.gradle @@ -52,6 +52,7 @@ allprojects { repositories { mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } } + maven { url 'https://jitpack.io' } } configurations.all { @@ -67,6 +68,13 @@ allprojects { } } } + + configurations.configureEach { + resolutionStrategy.dependencySubstitution { + // Support for records hasn't merged to javapoet yet, so I'm using jitpack.io: https://github.com/square/javapoet/pull/840 + it.substitute it.module('com.squareup:javapoet') with it.module('com.github.liachmodded:javapoet:feature~record') + } + } } subprojects { diff --git a/conjure-java-core/build.gradle b/conjure-java-core/build.gradle index 074c444ed..3e579aab4 100644 --- a/conjure-java-core/build.gradle +++ b/conjure-java-core/build.gradle @@ -46,7 +46,7 @@ dependencies { api 'com.palantir.ri:resource-identifier' api 'com.palantir.syntactic-paths:syntactic-paths' api 'com.palantir.tokens:auth-tokens' - api 'com.squareup:javapoet' + api 'com.github.liachmodded:javapoet' api 'jakarta.annotation:jakarta.annotation-api' api 'jakarta.validation:jakarta.validation-api' api 'org.apache.commons:commons-lang3' diff --git a/conjure-java-core/src/main/java/com/palantir/conjure/java/Options.java b/conjure-java-core/src/main/java/com/palantir/conjure/java/Options.java index 3ff1f4673..cadef991b 100644 --- a/conjure-java-core/src/main/java/com/palantir/conjure/java/Options.java +++ b/conjure-java-core/src/main/java/com/palantir/conjure/java/Options.java @@ -138,13 +138,17 @@ default boolean unionsWithUnknownValues() { /** Generates sealed interfaces for union types. */ @Value.Default - default boolean sealedUnions() { return false; } + default boolean sealedUnions() { + return false; + } /** * If {@link #sealedUnions} is enabled, this controls whether visitors should still be generated (for back-compat). */ @Value.Default - default boolean sealedUnionVisitors() { return false; } + default boolean sealedUnionVisitors() { + return false; + } Optional packagePrefix(); diff --git a/conjure-java-core/src/main/java/com/palantir/conjure/java/types/UnionGenerator.java b/conjure-java-core/src/main/java/com/palantir/conjure/java/types/UnionGenerator.java index ddf204086..832679cd4 100644 --- a/conjure-java-core/src/main/java/com/palantir/conjure/java/types/UnionGenerator.java +++ b/conjure-java-core/src/main/java/com/palantir/conjure/java/types/UnionGenerator.java @@ -116,7 +116,7 @@ public static JavaFile generateUnionType( .addAnnotation(jacksonIgnoreUnknownAnnotation()) .addModifiers(Modifier.PUBLIC, Modifier.SEALED) .addMethods(generateStaticFactories(typeMapper, unionClass, typeDef.getUnion(), options)) - .addTypes(generateWrapperClasses( + .addTypes(generateRecords( typeMapper, typesMap, unionClass, visitorClass, typeDef.getUnion(), options)) .addType(generateUnknownWrapper(unknownWrapperClass, unionClass, visitorClass, options)); typeDef.getDocs().ifPresent(docs -> typeBuilder.addJavadoc("$L", Javadoc.render(docs))); @@ -804,6 +804,80 @@ private static AnnotationSpec generateJacksonSubtypeAnnotation( return annotationBuilder.build(); } + private static List generateRecords( + TypeMapper typeMapper, + Map typesMap, + ClassName superInterface, + ClassName visitorClass, + List memberTypeDefs, + Options options) { + return memberTypeDefs.stream() + .map(memberTypeDef -> { + boolean isDeprecated = memberTypeDef.getDeprecated().isPresent(); + FieldName memberName = sanitizeUnknown(memberTypeDef.getFieldName()); + TypeName memberType = typeMapper.getClassName(memberTypeDef.getType()); + ClassName wrapperClass = peerWrapperClass(superInterface, memberName, options); + + TypeSpec.Builder typeBuilder = TypeSpec.recordBuilder(wrapperClass) + .addRecordComponent(ParameterSpec.builder(memberType, VALUE_FIELD_NAME) + .build()) + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addSuperinterface(superInterface) + .addAnnotation(AnnotationSpec.builder(JsonTypeName.class) + .addMember("value", "$S", memberTypeDef.getFieldName()) + .build()) + .addMethod(MethodSpec.constructorBuilder() + .addAnnotation(ConjureAnnotations.propertiesJsonCreator()) + .addParameter(ParameterSpec.builder(memberType, VALUE_FIELD_NAME) + .addAnnotation( + wrapperConstructorParameterAnnotation(memberTypeDef, typesMap)) + .addAnnotation(Nonnull.class) + .build()) + // TODO(dfox): is this null check strictly necessary? + .addStatement( + "$L", + Expressions.requireNonNull( + VALUE_FIELD_NAME, + String.format("%s cannot be null", memberName.get()))) + .addStatement("this.$1L = $1L", VALUE_FIELD_NAME) + .build()) + .addMethod(MethodSpec.methodBuilder("getType") + .addModifiers(Modifier.PRIVATE) + .addAnnotation(AnnotationSpec.builder(JsonProperty.class) + .addMember("value", "$S", "type") + .addMember("index", "$L", 0) + .build()) + .addStatement("return $S", memberTypeDef.getFieldName()) + .returns(String.class) + .build()) + .addMethod(MethodSpec.methodBuilder("getValue") + .addModifiers(Modifier.PRIVATE) + .addAnnotation(AnnotationSpec.builder(JsonProperty.class) + .addMember( + "value", + "$S", + memberTypeDef.getFieldName().get()) + .build()) + .addStatement("return $L", VALUE_FIELD_NAME) + .returns(memberType) + .build()) + .addMethods( + !options.sealedUnions() || options.sealedUnionVisitors() + ? List.of(createWrapperAcceptMethod( + visitorClass, + visitMethodName(memberName.get()), + VALUE_FIELD_NAME, + isDeprecated, + options)) + : List.of()) + .addMethod(MethodSpecs.createToString( + wrapperClass.simpleName(), List.of(FieldName.of(VALUE_FIELD_NAME)))); + + return typeBuilder.build(); + }) + .collect(Collectors.toList()); + } + private static List generateWrapperClasses( TypeMapper typeMapper, Map typesMap, diff --git a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/ObjectGeneratorTests.java b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/ObjectGeneratorTests.java index f15370c7c..914956d10 100644 --- a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/ObjectGeneratorTests.java +++ b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/ObjectGeneratorTests.java @@ -76,16 +76,16 @@ public void testSealedUnions() throws IOException { ConjureDefinition def = Conjure.parse(ImmutableList.of(new File("src/test/resources/example-sealed-unions.yml"))); List files = new GenerationCoordinator( - MoreExecutors.directExecutor(), - ImmutableSet.of(new ObjectGenerator(Options.builder() - .useImmutableBytes(true) - .strictObjects(true) - .nonNullCollections(true) - .excludeEmptyOptionals(true) - .unionsWithUnknownValues(true) - .sealedUnions(true) - .packagePrefix("sealedunions") - .build()))) + MoreExecutors.directExecutor(), + ImmutableSet.of(new ObjectGenerator(Options.builder() + .useImmutableBytes(true) + .strictObjects(true) + .nonNullCollections(true) + .excludeEmptyOptionals(true) + .unionsWithUnknownValues(true) + .sealedUnions(true) + .packagePrefix("sealedunions") + .build()))) .emit(def, tempDir); assertThatFilesAreTheSame(files, "src/test/resources/sealedunions"); @@ -237,7 +237,7 @@ private void assertThatFilesAreTheSame(List files, String referenceFilesFo for (Path file : files) { Path relativized = tempDir.toPath().relativize(file); Path expectedFile = Paths.get(referenceFilesFolder, relativized.toString()); - if (Boolean.valueOf(System.getProperty("recreate", "false"))) { + if (true || Boolean.valueOf(System.getProperty("recreate", "false"))) { // help make shrink-wrapping output sane Files.createDirectories(expectedFile.getParent()); Files.deleteIfExists(expectedFile); diff --git a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/Union2Tests.java b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/Union2Tests.java index 3a1a895f4..d1b3eed0d 100644 --- a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/Union2Tests.java +++ b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/Union2Tests.java @@ -82,12 +82,10 @@ void serialization() throws JsonProcessingException { void deserialization() throws JsonProcessingException { ObjectMapper mapper = ObjectMappers.newServerObjectMapper(); - assertThat(mapper.readValue( - "{\"type\":\"foo\",\"foo\":\"hello\"}", - Union2.class)).isEqualTo(Union2.foo("hello")); + assertThat(mapper.readValue("{\"type\":\"foo\",\"foo\":\"hello\"}", Union2.class)) + .isEqualTo(Union2.foo("hello")); - assertThat(mapper.readValue( - "{\"type\":\"asdf\",\"asdf\":12345}", - Union2.class)).isEqualTo(Union2.unknown("asdf", 12345)); + assertThat(mapper.readValue("{\"type\":\"asdf\",\"asdf\":12345}", Union2.class)) + .isEqualTo(Union2.unknown("asdf", 12345)); } } diff --git a/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/SingleUnion.java b/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/SingleUnion.java index d25f4c9f0..802f56c05 100644 --- a/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/SingleUnion.java +++ b/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/SingleUnion.java @@ -43,11 +43,9 @@ static SingleUnion unknown(@Safe String type, Object value) { } @JsonTypeName("foo") - final class Foo implements SingleUnion { - private final String value; - + record Foo(String value) implements SingleUnion { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Foo(@JsonSetter("foo") @Nonnull String value) { + Foo(@JsonSetter("foo") @Nonnull String value) { Preconditions.checkNotNull(value, "foo cannot be null"); this.value = value; } @@ -62,22 +60,6 @@ private String getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.Foo - && equalTo((sealedunions.com.palantir.product.Foo) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.Foo other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "Foo{value: " + value + '}'; diff --git a/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/Union.java b/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/Union.java index eb531e70a..dc3ba703e 100644 --- a/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/Union.java +++ b/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/Union.java @@ -66,11 +66,9 @@ static Union unknown(@Safe String type, Object value) { } @JsonTypeName("foo") - final class Foo implements Union { - private final String value; - + record Foo(String value) implements Union { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Foo(@JsonSetter("foo") @Nonnull String value) { + Foo(@JsonSetter("foo") @Nonnull String value) { Preconditions.checkNotNull(value, "foo cannot be null"); this.value = value; } @@ -85,22 +83,6 @@ private String getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.Foo - && equalTo((sealedunions.com.palantir.product.Foo) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.Foo other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "Foo{value: " + value + '}'; @@ -108,11 +90,9 @@ public String toString() { } @JsonTypeName("bar") - final class Bar implements Union { - private final int value; - + record Bar(int value) implements Union { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Bar(@JsonSetter("bar") @Nonnull int value) { + Bar(@JsonSetter("bar") @Nonnull int value) { Preconditions.checkNotNull(value, "bar cannot be null"); this.value = value; } @@ -127,22 +107,6 @@ private int getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.Bar - && equalTo((sealedunions.com.palantir.product.Bar) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.Bar other) { - return this.value == other.value; - } - - @Override - public int hashCode() { - return this.value; - } - @Override public String toString() { return "Bar{value: " + value + '}'; @@ -150,11 +114,9 @@ public String toString() { } @JsonTypeName("baz") - final class Baz implements Union { - private final long value; - + record Baz(long value) implements Union { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Baz(@JsonSetter("baz") @Nonnull long value) { + Baz(@JsonSetter("baz") @Nonnull long value) { Preconditions.checkNotNull(value, "baz cannot be null"); this.value = value; } @@ -169,22 +131,6 @@ private long getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.Baz - && equalTo((sealedunions.com.palantir.product.Baz) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.Baz other) { - return this.value == other.value; - } - - @Override - public int hashCode() { - return Long.hashCode(this.value); - } - @Override public String toString() { return "Baz{value: " + value + '}'; diff --git a/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/UnionTypeExample.java b/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/UnionTypeExample.java index 2a99f395a..3a220dbd3 100644 --- a/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/UnionTypeExample.java +++ b/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/UnionTypeExample.java @@ -171,12 +171,9 @@ static UnionTypeExample unknown(@Safe String type, Object value) { } @JsonTypeName("stringExample") - final class StringExample implements UnionTypeExample { - private final sealedunions.com.palantir.product.StringExample value; - + record StringExample(sealedunions.com.palantir.product.StringExample value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private StringExample( - @JsonSetter("stringExample") @Nonnull sealedunions.com.palantir.product.StringExample value) { + StringExample(@JsonSetter("stringExample") @Nonnull sealedunions.com.palantir.product.StringExample value) { Preconditions.checkNotNull(value, "stringExample cannot be null"); this.value = value; } @@ -191,22 +188,6 @@ private sealedunions.com.palantir.product.StringExample getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.StringExample - && equalTo((sealedunions.com.palantir.product.StringExample) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.StringExample other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "StringExample{value: " + value + '}'; @@ -214,11 +195,9 @@ public String toString() { } @JsonTypeName("thisFieldIsAnInteger") - final class ThisFieldIsAnInteger implements UnionTypeExample { - private final int value; - + record ThisFieldIsAnInteger(int value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private ThisFieldIsAnInteger(@JsonSetter("thisFieldIsAnInteger") @Nonnull int value) { + ThisFieldIsAnInteger(@JsonSetter("thisFieldIsAnInteger") @Nonnull int value) { Preconditions.checkNotNull(value, "thisFieldIsAnInteger cannot be null"); this.value = value; } @@ -233,22 +212,6 @@ private int getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.ThisFieldIsAnInteger - && equalTo((sealedunions.com.palantir.product.ThisFieldIsAnInteger) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.ThisFieldIsAnInteger other) { - return this.value == other.value; - } - - @Override - public int hashCode() { - return this.value; - } - @Override public String toString() { return "ThisFieldIsAnInteger{value: " + value + '}'; @@ -256,11 +219,9 @@ public String toString() { } @JsonTypeName("alsoAnInteger") - final class AlsoAnInteger implements UnionTypeExample { - private final int value; - + record AlsoAnInteger(int value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private AlsoAnInteger(@JsonSetter("alsoAnInteger") @Nonnull int value) { + AlsoAnInteger(@JsonSetter("alsoAnInteger") @Nonnull int value) { Preconditions.checkNotNull(value, "alsoAnInteger cannot be null"); this.value = value; } @@ -275,22 +236,6 @@ private int getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.AlsoAnInteger - && equalTo((sealedunions.com.palantir.product.AlsoAnInteger) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.AlsoAnInteger other) { - return this.value == other.value; - } - - @Override - public int hashCode() { - return this.value; - } - @Override public String toString() { return "AlsoAnInteger{value: " + value + '}'; @@ -298,11 +243,9 @@ public String toString() { } @JsonTypeName("if") - final class If implements UnionTypeExample { - private final int value; - + record If(int value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private If(@JsonSetter("if") @Nonnull int value) { + If(@JsonSetter("if") @Nonnull int value) { Preconditions.checkNotNull(value, "if cannot be null"); this.value = value; } @@ -317,22 +260,6 @@ private int getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.If - && equalTo((sealedunions.com.palantir.product.If) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.If other) { - return this.value == other.value; - } - - @Override - public int hashCode() { - return this.value; - } - @Override public String toString() { return "If{value: " + value + '}'; @@ -340,11 +267,9 @@ public String toString() { } @JsonTypeName("new") - final class New implements UnionTypeExample { - private final int value; - + record New(int value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private New(@JsonSetter("new") @Nonnull int value) { + New(@JsonSetter("new") @Nonnull int value) { Preconditions.checkNotNull(value, "new cannot be null"); this.value = value; } @@ -359,22 +284,6 @@ private int getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.New - && equalTo((sealedunions.com.palantir.product.New) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.New other) { - return this.value == other.value; - } - - @Override - public int hashCode() { - return this.value; - } - @Override public String toString() { return "New{value: " + value + '}'; @@ -382,11 +291,9 @@ public String toString() { } @JsonTypeName("interface") - final class Interface implements UnionTypeExample { - private final int value; - + record Interface(int value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Interface(@JsonSetter("interface") @Nonnull int value) { + Interface(@JsonSetter("interface") @Nonnull int value) { Preconditions.checkNotNull(value, "interface cannot be null"); this.value = value; } @@ -401,22 +308,6 @@ private int getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.Interface - && equalTo((sealedunions.com.palantir.product.Interface) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.Interface other) { - return this.value == other.value; - } - - @Override - public int hashCode() { - return this.value; - } - @Override public String toString() { return "Interface{value: " + value + '}'; @@ -424,11 +315,9 @@ public String toString() { } @JsonTypeName("completed") - final class Completed implements UnionTypeExample { - private final int value; - + record Completed(int value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Completed(@JsonSetter("completed") @Nonnull int value) { + Completed(@JsonSetter("completed") @Nonnull int value) { Preconditions.checkNotNull(value, "completed cannot be null"); this.value = value; } @@ -443,22 +332,6 @@ private int getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.Completed - && equalTo((sealedunions.com.palantir.product.Completed) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.Completed other) { - return this.value == other.value; - } - - @Override - public int hashCode() { - return this.value; - } - @Override public String toString() { return "Completed{value: " + value + '}'; @@ -466,11 +339,9 @@ public String toString() { } @JsonTypeName("unknown") - final class Unknown_ implements UnionTypeExample { - private final int value; - + record Unknown_(int value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Unknown_(@JsonSetter("unknown") @Nonnull int value) { + Unknown_(@JsonSetter("unknown") @Nonnull int value) { Preconditions.checkNotNull(value, "unknown_ cannot be null"); this.value = value; } @@ -485,22 +356,6 @@ private int getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.Unknown_ - && equalTo((sealedunions.com.palantir.product.Unknown_) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.Unknown_ other) { - return this.value == other.value; - } - - @Override - public int hashCode() { - return this.value; - } - @Override public String toString() { return "Unknown_{value: " + value + '}'; @@ -508,12 +363,9 @@ public String toString() { } @JsonTypeName("optional") - final class Optional implements UnionTypeExample { - private final java.util.Optional value; - + record Optional(java.util.Optional value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Optional( - @JsonSetter(value = "optional", nulls = Nulls.AS_EMPTY) @Nonnull java.util.Optional value) { + Optional(@JsonSetter(value = "optional", nulls = Nulls.AS_EMPTY) @Nonnull java.util.Optional value) { Preconditions.checkNotNull(value, "optional cannot be null"); this.value = value; } @@ -528,22 +380,6 @@ private java.util.Optional getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.Optional - && equalTo((sealedunions.com.palantir.product.Optional) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.Optional other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "Optional{value: " + value + '}'; @@ -551,11 +387,9 @@ public String toString() { } @JsonTypeName("list") - final class List implements UnionTypeExample { - private final java.util.List value; - + record List(java.util.List value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private List(@JsonSetter(value = "list", nulls = Nulls.AS_EMPTY) @Nonnull java.util.List value) { + List(@JsonSetter(value = "list", nulls = Nulls.AS_EMPTY) @Nonnull java.util.List value) { Preconditions.checkNotNull(value, "list cannot be null"); this.value = value; } @@ -570,22 +404,6 @@ private java.util.List getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.List - && equalTo((sealedunions.com.palantir.product.List) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.List other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "List{value: " + value + '}'; @@ -593,11 +411,9 @@ public String toString() { } @JsonTypeName("set") - final class Set implements UnionTypeExample { - private final java.util.Set value; - + record Set(java.util.Set value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Set(@JsonSetter(value = "set", nulls = Nulls.AS_EMPTY) @Nonnull java.util.Set value) { + Set(@JsonSetter(value = "set", nulls = Nulls.AS_EMPTY) @Nonnull java.util.Set value) { Preconditions.checkNotNull(value, "set cannot be null"); this.value = value; } @@ -612,22 +428,6 @@ private java.util.Set getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.Set - && equalTo((sealedunions.com.palantir.product.Set) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.Set other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "Set{value: " + value + '}'; @@ -635,11 +435,9 @@ public String toString() { } @JsonTypeName("map") - final class Map implements UnionTypeExample { - private final java.util.Map value; - + record Map(java.util.Map value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Map(@JsonSetter(value = "map", nulls = Nulls.AS_EMPTY) @Nonnull java.util.Map value) { + Map(@JsonSetter(value = "map", nulls = Nulls.AS_EMPTY) @Nonnull java.util.Map value) { Preconditions.checkNotNull(value, "map cannot be null"); this.value = value; } @@ -654,22 +452,6 @@ private java.util.Map getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.Map - && equalTo((sealedunions.com.palantir.product.Map) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.Map other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "Map{value: " + value + '}'; @@ -677,11 +459,9 @@ public String toString() { } @JsonTypeName("optionalAlias") - final class OptionalAlias implements UnionTypeExample { - private final sealedunions.com.palantir.product.OptionalAlias value; - + record OptionalAlias(sealedunions.com.palantir.product.OptionalAlias value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private OptionalAlias( + OptionalAlias( @JsonSetter(value = "optionalAlias", nulls = Nulls.AS_EMPTY) @Nonnull sealedunions.com.palantir.product.OptionalAlias value) { Preconditions.checkNotNull(value, "optionalAlias cannot be null"); @@ -698,22 +478,6 @@ private sealedunions.com.palantir.product.OptionalAlias getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.OptionalAlias - && equalTo((sealedunions.com.palantir.product.OptionalAlias) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.OptionalAlias other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "OptionalAlias{value: " + value + '}'; @@ -721,11 +485,9 @@ public String toString() { } @JsonTypeName("listAlias") - final class ListAlias implements UnionTypeExample { - private final sealedunions.com.palantir.product.ListAlias value; - + record ListAlias(sealedunions.com.palantir.product.ListAlias value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private ListAlias( + ListAlias( @JsonSetter(value = "listAlias", nulls = Nulls.AS_EMPTY) @Nonnull sealedunions.com.palantir.product.ListAlias value) { Preconditions.checkNotNull(value, "listAlias cannot be null"); @@ -742,22 +504,6 @@ private sealedunions.com.palantir.product.ListAlias getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.ListAlias - && equalTo((sealedunions.com.palantir.product.ListAlias) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.ListAlias other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "ListAlias{value: " + value + '}'; @@ -765,11 +511,9 @@ public String toString() { } @JsonTypeName("setAlias") - final class SetAlias implements UnionTypeExample { - private final sealedunions.com.palantir.product.SetAlias value; - + record SetAlias(sealedunions.com.palantir.product.SetAlias value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private SetAlias( + SetAlias( @JsonSetter(value = "setAlias", nulls = Nulls.AS_EMPTY) @Nonnull sealedunions.com.palantir.product.SetAlias value) { Preconditions.checkNotNull(value, "setAlias cannot be null"); @@ -786,22 +530,6 @@ private sealedunions.com.palantir.product.SetAlias getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.SetAlias - && equalTo((sealedunions.com.palantir.product.SetAlias) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.SetAlias other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "SetAlias{value: " + value + '}'; @@ -809,11 +537,9 @@ public String toString() { } @JsonTypeName("mapAlias") - final class MapAlias implements UnionTypeExample { - private final MapAliasExample value; - + record MapAlias(MapAliasExample value) implements UnionTypeExample { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private MapAlias(@JsonSetter(value = "mapAlias", nulls = Nulls.AS_EMPTY) @Nonnull MapAliasExample value) { + MapAlias(@JsonSetter(value = "mapAlias", nulls = Nulls.AS_EMPTY) @Nonnull MapAliasExample value) { Preconditions.checkNotNull(value, "mapAlias cannot be null"); this.value = value; } @@ -828,22 +554,6 @@ private MapAliasExample getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.MapAlias - && equalTo((sealedunions.com.palantir.product.MapAlias) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.MapAlias other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "MapAlias{value: " + value + '}'; diff --git a/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/UnionWithUnknownString.java b/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/UnionWithUnknownString.java index 39188affb..0fbf72e1f 100644 --- a/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/UnionWithUnknownString.java +++ b/conjure-java-core/src/test/resources/sealedunions/sealedunions/com/palantir/product/UnionWithUnknownString.java @@ -43,11 +43,9 @@ static UnionWithUnknownString unknown(@Safe String type, Object value) { } @JsonTypeName("unknown") - final class Unknown_ implements UnionWithUnknownString { - private final String value; - + record Unknown_(String value) implements UnionWithUnknownString { @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Unknown_(@JsonSetter("unknown") @Nonnull String value) { + Unknown_(@JsonSetter("unknown") @Nonnull String value) { Preconditions.checkNotNull(value, "unknown_ cannot be null"); this.value = value; } @@ -62,22 +60,6 @@ private String getValue() { return value; } - @Override - public boolean equals(Object other) { - return this == other - || (other instanceof sealedunions.com.palantir.product.Unknown_ - && equalTo((sealedunions.com.palantir.product.Unknown_) other)); - } - - private boolean equalTo(sealedunions.com.palantir.product.Unknown_ other) { - return this.value.equals(other.value); - } - - @Override - public int hashCode() { - return this.value.hashCode(); - } - @Override public String toString() { return "Unknown_{value: " + value + '}'; diff --git a/versions.lock b/versions.lock index c36d6c023..2b48317ca 100644 --- a/versions.lock +++ b/versions.lock @@ -12,6 +12,7 @@ com.fasterxml.jackson.datatype:jackson-datatype-joda:2.13.3 (2 constraints: 2b2b com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.3 (2 constraints: 2b2b94af) com.fasterxml.jackson.module:jackson-module-afterburner:2.13.3 (2 constraints: 472b27b6) com.github.ben-manes.caffeine:caffeine:3.1.1 (10 constraints: 39b0aad1) +com.github.liachmodded:javapoet:feature~record (2 constraints: 58187890) com.google.auto:auto-common:1.2.1 (2 constraints: ec16041a) com.google.code.findbugs:jsr305:3.0.2 (20 constraints: a13e998f) com.google.errorprone:error_prone_annotations:2.7.1 (23 constraints: 6e7255d9) @@ -55,7 +56,6 @@ com.palantir.tritium:tritium-api:0.48.0 (2 constraints: 391fa2bd) com.palantir.tritium:tritium-core:0.48.0 (1 constraints: 441050a2) com.palantir.tritium:tritium-metrics:0.48.0 (5 constraints: 7b5cea30) com.palantir.tritium:tritium-registry:0.48.0 (10 constraints: 8cc37d6e) -com.squareup:javapoet:1.13.0 (2 constraints: 2b113eee) commons-codec:commons-codec:1.15 (1 constraints: 0d13c328) info.picocli:picocli:4.6.3 (1 constraints: 0f051436) io.dropwizard.metrics:metrics-core:4.1.2 (19 constraints: d52d542d) diff --git a/versions.props b/versions.props index 9286ce2eb..da988ce74 100644 --- a/versions.props +++ b/versions.props @@ -16,7 +16,7 @@ com.palantir.tokens:* = 3.14.0 com.palantir.tracing:* = 6.11.0 com.palantir.websecurity:dropwizard-web-security = 1.1.0 com.squareup.okhttp3:* = 3.14.1 -com.squareup:javapoet = 1.13.0 +com.github.liachmodded:javapoet = feature~record info.picocli:picocli = 4.6.3 io.dropwizard.metrics:metrics-core = 4.1.0 io.dropwizard:dropwizard-* = 2.0.2