Skip to content

Commit

Permalink
Rename system property
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Jul 8, 2023
1 parent fa64d73 commit 37fd5d0
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<T>` to `<reified T>`, 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.
2 changes: 1 addition & 1 deletion gson/src/main/java/com/google/gson/reflect/TypeToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ void test() {
e = assertThrows(IllegalArgumentException.class, () -> new TypeToken<Enclosing<T>.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");
Expand Down

0 comments on commit 37fd5d0

Please sign in to comment.