MCPcopy Index your code
hub / github.com/beanshell/beanshell / BSHEnhancedForStatement

Class BSHEnhancedForStatement

src/bsh/BSHEnhancedForStatement.java:39–122  ·  view source on GitHub ↗

Implementation of the enhanced for(:) statement. This statement uses Iterator to support iteration over a wide variety of iterable types. @author Daniel Leuck @author Pat Niemeyer

Source from the content-addressed store, hash-verified

37 @author Pat Niemeyer
38*/
39class BSHEnhancedForStatement extends SimpleNode implements ParserConstants
40{
41 String varName;
42
43 BSHEnhancedForStatement(int id) { super(id); }
44
45 public Object eval( CallStack callstack , Interpreter interpreter )
46 throws EvalError
47 {
48 Class elementType = null;
49 SimpleNode expression, statement=null;
50
51 NameSpace enclosingNameSpace = callstack.top();
52 SimpleNode firstNode =((SimpleNode)jjtGetChild(0));
53 int nodeCount = jjtGetNumChildren();
54 if (firstNode instanceof BSHType) {
55 elementType=((BSHType)firstNode).getType( callstack, interpreter );
56 expression=((SimpleNode)jjtGetChild(1));
57 if (nodeCount > 2) {
58 statement=((SimpleNode)jjtGetChild(2));
59 }
60 } else {
61 expression=firstNode;
62 if (nodeCount > 1) {
63 statement=((SimpleNode)jjtGetChild(1));
64 }
65 }
66 BlockNameSpace eachNameSpace = new BlockNameSpace( enclosingNameSpace );
67 callstack.swap( eachNameSpace );
68
69 final Object iteratee = expression.eval( callstack, interpreter );
70 if (iteratee == Primitive.NULL) {
71 throw new EvalError("The collection, array, map, iterator, or " + "enumeration portion of a for statement cannot be null.", this, callstack);
72 }
73 CollectionManager cm = CollectionManager.getCollectionManager();
74 if ( !cm.isBshIterable( iteratee ) )
75 throw new EvalError("Can't iterate over type: "
76 +iteratee.getClass(), this, callstack );
77 Iterator iterator = cm.getBshIterator( iteratee );
78
79 Object returnControl = Primitive.VOID;
80 while (iterator.hasNext()) {
81 try {
82 Object value = iterator.next();
83 if (value == null) {
84 value = Primitive.NULL;
85 }
86 if (elementType != null) {
87 eachNameSpace.setTypedVariable(varName/*name*/, elementType/*type*/, value/*value*/, new Modifiers()/*none*/);
88 } else {
89 eachNameSpace.setVariable( varName, value, false );
90 }
91 } catch ( UtilEvalError e ) {
92 throw e.toEvalError("for loop iterator variable:" + varName, this, callstack);
93 }
94
95 boolean breakout = false; // switch eats a multi-level break here?
96 if (statement != null) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected