Java Javascript Mozilla Library parentOfType(AstNode node, Class type)

Here you can find the source of parentOfType(AstNode node, Class type)

Description

return nearest parent whose type is type.

License

Open Source License

Declaration

public static <T extends AstNode> T parentOfType(AstNode node, Class<T> type) 

Method Source Code


//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;
    }
}

Related

  1. objArg(Object[] args, int pos, Class type, boolean required)
  2. objectToXMLString(Object xml)
  3. omitLineBreak(AstNode node)
  4. oneLineStringOf(AstNode node)
  5. parentOfAnyTypes(AstNode node, Set types, boolean isStrict)
  6. prototypeCast(Scriptable s, Class type)
  7. removeLinkFromXhtml(Object xhtml, String link)
  8. runWithAllOptimizationLevels(final ContextAction action)
  9. runWithOptimizationLevel(final ContextAction action, final int optimizationLevel)