public List<ClassNode> getClasses() { if (createClassForStatements && (!statementBlock.isEmpty() || !methods.isEmpty() || isPackageInfo())) { ClassNode mainClass = createStatementsClass(); mainClassName = mainClass.getName(); createClassForStatements = false; classes.add(0, mainClass); mainClass.setModule(this); addToCompileUnit(mainClass); } return classes; }
private static Expression getConstructorCall(Statement code) { if (code==null) return null; if (code instanceof BlockStatement) { BlockStatement bs = (BlockStatement) code; if (bs.isEmpty()) return null; return getConstructorCall(bs.getStatements().get(0)); } if (!(code instanceof ExpressionStatement)) return null; ExpressionStatement es = (ExpressionStatement) code; Expression exp = es.getExpression(); if (!(exp instanceof ConstructorCallExpression)) return null; ConstructorCallExpression cce = (ConstructorCallExpression) exp; if (!cce.isSpecialCall()) return null; return cce; }
private void handleMainMethodIfPresent(List methods) { boolean found = false; for (Iterator iter = methods.iterator(); iter.hasNext();) { MethodNode node = (MethodNode) iter.next(); if(node.getName().equals("main")) { if (node.isStatic() && node.getParameters().length == 1) { boolean retTypeMatches, argTypeMatches; ClassNode argType = node.getParameters()[0].getType(); ClassNode retType = node.getReturnType(); argTypeMatches = (argType.equals(ClassHelper.OBJECT_TYPE) || argType.getName().contains("String[]")); retTypeMatches = (retType == ClassHelper.VOID_TYPE || retType == ClassHelper.OBJECT_TYPE); if(retTypeMatches && argTypeMatches) { if(found) { throw new RuntimeException("Repetitive main method found."); } else { found = true; } // if script has both loose statements as well as main(), then main() is ignored if(statementBlock.isEmpty()) { addStatement(node.getCode()); } iter.remove(); } } } } }
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 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()); } }
@Override public void visitBlockStatement(BlockStatement block) { opt.push(); boolean optAll = true; for (Statement statement : block.getStatements()) { opt.push(); statement.visit(this); optAll = optAll && opt.canOptimize(); opt.pop(true); } if (block.isEmpty()) { opt.chainCanOptimize(true); opt.pop(true); } else { opt.chainShouldOptimize(optAll); if (optAll) addMeta(block,opt); opt.pop(optAll); } }
body.addStatement(stmt(ctorX(ClassNode.SUPER, args(superParams)))); if (!preBody.isEmpty()) { body.addStatements(preBody.getStatements()); cNode.addConstructor(consNode); if (sourceUnit != null && !body.isEmpty()) { VariableScopeVisitor scopeVisitor = new VariableScopeVisitor(sourceUnit); scopeVisitor.visitClass(cNode); if (body.isEmpty()) { body.addStatement(new ExpressionStatement(ConstantExpression.EMPTY_EXPRESSION));
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()); } }
public static ASTNode getEmptyBlock(Statement origStatement) { Stack<ASTNode> stack = new Stack<ASTNode>(); stack.push(origStatement); while (!stack.isEmpty()) { ASTNode statement = stack.pop(); if (!(statement instanceof BlockStatement)) { return null; } if (((BlockStatement) statement).isEmpty()) { return statement; } if (((BlockStatement) statement).getStatements().size() != 1) { return null; } stack.push(((BlockStatement) statement).getStatements().get(0)); } return null; }
public List getClasses() { if (createClassForStatements && (!statementBlock.isEmpty() || !methods.isEmpty())) { ClassNode mainClass = createStatementsClass(); createClassForStatements = false; classes.add(0, mainClass); mainClass.setModule(this); addToCompileUnit(mainClass); } return classes; }
/** * Return true if the Statement is a block and it is empty (contains no "meaningful" statements). * This implementation also addresses some "weirdness" around some statement types (specifically finally) * where the BlockStatement answered false to isEmpty() even if it was. * @param origStatement - the Statement to check * @return true if the BlockStatement is empty */ public static boolean isEmptyBlock(Statement origStatement) { Stack<ASTNode> stack = new Stack<ASTNode>(); stack.push(origStatement); while (!stack.isEmpty()) { ASTNode statement = stack.pop(); if (!(statement instanceof BlockStatement)) { return false; } if (((BlockStatement) statement).isEmpty()) { return true; } if (((BlockStatement) statement).getStatements().size() != 1) { return false; } stack.push(((BlockStatement) statement).getStatements().get(0)); } return false; }
public List getClasses() { if (createClassForStatements && (!statementBlock.isEmpty() || !methods.isEmpty())) { ClassNode mainClass = createStatementsClass(); createClassForStatements = false; classes.add(0, mainClass); mainClass.setModule(this); addToCompileUnit(mainClass); } return classes; }
public List getClasses() { if (createClassForStatements && (!statementBlock.isEmpty() || !methods.isEmpty())) { ClassNode mainClass = createStatementsClass(); createClassForStatements = false; classes.add(0, mainClass); mainClass.setModule(this); addToCompileUnit(mainClass); } return classes; }
public List<ClassNode> getClasses() { if (createClassForStatements && (!statementBlock.isEmpty() || !methods.isEmpty() || isPackageInfo())) { ClassNode mainClass = createStatementsClass(); mainClassName = mainClass.getName(); createClassForStatements = false; classes.add(0, mainClass); mainClass.setModule(this); addToCompileUnit(mainClass); } return classes; }
private void handleMainMethodIfPresent(List methods) { for (Iterator iter = methods.iterator(); iter.hasNext();) { MethodNode node = (MethodNode) iter.next(); if(node.getName().equals("main")) { int modifiers = node.getModifiers(); if (node.isStatic() && node.getParameters().length == 1) { boolean retTypeMatches, argTypeMatches; ClassNode argType = node.getParameters()[0].getType(); ClassNode retType = node.getReturnType(); argTypeMatches = (argType.equals(ClassHelper.OBJECT_TYPE) || argType.getName().contains("String[]")); retTypeMatches = (retType == ClassHelper.VOID_TYPE || retType == ClassHelper.OBJECT_TYPE); if(retTypeMatches && argTypeMatches) { // if script has both loose statements as well as main(), then main() is ignored if(statementBlock.isEmpty()) { addStatement(node.getCode()); } iter.remove(); } } } } }
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; }
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; }
@Override public void visitBlockStatement(BlockStatement block) { if (block.isEmpty() || this.xformed) { return; } ClosureExpression closure = beans(block); if (closure != null) { // Add a marker interface to the current script this.classNode.addInterface(ClassHelper.make(SOURCE_INTERFACE)); // Implement the interface by adding a public read-only property with the // same name as the method in the interface (getBeans). Make it return the // closure. this.classNode.addProperty( new PropertyNode(BEANS, Modifier.PUBLIC | Modifier.FINAL, ClassHelper.CLOSURE_TYPE.getPlainNodeReference(), this.classNode, closure, null, null)); // Only do this once per class this.xformed = true; } }
@Override public void visitBlockStatement(BlockStatement block) { opt.push(); boolean optAll = true; for (Statement statement : block.getStatements()) { opt.push(); statement.visit(this); optAll = optAll && opt.canOptimize(); opt.pop(true); } if (block.isEmpty()) { opt.chainCanOptimize(true); opt.pop(true); } else { opt.chainShouldOptimize(optAll); if (optAll) addMeta(block,opt); opt.pop(optAll); } }
@Override public void visitBlockStatement(BlockStatement block) { if (block.isEmpty() || this.xformed) { return; } ClosureExpression closure = ratpack(block); if (closure != null) { this.classNode.addAnnotation(new AnnotationNode(ClassHelper .make("Component"))); // Add a marker interface to the current script this.classNode.addInterface(ClassHelper.make(SOURCE_INTERFACE)); // Implement the interface by adding a public read-only property with the // same name as the method in the interface (getRatpack). Make it return the // closure. this.classNode.addProperty(new PropertyNode(RATPACK, Modifier.PUBLIC | Modifier.FINAL, ClassHelper.CLOSURE_TYPE .getPlainNodeReference(), this.classNode, closure, null, null)); // Only do this once per class this.xformed = true; } }