use of de.fraunhofer.aisec.cpg.graph.Node in project cpg by Fraunhofer-AISEC.
the class SubgraphWalker method activateTypes.
public static void activateTypes(Node node, ScopeManager scopeManager) {
AtomicInteger num = new AtomicInteger();
Map<HasType, Set<Type>> typeCache = TypeManager.getInstance().getTypeCache();
IterativeGraphWalker walker = new IterativeGraphWalker();
walker.registerOnNodeVisit(scopeManager::enterScopeIfExists);
walker.registerOnScopeExit(n -> {
if (n instanceof HasType) {
HasType typeNode = (HasType) n;
typeCache.getOrDefault(typeNode, Collections.emptySet()).forEach(t -> {
t = TypeManager.getInstance().resolvePossibleTypedef(t);
((HasType) n).setType(t);
});
typeCache.remove((HasType) n);
num.getAndIncrement();
}
});
walker.iterate(node);
LOGGER.debug("Activated {} nodes for {}", num, node.getName());
// For some nodes it may happen that they are not reachable via AST, but we still need to set
// their type to the requested value
typeCache.forEach((n, types) -> types.forEach(t -> {
t = TypeManager.getInstance().resolvePossibleTypedef(t);
n.setType(t);
}));
}
use of de.fraunhofer.aisec.cpg.graph.Node 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();
}
Aggregations