Here you can find the source of parentOfType(AstNode node, Class
public static <T extends AstNode> T parentOfType(AstNode node, Class<T> type)
//package com.java2s; //License from project: Open Source License import org.mozilla.javascript.ast.AstNode; public class Main { /**//w w w . j ava 2 s . c o m * return nearest parent whose type is type. if such node is not exist, * return null. */ public static <T extends AstNode> T parentOfType(AstNode node, Class<T> type) { while (node != null) { if (type.isInstance(node)) { // This cast always success since I check type.isInstance(node) @SuppressWarnings("unchecked") T ret = (T) node; return ret; } node = node.getParent(); } return null; } }