Class ReflectionIntrospectionPolicy

java.lang.Object
io.micronaut.reflection.ReflectionIntrospectionPolicy

public final class ReflectionIntrospectionPolicy extends Object
The types the shared BeanIntrospector may describe reflectively when it has no generated introspection for them.

Reading a class back is what the framework is built to avoid, and a reflective introspection exposes every member of a class to whatever asks for it, so no type is allowed until an application says so. A type is allowed when its name matches one of the patterns of the "micronaut.introspection.allow-reflection" property, set either as a system property or in the application configuration, or one of the patterns allowed programmatically. A pattern is a class name where * stands for any sequence of characters, matched against the whole name: com.example.model.* allows the classes of a package and of its sub packages, and only those - com.example.modelling.Order is not one of them - com.example.Order allows one class, *.Order every class of that name and * every class.

The policy guards only the fallback of the shared introspector. Code that reflects on a type explicitly - ReflectionBeanIntrospection.of(Class), ReflectionBeanIntrospector - is not subject to it: that code made its choice.

Since:
5.2.0
Author:
Denis Stepanov
  • Field Details

    • PROPERTY_ALLOW_REFLECTION

      public static final String PROPERTY_ALLOW_REFLECTION
      The property listing the patterns of the types the shared introspector may describe reflectively.
      See Also:
  • Method Details

    • version

      public static long version()
      The version of what is configured, which changes whenever a contribution is made or taken back or a predicate is allowed. A caller caching what describe(Class) answered holds the version it answered at, and asks again once the version has moved.
      Returns:
      The version
    • describe

      public static AnnotationMetadata describe(Class<?> type)
      The annotations an application configured for a type: the Introspected members it means the reflective description of the type to be built with, out of the contributions whose patterns the type matches. Where two contributions describe the same type, the one contributed first wins on the members it sets, and the other still supplies the members it does not.
      Parameters:
      type - The type
      Returns:
      The annotations, AnnotationMetadata.EMPTY_METADATA when nothing is configured for the type
    • isAllowed

      public static boolean isAllowed(Class<?> type)
      Whether a type is allowed.
      Parameters:
      type - The type
      Returns:
      Whether the shared introspector may describe the type reflectively
    • allow

      public static void allow(String... patterns)
      Allows the types matching the patterns, in addition to the ones already allowed.
      Parameters:
      patterns - The patterns
    • allow

      public static void allow(Collection<String> patterns)
      Allows the types matching the patterns, in addition to the ones already allowed.
      Parameters:
      patterns - The patterns
    • allow

      public static void allow(Predicate<Class<?>> types)
      Allows the types a predicate accepts, in addition to the ones already allowed. Callers of this method never lose one another's predicate, and a type is allowed as soon as one of them accepts it.
      Parameters:
      types - The predicate
    • configure

      public static ReflectionIntrospectionPolicy.Registration configure(Collection<String> patterns)
      Contributes the patterns an application configuration allows, in addition to the patterns the other contributions, the system property and the programmatically allowed types allow. A contribution is not a replacement: the contexts running at the same time in one JVM each contribute their own patterns, and a context takes back exactly the patterns it contributed by closing the registration it was given.
      Parameters:
      patterns - The patterns
      Returns:
      The registration of these patterns, to close when the configuration that contributed them goes away
    • configure

      public static ReflectionIntrospectionPolicy.Registration configure(Collection<String> patterns, AnnotationMetadata description)
      Contributes the patterns an application configuration allows together with the annotations it means the types matching them to carry, as configure(Collection) contributes the patterns alone. The annotations are the Introspected members the reflective description of such a type is built with, which is how a type that cannot be annotated - one of a library, one compiled without the processor - is described the way the application means it to be.
      Parameters:
      patterns - The patterns
      description - The annotations the matching types carry, empty to contribute the patterns alone
      Returns:
      The registration of this contribution, to close when the configuration that made it goes away
    • reset

      public static void reset()
      Forgets everything configured and allowed: the patterns of every contribution, whether or not the configuration that contributed them is still running, and every programmatically allowed type. The system property still applies, as it is not something that can be taken back.