package org.torquebox.injection;

import org.jruby.ast.ArrayNode;
import org.jruby.ast.FCallNode;
import org.jruby.ast.Node;
import org.jruby.ast.NodeType;
import org.jruby.ast.StrNode;
import org.jruby.ast.SymbolNode;
import org.torquebox.interp.analysis.DefaultNodeVisitor;

public class InjectBlockVisitor extends DefaultNodeVisitor {
    
    public InjectBlockVisitor(InjectionAnalyzer analyzer) {
        this.analyzer = analyzer;
    }

    @Override
    public Object visitFCallNode(FCallNode node) {
        if (node.getName().equals( "inject" )) {
            Node genericArgsNode = node.getArgsNode();

            if (genericArgsNode instanceof ArrayNode) {
                ArrayNode argsNode = (ArrayNode) genericArgsNode;

                if (argsNode.size() == 2) {
                    Node siteNode = argsNode.get( 0 );
                    Node injectableNode = argsNode.get( 1 );

                    Injectable injectable = (Injectable) injectableNode.accept(  this  );
                    return injectable;
                }
            }
        } else {
            String type = node.getName();
            InjectableHandler handler = this.analyzer.getInjectableHandlerRegistry().getHandler( type );
            if ( handler != null ) {
                return handler.handle(  node.getArgsNode() );
            }
        }

        return null;
    }

    protected static String getString(Node node) {
        String str = null;

        if (node.getNodeType() == NodeType.STRNODE) {
            str = ((StrNode) node).getValue().toString();
        } else if (node.getNodeType() == NodeType.SYMBOLNODE) {
            str = ((SymbolNode) node).getName();
        } else if ( node.getNodeType() == NodeType.ARRAYNODE ) {
            if ( ((ArrayNode)node) .size() == 1 ) {
                str = getString( ((ArrayNode)node).get( 0 ) );
            }
        }
        return str;
    }

    private InjectionAnalyzer analyzer;


}

@@ -335,13 +335,9 @@
 r %7B%0A
-    
 %0A
+
     
@@ -502,579 +502,8 @@
 ) %7B%0A
-        if (node.getName().equals( %22inject%22 )) %7B%0A            Node genericArgsNode = node.getArgsNode();%0A%0A            if (genericArgsNode instanceof ArrayNode) %7B%0A                ArrayNode argsNode = (ArrayNode) genericArgsNode;%0A%0A                if (argsNode.size() == 2) %7B%0A                    Node siteNode = argsNode.get( 0 );%0A                    Node injectableNode = argsNode.get( 1 );%0A%0A                    Injectable injectable = (Injectable) injectableNode.accept(  this  );%0A                    return injectable;%0A                %7D%0A            %7D%0A        %7D else %7B%0A    
     
@@ -532,28 +532,24 @@
 .getName();%0A
-    
         Inje
@@ -645,25 +645,20 @@
         
-    
 if (
- 
 handler 
@@ -664,17 +664,16 @@
  != null
- 
 ) %7B%0A    
@@ -676,28 +676,24 @@
             
- 
-   
 return handl
@@ -702,17 +702,16 @@
 .handle(
- 
  node.ge
@@ -721,38 +721,24 @@
 gsNode() );%0A
-            %7D%0A
         %7D%0A%0A 
@@ -1087,17 +1087,16 @@
 lse if (
- 
 node.get
@@ -1127,17 +1127,16 @@
 RRAYNODE
- 
 ) %7B%0A    
@@ -1147,17 +1147,16 @@
     if (
- 
 ((ArrayN
@@ -1159,22 +1159,22 @@
 rayNode)
+ 
 node)
- 
 .size() 
@@ -1177,17 +1177,16 @@
 e() == 1
- 
 ) %7B%0A    
@@ -1226,16 +1226,17 @@
 rayNode)
+ 
 node).ge
@@ -1292,24 +1292,24 @@
 str;%0A    %7D%0A%0A
+
     private 
@@ -1337,11 +1337,10 @@
 lyzer;%0A%0A
-%0A
 %7D%0A

Remove now-unused branch of the analysis visitor.