Revision 46695 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/programming/CodeBlockFunction.java
CodeBlockFunction.java | ||
---|---|---|
1 | 1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
2 | 2 |
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
5 | 3 |
import org.apache.commons.lang3.Range; |
6 |
import org.apache.commons.lang3.StringUtils; |
|
7 |
import org.apache.commons.lang3.mutable.MutableBoolean; |
|
8 | 4 |
import org.gvsig.expressionevaluator.Code; |
9 |
import org.gvsig.expressionevaluator.CodeBuilder; |
|
10 | 5 |
import org.gvsig.expressionevaluator.Codes; |
11 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
12 | 6 |
import org.gvsig.expressionevaluator.Function; |
13 | 7 |
import org.gvsig.expressionevaluator.Interpreter; |
8 |
import static org.gvsig.expressionevaluator.impl.DefaultExpressionEvaluatorManager.resolveHostExpressions0; |
|
14 | 9 |
import org.gvsig.expressionevaluator.impl.DefaultInterpreter; |
15 | 10 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
16 |
import org.gvsig.tools.exception.BaseException; |
|
17 |
import org.gvsig.tools.visitor.FilteredVisitable; |
|
18 |
import org.gvsig.tools.visitor.VisitCanceledException; |
|
19 | 11 |
|
20 | 12 |
@SuppressWarnings("UseSpecificCatch") |
21 | 13 |
public class CodeBlockFunction extends AbstractFunction { |
... | ... | |
58 | 50 |
public Object call(Interpreter interpreter, Codes args) throws Exception { |
59 | 51 |
Object value = null; |
60 | 52 |
for (Code statement : args) { |
61 |
statement = resolveHostExpressions((DefaultInterpreter) interpreter, statement); |
|
53 |
statement = resolveHostExpressions0((DefaultInterpreter) interpreter, statement);
|
|
62 | 54 |
// LLamamos a runCode para que no atrape los returns. |
63 | 55 |
value = ((DefaultInterpreter) interpreter).runCode(statement); |
64 | 56 |
} |
65 | 57 |
return value; |
66 | 58 |
} |
67 | 59 |
|
68 |
/*friend*/ static Code resolveHostExpressions(DefaultInterpreter interpreter, Code statement) throws Exception { |
|
69 |
List<Code> hostExpressions = new ArrayList<>(); |
|
70 |
statement.accept( |
|
71 |
(Object code) -> { |
|
72 |
hostExpressions.add((Code) code); |
|
73 |
}, |
|
74 |
(FilteredVisitable code) -> !(code instanceof Code.Callable |
|
75 |
&& StringUtils.equalsIgnoreCase(((Code.Callable) code).name(), ExpressionBuilder.FUNCTION_$HOSTEXPRESSION)) |
|
76 |
); |
|
77 |
if (hostExpressions.isEmpty()) { |
|
78 |
return statement; |
|
79 |
} |
|
80 |
Code newstatement = (Code) statement.clone(); |
|
81 |
hostExpressions.clear(); |
|
82 |
newstatement.accept( |
|
83 |
(Object code) -> { |
|
84 |
hostExpressions.add((Code) code); |
|
85 |
}, |
|
86 |
(FilteredVisitable code) -> !(code instanceof Code.Callable |
|
87 |
&& StringUtils.equalsIgnoreCase(((Code.Callable) code).name(), ExpressionBuilder.FUNCTION_$HOSTEXPRESSION)) |
|
88 |
); |
|
89 |
if (hostExpressions.isEmpty()) { |
|
90 |
return statement; |
|
91 |
} |
|
92 |
|
|
93 |
CodeBuilder codeBuilder = interpreter.getCodeBuilder(); |
|
94 |
List<Code.Constant> constants = new ArrayList<>(); |
|
95 |
for (Code hostExpression : hostExpressions) { |
|
96 |
Object v = interpreter.run(hostExpression); |
|
97 |
constants.add(codeBuilder.constant(v)); |
|
98 |
} |
|
99 |
|
|
100 |
for (int i = 0; i < hostExpressions.size(); i++) { |
|
101 |
Code hostExpression = hostExpressions.get(i); |
|
102 |
Code value = constants.get(i); |
|
103 |
newstatement.replace(hostExpression, value); |
|
104 |
} |
|
105 |
return newstatement; |
|
106 |
// return statement; |
|
107 |
} |
|
108 | 60 |
} |
Also available in: Unified diff