use of de.fraunhofer.aisec.cpg.graph.SubGraph in project cpg by Fraunhofer-AISEC.
the class Strategy method AST_FORWARD.
/**
* Traverse AST in forward direction.
*
* @param x
* @return
*/
@NonNull
public static Iterator<Node> AST_FORWARD(@NonNull Node x) {
// Set for duplicate elimination
HashSet<Node> children = new HashSet<>();
Class<?> classType = x.getClass();
for (Field field : getAllFields(classType)) {
SubGraph subGraph = field.getAnnotation(SubGraph.class);
if (subGraph != null && Arrays.asList(subGraph.value()).contains("AST")) {
try {
// disable access mechanisms
field.setAccessible(true);
Object obj = field.get(x);
// restore old state
field.setAccessible(false);
// skip, if null
if (obj == null) {
continue;
}
obj = handlePropertyEdges(field, obj);
if (obj instanceof Node) {
children.add((Node) obj);
} else if (obj instanceof Collection) {
Collection<? extends Node> astChildren = (Collection<? extends Node>) obj;
astChildren.removeIf(Objects::isNull);
children.addAll(astChildren);
}
} catch (IllegalAccessException ex) {
// Nothing to do here
}
}
}
return children.iterator();
}