package Mycompiler;
//java_10
// file name: ClassDeclaration.java
//10******************************************************************************
//******************************************************************************
/**
* 该类代表了一个java类声明. 它是关于二进制或者源定义的。 按需装载,
* 类定义是持续渐进的.类的定义按阶段获取
* 状态域描述了类定义的阶段, see Constants.java
* CS==CLASS
* CLASS_UNDEFINED=0 - 该定义尚未被装载
* CLASS_UNDECIDED=1 - 二进制定义被装载, 但是尚未清楚源定义是否被装载
* CLASS_BINARY =2 - 被装载的二进制文件(binary file)
* CLASS_SOURCE =3 - 该类从源文件中被装载, 也知道类型信息
* CLASS_PARSED =4 - 但是类文件(编译单元)尚未被编译
* CLASS_CHECKED =5 - 该类从源文件中导入 并且 已经经过类型检查
* CLASS_COMPILED =6 - 该类已经被检查,编译,和输出
* CLASS_NOTFOUND =7 - 找不到类定义
*/
// 顶层类(toplevel class)==外部类
public final class ClassDeclaration implements Constants { //访问修饰符public
int status;
Type type;
ClassDefinition definition;
public ClassDeclaration(){ // just as default constructor
}
public ClassDeclaration(Identifier name) {
this.type = Type.tClass(name);
}
//获取该类的状态,上述的8种状态之一
public int getStatus() {
return status;
}
//获取该类的名字
public Identifier getName() {
return type.getClassName(); //获取类名
}
//获取该类的类型
public Type getType() {
System.out.println("ClassDeclaration.java : type="+type);
return type;
}
public boolean isDefined() { //判断该类是否被定义
switch (status) {
//如果是下面这4种状态,则该类是被定义的
case CLASS_BINARY:
case CLASS_PARSED:
case CLASS_CHECKED:
case CLASS_COMPILED:
return true ;
}
return false;
}
//获取该类的定义,如果未定义则返回null
public ClassDefinition getClassDefinition() {
return definition;
}
/**
* (环境)如果通过之前的方法能够成功的查找到该类,则用它来标记
*/
private boolean found = false;
/**
* 获取该类的定义,如果该类尚未定义则装载该定义,
* 否则装载此类将抛出各种各样的异常
*/
public ClassDefinition getClassDefinition(Environment env)throws ClassNotFound {
if (tracing) env.dtEvent("getClassDefinition: " +getName() + ", status " + getStatus());
// 调用getClassDefinition()函数是重复的
// 这样的检查让它更快,它同样也避免了重复。
//调用basicCheck()方法变得毫无意义.
if (found) {
return definition; //返回类定义
}
for(;;) {
switch (status) {
case CLASS_UNDEFINED:
case CLASS_UNDECIDED:
case CLASS_SOURCE:
env.loadDefinition(this);
break;
case CLASS_BINARY:
case CLASS_PARSED:
if (!definition.isInsideLocal()) {
// 块中的类, 包含匿名类,当它的成员被检查的时候,它自身也将被检查
definition.basicCheck(env); //类型检查
}
found = true;
return definition;
case CLASS_CHECKED:
case CLASS_COMPILED:
found = true;
return definition;
default:
throw new ClassNotFound(getName());
}
}
}
//获取该类的定义,如果类未定义,则装载该定义,装载一个类可能会抛出各种异常,在该类上执行no basicCheck()函数
public ClassDefinition getClassDefinitionNoCheck(Environment env) throws ClassNotFound {
if (tracing) env.dtEvent("getClassDefinition: " + getName() + ", status " + getStatus());
for(;;) {
switch (status) {
case CLASS_UNDEFINED:
case CLASS_UNDECIDED:
case CLASS_SOURCE:
env.loadDefinition(this); //装载定义
break;
case CLASS_BINARY:
case CLASS_PARSED:
case CLASS_CHECKED:
case CLASS_COMPILED:
return definition;
default:
throw new ClassNotFound(getName());
}
}
}
//设置类定义
public void setDefinition(ClassDefinition definition, int status) {
// 完善性检验
// 名字的定义应该与声明相匹配.
if ((definition != null) && !getName().equals(definition.getName())) {
throw new CompilerError("setDefinition: name mismatch: " +this + ", " + definition);
}
this.definition = definition;
this.status = status;
}
//等价性
public boolean equals(Object obj) { //Object是原始类(primordial class)
if ((obj != null) && (obj instanceof ClassDeclaration)) {
return type.equals(((ClassDeclaration)obj).type);
}
return false;
}
//转换成字符串
public String toString() {
String name = getName().toString();
String type = "type ";
String nested = getName().isInner() ? "nested " : "";
if (getClassDefinition() != null) {
if (getClassDefinition().isInterface()) {
type = "interface ";
} else {
type = "class ";
}
if (!getClassDefinition().isTopLevel()) {//外部类==顶层类
nested = "inner "; //内部类
if (getClassDefinition().isLocal()) {
nested = "local "; //局部类
if (!getClassDefinition().isAnonymous()) { //匿名类
name = getClassDefinition().getLocalName() +
" (" + name + ")";
}
}
}
}
return nested + type + name; // type: String
}
}