Java Code Examples for com.github.javaparser.ast.Node#getChildNodes()

The following examples show how to use com.github.javaparser.ast.Node#getChildNodes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ASTHelper.java    From stategen with GNU Affero General Public License v3.0 7 votes vote down vote up
/**
 * Gets the type index.
 *
 * @param <N> the number type
 * @param container the container
 * @param clazz the clazz
 * @return the type index
 */
static <N extends Node> Integer getTypeIndex(Node container, Class<N> clazz) {
    List<Node> children = container.getChildNodes();
    if (CollectionUtil.isNotEmpty(children)) {
        for (int i = 0; i < children.size(); i++) {
            Node child = children.get(i);
            if (clazz.isInstance(child)) {
                return i;
            }
            Integer temIdx = getTypeIndex(child, clazz);
            if (temIdx != null)
                return temIdx;
        }
    }

    return null;
}
 
Example 2
Source File: ASTHelper.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Gets the field declaration map.
 *
 * @param <T> the generic type
 * @param node the class declaration
 * @param type the type
 * @return the field declaration map
 */
static <T extends BodyDeclaration> Map<String, T> getBodyDeclarationMap(Node node, Class<T> type) {
    List<Node> children = node.getChildNodes();
    if (CollectionUtil.isEmpty(children)) {
        return new HashMap<String, T>(0);
    }

    Map<String, T> bodyMap = new CaseInsensitiveHashMap<T>(children.size());

    List<T> bodyDeclarations = getNodesByType(node, type);

    for (T bodyDeclaration : bodyDeclarations) {
        if (bodyDeclaration instanceof FieldDeclaration) {
            FieldDeclaration fieldDeclaration = (FieldDeclaration) bodyDeclaration;
            VariableDeclarator variableDeclaratorId = getOneNodesByType(fieldDeclaration, VariableDeclarator.class);
            bodyMap.put(variableDeclaratorId.getName().toString(), bodyDeclaration);
        } else if (bodyDeclaration instanceof MethodDeclaration) {
            MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
            bodyMap.put(methodDeclaration.getNameAsString(), bodyDeclaration);
        }
    }
    return bodyMap;
}
 
Example 3
Source File: SourceCode.java    From Recaf with MIT License 6 votes vote down vote up
private Node getNodeAt(int line, int column, Node root, Predicate<Node> filter) {
	if (!filter.test(root))
		return null;
	// Check cursor is in bounds
	// We won't instantly return null because the root range may be SMALLER than
	// the range of children. This is really stupid IMO but thats how JavaParser is...
	boolean bounds = true;
	Position cursor = Position.pos(line, column);
	if (cursor.isBefore(root.getBegin().get()) || cursor.isAfter(root.getEnd().get()))
		bounds = false;
	// Iterate over children, return non-null child
	for (Node child : root.getChildNodes()) {
		Node ret = getNodeAt(line, column, child, filter);
		if (ret != null)
			return ret;
	}
	// If we're not in bounds and none of our children are THEN we assume this node is bad.
	if (!bounds)
		return null;
	// In bounds so we're good!
	return root;
}
 
Example 4
Source File: ASTHelper.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Gets the one nodes by type.
 *
 * @param <N> the number type
 * @param container the container
 * @param clazz the clazz
 * @return the one nodes by type
 */
@SuppressWarnings("unchecked")
static <N extends Node> N getOneNodesByType(Node container, Class<N> clazz) {
    for (Node child : container.getChildNodes()) {
        if (clazz.isInstance(child)) {
            return (N) child;
        }
        Node result = getOneNodesByType(child, clazz);
        if (result != null)
            return (N) result;
    }
    return null;
}
 
Example 5
Source File: ASTHelper.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Gets the nodes by type.
 *
 * @param <N> the number type
 * @param container the container
 * @param clazz the clazz
 * @return the nodes by type
 */
static <N extends Node> List<N> getNodesByType(Node container, Class<N> clazz) {
    List<N> nodes = new ArrayList<N>();
    boolean find = false;
    for (Node child : container.getChildNodes()) {
        if (clazz.isInstance(child)) {
            nodes.add(clazz.cast(child));
            find = true;
        }
        if (!find) {
            nodes.addAll(getNodesByType(child, clazz));
        }
    }
    return nodes;
}
 
Example 6
Source File: ASTHelper.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Prints the self and children.
 *
 * @param container the container
 */
static void printSelfAndChildren(Node container) {
    GLogger.println("当前类为:" + container.getClass() + "...................");
    for (Node child : container.getChildNodes()) {
        GLogger.info("第一级子类为:" + child.getClass() + "..................." + child);
    }
    GLogger.println("打印结束");
    GLogger.println("\n\n\n");
}