|
1 | 1 | package com.indeed.proctor.common.el.filter; |
2 | 2 |
|
3 | | -import com.google.common.collect.ImmutableList; |
4 | 3 | import com.indeed.proctor.common.ProctorRuleFunctions.MaybeBool; |
5 | 4 | import org.apache.el.parser.AstAnd; |
6 | 5 | import org.apache.el.parser.AstFunction; |
|
9 | 8 | import org.apache.el.parser.AstNot; |
10 | 9 | import org.apache.el.parser.AstNotEqual; |
11 | 10 | import org.apache.el.parser.AstOr; |
12 | | -import org.apache.el.parser.ELParserTreeConstants; |
13 | 11 | import org.apache.el.parser.Node; |
14 | 12 | import org.apache.el.parser.NodeVisitor; |
15 | 13 | import org.apache.el.parser.SimpleNode; |
16 | 14 |
|
17 | | -import java.lang.reflect.Constructor; |
18 | 15 | import java.lang.reflect.InvocationTargetException; |
19 | 16 | import java.util.Collections; |
20 | 17 | import java.util.IdentityHashMap; |
21 | | -import java.util.List; |
22 | 18 | import java.util.Map; |
23 | 19 | import java.util.Set; |
24 | 20 | import java.util.Stack; |
25 | | -import java.util.stream.Collectors; |
26 | 21 |
|
27 | 22 | class NodeHunter implements NodeVisitor { |
28 | | - private static final List<String> NODE_TYPES = ImmutableList.copyOf(ELParserTreeConstants.jjtNodeName) |
29 | | - .stream() |
30 | | - .map(nodeName -> "Ast" + nodeName) |
31 | | - .collect(Collectors.toList()); |
32 | | - private static final Map<String, Integer> NODE_TYPE_IDS = NODE_TYPES.stream() |
33 | | - .collect(Collectors.toMap(nodeType -> nodeType, NODE_TYPES::indexOf)); |
34 | | - |
35 | 23 | private final Set<Node> initialUnknowns = Collections.newSetFromMap(new IdentityHashMap<>()); |
36 | 24 | private final Map<Node, Node> replacements = new IdentityHashMap<>(); |
37 | 25 | private final Set<String> variablesDefined; |
@@ -125,21 +113,13 @@ private Node replaceNodes( |
125 | 113 | } |
126 | 114 | return newNode; |
127 | 115 | } |
128 | | - final Class<?> nodeClass = node.getClass(); |
129 | | - final Constructor<?> constructor = nodeClass.getConstructor(int.class); |
130 | | - final SimpleNode newNode = (SimpleNode) constructor.newInstance(NODE_TYPE_IDS.get(nodeClass.getSimpleName())); |
131 | | - for (int i = 0; i < node.jjtGetNumChildren(); i++) { |
132 | | - final Node newChild = replaceNodes(node.jjtGetChild(i)); |
133 | | - newChild.jjtSetParent(newNode); |
134 | | - newNode.jjtAddChild(newChild, i); |
135 | | - } |
136 | | - newNode.jjtSetParent(node.jjtGetParent()); |
137 | | - newNode.setImage(node.getImage()); |
138 | | - if (newNode instanceof AstFunction) { |
139 | | - ((AstFunction) newNode).setPrefix(((AstFunction) node).getPrefix()); |
140 | | - ((AstFunction) newNode).setLocalName(((AstFunction) node).getLocalName()); |
| 116 | + final SimpleNode asSimpleNode = (SimpleNode) node; |
| 117 | + for (int i = 0; i < asSimpleNode.jjtGetNumChildren(); i++) { |
| 118 | + final Node newChild = replaceNodes(asSimpleNode.jjtGetChild(i)); |
| 119 | + asSimpleNode.jjtAddChild(newChild, i); |
| 120 | + newChild.jjtSetParent(asSimpleNode); |
141 | 121 | } |
142 | | - return newNode; |
| 122 | + return node; |
143 | 123 | } |
144 | 124 |
|
145 | 125 | @Override |
|
0 commit comments