Search code examples
javadependency-injectionioc-containerpicocontainer

Problem auto wiring when facing object creation through methods


I am using PicoContainer to do the wiring of the classes of my application. As you can see below, everything goes perfect but in one place:

    CharArrayReader reader = new CharArrayReader(acCode.toCharArray());
    CharStream charStream = new CharStream(reader);
    Scanner scanner = new Scanner(charStream);
    TokenStream tokenStream = new TokenStream(scanner);
    ParserGeneratingAST parserGeneratingAST = new ParserGeneratingAST(tokenStream);
--> AbstractSyntaxTree ast = parserGeneratingAST.generateAST(); <-- here is the problem
    SymbolsTable symbolsTable = new SymbolsTable();
    ErrorsTable errorsTable = new ErrorsTable();

    SymbolsTableFillerVisitor symbolsTableFillerVisitor = new SymbolsTableFillerVisitor(symbolsTable, errorsTable);
    TypeCheckingVisitor typeCheckingVisitor = new TypeCheckingVisitor(symbolsTable, errorsTable);
    InstructionsList instructionsList = new InstructionsList();
    CodeGenerationVisitor codeGenerationVisitor = new CodeGenerationVisitor(instructionsList);

    symbolsTableFillerVisitor.visit(ast);
    typeCheckingVisitor.visit(ast);
    codeGenerationVisitor.visit(ast);        

The problem line is

AbstractSyntaxTree ast = parserGeneratingAST.generateAST();

Is there any way of getting around this problem using PicoContainer? I'd like to keep my classes without annotations, if possible.

Thanks


Solution

  • If I understand correctly, your problem is to have AbstractSyntaxTree being injected in your class, but you must use a ParserGeneratingAST to generate an instance of AbstractSyntaxTree. Although I'm not an expert on PicoContainer, I think that in this case you can use a Factory that creates AbstractSyntaxTree objects (by using a ParserGeneratingAST), and then you can use Factory Injection