forked from google/gson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix failing to serialize Collection or Map with inaccessible construc…
…tor (google#1902) * Remove UnsafeReflectionAccessor Revert google#1218 Usage of sun.misc.Unsafe to change internal AccessibleObject.override field to suppress JPMS warnings goes against the intentions of the JPMS and does not work anymore in newer versions, see google#1540. Therefore remove it and instead create a descriptive exception when making a member accessible fails. If necessary users can also still use `java` command line flags to open external modules. * Fix failing to serialize Collection or Map with inaccessible constructor Also remove tests which rely on Java implementation details. * Don't keep reference to access exception of ConstructorConstructor This also avoids a confusing stack trace, since the previously caught exception might have had a complete unrelated stack trace. * Remove Maven toolchain requirement * Address review feedback * Add back test for Security Manager
- Loading branch information
1 parent
d2830a5
commit 72761f4
Showing
8 changed files
with
294 additions
and
134 deletions.
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
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
78 changes: 0 additions & 78 deletions
78
gson/src/main/java/com/google/gson/internal/reflect/ReflectionAccessor.java
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
/* | ||
* Copyright (C) 2017 The Gson authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.gson.internal.reflect; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.reflect.AccessibleObject; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Method; | ||
|
||
import com.google.gson.JsonIOException; | ||
|
||
public class ReflectionHelper { | ||
private ReflectionHelper() { } | ||
|
||
private static final MethodHandle trySetAccessible = getTrySetAccessibleMethod(); | ||
|
||
/** | ||
* Tries making the member accessible, wrapping any thrown exception in a | ||
* {@link JsonIOException} with descriptive message. | ||
* | ||
* @param ao member to make accessible | ||
*/ | ||
public static void makeAccessible(AccessibleObject ao) { | ||
try { | ||
if (trySetAccessible != null) { | ||
trySetAccessible.invoke(ao); | ||
} else if (!ao.isAccessible()) { // if trySetAccessible() is not available then neither is canAccess() | ||
ao.setAccessible(true); | ||
} | ||
} catch (Throwable error) { | ||
throw new JsonIOException("Failed making " + ao + " accessible;" + | ||
" either change its visibility or write a custom " | ||
+ (ao instanceof Constructor ? "InstanceCreator or " : "") | ||
+ "TypeAdapter for its declaring type", error); | ||
} | ||
} | ||
|
||
/** | ||
* Tries making the member accessible | ||
* | ||
* @param ao member to make accessible | ||
* @return exception message; {@code null} if successful, non-{@code null} if | ||
* unsuccessful | ||
*/ | ||
public static String tryMakeAccessible(AccessibleObject ao) { | ||
try { | ||
makeAccessible(ao); | ||
return null; | ||
} catch (JsonIOException error) { | ||
return error.getMessage(); | ||
} | ||
} | ||
|
||
private static MethodHandle getTrySetAccessibleMethod() { | ||
try { | ||
Method method = AccessibleObject.class.getMethod("trySetAccessible"); | ||
return MethodHandles.publicLookup().unreflect(method); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
} |
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.