1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jexl3.internal;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21 import org.apache.commons.jexl3.JexlEngine;
22 import org.apache.commons.jexl3.JexlException;
23 import org.apache.commons.jexl3.JexlFeatures;
24 import org.apache.commons.jexl3.JexlScript;
25 import org.apache.commons.jexl3.parser.ASTJexlScript;
26 import org.apache.commons.jexl3.parser.JexlNode;
27
28
29
30
31 public class Util {
32
33
34
35
36
37
38 public static void debuggerCheck(final JexlEngine ijexl) throws Exception {
39 final Engine jexl = (Engine) ijexl;
40
41 if (jexl == null || jexl.cache == null) {
42 return;
43 }
44 final Engine jdbg = new Engine();
45 jdbg.parser.allowRegisters(true);
46 final Debugger dbg = new Debugger();
47
48 for (final Map.Entry<Source, ASTJexlScript> entry : jexl.cache.entries()) {
49 final JexlNode node = entry.getValue();
50
51 dbg.debug(node);
52 final String expressiondbg = dbg.toString();
53 final JexlFeatures features = entry.getKey().getFeatures();
54
55 try {
56 final Script exprdbg = jdbg.createScript(features, null, expressiondbg, null);
57
58 JexlNode root = exprdbg.script;
59 while (root.jjtGetParent() != null) {
60 root = root.jjtGetParent();
61 }
62
63 final String reason = checkEquals(root, node);
64 if (reason != null) {
65 throw new RuntimeException("check equal failed: "
66 + expressiondbg
67 + " /**** " + reason + " **** */ "
68 + entry.getKey());
69 }
70 } catch (final JexlException xjexl) {
71 throw new RuntimeException("check parse failed: "
72 + expressiondbg
73 + " /*********/ "
74 + entry.getKey(), xjexl);
75
76 }
77 }
78 }
79
80
81
82
83
84
85 protected static ArrayList<JexlNode> flatten(final JexlNode node) {
86 final ArrayList<JexlNode> list = new ArrayList<>();
87 flatten(list, node);
88 return list;
89 }
90
91
92
93
94
95
96 private static void flatten(final List<JexlNode> list, final JexlNode node) {
97 final int nc = node.jjtGetNumChildren();
98 list.add(node);
99 for (int c = 0; c < nc; ++c) {
100 flatten(list, node.jjtGetChild(c));
101 }
102 }
103
104
105
106
107
108
109
110
111 private static String checkEquals(JexlNode lhs, JexlNode rhs) {
112 if (lhs != rhs) {
113 final ArrayList<JexlNode> lhsl = flatten(lhs);
114 final ArrayList<JexlNode> rhsl = flatten(rhs);
115 if (lhsl.size() != rhsl.size()) {
116 return "size: " + lhsl.size() + " != " + rhsl.size();
117 }
118 for (int n = 0; n < lhsl.size(); ++n) {
119 lhs = lhsl.get(n);
120 rhs = rhsl.get(n);
121 if (lhs.getClass() != rhs.getClass()) {
122 return "class: " + lhs.getClass() + " != " + rhs.getClass();
123 }
124 final String lhss = lhs.toString();
125 final String rhss = rhs.toString();
126 if ((lhss == null && rhss != null)
127 || (lhss != null && rhss == null)) {
128 return "image: " + lhss + " != " + rhss;
129 }
130 if (lhss != null && !lhss.equals(rhss)) {
131 return "image: " + lhss + " != " + rhss;
132 }
133 }
134 }
135 return null;
136 }
137
138
139
140
141
142
143 protected static String flattenedStr(final JexlScript e) {
144 return "";
145 }
146
147 private static String indent(JexlNode node) {
148 final StringBuilder strb = new StringBuilder();
149 while (node != null) {
150 strb.append(" ");
151 node = node.jjtGetParent();
152 }
153 return strb.toString();
154 }
155
156 private String flattenedStr(final JexlNode node) {
157 final ArrayList<JexlNode> flattened = flatten(node);
158 final StringBuilder strb = new StringBuilder();
159 for (final JexlNode flat : flattened) {
160 strb.append(indent(flat));
161 strb.append(flat.getClass().getSimpleName());
162 final String sflat = flat.toString();
163 if (sflat != null) {
164 strb.append(" = ");
165 strb.append(sflat);
166 }
167 strb.append("\n");
168 }
169 return strb.toString();
170 }
171 }