Search in sources :

Example 1 with SubGraph

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();
}
Also used : Field(java.lang.reflect.Field) Node(de.fraunhofer.aisec.cpg.graph.Node) SubGraph(de.fraunhofer.aisec.cpg.graph.SubGraph) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Aggregations

Node (de.fraunhofer.aisec.cpg.graph.Node)1 SubGraph (de.fraunhofer.aisec.cpg.graph.SubGraph)1 Field (java.lang.reflect.Field)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1