< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/util/Context.java

Print this page




  42  * the component itself, and (2) a convention for a pattern of usage
  43  * in which each base component registers itself by calling an
  44  * instance method that is overridden in extended components.  A base
  45  * phase supporting extension would look something like this:
  46  *
  47  * <pre>{@code
  48  * public class Phase {
  49  *     protected static final Context.Key<Phase> phaseKey =
  50  *         new Context.Key<Phase>();
  51  *
  52  *     public static Phase instance(Context context) {
  53  *         Phase instance = context.get(phaseKey);
  54  *         if (instance == null)
  55  *             // the phase has not been overridden
  56  *             instance = new Phase(context);
  57  *         return instance;
  58  *     }
  59  *
  60  *     protected Phase(Context context) {
  61  *         context.put(phaseKey, this);
  62  *         // other intitialization follows...
  63  *     }
  64  * }
  65  * }</pre>
  66  *
  67  * <p>In the compiler, we simply use Phase.instance(context) to get
  68  * the reference to the phase.  But in extensions of the compiler, we
  69  * must register extensions of the phases to replace the base phase,
  70  * and this must be done before any reference to the phase is accessed
  71  * using Phase.instance().  An extended phase might be declared thus:
  72  *
  73  * <pre>{@code
  74  * public class NewPhase extends Phase {
  75  *     protected NewPhase(Context context) {
  76  *         super(context);
  77  *     }
  78  *     public static void preRegister(final Context context) {
  79  *         context.put(phaseKey, new Context.Factory<Phase>() {
  80  *             public Phase make() {
  81  *                 return new NewPhase(context);
  82  *             }




  42  * the component itself, and (2) a convention for a pattern of usage
  43  * in which each base component registers itself by calling an
  44  * instance method that is overridden in extended components.  A base
  45  * phase supporting extension would look something like this:
  46  *
  47  * <pre>{@code
  48  * public class Phase {
  49  *     protected static final Context.Key<Phase> phaseKey =
  50  *         new Context.Key<Phase>();
  51  *
  52  *     public static Phase instance(Context context) {
  53  *         Phase instance = context.get(phaseKey);
  54  *         if (instance == null)
  55  *             // the phase has not been overridden
  56  *             instance = new Phase(context);
  57  *         return instance;
  58  *     }
  59  *
  60  *     protected Phase(Context context) {
  61  *         context.put(phaseKey, this);
  62  *         // other initialization follows...
  63  *     }
  64  * }
  65  * }</pre>
  66  *
  67  * <p>In the compiler, we simply use Phase.instance(context) to get
  68  * the reference to the phase.  But in extensions of the compiler, we
  69  * must register extensions of the phases to replace the base phase,
  70  * and this must be done before any reference to the phase is accessed
  71  * using Phase.instance().  An extended phase might be declared thus:
  72  *
  73  * <pre>{@code
  74  * public class NewPhase extends Phase {
  75  *     protected NewPhase(Context context) {
  76  *         super(context);
  77  *     }
  78  *     public static void preRegister(final Context context) {
  79  *         context.put(phaseKey, new Context.Factory<Phase>() {
  80  *             public Phase make() {
  81  *                 return new NewPhase(context);
  82  *             }


< prev index next >