Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 812f7dc

Browse files
author
Joseph Barratt
committed
PROC-1334: Simplify node replacement in rule filtering
1 parent ed45c4d commit 812f7dc

1 file changed

Lines changed: 6 additions & 26 deletions

File tree

  • proctor-common/src/main/java/com/indeed/proctor/common/el/filter

proctor-common/src/main/java/com/indeed/proctor/common/el/filter/NodeHunter.java

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.indeed.proctor.common.el.filter;
22

3-
import com.google.common.collect.ImmutableList;
43
import com.indeed.proctor.common.ProctorRuleFunctions.MaybeBool;
54
import org.apache.el.parser.AstAnd;
65
import org.apache.el.parser.AstFunction;
@@ -9,29 +8,18 @@
98
import org.apache.el.parser.AstNot;
109
import org.apache.el.parser.AstNotEqual;
1110
import org.apache.el.parser.AstOr;
12-
import org.apache.el.parser.ELParserTreeConstants;
1311
import org.apache.el.parser.Node;
1412
import org.apache.el.parser.NodeVisitor;
1513
import org.apache.el.parser.SimpleNode;
1614

17-
import java.lang.reflect.Constructor;
1815
import java.lang.reflect.InvocationTargetException;
1916
import java.util.Collections;
2017
import java.util.IdentityHashMap;
21-
import java.util.List;
2218
import java.util.Map;
2319
import java.util.Set;
2420
import java.util.Stack;
25-
import java.util.stream.Collectors;
2621

2722
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-
3523
private final Set<Node> initialUnknowns = Collections.newSetFromMap(new IdentityHashMap<>());
3624
private final Map<Node, Node> replacements = new IdentityHashMap<>();
3725
private final Set<String> variablesDefined;
@@ -125,21 +113,13 @@ private Node replaceNodes(
125113
}
126114
return newNode;
127115
}
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);
141121
}
142-
return newNode;
122+
return node;
143123
}
144124

145125
@Override

0 commit comments

Comments
 (0)