classdecl.java
来自「JDK1.4编译器后端」· Java 代码 · 共 1,239 行 · 第 1/3 页
JAVA
1,239 行
package AST;
import java.util.HashSet;import java.util.LinkedHashSet;import java.io.FileNotFoundException;import java.io.File;import java.util.*;import beaver.*;import java.util.ArrayList;import java.util.zip.*;import java.io.*;// 4.3 Reference Types and Valuespublic class ClassDecl extends ReferenceType implements Cloneable {
public void flushCache() { super.flushCache(); noConstructor_computed = false; interfacesMethodsSignatureMap_computed = false; interfacesMethodsSignatureMap_value = null; methodsSignatureMap_computed = false; methodsSignatureMap_value = null; ancestorMethods_String_values = null; memberTypes_String_values = null; memberFields_String_values = null; unimplementedMethods_computed = false; unimplementedMethods_value = null; hasAbstract_computed = false; castingConversionTo_TypeDecl_values = null; isString_computed = false; isObject_computed = false; instanceOf_TypeDecl_values = null; isCircular_visited = 0; isCircular_computed = false; isCircular_initialized = false; typeDescriptor_computed = false; typeDescriptor_value = null; } @SuppressWarnings({"unchecked", "cast"}) public ClassDecl clone() throws CloneNotSupportedException { ClassDecl node = (ClassDecl)super.clone(); node.noConstructor_computed = false; node.interfacesMethodsSignatureMap_computed = false; node.interfacesMethodsSignatureMap_value = null; node.methodsSignatureMap_computed = false; node.methodsSignatureMap_value = null; node.ancestorMethods_String_values = null; node.memberTypes_String_values = null; node.memberFields_String_values = null; node.unimplementedMethods_computed = false; node.unimplementedMethods_value = null; node.hasAbstract_computed = false; node.castingConversionTo_TypeDecl_values = null; node.isString_computed = false; node.isObject_computed = false; node.instanceOf_TypeDecl_values = null; node.isCircular_visited = 0; node.isCircular_computed = false; node.isCircular_initialized = false; node.typeDescriptor_computed = false; node.typeDescriptor_value = null; node.in$Circle(false); node.is$Final(false); return node; } @SuppressWarnings({"unchecked", "cast"}) public ClassDecl copy() { try { ClassDecl node = (ClassDecl)clone(); if(children != null) node.children = (ASTNode[])children.clone(); return node; } catch (CloneNotSupportedException e) { } System.err.println("Error: Could not clone node of type " + getClass().getName() + "!"); return null; } @SuppressWarnings({"unchecked", "cast"}) public ClassDecl fullCopy() { ClassDecl res = (ClassDecl)copy(); for(int i = 0; i < getNumChildNoTransform(); i++) { ASTNode node = getChildNoTransform(i); if(node != null) node = node.fullCopy(); res.setChild(node, i); } return res; } // Declared in AccessControl.jrag at line 147 public void accessControl() { super.accessControl(); // 8.1.1.2 final Classes TypeDecl typeDecl = hasSuperclass() ? superclass() : null; if(typeDecl != null && !typeDecl.accessibleFromExtend(this)) //if(typeDecl != null && !isCircular() && !typeDecl.accessibleFrom(this)) error("class " + fullName() + " may not extend non accessible type " + typeDecl.fullName()); if(hasSuperclass() && !superclass().accessibleFrom(this)) error("a superclass must be accessible which " + superclass().name() + " is not"); // 8.1.4 for(int i = 0; i < getNumImplements(); i++) { TypeDecl decl = getImplements(i).type(); if(!decl.isCircular() && !decl.accessibleFrom(this)) error("class " + fullName() + " can not implement non accessible type " + decl.fullName()); } } // Declared in ExceptionHandling.jrag at line 92 public void exceptionHandling() { constructors(); super.exceptionHandling(); } // Declared in LookupMethod.jrag at line 248 // iterator over all methods in implemented interfaces public Iterator interfacesMethodsIterator() { return new Iterator() { private Iterator outer = interfacesMethodsSignatureMap().values().iterator(); private Iterator inner = null; public boolean hasNext() { if((inner == null || !inner.hasNext()) && outer.hasNext()) inner = ((SimpleSet)outer.next()).iterator(); return inner == null ? false : inner.hasNext(); } public Object next() { return inner.next(); } public void remove() { throw new UnsupportedOperationException(); } }; } // Declared in Modifiers.jrag at line 94 public void checkModifiers() { super.checkModifiers(); // 8.1.1.2 final Classes TypeDecl typeDecl = hasSuperclass() ? superclass() : null; if(typeDecl != null && typeDecl.isFinal()) { error("class " + fullName() + " may not extend final class " + typeDecl.fullName()); } } // Declared in PrettyPrint.jadd at line 74 public void toString(StringBuffer s) { getModifiers().toString(s); s.append("class " + name()); if(hasSuperClassAccess()) { s.append(" extends "); getSuperClassAccess().toString(s); } if(getNumImplements() > 0) { s.append(" implements "); getImplements(0).toString(s); for(int i = 1; i < getNumImplements(); i++) { s.append(", "); getImplements(i).toString(s); } } s.append(" {\n"); indent++; for(int i=0; i < getNumBodyDecl(); i++) { getBodyDecl(i).toString(s); } indent--; s.append(indent() + "}\n"); } // Declared in TypeAnalysis.jrag at line 594 public boolean hasSuperclass() { return !isObject(); } // Declared in TypeAnalysis.jrag at line 598 public ClassDecl superclass() { if(isObject()) return null; if(hasSuperClassAccess() && !isCircular() && getSuperClassAccess().type().isClassDecl()) return (ClassDecl)getSuperClassAccess().type(); return (ClassDecl)typeObject(); } // Declared in TypeAnalysis.jrag at line 613 public Iterator interfacesIterator() { return new Iterator() { public boolean hasNext() { computeNextCurrent(); return current != null; } public Object next() { return current; } public void remove() { throw new UnsupportedOperationException(); } private int index = 0; private TypeDecl current = null; private void computeNextCurrent() { current = null; if(isObject() || isCircular()) return; while(index < getNumImplements()) { TypeDecl typeDecl = getImplements(index++).type(); if(!typeDecl.isCircular() && typeDecl.isInterfaceDecl()) { current = typeDecl; return; } } } }; } // Declared in TypeHierarchyCheck.jrag at line 239 public void nameCheck() { super.nameCheck(); if(hasSuperClassAccess() && !getSuperClassAccess().type().isClassDecl()) error("class may only inherit a class and not " + getSuperClassAccess().type().typeName()); if(isObject() && hasSuperClassAccess()) error("class Object may not have superclass"); if(isObject() && getNumImplements() != 0) error("class Object may not implement interfaces"); // 8.1.3 if(isCircular()) error("circular inheritance dependency in " + typeName()); // 8.1.4 HashSet set = new HashSet(); for(int i = 0; i < getNumImplements(); i++) { TypeDecl decl = getImplements(i).type(); if(!decl.isInterfaceDecl() && !decl.isUnknown()) error("type " + fullName() + " tries to implement non interface type " + decl.fullName()); if(set.contains(decl)) error("type " + decl.fullName() + " mentionened multiple times in implements clause"); set.add(decl); } for(Iterator iter = interfacesMethodsIterator(); iter.hasNext(); ) { MethodDecl m = (MethodDecl)iter.next(); if(localMethodsSignature(m.signature()).isEmpty()) { SimpleSet s = superclass().methodsSignature(m.signature()); for(Iterator i2 = s.iterator(); i2.hasNext(); ) { MethodDecl n = (MethodDecl)i2.next(); if(n.accessibleFrom(this)) { interfaceMethodCompatibleWithInherited(m, n); } } if(s.isEmpty()) { for(Iterator i2 = interfacesMethodsSignature(m.signature()).iterator(); i2.hasNext(); ) { MethodDecl n = (MethodDecl)i2.next(); if(!n.mayOverrideReturn(m) && !m.mayOverrideReturn(n)) error("Xthe return type of method " + m.signature() + " in " + m.hostType().typeName() + " does not match the return type of method " + n.signature() + " in " + n.hostType().typeName() + " and may thus not be overriden"); } } } } } // Declared in TypeHierarchyCheck.jrag at line 286 private void interfaceMethodCompatibleWithInherited(MethodDecl m, MethodDecl n) { if(n.isStatic()) error("Xa static method may not hide an instance method"); if(!n.isAbstract() && !n.isPublic()) error("Xoverriding access modifier error for " + m.signature() + " in " + m.hostType().typeName() + " and " + n.hostType().typeName()); if(!n.mayOverrideReturn(m) && !m.mayOverrideReturn(m)) error("Xthe return type of method " + m.signature() + " in " + m.hostType().typeName() + " does not match the return type of method " + n.signature() + " in " + n.hostType().typeName() + " and may thus not be overriden"); if(!n.isAbstract()) { // n implements and overrides method m in the interface // may not throw more checked exceptions for(int i = 0; i < n.getNumException(); i++) { Access e = n.getException(i); boolean found = false; for(int j = 0; !found && j < m.getNumException(); j++) { if(e.type().instanceOf(m.getException(j).type())) found = true; } if(!found && e.type().isUncheckedException()) error("X" + n.signature() + " in " + n.hostType().typeName() + " may not throw more checked exceptions than overridden method " + m.signature() + " in " + m.hostType().typeName()); } } } // Declared in GenerateClassfile.jrag at line 38 public void generateClassfile() { super.generateClassfile(); String fileName = destinationPath() + File.separator + constantPoolName() + ".class"; if(Program.verbose()) System.out.println("Writing class file to " + fileName); try { ConstantPool cp = constantPool(); // force building of constant pool cp.addClass(constantPoolName()); if(hasSuperclass()) { cp.addClass(superclass().constantPoolName()); } int numInterfaces = 0; for(Iterator iter = interfacesIterator(); iter.hasNext(); numInterfaces++) cp.addClass(((TypeDecl)iter.next()).constantPoolName()); for(Iterator iter = bcFields().iterator(); iter.hasNext(); ) { FieldDeclaration field = (FieldDeclaration) iter.next(); cp.addUtf8(field.name()); cp.addUtf8(field.type().typeDescriptor()); field.attributes(); } if(needsEnclosing()) { cp.addUtf8("this$0"); cp.addUtf8(enclosing().typeDescriptor()); cp.addUtf8("Synthetic"); } for(Iterator iter = bcMethods().iterator(); iter.hasNext(); ) { BodyDecl decl = (BodyDecl)iter.next(); decl.touchMethod(cp); } if(hasClinit()) { cp.addUtf8("<clinit>"); cp.addUtf8("()V"); clinit_attributes(); } attributes(); // Actual ClassFile generation File dest = new File(fileName); File parentFile = dest.getParentFile(); if(parentFile != null) parentFile.mkdirs(); FileOutputStream f = new FileOutputStream(fileName); DataOutputStream out = new DataOutputStream(new BufferedOutputStream(f)); out.writeInt(magicHeader()); out.writeChar(minorVersion()); out.writeChar(majorVersion()); cp.emit(out); int flags = flags(); if(isNestedType()) flags = mangledFlags(flags); flags |= Modifiers.ACC_SUPER; out.writeChar(flags); out.writeChar(cp.addClass(constantPoolName())); out.writeChar(hasSuperclass() ? cp.addClass(superclass().constantPoolName()) : 0); out.writeChar(numInterfaces); for(Iterator iter = interfacesIterator(); iter.hasNext(); ) out.writeChar(cp.addClass(((TypeDecl)iter.next()).constantPoolName())); Collection fields = bcFields(); out.writeChar(fields.size() + (needsEnclosing() ? 1 : 0)); for(Iterator iter = fields.iterator(); iter.hasNext(); ) { FieldDeclaration field = (FieldDeclaration) iter.next(); out.writeChar(field.flags()); out.writeChar(cp.addUtf8(field.name())); out.writeChar(cp.addUtf8(field.type().typeDescriptor())); out.writeChar(field.attributes().size()); for(Iterator itera = field.attributes().iterator(); itera.hasNext();) ((Attribute)itera.next()).emit(out); } if(needsEnclosing()) { out.writeChar(0 /*Modifiers.ACC_PRIVATE*/); out.writeChar(cp.addUtf8("this$0")); out.writeChar(cp.addUtf8(enclosing().typeDescriptor())); out.writeChar(1); new SyntheticAttribute(cp).emit(out); } Collection methods = bcMethods(); out.writeChar(methods.size() + (hasClinit() ? 1 : 0)); for(Iterator iter = methods.iterator(); iter.hasNext(); ) { BodyDecl b = (BodyDecl)iter.next(); b.generateMethod(out, cp); } if(hasClinit()) { out.writeChar(Modifiers.ACC_STATIC); out.writeChar(cp.addUtf8("<clinit>")); out.writeChar(cp.addUtf8("()V")); out.writeChar(clinit_attributes().size()); for(Iterator itera = clinit_attributes().iterator(); itera.hasNext();) ((Attribute)itera.next()).emit(out); } out.writeChar(attributes().size()); for(Iterator itera = attributes().iterator(); itera.hasNext();) ((Attribute)itera.next()).emit(out); out.close(); } catch (IOException e) { e.printStackTrace(); } } // Declared in java.ast at line 3 // Declared in java.ast line 63
public ClassDecl() { super();
setChild(new Opt(), 1);
setChild(new List(), 2);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?