StatementAndBraceFinder extends JavaParserVisitorAdapter { public void buildDataFlowFor(JavaNode node) { if (!(node instanceof ASTMethodDeclaration) && !(node instanceof ASTConstructorDeclaration)) { throw new RuntimeException("Can't build a data flow for anything other than a method or a constructor"); } this.dataFlow = new Structure(dataFlowHandler); this.dataFlow.createStartNode(node.getBeginLine()); this.dataFlow.createNewNode(node); node.jjtAccept(this, dataFlow); this.dataFlow.createEndNode(node.getEndLine()); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("DataFlow is " + this.dataFlow.dump()); } Linker linker = new Linker(dataFlowHandler, dataFlow.getBraceStack(), dataFlow.getContinueBreakReturnStack()); try { linker.computePaths(); } catch (SequenceException | LinkerException e) { e.printStackTrace(); } } StatementAndBraceFinder(DataFlowHandler dataFlowHandler); void buildDataFlowFor(JavaNode node); @Override Object visit(ASTStatementExpression node, Object data); @Override Object visit(ASTVariableDeclarator node, Object data); @Override Object visit(ASTExpression node, Object data); @Override Object visit(ASTForInit node, Object data); @Override Object visit(ASTLabeledStatement node, Object data); @Override Object visit(ASTForUpdate node, Object data); @Override Object visit(ASTStatement node, Object data); @Override Object visit(ASTSwitchStatement node, Object data); @Override Object visit(ASTSwitchLabel node, Object data); @Override Object visit(ASTBreakStatement node, Object data); @Override Object visit(ASTContinueStatement node, Object data); @Override Object visit(ASTReturnStatement node, Object data); @Override Object visit(ASTThrowStatement node, Object data);  }
@Test(expected = RuntimeException.class) public void testOnlyWorksForMethodsAndConstructors() { StatementAndBraceFinder sbf = new StatementAndBraceFinder(LanguageRegistry.getLanguage(JavaLanguageModule.NAME) .getDefaultVersion().getLanguageVersionHandler().getDataFlowHandler()); sbf.buildDataFlowFor(new ASTMethodDeclaration(1)); sbf.buildDataFlowFor(new ASTConstructorDeclaration(1)); sbf.buildDataFlowFor(new ASTCompilationUnit(1)); }