From fa2e15ed594bff460bc2defaedc825ba5bdddbf6 Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Fri, 23 Aug 2019 10:53:06 -0700 Subject: [PATCH] Only import the necessary set of types (#235) We were previously importing the java.util.{Date,List,Map,Set} types unconditionally. We now detect which types are needed based on walking the schema and only emitting imports for the types that are actually used. --- Examples/Java/Sources/Board.java | 1 - Examples/Java/Sources/Image.java | 4 ---- Examples/Java/Sources/Model.java | 4 ---- Examples/Java/Sources/Pin.java | 1 - Examples/Java/Sources/User.java | 2 -- Examples/Java/Sources/VariableSubtitution.java | 4 ---- Sources/Core/JavaModelRenderer.swift | 16 +++++++++++----- 7 files changed, 11 insertions(+), 21 deletions(-) diff --git a/Examples/Java/Sources/Board.java b/Examples/Java/Sources/Board.java index e5e23c4a..b9dfb6cf 100644 --- a/Examples/Java/Sources/Board.java +++ b/Examples/Java/Sources/Board.java @@ -20,7 +20,6 @@ import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.util.Date; -import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; diff --git a/Examples/Java/Sources/Image.java b/Examples/Java/Sources/Image.java index 767e00b9..d8c691cf 100644 --- a/Examples/Java/Sources/Image.java +++ b/Examples/Java/Sources/Image.java @@ -19,11 +19,7 @@ import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; import java.io.IOException; -import java.util.Date; -import java.util.List; -import java.util.Map; import java.util.Objects; -import java.util.Set; public class Image { diff --git a/Examples/Java/Sources/Model.java b/Examples/Java/Sources/Model.java index f25e0231..a25034b3 100644 --- a/Examples/Java/Sources/Model.java +++ b/Examples/Java/Sources/Model.java @@ -19,11 +19,7 @@ import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; import java.io.IOException; -import java.util.Date; -import java.util.List; -import java.util.Map; import java.util.Objects; -import java.util.Set; public class Model { diff --git a/Examples/Java/Sources/Pin.java b/Examples/Java/Sources/Pin.java index 4ed74776..5f773c4e 100644 --- a/Examples/Java/Sources/Pin.java +++ b/Examples/Java/Sources/Pin.java @@ -23,7 +23,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Set; interface PinAttributionObjectsMatcher { R match(@Nullable Board value0); diff --git a/Examples/Java/Sources/User.java b/Examples/Java/Sources/User.java index 677912d4..c8c04f22 100644 --- a/Examples/Java/Sources/User.java +++ b/Examples/Java/Sources/User.java @@ -20,10 +20,8 @@ import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.util.Date; -import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Set; public class User { diff --git a/Examples/Java/Sources/VariableSubtitution.java b/Examples/Java/Sources/VariableSubtitution.java index 756ec291..f5cbb69b 100644 --- a/Examples/Java/Sources/VariableSubtitution.java +++ b/Examples/Java/Sources/VariableSubtitution.java @@ -19,11 +19,7 @@ import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; import java.io.IOException; -import java.util.Date; -import java.util.List; -import java.util.Map; import java.util.Objects; -import java.util.Set; public class VariableSubtitution { diff --git a/Sources/Core/JavaModelRenderer.swift b/Sources/Core/JavaModelRenderer.swift index 1c9133b4..65746c33 100644 --- a/Sources/Core/JavaModelRenderer.swift +++ b/Sources/Core/JavaModelRenderer.swift @@ -389,6 +389,16 @@ public struct JavaModelRenderer: JavaFileRenderer { fatalError("java_nullability_annotation_type must be either android-support or androidx. Invalid type provided: " + params[.javaNullabilityAnnotationType]!) } + let propertyTypeImports = properties.compactMap { (_, prop) -> String? in + switch prop.schema { + case .array: return "java.util.List" + case .map: return "java.util.Map" + case .set: return "java.util.Set" + case .string(format: .some(.dateTime)): return "java.util.Date" + default: return nil + } + } + let imports = [ JavaIR.Root.imports(names: Set([ "com.google.gson.Gson", @@ -400,14 +410,10 @@ public struct JavaModelRenderer: JavaFileRenderer { "com.google.gson.stream.JsonToken", "com.google.gson.stream.JsonWriter", "java.io.IOException", - "java.util.Date", - "java.util.Map", - "java.util.Set", - "java.util.List", "java.util.Objects", nullabilityAnnotationType.package + ".NonNull", nullabilityAnnotationType.package + ".Nullable", - ] + (self.decorations.imports ?? []))), + ] + propertyTypeImports + (self.decorations.imports ?? []))), ] let enumProps = properties.flatMap { (param, prop) -> [JavaIR.Enum] in