/** * Returns the top-level statements in a script. If no such statements are * found, an empty list is returned. * * @return the top-level statements in a script */ public List<Statement> getScriptStatements() { return getStatements(module.getStatementBlock()); }
@SuppressWarnings("unchecked") private void indexAstNodes() { visitor.visitBlockStatement(module.getStatementBlock()); for (MethodNode method : module.getMethods()) visitor.visitMethod(method); for (ClassNode clazz : module.getClasses()) visitor.visitClass(clazz); }
public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduction cst) throws ParserException { setClassLoader(classLoader); makeModule(); try { convertGroovy(ast); if (output.getStatementBlock().isEmpty() && output.getMethods().isEmpty() && output.getClasses().isEmpty()) { output.addStatement(ReturnStatement.RETURN_NULL_OR_VOID); } // set the script source position ClassNode scriptClassNode = output.getScriptClassDummy(); if (scriptClassNode != null) { List<Statement> statements = output.getStatementBlock().getStatements(); if (!statements.isEmpty()) { Statement firstStatement = statements.get(0); Statement lastStatement = statements.get(statements.size() - 1); scriptClassNode.setSourcePosition(firstStatement); scriptClassNode.setLastColumnNumber(lastStatement.getLastColumnNumber()); scriptClassNode.setLastLineNumber(lastStatement.getLastLineNumber()); } } } catch (ASTRuntimeException e) { throw new ASTParserException(e.getMessage() + ". File: " + sourceUnit.getName(), e); } return output; }
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) { for (Object statement : source.getAST().getStatementBlock().getStatements()) ((ExpressionStatement) statement).visit(new CustomCodeVisitorSupport()); } }
public void testStatementClass_FAILS() throws Exception { if (notYetImplemented()) return; ModuleNode module = parse("x = [1, 2, 3]; println(x)", "Cheese.groovy"); assertTrue("Should have statements", !module.getStatementBlock().isEmpty()); List classes = module.getClasses(); assertEquals("Number of classes", 1, classes.size()); ClassNode classNode = (ClassNode) classes.get(0); assertEquals("Class name", "Cheese", classNode.getName()); } }
unit.convert(); BlockStatement blockStat = unit.getAST().getStatementBlock(); Assert.that(blockStat != null && blockStat.getStatements().size() == 1); Statement stat = blockStat.getStatements().get(0);
ast.getStatementBlock().visit(visitor); for (ClassNode clNode : ast.getClasses()) { if (clNode!=classNode) {
public void visitModuleNode(ModuleNode moduleNode) { //visit imports like import java.io.File and import java.io.File as MyFile for (ImportNode importNode : moduleNode.getImports()) { visitNode(importNode.getType()); } //visit static imports like import java.lang.Math.* for (ImportNode importNode : moduleNode.getStaticStarImports().values()) { visitNode(importNode.getType()); } //visit static imports like import java.lang.Math.cos for (ImportNode importNode : moduleNode.getStaticImports().values()) { visitNode(importNode.getType()); } for (ClassNode classNode : moduleNode.getClasses()) { if (!classNode.isScript()) { visitClass(classNode); } else { for (MethodNode method : moduleNode.getMethods()) { visitMethod(method); } } } //visit Statements that are not inside a class if (!moduleNode.getStatementBlock().isEmpty()) { visitBlockStatement(moduleNode.getStatementBlock()); } }
if (sourceUnit.getAST().getStatementBlock() != null) { sourceUnit.getAST().getStatementBlock().visit(transformer);
public static void filterAndTransformStatements(SourceUnit source, StatementTransformer transformer) { ListIterator<Statement> statementIterator = source.getAST().getStatementBlock().getStatements().listIterator(); while (statementIterator.hasNext()) { Statement originalStatement = statementIterator.next(); Statement transformedStatement = transformer.transform(source, originalStatement); if (transformedStatement == null) { statementIterator.remove(); } else if (transformedStatement != originalStatement) { statementIterator.set(transformedStatement); } } }
@Override public void call(SourceUnit source) throws CompilationFailedException { ListIterator<Statement> iterator = source.getAST().getStatementBlock().getStatements().listIterator(); while (iterator.hasNext()) { if (spec.isSatisfiedBy(iterator.next())) { iterator.remove(); } } } }
private boolean isEmpty(SourceUnit source) { List<Statement> statements = source.getAST().getStatementBlock().getStatements(); for (Statement statement : statements) { if (AstUtils.mayHaveAnEffect(statement)) { return false; } } // No statements, or no statements that have an effect return true; }
public static ClassNode getScriptClass(SourceUnit source) { if (source.getAST().getStatementBlock().getStatements().isEmpty() && source.getAST().getMethods().isEmpty()) { // There is no script class when there are no statements or methods declared in the script return null; } return source.getAST().getClasses().get(0); }
public Map<OffsetRange, Set<ColoringAttributes>> annotate() { highlights.clear(); for (Object object : root.getClasses()) { visitClass((ClassNode)object); } for (Object object : root.getMethods()) { visitMethod((MethodNode)object); } visitBlockStatement(root.getStatementBlock()); return highlights; }
@Override public void call(SourceUnit source) throws CompilationFailedException { BlockStatement statementBlock = source.getAST().getStatementBlock(); List<Statement> statements = statementBlock.getStatements(); for (Statement statement : statements) { if (!AstUtils.mayHaveAnEffect(statement)) { continue; } ScriptBlock scriptBlock = AstUtils.detectScriptBlock(statement); if (scriptBlock != null && scriptBlock.getName().equals(ModelBlockTransformer.MODEL)) { continue; } imperativeStatementDetected = true; break; } } }
public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduction cst) throws ParserException { setClassLoader(classLoader); makeModule(); try { convertGroovy(ast); if (output.getStatementBlock().isEmpty() && output.getMethods().isEmpty() && output.getClasses().isEmpty()) { output.addStatement(ReturnStatement.RETURN_NULL_OR_VOID); } // set the script source position ClassNode scriptClassNode = output.getScriptClassDummy(); if (scriptClassNode != null) { List<Statement> statements = output.getStatementBlock().getStatements(); if (statements.size() > 0) { Statement firstStatement = statements.get(0); Statement lastStatement = statements.get(statements.size() - 1); scriptClassNode.setSourcePosition(firstStatement); scriptClassNode.setLastColumnNumber(lastStatement.getLastColumnNumber()); scriptClassNode.setLastLineNumber(lastStatement.getLastLineNumber()); } } } catch (ASTRuntimeException e) { throw new ASTParserException(e.getMessage() + ". File: " + sourceUnit.getName(), e); } return output; }
public static void visitScriptCode(SourceUnit source, GroovyCodeVisitor transformer) { source.getAST().getStatementBlock().visit(transformer); for (Object method : source.getAST().getMethods()) { MethodNode methodNode = (MethodNode) method; methodNode.getCode().visit(transformer); } }
public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduction cst) throws ParserException { setClassLoader(classLoader); makeModule(); try { convertGroovy(ast); if(output.getStatementBlock().isEmpty() && output.getMethods().isEmpty() && output.getClasses().isEmpty()) { output.addStatement(ReturnStatement.RETURN_NULL_OR_VOID); } } catch (ASTRuntimeException e) { throw new ASTParserException(e.getMessage() + ". File: " + sourceUnit.getName(), e); } return output; }
ast.getStatementBlock().visit(visitor); for (ClassNode clNode : ast.getClasses()) { if (clNode!=classNode) {
BlockStatement blockStatement = moduleNode.getStatementBlock(); VariableScope variableScope = blockStatement.getVariableScope(); if (variableScope.getDeclaredVariable(variable.getName()) != null) {