Top Description Inners Fields Methods
com.google.gson

public Interface ReflectionAccessFilter

Imports
com.google.gson.internal.ReflectionAccessFilterHelper, java.lang.reflect.AccessibleObject

Filter for determining whether reflection based serialization and deserialization is allowed for a class.

A filter can be useful in multiple scenarios, for example when upgrading to newer Java versions which use the Java Platform Module System (JPMS). A filter then allows to prevent making inaccessible members accessible, even if the used Java version might still allow illegal access (but logs a warning), or if java command line arguments are used to open the inaccessible packages to other parts of the application. This interface defines some convenience filters for this task, such as BLOCK_INACCESSIBLE_JAVA.

A filter can also be useful to prevent mixing model classes of a project with other non-model classes; the filter could block all reflective access to non-model classes.

A reflection access filter is similar to an ExclusionStrategy with the major difference that a filter will cause an exception to be thrown when access is disallowed while an exclusion strategy just skips fields and classes.

Since
2.9.1
See Also
GsonBuilder#addReflectionAccessFilter(ReflectionAccessFilter)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
ReflectionAccessFilter.FilterResult

Result of a filter check.

Field Summary

Modifier and TypeField and Description
public static final ReflectionAccessFilter
BLOCK_ALL_ANDROID

Blocks all reflection access to members of standard Android classes.

public static final ReflectionAccessFilter
BLOCK_ALL_JAVA

Blocks all reflection access to members of standard Java classes.

public static final ReflectionAccessFilter
BLOCK_ALL_PLATFORM

Blocks all reflection access to members of classes belonging to programming language platforms, such as Java, Android, Kotlin or Scala.

public static final ReflectionAccessFilter
BLOCK_INACCESSIBLE_JAVA

Blocks all reflection access to members of standard Java classes which are not accessible by default.

Method Summary

Modifier and TypeMethod and Description
public ReflectionAccessFilter.FilterResult

Returns:

Result indicating whether reflection access is allowed
check
(Class<?>
Class to check
rawClass
)

Checks if reflection access should be allowed for a class.

Field Detail

BLOCK_ALL_ANDROIDback to summary
public static final ReflectionAccessFilter BLOCK_ALL_ANDROID

Blocks all reflection access to members of standard Android classes.

If this filter encounters a class other than a standard Android class it returns FilterResult#INDECISIVE.

This filter is mainly intended to prevent depending on implementation details of the Android platform.

Note that this filter might not cover all standard Android classes. Currently only classes in an android.* or androidx.* package, and standard Java classes in a java.* or javax.* package are considered. The set of detected classes might be expanded in the future without prior notice.

See Also
FilterResult#BLOCK_ALL
BLOCK_ALL_JAVAback to summary
public static final ReflectionAccessFilter BLOCK_ALL_JAVA

Blocks all reflection access to members of standard Java classes.

If this filter encounters a class other than a standard Java class it returns FilterResult#INDECISIVE.

This filter is mainly intended to prevent depending on implementation details of the Java platform and to help applications prepare for upgrading to the Java Platform Module System.

Note that this filter might not cover all standard Java classes. Currently only classes in a java.* or javax.* package are considered. The set of detected classes might be expanded in the future without prior notice.

See Also
BLOCK_INACCESSIBLE_JAVA, FilterResult#BLOCK_ALL
BLOCK_ALL_PLATFORMback to summary
public static final ReflectionAccessFilter BLOCK_ALL_PLATFORM

Blocks all reflection access to members of classes belonging to programming language platforms, such as Java, Android, Kotlin or Scala.

If this filter encounters a class other than a standard platform class it returns FilterResult#INDECISIVE.

This filter is mainly intended to prevent depending on implementation details of the platform classes.

Note that this filter might not cover all platform classes. Currently it combines the filters BLOCK_ALL_JAVA and BLOCK_ALL_ANDROID, and checks for other language-specific platform classes like kotlin.*. The set of detected classes might be expanded in the future without prior notice.

