-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix failing to serialize Collection or Map with inaccessible constructor #1902
Merged
eamonnmcmanus
merged 8 commits into
google:master
from
Marcono1234:marcono1234/inaccessible-object-exception
Nov 9, 2021
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
84afa36
Remove UnsafeReflectionAccessor
Marcono1234 9620747
Fix failing to serialize Collection or Map with inaccessible constructor
Marcono1234 3acc2e4
Don't keep reference to access exception of ConstructorConstructor
Marcono1234 6819419
Remove Maven toolchain requirement
Marcono1234 41ff3e3
Merge branch 'master' into marcono1234/inaccessible-object-exception
Marcono1234 0696e12
Merge branch 'master' into marcono1234/inaccessible-object-exception
Marcono1234 2dfccd2
Address review feedback
Marcono1234 9140460
Add back test for Security Manager
Marcono1234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
gson/src/main/java/com/google/gson/internal/reflect/PreJava9ReflectionAccessor.java
This file was deleted.
Oops, something went wrong.
54 changes: 0 additions & 54 deletions
54
gson/src/main/java/com/google/gson/internal/reflect/ReflectionAccessor.java
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.google.gson.internal.reflect; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Field; | ||
|
||
import com.google.gson.JsonIOException; | ||
Marcono1234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public class ReflectionHelper { | ||
private ReflectionHelper() { } | ||
|
||
/** | ||
* Tries making the field accessible, wrapping any thrown exception in a | ||
* {@link JsonIOException} with descriptive message. | ||
* | ||
* @param field | ||
Marcono1234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Field to make accessible | ||
* @throws JsonIOException | ||
* If making the field accessible fails | ||
*/ | ||
public static void makeAccessible(Field field) throws JsonIOException { | ||
try { | ||
field.setAccessible(true); | ||
} catch (Exception exception) { | ||
throw new JsonIOException("Failed making field '" + field.getDeclaringClass().getName() + "#" | ||
+ field.getName() + "' accessible; either change its visibility or write a custom " | ||
+ "TypeAdapter for its declaring type", exception); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a string representation for a constructor. | ||
* E.g.: {@code java.lang.String#String(char[], int, int)} | ||
*/ | ||
private static String constructorToString(Constructor<?> constructor) { | ||
StringBuilder stringBuilder = new StringBuilder(constructor.getDeclaringClass().getName()) | ||
.append('#') | ||
.append(constructor.getDeclaringClass().getSimpleName()) | ||
.append('('); | ||
Class<?>[] parameters = constructor.getParameterTypes(); | ||
for (int i = 0; i < parameters.length; i++) { | ||
eamonnmcmanus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (i > 0) { | ||
stringBuilder.append(", "); | ||
} | ||
stringBuilder.append(parameters[i].getSimpleName()); | ||
} | ||
|
||
return stringBuilder.append(")").toString(); | ||
} | ||
|
||
/** | ||
* Tries making the constructor accessible, returning an exception message | ||
* if this fails. | ||
* | ||
* @param constructor | ||
Marcono1234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Constructor to make accessible | ||
* @return | ||
* Exception message; {@code null} if successful, non-{@code null} if | ||
* unsuccessful | ||
*/ | ||
public static String tryMakeAccessible(Constructor<?> constructor) { | ||
try { | ||
constructor.setAccessible(true); | ||
return null; | ||
} catch (Exception exception) { | ||
return "Failed making constructor '" + constructorToString(constructor) + "' accessible; " | ||
+ "either change its visibility or write a custom InstanceCreator for its declaring type: " | ||
// Include the message since it might contain more detailed information | ||
+ exception.getMessage(); | ||
} | ||
} | ||
} |
86 changes: 0 additions & 86 deletions
86
gson/src/main/java/com/google/gson/internal/reflect/UnsafeReflectionAccessor.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JsonIOException
thrown here and inReflectionHelper
is not fitting very well, but Gson has currently no better fitting exception type.(maybe the whole Gson exception hierarchy should be refactored in the future)