Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jruby.ast.visitor;
  
  import java.util.HashSet;
  import java.util.List;
  import java.util.Set;
Visitor to search AST nodes for instance variables. Certain nodes are ignored during walking since they always create a new context with a new self. Example usage: Node node = getNodeFromSomewhere(); InstanceVariableFinder finder = new InstanceVariableFinder(); node.accept(finder); System.out.println("found: " + finder.getFoundVariables);
 
 public class InstanceVariableFinder extends AbstractNodeVisitor {
    
The set of instance variables found during walking.
 
     private final Set<StringfoundVariables = new HashSet<String>();
    
    
Walk a node and its children looking for instance variables using a new InstanceVariableFinder. Return an array of the variable names found.

Parameters:
node the node to walk
Returns:
an array of instance variable names found
 
     public static Set<StringfindVariables(Node node) {
         InstanceVariableFinder ivf = new InstanceVariableFinder();
         node.accept(ivf);
         return ivf.getFoundVariables();
     }
    
    
Return the Set of all instance variables found during walking.

Returns:
a Set of all instance variable names found
 
     public Set<StringgetFoundVariables() {
         return ;
     }
    
    
ClassNode creates a new scope and self, so do not search for ivars.

Returns:
null
 
     @Override
     public Object visitClassNode(ClassNode iVisited) {
         return null;
     }

    
Add the name of the instance variable being assigned to our set of instance variable names and continue to walk child nodes.

Returns:
null
 
     @Override
     public Object visitInstAsgnNode(InstAsgnNode iVisited) {
         .add(iVisited.getName());
         List<Nodenodes = iVisited.childNodes();
         for (int i = 0; i < nodes.size(); i++) {
             Node node = nodes.get(i);
             node.accept(this);
         }
         return null;
     }

    
Add the name of the instance variable being retrieved to our set of instance variable names and continue to walk child nodes.

Returns:
null
 
     @Override
     public Object visitInstVarNode(InstVarNode iVisited) {
         .add(iVisited.getName());
         List<Nodenodes = iVisited.childNodes();
         for (int i = 0; i < nodes.size(); i++) {
             Node node = nodes.get(i);
         }
         return null;
     }

    
ModuleNode creates a new scope and self, so do not search for ivars.

Returns:
null
    @Override
    public Object visitModuleNode(ModuleNode iVisited) {
        return null;
    }

    
PreExeNode can't appear in methods, so do not search for ivars.

Returns:
null
    @Override
    public Object visitPreExeNode(PreExeNode iVisited) {
        return null;
    }

    
PostExeNode can't appear in methods, so do not search for ivars.

Returns:
null
    @Override
    public Object visitPostExeNode(PostExeNode iVisited) {
        return null;
    }
New to GrepCode? Check out our FAQ X