- java.lang.Object
-
- com.google.gson.internal.reflect.ReflectionHelper
-
public class ReflectionHelper extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ReflectionHelper.RecordHelper
Internal abstraction over reflection when Records are supported.private static class
ReflectionHelper.RecordNotSupportedHelper
Instance used when records are not supportedprivate static class
ReflectionHelper.RecordSupportedHelper
-
Field Summary
Fields Modifier and Type Field Description private static ReflectionHelper.RecordHelper
RECORD_HELPER
-
Constructor Summary
Constructors Modifier Constructor Description private
ReflectionHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static void
appendExecutableParameters(java.lang.reflect.AccessibleObject executable, java.lang.StringBuilder stringBuilder)
static java.lang.String
constructorToString(java.lang.reflect.Constructor<?> constructor)
Creates a string representation for a constructor.private static java.lang.RuntimeException
createExceptionForRecordReflectionException(java.lang.ReflectiveOperationException exception)
static java.lang.RuntimeException
createExceptionForUnexpectedIllegalAccess(java.lang.IllegalAccessException exception)
static java.lang.String
fieldToString(java.lang.reflect.Field field)
Creates a string representation for a field, omitting modifiers and the field type.static java.lang.String
getAccessibleObjectDescription(java.lang.reflect.AccessibleObject object, boolean uppercaseFirstLetter)
Returns a short string describing theAccessibleObject
in a human-readable way.static java.lang.reflect.Method
getAccessor(java.lang.Class<?> raw, java.lang.reflect.Field field)
Looks up the record accessor method that corresponds to the given record fieldstatic <T> java.lang.reflect.Constructor<T>
getCanonicalRecordConstructor(java.lang.Class<T> raw)
static java.lang.String[]
getRecordComponentNames(java.lang.Class<?> raw)
static boolean
isRecord(java.lang.Class<?> raw)
If records are supported on the JVM, this is equivalent to a call to Class.isRecord()static void
makeAccessible(java.lang.reflect.AccessibleObject object)
Internal implementation of making anAccessibleObject
accessible.static java.lang.String
tryMakeAccessible(java.lang.reflect.Constructor<?> constructor)
Tries making the constructor accessible, returning an exception message if this fails.
-
-
-
Field Detail
-
RECORD_HELPER
private static final ReflectionHelper.RecordHelper RECORD_HELPER
-
-
Method Detail
-
makeAccessible
public static void makeAccessible(java.lang.reflect.AccessibleObject object) throws JsonIOException
Internal implementation of making anAccessibleObject
accessible.- Parameters:
object
- the object thatAccessibleObject.setAccessible(boolean)
should be called on.- Throws:
JsonIOException
- if making the object accessible fails
-
getAccessibleObjectDescription
public static java.lang.String getAccessibleObjectDescription(java.lang.reflect.AccessibleObject object, boolean uppercaseFirstLetter)
Returns a short string describing theAccessibleObject
in a human-readable way. The result is normally shorter thanObject.toString()
because it omits modifiers (e.g.final
) and uses simple names for constructor and method parameter types.- Parameters:
object
- object to describeuppercaseFirstLetter
- whether the first letter of the description should be uppercased
-
fieldToString
public static java.lang.String fieldToString(java.lang.reflect.Field field)
Creates a string representation for a field, omitting modifiers and the field type.
-
constructorToString
public static java.lang.String constructorToString(java.lang.reflect.Constructor<?> constructor)
Creates a string representation for a constructor. E.g.:java.lang.String(char[], int, int)
-
appendExecutableParameters
private static void appendExecutableParameters(java.lang.reflect.AccessibleObject executable, java.lang.StringBuilder stringBuilder)
-
tryMakeAccessible
public static java.lang.String tryMakeAccessible(java.lang.reflect.Constructor<?> constructor)
Tries making the constructor accessible, returning an exception message if this fails.- Parameters:
constructor
- constructor to make accessible- Returns:
- exception message;
null
if successful, non-null
if unsuccessful
-
isRecord
public static boolean isRecord(java.lang.Class<?> raw)
If records are supported on the JVM, this is equivalent to a call to Class.isRecord()
-
getRecordComponentNames
public static java.lang.String[] getRecordComponentNames(java.lang.Class<?> raw)
-
getAccessor
public static java.lang.reflect.Method getAccessor(java.lang.Class<?> raw, java.lang.reflect.Field field)
Looks up the record accessor method that corresponds to the given record field
-
getCanonicalRecordConstructor
public static <T> java.lang.reflect.Constructor<T> getCanonicalRecordConstructor(java.lang.Class<T> raw)
-
createExceptionForUnexpectedIllegalAccess
public static java.lang.RuntimeException createExceptionForUnexpectedIllegalAccess(java.lang.IllegalAccessException exception)
-
createExceptionForRecordReflectionException
private static java.lang.RuntimeException createExceptionForRecordReflectionException(java.lang.ReflectiveOperationException exception)
-
-