大家帮忙看下这个类,设计来来做什么的!是GJC的一个工具类

七年 2008-04-27 04:26:23
在学编译原理,所以想看看GJC,有些地方不是很懂,大家帮忙看看

/**
* @(#)Context.java 1.5 03/01/23
*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.sun.tools.javac.v8.util;

/**
*
* Support for an abstract context, modelled loosely after ThreadLocal
* but using a user-provided context instead of the current thread.
*
* Within the compiler, a single Context is used for each invocation of
* the compiler. The context is then used to ensure a single copy of
* each compiler phase exists per compiler invocation.
*
* <p>Typical usage pattern is:
* <pre>
* public class Phase {
* private static final Context.Key<Phase> phaseKey = new Context.Key<Phase>();
*
* public static Phase instance(Context context) {
* Phase instance = context.get(phaseKey);
* if (instance == null)
* instance = new Phase(context);
* return instance;
* }
*
* protected Phase(Context context) {
* context.put(thaseKey, this);
* // other intitialization follows...
* }
* }
* </pre>
*/
public class Context {

/**
* The client creates an instance of this class for each key.
*/
public static class Key {

public Key() {
super();
}
}

/**
*
* The underlying map storing the data.
* We maintain the invariant that this table contains only
* mappings of the form
* Key<T> -> T
*/
private Hashtable ht = Hashtable.make();

/**
* Set the value for the key in this context.
*/
public void put(Key key, Object data) {
if (ht.put(key, data) != null)
throw new AssertionError("duplicate context value");
}

/**
* Get the value for the key in this context.
*/
public Object get(Key key) {
return (Object) ht.get(key);
}

public Context() {
super();
}
}
...全文
57 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Inhibitory 2008-04-28
  • 打赏
  • 举报
回复
楼主加油,
sunyujia 2008-04-27
  • 打赏
  • 举报
回复
再小小的补充下Hashtable的方法是Synchronize的,这样就保证了线程安全
cl55 2008-04-27
  • 打赏
  • 举报
回复
Within the compiler, a single Context is used for each invocation of
the compiler. The context is then used to ensure a single copy of
each compiler phase exists per compiler invocation.

你在学编译原理应该了解compiler phase吧, 如它自己所说,这个东西就是用来保证a single copy of
each compiler phase exists per compiler invocation的。
老紫竹 2008-04-27
  • 打赏
  • 举报
回复
在 Context 里面保存数据而已,

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