Class Scope

java.lang.Object
org.inferred.freebuilder.processor.source.Scope
Direct Known Subclasses:
CompilationUnitBuilder.InitialScope, Scope.FileScope, Scope.MethodScope

public abstract class Scope extends Object
An append-only, hierarchical map with key-specific value typing.

Scopes allow source generators to cooperate with each other—to avoid namespace clashes or reuse variables, for example—in a decoupled fashion using a shared key-value space. The key type dictates the type of the value that can be stored in the map, as well as at what level in the source the key should be unique at: FILE keys are unique within a compilation unit, while METHOD keys are scoped to the current method.

While a Scope has to be mutable, it limits the potential for complex bugs by only permitting key-values pairs to be inserted, not modified or removed.

  • Field Details

    • RECURSION_SENTINEL

      private static final Object RECURSION_SENTINEL
    • entries

      private final Map<Scope.Key<?>, Object> entries
    • parent

      private final Scope parent
  • Constructor Details

    • Scope

      protected Scope()
    • Scope

      protected Scope(Scope parent)
  • Method Details

    • canStore

      protected abstract boolean canStore(Scope.Key<?> key)
    • isEmpty

      public boolean isEmpty()
    • contains

      public boolean contains(Scope.Key<?> key)
    • get

      public <V> V get(Scope.Key<V> key)
    • computeIfAbsent

      public <V> V computeIfAbsent(Scope.Key<V> key, Supplier<V> supplier)
      If key is not already associated with a value, computes its value using supplier and enters it into the scope.
      Returns:
      the current (existing or computed) value associated with key
    • keysOfType

      public <V> Set<V> keysOfType(Class<V> keyType)
    • putIfAbsent

      public <V> V putIfAbsent(Scope.Key<V> key, V value)
      If key is not already associated with a value, associates it with value.
      Returns:
      the original value, or null if there was no value associated