1 33 34 package bsh; 35 36 class BSHMethodDeclaration extends SimpleNode 37 { 38 public String name; 39 40 42 BSHReturnType returnTypeNode; 43 BSHFormalParameters paramsNode; 44 BSHBlock blockNode; 45 int firstThrowsClause; 47 48 50 public Modifiers modifiers; 51 52 Class returnType; int numThrows = 0; 55 56 BSHMethodDeclaration(int id) { super(id); } 57 58 62 synchronized void insureNodesParsed() 63 { 64 if ( paramsNode != null ) return; 66 67 Object firstNode = jjtGetChild(0); 68 firstThrowsClause = 1; 69 if ( firstNode instanceof BSHReturnType ) 70 { 71 returnTypeNode = (BSHReturnType)firstNode; 72 paramsNode = (BSHFormalParameters)jjtGetChild(1); 73 if ( jjtGetNumChildren() > 2+numThrows ) 74 blockNode = (BSHBlock)jjtGetChild(2+numThrows); ++firstThrowsClause; 76 } 77 else 78 { 79 paramsNode = (BSHFormalParameters)jjtGetChild(0); 80 blockNode = (BSHBlock)jjtGetChild(1+numThrows); } 82 } 83 84 88 Class evalReturnType( CallStack callstack, Interpreter interpreter ) 89 throws EvalError 90 { 91 insureNodesParsed(); 92 if ( returnTypeNode != null ) 93 return returnTypeNode.evalReturnType( callstack, interpreter ); 94 else 95 return null; 96 } 97 98 String getReturnTypeDescriptor( 99 CallStack callstack, Interpreter interpreter, String defaultPackage ) 100 { 101 insureNodesParsed(); 102 if ( returnTypeNode == null ) 103 return null; 104 else 105 return returnTypeNode.getTypeDescriptor( 106 callstack, interpreter, defaultPackage ); 107 } 108 109 BSHReturnType getReturnTypeNode() { 110 insureNodesParsed(); 111 return returnTypeNode; 112 } 113 114 118 public Object eval( CallStack callstack, Interpreter interpreter ) 119 throws EvalError 120 { 121 returnType = evalReturnType( callstack, interpreter ); 122 evalNodes( callstack, interpreter ); 123 124 127 132 NameSpace namespace = callstack.top(); 133 BshMethod bshMethod = new BshMethod( this, namespace, modifiers ); 134 try { 135 namespace.setMethod( name, bshMethod ); 136 } catch ( UtilEvalError e ) { 137 throw e.toEvalError(this,callstack); 138 } 139 140 return Primitive.VOID; 141 } 142 143 private void evalNodes( CallStack callstack, Interpreter interpreter ) 144 throws EvalError 145 { 146 insureNodesParsed(); 147 148 for(int i=firstThrowsClause; i<numThrows+firstThrowsClause; i++) 150 ((BSHAmbiguousName)jjtGetChild(i)).toClass( 151 callstack, interpreter ); 152 153 paramsNode.eval( callstack, interpreter ); 154 155 if ( interpreter.getStrictJava() ) 157 { 158 for(int i=0; i<paramsNode.paramTypes.length; i++) 159 if ( paramsNode.paramTypes[i] == null ) 160 throw new EvalError( 163 "(Strict Java Mode) Undeclared argument type, parameter: " + 164 paramsNode.getParamNames()[i] + " in method: " 165 + name, this, null ); 166 167 if ( returnType == null ) 168 throw new EvalError( 171 "(Strict Java Mode) Undeclared return type for method: " 172 + name, this, null ); 173 } 174 } 175 176 public String toString() { 177 return "MethodDeclaration: "+name; 178 } 179 } 180 | Popular Tags |