Skip to content

Commit

Permalink
Only import the necessary set of types (#235)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jparise authored and rahul-malik committed Aug 23, 2019
1 parent 920dcee commit fa2e15e
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 21 deletions.
1 change: 0 additions & 1 deletion Examples/Java/Sources/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions Examples/Java/Sources/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
4 changes: 0 additions & 4 deletions Examples/Java/Sources/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
1 change: 0 additions & 1 deletion Examples/Java/Sources/Pin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

interface PinAttributionObjectsMatcher<R> {
R match(@Nullable Board value0);
Expand Down
2 changes: 0 additions & 2 deletions Examples/Java/Sources/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
4 changes: 0 additions & 4 deletions Examples/Java/Sources/VariableSubtitution.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
16 changes: 11 additions & 5 deletions Sources/Core/JavaModelRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down

0 comments on commit fa2e15e

Please sign in to comment.