📄 methoddecl.java
字号:
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.*;public class MethodDecl extends MemberDecl implements Cloneable, SimpleSet, Iterator, ExceptionHolder {
public void flushCache() { super.flushCache(); accessibleFrom_TypeDecl_values = null; throwsException_TypeDecl_values = null; signature_computed = false; signature_value = null; moreSpecificThan_MethodDecl_values = null; overrides_MethodDecl_values = null; hides_MethodDecl_values = null; parameterDeclaration_String_values = null; type_computed = false; type_value = null; attributes_computed = false; attributes_value = null; descName_computed = false; descName_value = null; bytecodes_ConstantPool_values = null; flags_computed = false; offsetBeforeParameters_computed = false; offsetAfterParameters_computed = false; resultOffset_computed = false; handlesException_TypeDecl_values = null; } @SuppressWarnings({"unchecked", "cast"}) public MethodDecl clone() throws CloneNotSupportedException { MethodDecl node = (MethodDecl)super.clone(); node.accessibleFrom_TypeDecl_values = null; node.throwsException_TypeDecl_values = null; node.signature_computed = false; node.signature_value = null; node.moreSpecificThan_MethodDecl_values = null; node.overrides_MethodDecl_values = null; node.hides_MethodDecl_values = null; node.parameterDeclaration_String_values = null; node.type_computed = false; node.type_value = null; node.attributes_computed = false; node.attributes_value = null; node.descName_computed = false; node.descName_value = null; node.bytecodes_ConstantPool_values = null; node.flags_computed = false; node.offsetBeforeParameters_computed = false; node.offsetAfterParameters_computed = false; node.resultOffset_computed = false; node.handlesException_TypeDecl_values = null; node.in$Circle(false); node.is$Final(false); return node; } @SuppressWarnings({"unchecked", "cast"}) public MethodDecl copy() { try { MethodDecl node = (MethodDecl)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 MethodDecl fullCopy() { MethodDecl res = (MethodDecl)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 BoundNames.jrag at line 77 public Access createBoundAccess(List args) { if(isStatic()) { return hostType().createQualifiedAccess().qualifiesAccess( new BoundMethodAccess(name(), args, this) ); } return new BoundMethodAccess(name(), args, this); } // Declared in DataStructures.jrag at line 134 public SimpleSet add(Object o) { return new SimpleSetImpl().add(this).add(o); } // Declared in DataStructures.jrag at line 140 private MethodDecl iterElem; // Declared in DataStructures.jrag at line 141 public Iterator iterator() { iterElem = this; return this; } // Declared in DataStructures.jrag at line 142 public boolean hasNext() { return iterElem != null; } // Declared in DataStructures.jrag at line 143 public Object next() { Object o = iterElem; iterElem = null; return o; } // Declared in DataStructures.jrag at line 144 public void remove() { throw new UnsupportedOperationException(); } // Declared in Modifiers.jrag at line 127 // 8.4.3 public void checkModifiers() { super.checkModifiers(); if(hostType().isClassDecl()) { // 8.4.3.1 if(isAbstract() && !hostType().isAbstract()) error("class must be abstract to include abstract methods"); // 8.4.3.1 if(isAbstract() && isPrivate()) error("method may not be abstract and private"); // 8.4.3.1 // 8.4.3.2 if(isAbstract() && isStatic()) error("method may not be abstract and static"); if(isAbstract() && isSynchronized()) error("method may not be abstract and synchronized"); // 8.4.3.4 if(isAbstract() && isNative()) error("method may not be abstract and native"); if(isAbstract() && isStrictfp()) error("method may not be abstract and strictfp"); if(isNative() && isStrictfp()) error("method may not be native and strictfp"); } if(hostType().isInterfaceDecl()) { // 9.4 if(isStatic()) error("interface method " + signature() + " in " + hostType().typeName() + " may not be static"); if(isStrictfp()) error("interface method " + signature() + " in " + hostType().typeName() + " may not be strictfp"); if(isNative()) error("interface method " + signature() + " in " + hostType().typeName() + " may not be native"); if(isSynchronized()) error("interface method " + signature() + " in " + hostType().typeName() + " may not be synchronized"); if(isProtected()) error("interface method " + signature() + " in " + hostType().typeName() + " may not be protected"); if(isPrivate()) error("interface method " + signature() + " in " + hostType().typeName() + " may not be private"); else if(isFinal()) error("interface method " + signature() + " in " + hostType().typeName() + " may not be final"); } } // Declared in NameCheck.jrag at line 96 public void nameCheck() { // 8.4 // 8.4.2 if(!hostType().methodsSignature(signature()).contains(this)) error("method with signature " + signature() + " is multiply declared in type " + hostType().typeName()); // 8.4.3.4 if(isNative() && hasBlock()) error("native methods must have an empty semicolon body"); // 8.4.5 if(isAbstract() && hasBlock()) error("abstract methods must have an empty semicolon body"); // 8.4.5 if(!hasBlock() && !(isNative() || isAbstract())) error("only abstract and native methods may have an empty semicolon body"); } // Declared in PrettyPrint.jadd at line 194 public void toString(StringBuffer s) { s.append(indent()); getModifiers().toString(s); getTypeAccess().toString(s); s.append(" " + name() + "("); if(getNumParameter() > 0) { getParameter(0).toString(s); for(int i = 1; i < getNumParameter(); i++) { s.append(", "); getParameter(i).toString(s); } } s.append(")"); if(getNumException() > 0) { s.append(" throws "); getException(0).toString(s); for(int i = 1; i < getNumException(); i++) { s.append(", "); getException(i).toString(s); } } if(hasBlock()) { s.append(" "); getBlock().toString(s); } else { s.append(";\n"); } } // Declared in TypeCheck.jrag at line 386 public void typeCheck() { // Thrown vs super class method see MethodDecl.nameCheck // 8.4.4 TypeDecl exceptionType = typeThrowable(); for(int i = 0; i < getNumException(); i++) { TypeDecl typeDecl = getException(i).type(); if(!typeDecl.instanceOf(exceptionType)) error(signature() + " throws non throwable type " + typeDecl.fullName()); } // check returns if(!isVoid() && hasBlock() && getBlock().canCompleteNormally()) error("the body of a non void method may not complete normally"); } // Declared in CodeGeneration.jrag at line 872 // emitInvoke public void emitInvokeMethod(CodeGeneration gen, TypeDecl hostType) { if(hostType.isInterfaceDecl()) { int size = type().variableSize() - 1; for(int i = 0; i < getNumParameter(); i++) size -= getParameter(i).type().variableSize(); String classname = hostType.constantPoolName(); String desc = descName(); String name = name(); int index = gen.constantPool().addInterfaceMethodref(classname, name, desc); int numArg = 1; // instance for(int i = 0; i < getNumParameter(); i++) numArg += getParameter(i).type().variableSize(); gen.emit(Bytecode.INVOKEINTERFACE, size).add2(index).add(numArg).add(0); } else { String classname = hostType.constantPoolName(); String desc = descName(); String name = name(); int index = gen.constantPool().addMethodref(classname, name, desc); if(isStatic()) { int size = type().variableSize(); for(int i = 0; i < getNumParameter(); i++) size -= getParameter(i).type().variableSize(); gen.emit(Bytecode.INVOKESTATIC, size).add2(index); } else { int size = type().variableSize() - 1; for(int i = 0; i < getNumParameter(); i++) size -= getParameter(i).type().variableSize(); gen.emit(Bytecode.INVOKEVIRTUAL, size).add2(index); } } } // Declared in CodeGeneration.jrag at line 906 public void emitInvokeSpecialMethod(CodeGeneration gen, TypeDecl hostType) { String classname = hostType.constantPoolName(); String desc = descName(); String name = name(); int index = gen.constantPool().addMethodref(classname, name, desc); int size = type().variableSize() - 1; for(int i = 0; i < getNumParameter(); i++) size -= getParameter(i).type().variableSize(); gen.emit(Bytecode.INVOKESPECIAL, size).add2(index); } // Declared in CreateBCode.jrag at line 73 private void generateBytecodes(CodeGeneration gen) { int label = gen.variableScopeLabel(); if(!isStatic()) gen.addLocalVariableEntryAtCurrentPC("this", hostType().typeDescriptor(), 0, label); for(int i = 0; i < getNumParameter(); i++) { ParameterDeclaration p = (ParameterDeclaration)getParameter(i); gen.addLocalVariableEntryAtCurrentPC( p.name(), p.type().typeDescriptor(), p.localNum(), label ); } createBCode(gen); if(type() instanceof VoidType) // TODO: canCompleteNormally check as well gen.emitReturn(); gen.addVariableScopeLabel(label); } // Declared in CreateBCode.jrag at line 114 public void createBCode(CodeGeneration gen) { try { if(hasBlock()) { gen.maxLocals = Math.max(gen.maxLocals, getBlock().localNum()); getBlock().createBCode(gen); } } catch (Error e) { System.err.println(hostType().typeName() + ": " + this); throw e; } } // Declared in GenerateClassfile.jrag at line 244 public void generateMethod(DataOutputStream out, ConstantPool cp) throws IOException { out.writeChar(flags()); out.writeChar(cp.addUtf8(name())); out.writeChar(cp.addUtf8(descName())); out.writeChar(attributes().size()); for(Iterator itera = attributes().iterator(); itera.hasNext();) ((Attribute)itera.next()).emit(out); } // Declared in GenerateClassfile.jrag at line 263 public void touchMethod(ConstantPool cp) { cp.addUtf8(name()); cp.addUtf8(descName()); attributes(); } // Declared in GenerateClassfile.jrag at line 363 public boolean clear() { if(hasBlock()) { getBlock().clear(); setBlock(new Block(new List())); } bytecodes_ConstantPool_values = null; return false; } // Declared in InnerClasses.jrag at line 196 public MethodDecl createAccessor(TypeDecl methodQualifier) { MethodDecl m = (MethodDecl)methodQualifier.getAccessor(this, "method"); if(m != null) return m; int accessorIndex = methodQualifier.accessorCounter++; // add synthetic flag to modifiers Modifiers modifiers = new Modifiers(new List()); if(getModifiers().isStatic()) modifiers.addModifier(new Modifier("static")); modifiers.addModifier(new Modifier("synthetic")); modifiers.addModifier(new Modifier("public"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -