Skip to content

Commit 59ca22d

Browse files
committed
Correct usage of TypeTokens
1 parent 5889ff7 commit 59ca22d

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package com.tw.go.config.json;
22

33

4+
import com.google.gson.Gson;
45
import com.google.gson.GsonBuilder;
56
import com.google.gson.reflect.TypeToken;
67

8+
import java.util.Map;
9+
710
class JSONUtils {
8-
static <T> T fromJSON(String json) {
9-
return new GsonBuilder().create().fromJson(json, new TypeToken<T>() {}.getType());
11+
12+
private static final Gson GSON = new GsonBuilder().create();
13+
14+
static Map<String, String> fromJSON(String json) {
15+
return GSON.fromJson(json, new TypeToken<Map<String, String>>() {}.getType());
1016
}
1117

1218
static String toJSON(Object object) {
13-
return new GsonBuilder().create().toJson(object);
19+
return GSON.toJson(object);
1420
}
1521
}

src/main/java/com.tw.go.config.json/JsonConfigPlugin.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private GoPluginApiResponse handleParseContentRequest(GoPluginApiRequest request
158158
return handlingErrors(() -> {
159159
ParsedRequest parsed = ParsedRequest.parse(request);
160160
FilenameMatcher matcher = new FilenameMatcher(getPipelinePattern(parsed), getEnvironmentPattern(parsed));
161-
Map<String, String> contents = parsed.getParam("contents");
161+
Map<String, String> contents = parsed.getParam("contents", String.class);
162162

163163
JsonConfigCollection result = new JsonConfigCollection();
164164
contents.forEach((filename, content) -> {
@@ -190,7 +190,7 @@ private GoPluginApiResponse handlePipelineExportRequest(GoPluginApiRequest reque
190190
return handlingErrors(() -> {
191191
ParsedRequest parsed = ParsedRequest.parse(request);
192192

193-
Map<String, Object> pipeline = parsed.getParam("pipeline");
193+
Map<String, Object> pipeline = parsed.getParam("pipeline", Object.class);
194194
String name = (String) pipeline.get("name");
195195

196196
DefaultGoPluginApiResponse response = success(gson.toJson(Collections.singletonMap("pipeline", prettyPrint.toJson(pipeline))));

src/main/java/com.tw.go.config.json/ParsedRequest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.google.gson.reflect.TypeToken;
66
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
77

8+
import java.util.Map;
9+
810
import static java.lang.String.format;
911

1012
class ParsedRequest {
@@ -15,7 +17,6 @@ class ParsedRequest {
1517
private static final String PARAM_NOT_A_STRING_MESSAGE = "Expected `%s` param in request type `%s` to be a string";
1618
private static final String PARAM_FAILED_TO_PARSE_TO_TYPE = "Failed to parse parameter `%s` for request type `%s`: %s";
1719
private static final String PARAM_CONFIGURATIONS = "configurations";
18-
private static final String INVALID_REPO_CONFIGURATION_KEY = "Config repo configuration has invalid key `%s`";
1920
private final String requestName;
2021
private final JsonObject params;
2122

@@ -70,15 +71,15 @@ String getStringParam(String paramName) {
7071
}
7172
}
7273

73-
<T> T getParam(String paramName) {
74+
<V> Map<String, V> getParam(String paramName, Class<V> valueClass) {
7475
try {
7576
JsonElement json = params.get(paramName);
7677

7778
if (null == json || json.isJsonNull()) {
7879
throw new RequestParseException(format(MISSING_PARAM_MESSAGE, paramName, requestName));
7980
}
8081

81-
return GSON.fromJson(json, new TypeToken<T>() {}.getType());
82+
return GSON.fromJson(json, TypeToken.getParameterized(Map.class, String.class, valueClass).getType());
8283
} catch (Exception e) {
8384
throw new RequestParseException(format(PARAM_FAILED_TO_PARSE_TO_TYPE, paramName, requestName, e.getMessage()));
8485
}

0 commit comments

Comments
 (0)