See Also
FilterResult#BLOCK_ALL
BLOCK_INACCESSIBLE_JAVAback to summary
public static final ReflectionAccessFilter BLOCK_INACCESSIBLE_JAVA

Blocks all reflection access to members of standard Java classes which are not accessible by default. However, reflection access is still allowed for classes for which all fields are accessible and which have an accessible no-args constructor (or for which an InstanceCreator has been registered).

If this filter encounters a class other than a standard Java class it returns FilterResult#INDECISIVE.

This filter is mainly intended to help enforcing the access checks of Java Platform Module System. It allows detecting illegal access, even if the used Java version would only log a warning, or is configured to open packages for reflection. However, this filter only works for Java 9 and higher, when using an older Java version its functionality will be limited.

Note that this filter might not cover all standard Java classes. Currently only classes in a java.* or javax.* package are considered. The set of detected classes might be expanded in the future without prior notice.

See Also
FilterResult#BLOCK_INACCESSIBLE

Method Detail

checkback to summary
public ReflectionAccessFilter.FilterResult check(Class<?> rawClass)

Checks if reflection access should be allowed for a class.

Parameters
rawClass:Class<?>

Class to check

Returns:ReflectionAccessFilter.FilterResult

Result indicating whether reflection access is allowed

com.google.gson back to summary

public final Enum ReflectionAccessFilter.FilterResult

extends Enum<ReflectionAccessFilter.FilterResult>
Class Inheritance

Result of a filter check.
Since
2.9.1

Field Summary

Modifier and TypeField and Description
public static final ReflectionAccessFilter.FilterResult
ALLOW

Reflection access for the class is allowed.

public static final ReflectionAccessFilter.FilterResult
BLOCK_ALL

Blocks all reflection access for the class.

public static final ReflectionAccessFilter.FilterResult
BLOCK_INACCESSIBLE

Blocks reflection access if a member of the class is not accessible by default and would have to be made accessible.

public static final ReflectionAccessFilter.FilterResult
INDECISIVE

The filter is indecisive whether reflection access should be allowed.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ReflectionAccessFilter.FilterResult
public static ReflectionAccessFilter.FilterResult[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ALLOWback to summary
public static final ReflectionAccessFilter.FilterResult ALLOW

Reflection access for the class is allowed.

Note that this does not affect the Java access checks in any way, it only permits Gson to try using reflection for a class. The Java runtime might still deny such access.

BLOCK_ALLback to summary
public static final ReflectionAccessFilter.FilterResult BLOCK_ALL

Blocks all reflection access for the class. Other means for serializing and deserializing the class, such as a TypeAdapter, have to be used.

BLOCK_INACCESSIBLEback to summary
public static final ReflectionAccessFilter.FilterResult BLOCK_INACCESSIBLE

Blocks reflection access if a member of the class is not accessible by default and would have to be made accessible. This is unaffected by any java command line arguments being used to make packages accessible, or by module declaration directives which open the complete module or certain packages for reflection and will consider such packages inaccessible.

Note that this only works for Java 9 and higher, for older Java versions its functionality will be limited and it might behave like ALLOW. Access checks are only performed as defined by the Java Language Specification (JLS 11 §6.6), restrictions imposed by a SecurityManager are not considered.

This result type is mainly intended to help enforce the access checks of the Java Platform Module System. It allows detecting illegal access, even if the used Java version would only log a warning, or is configured to open packages for reflection using command line arguments.

See Also
AccessibleObject#canAccess(Object)
INDECISIVEback to summary
public static final ReflectionAccessFilter.FilterResult INDECISIVE

The filter is indecisive whether reflection access should be allowed. The next registered filter will be consulted to get the result. If there is no next filter, this result acts like ALLOW.

Constructor Detail

FilterResultback to summary
private FilterResult()

Method Detail

valueOfback to summary
public static ReflectionAccessFilter.FilterResult valueOf(String name)
valuesback to summary
public static ReflectionAccessFilter.FilterResult[] values()