From 5073decfb292a31328765d9deaa8d0ca02ee8ab2 Mon Sep 17 00:00:00 2001 From: Hunter Mellema Date: Fri, 26 Apr 2024 09:09:38 -0600 Subject: [PATCH] Downgrade codegen and remaining subprojects to JDK 17 --- .github/workflows/ci.yml | 2 +- .../smithy-java.java-conventions.gradle | 6 +-- codegen/build.gradle | 4 -- .../java/codegen/JavaSymbolProvider.java | 11 +++-- .../generators/TraitInitializerGenerator.java | 23 ++++++---- .../javadoc/JavadocInjectorInterceptor.java | 13 +++--- .../java/codegen/writer/JavaWriter.java | 43 +++++++++++-------- config/spotless/formatting.xml | 4 +- examples/restjson-example/build.gradle | 4 -- 9 files changed, 58 insertions(+), 52 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3485dcfac..94fe8d349 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: name: Java ${{ matrix.java }} ${{ matrix.os }} strategy: matrix: - java: [21] + java: [17] os: [macos-latest, ubuntu-latest, windows-latest] steps: diff --git a/buildSrc/src/main/groovy/smithy-java.java-conventions.gradle b/buildSrc/src/main/groovy/smithy-java.java-conventions.gradle index 7f02af220..a668996a9 100644 --- a/buildSrc/src/main/groovy/smithy-java.java-conventions.gradle +++ b/buildSrc/src/main/groovy/smithy-java.java-conventions.gradle @@ -10,14 +10,10 @@ plugins { java { toolchain { - languageVersion = JavaLanguageVersion.of(21) + languageVersion = JavaLanguageVersion.of(17) } } -tasks.compileJava { - options.release = 17 -} - /* * Common test configuration * =============================== diff --git a/codegen/build.gradle b/codegen/build.gradle index d9e559fc3..e1f9f983a 100644 --- a/codegen/build.gradle +++ b/codegen/build.gradle @@ -4,10 +4,6 @@ plugins { description = "This module provides the codegen functionality for Smithy java" -tasks.compileJava { - options.release = 21 -} - ext { displayName = "Smithy :: Java :: Codegen" moduleName = "software.amazon.smithy.smithy-java.codegen" diff --git a/codegen/src/main/java/software/amazon/smithy/java/codegen/JavaSymbolProvider.java b/codegen/src/main/java/software/amazon/smithy/java/codegen/JavaSymbolProvider.java index c52d1aa5b..0a58509b0 100644 --- a/codegen/src/main/java/software/amazon/smithy/java/codegen/JavaSymbolProvider.java +++ b/codegen/src/main/java/software/amazon/smithy/java/codegen/JavaSymbolProvider.java @@ -9,12 +9,14 @@ import java.time.Instant; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.SequencedSet; +import java.util.Set; +import java.util.SortedSet; import software.amazon.smithy.codegen.core.CodegenException; import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.codegen.core.SymbolProvider; @@ -48,6 +50,7 @@ import software.amazon.smithy.model.shapes.UnionShape; import software.amazon.smithy.model.traits.StreamingTrait; import software.amazon.smithy.model.traits.UniqueItemsTrait; +import software.amazon.smithy.utils.BuilderRef; import software.amazon.smithy.utils.CaseUtils; /** @@ -112,11 +115,11 @@ public Symbol booleanShape(BooleanShape booleanShape) { public Symbol listShape(ListShape listShape) { // Lists with unique Items are treated as Sequenced Sets if (listShape.hasTrait(UniqueItemsTrait.class)) { - return SymbolUtils.fromClass(SequencedSet.class) + return SymbolUtils.fromClass(Set.class) .toBuilder() - .putProperty(SymbolProperties.COLLECTION_COPY_METHOD, "unmodifiableSequencedSet") + .putProperty(SymbolProperties.COLLECTION_COPY_METHOD, "unmodifiableSet") .putProperty(SymbolProperties.COLLECTION_IMPLEMENTATION_CLASS, LinkedHashSet.class) - .putProperty(SymbolProperties.COLLECTION_EMPTY_METHOD, "emptySortedSet()") + .putProperty(SymbolProperties.COLLECTION_EMPTY_METHOD, "emptySet()") .addReference(listShape.getMember().accept(this)) .build(); } diff --git a/codegen/src/main/java/software/amazon/smithy/java/codegen/generators/TraitInitializerGenerator.java b/codegen/src/main/java/software/amazon/smithy/java/codegen/generators/TraitInitializerGenerator.java index 045039502..b6d9a48a6 100644 --- a/codegen/src/main/java/software/amazon/smithy/java/codegen/generators/TraitInitializerGenerator.java +++ b/codegen/src/main/java/software/amazon/smithy/java/codegen/generators/TraitInitializerGenerator.java @@ -50,17 +50,22 @@ public void run() { var iter = traitsToAdd.iterator(); while (iter.hasNext()) { var traitId = iter.next(); - switch (shape.getAllTraits().get(traitId)) { - case AnnotationTrait a -> writer.writeInline("new $T()", a.getClass()); - case StringTrait st -> writer.writeInline("new $T($S)", st.getClass(), st.getValue()); - case StringListTrait slt -> writer.writeInline( - "new $T($S, $T.NONE)", - slt.getClass(), - slt.getValues(), - SourceLocation.class + Trait trait = shape.getAllTraits().get(traitId); + if (trait instanceof AnnotationTrait) { + writer.writeInline("new $T()", trait.getClass()); + } else if (trait instanceof StringTrait) { + writer.writeInline("new $T($S)", trait.getClass(), ((StringTrait) trait).getValue()); + } else if (trait instanceof StringListTrait) { + writer.writeInline( + "new $T($S, $T.NONE)", + trait.getClass(), + ((StringListTrait) trait).getValues(), + SourceLocation.class ); - case Trait t -> traitFactoryInitializer(writer, t); + } else { + traitFactoryInitializer(writer, trait); } + if (iter.hasNext()) { writer.writeInline(",").newLine(); } diff --git a/codegen/src/main/java/software/amazon/smithy/java/codegen/integrations/javadoc/JavadocInjectorInterceptor.java b/codegen/src/main/java/software/amazon/smithy/java/codegen/integrations/javadoc/JavadocInjectorInterceptor.java index 035714911..9bf5d82b2 100644 --- a/codegen/src/main/java/software/amazon/smithy/java/codegen/integrations/javadoc/JavadocInjectorInterceptor.java +++ b/codegen/src/main/java/software/amazon/smithy/java/codegen/integrations/javadoc/JavadocInjectorInterceptor.java @@ -37,11 +37,14 @@ public boolean isIntercepted(CodeSection section) { @Override public void prepend(JavaWriter writer, CodeSection section) { - Shape shape = switch (section) { - case ClassSection s -> s.shape(); - case GetterSection s -> s.memberShape(); - default -> throw new IllegalArgumentException("Javadocs cannot be injected for section: " + section); - }; + Shape shape; + if (section instanceof ClassSection) { + shape = ((ClassSection) section).shape(); + } else if (section instanceof GetterSection) { + shape = ((GetterSection) section).memberShape(); + } else { + throw new IllegalArgumentException("Javadocs cannot be injected for section: " + section); + } writer.injectSection(new JavadocSection(shape)); diff --git a/codegen/src/main/java/software/amazon/smithy/java/codegen/writer/JavaWriter.java b/codegen/src/main/java/software/amazon/smithy/java/codegen/writer/JavaWriter.java index e7faba805..39d1804c2 100644 --- a/codegen/src/main/java/software/amazon/smithy/java/codegen/writer/JavaWriter.java +++ b/codegen/src/main/java/software/amazon/smithy/java/codegen/writer/JavaWriter.java @@ -143,15 +143,19 @@ public JavaWriter apply(String filename, String namespace) { private final class JavaTypeFormatter implements BiFunction { @Override public String apply(Object type, String indent) { - Symbol typeSymbol = switch (type) { - case Symbol s -> s; - case Class c -> SymbolUtils.fromClass(c); - case SymbolReference r -> r.getSymbol(); - default -> throw new IllegalArgumentException( - "Invalid type provided for $T. Expected a Symbol or Class" - + " but found: `" + type + "`." - ); - }; + + Symbol typeSymbol; + if (type instanceof Symbol) { + typeSymbol = (Symbol) type; + } else if (type instanceof Class) { + typeSymbol = SymbolUtils.fromClass((Class) type); + } else if (type instanceof SymbolReference) { + typeSymbol = ((SymbolReference) type).getSymbol(); + } else { + throw new IllegalArgumentException( + "Invalid type provided for $T. Expected a Symbol or Class" + + " but found: `" + type + "`."); + } if (typeSymbol.getReferences().isEmpty()) { return getPlaceholder(typeSymbol); @@ -187,15 +191,18 @@ private final class BoxedTypeFormatter implements BiFunction s; - case Class c -> SymbolUtils.fromClass(c); - case SymbolReference r -> r.getSymbol(); - default -> throw new IllegalArgumentException( - "Invalid type provided for $B. Expected a Symbol or Class" - + " but found: `" + type + "`." - ); - }; + Symbol typeSymbol; + if (type instanceof Symbol) { + typeSymbol = (Symbol) type; + } else if (type instanceof Class) { + typeSymbol = SymbolUtils.fromClass((Class) type); + } else if (type instanceof SymbolReference) { + typeSymbol = ((SymbolReference) type).getSymbol(); + } else { + throw new IllegalArgumentException( + "Invalid type provided for $B. Expected a Symbol or Class" + + " but found: `" + type + "`."); + } if (typeSymbol.getProperty(SymbolProperties.BOXED_TYPE).isPresent()) { typeSymbol = typeSymbol.expectProperty(SymbolProperties.BOXED_TYPE, Symbol.class); diff --git a/config/spotless/formatting.xml b/config/spotless/formatting.xml index 1b47322fb..e1fcbf9f2 100644 --- a/config/spotless/formatting.xml +++ b/config/spotless/formatting.xml @@ -1,6 +1,6 @@ - - + + diff --git a/examples/restjson-example/build.gradle b/examples/restjson-example/build.gradle index 9dbee0fb4..5530d9193 100644 --- a/examples/restjson-example/build.gradle +++ b/examples/restjson-example/build.gradle @@ -3,10 +3,6 @@ plugins { alias(libs.plugins.jmh) } -tasks.compileJava { - options.release = 21 -} - dependencies { api(project(":client-aws-rest-json1")) api(libs.smithy.aws.traits)