Class Scope

  • Direct Known Subclasses:
    CompilationUnitBuilder.InitialScope, Scope.FileScope, Scope.MethodScope

    public abstract class Scope
    extends java.lang.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.

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Scope()  
      protected Scope​(Scope parent)  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected abstract boolean canStore​(Scope.Key<?> key)  
      <V> V computeIfAbsent​(Scope.Key<V> key, java.util.function.Supplier<V> supplier)
      If key is not already associated with a value, computes its value using supplier and enters it into the scope.
      boolean contains​(Scope.Key<?> key)  
      <V> V get​(Scope.Key<V> key)  
      boolean isEmpty()  
      <V> java.util.Set<V> keysOfType​(java.lang.Class<V> keyType)  
      <V> V putIfAbsent​(Scope.Key<V> key, V value)
      If key is not already associated with a value, associates it with value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • RECURSION_SENTINEL

        private static final java.lang.Object RECURSION_SENTINEL
      • entries

        private final java.util.Map<Scope.Key<?>,​java.lang.Object> entries
      • parent

        private final Scope parent
    • Constructor Detail

      • Scope

        protected Scope()
      • Scope

        protected Scope​(Scope parent)
    • Method Detail

      • 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,
                                     java.util.function.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> java.util.Set<V> keysOfType​(java.lang.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