Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Nov 8, 2021
1 parent 0696e12 commit 2dfccd2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
Expand Up @@ -128,8 +128,7 @@ public T construct() {
@SuppressWarnings("unchecked") // T is the same raw type as is requested
@Override public T construct() {
try {
Object[] args = null;
return (T) constructor.newInstance(args);
return (T) constructor.newInstance();
} catch (InstantiationException e) {
// TODO: JsonParseException ?
throw new RuntimeException("Failed to invoke " + constructor + " with no args", e);
Expand Down
@@ -1,21 +1,18 @@
package com.google.gson.internal.reflect;

import com.google.gson.JsonIOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

import com.google.gson.JsonIOException;

public class ReflectionHelper {
private ReflectionHelper() { }

/**
* Tries making the field accessible, wrapping any thrown exception in a
* {@link JsonIOException} with descriptive message.
*
* @param field
* Field to make accessible
* @throws JsonIOException
* If making the field accessible fails
* @param field field to make accessible
* @throws JsonIOException if making the field accessible fails
*/
public static void makeAccessible(Field field) throws JsonIOException {
try {
Expand Down Expand Up @@ -44,17 +41,15 @@ private static String constructorToString(Constructor<?> constructor) {
stringBuilder.append(parameters[i].getSimpleName());
}

return stringBuilder.append(")").toString();
return stringBuilder.append(')').toString();
}

/**
* Tries making the constructor accessible, returning an exception message
* if this fails.
*
* @param constructor
* Constructor to make accessible
* @return
* Exception message; {@code null} if successful, non-{@code null} if
* @param constructor constructor to make accessible
* @return exception message; {@code null} if successful, non-{@code null} if
* unsuccessful
*/
public static String tryMakeAccessible(Constructor<?> constructor) {
Expand All @@ -63,7 +58,7 @@ public static String tryMakeAccessible(Constructor<?> constructor) {
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: "
+ "either change its visibility or write a custom InstanceCreator or TypeAdapter for its declaring type: "
// Include the message since it might contain more detailed information
+ exception.getMessage();
}
Expand Down
Expand Up @@ -4,12 +4,10 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Collections;

import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import java.util.Collections;
import org.junit.Test;

public class ReflectionAccessTest {
/**
Expand All @@ -36,8 +34,8 @@ public void testSerializeInternalImplementationObject() {
fail("Missing exception; test has to be run with `--illegal-access=deny`");
} catch (JsonIOException expected) {
assertTrue(expected.getMessage().startsWith(
"Failed making constructor 'java.util.Collections$EmptyList#EmptyList()' accessible; "
+ "either change its visibility or write a custom InstanceCreator for its declaring type"
"Failed making constructor 'java.util.Collections$EmptyList#EmptyList()' accessible; "
+ "either change its visibility or write a custom InstanceCreator or TypeAdapter for its declaring type"
));
}
}
Expand Down

0 comments on commit 2dfccd2

Please sign in to comment.