diff --git a/Troubleshooting.md b/Troubleshooting.md index 9ae6180a39..7da8933ec5 100644 --- a/Troubleshooting.md +++ b/Troubleshooting.md @@ -342,7 +342,7 @@ Note: Earlier version of Gson unfortunately did not prevent capturing type varia - For Kotlin users: Use [`reified` type parameters](https://kotlinlang.org/docs/inline-functions.html#reified-type-parameters), that means change `` to ``, if possible. If you have a chain of functions with type parameters you will probably have to make all of them `reified`. - If you don't actually use Gson's `TypeToken` for any Gson method, use a general purpose 'type token' implementation provided by a different library instead, for example Guava's [`com.google.common.reflect.TypeToken`](https://javadoc.io/doc/com.google.guava/guava/latest/com/google/common/reflect/TypeToken.html). -For backward compatibility it is possible to restore Gson's old behavior of allowing `TypeToken` to capture type variables by setting the [system property](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#setProperty(java.lang.String,java.lang.String)) `gson.allow-capturing-type-variables` to `"true"`, **however**: +For backward compatibility it is possible to restore Gson's old behavior of allowing `TypeToken` to capture type variables by setting the [system property](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#setProperty(java.lang.String,java.lang.String)) `gson.allowCapturingTypeVariables` to `"true"`, **however**: - This does not solve any of the type-safety problems mentioned above; in the long term you should prefer one of the other solutions listed above. This system property might be removed in future Gson versions. - You should only ever set the property to `"true"`, but never to any other value or manually clear it. Otherwise this might counteract any libraries you are using which might have deliberately set the system property because they rely on its behavior. diff --git a/gson/src/main/java/com/google/gson/reflect/TypeToken.java b/gson/src/main/java/com/google/gson/reflect/TypeToken.java index 0dc30f2016..5fdbdc1df3 100644 --- a/gson/src/main/java/com/google/gson/reflect/TypeToken.java +++ b/gson/src/main/java/com/google/gson/reflect/TypeToken.java @@ -93,7 +93,7 @@ private TypeToken(Type type) { } private static boolean isCapturingTypeVariablesForbidden() { - String value = System.getProperty("gson.allow-capturing-type-variables"); + String value = System.getProperty("gson.allowCapturingTypeVariables"); return value == null || !value.equals("true"); } diff --git a/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java b/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java index a54eb3aaba..cdabba0df8 100644 --- a/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java +++ b/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java @@ -273,7 +273,7 @@ void test() { e = assertThrows(IllegalArgumentException.class, () -> new TypeToken.Inner>() {}); assertThat(e).hasMessageThat().isEqualTo(expectedMessage); - String systemProperty = "gson.allow-capturing-type-variables"; + String systemProperty = "gson.allowCapturingTypeVariables"; try { // Any value other than 'true' should be ignored System.setProperty(systemProperty, "some-value");