use of org.codehaus.groovy.ast.stmt.BlockStatement in project grails-core by grails.
the class TestMixinTransformation method getOrCreateMethodBody.
protected static BlockStatement getOrCreateMethodBody(ClassNode classNode, MethodNode methodNode, String name) {
BlockStatement methodBody;
if (!methodNode.getDeclaringClass().equals(classNode)) {
methodBody = new BlockStatement();
methodNode = new MethodNode(name, Modifier.PUBLIC, methodNode.getReturnType(), GrailsArtefactClassInjector.ZERO_PARAMETERS, null, methodBody);
classNode.addMethod(methodNode);
} else {
final Statement setupMethodBody = methodNode.getCode();
if (!(setupMethodBody instanceof BlockStatement)) {
methodBody = new BlockStatement();
if (setupMethodBody != null) {
if (!(setupMethodBody instanceof ReturnStatement)) {
methodBody.addStatement(setupMethodBody);
}
}
methodNode.setCode(methodBody);
} else {
methodBody = (BlockStatement) setupMethodBody;
}
}
return methodBody;
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.
the class ModuleNode method createStatementsClass.
protected ClassNode createStatementsClass() {
ClassNode classNode = getScriptClassDummy();
if (classNode.getName().endsWith("package-info")) {
return classNode;
}
handleMainMethodIfPresent(methods);
// return new Foo(new ShellContext(args)).run()
classNode.addMethod(new MethodNode("main", ACC_PUBLIC | ACC_STATIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ClassHelper.STRING_TYPE.makeArray(), "args") }, ClassNode.EMPTY_ARRAY, new ExpressionStatement(new MethodCallExpression(new ClassExpression(ClassHelper.make(InvokerHelper.class)), "runScript", new ArgumentListExpression(new ClassExpression(classNode), new VariableExpression("args"))))));
MethodNode methodNode = new MethodNode("run", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementBlock);
methodNode.setIsScriptBody();
classNode.addMethod(methodNode);
classNode.addConstructor(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BlockStatement());
Statement stmt;
// (like @BaseScript) that could change this. But this is cautious and anticipates possible compiler changes.
if (classNode.getSuperClass().getDeclaredConstructor(SCRIPT_CONTEXT_CTOR) != null) {
stmt = new ExpressionStatement(new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(new VariableExpression("context"))));
} else {
// Fallback for non-standard base "script" classes with no context (Binding) constructor.
stmt = new ExpressionStatement(new MethodCallExpression(new VariableExpression("super"), "setBinding", new ArgumentListExpression(new VariableExpression("context"))));
}
classNode.addConstructor(ACC_PUBLIC, new Parameter[] { new Parameter(ClassHelper.make(Binding.class), "context") }, ClassNode.EMPTY_ARRAY, stmt);
for (MethodNode node : methods) {
int modifiers = node.getModifiers();
if ((modifiers & ACC_ABSTRACT) != 0) {
throw new RuntimeException("Cannot use abstract methods in a script, they are only available inside classes. Method: " + node.getName());
}
// br: the old logic seems to add static to all def f().... in a script, which makes enclosing
// inner classes (including closures) in a def function difficult. Comment it out.
node.setModifiers(modifiers);
classNode.addMethod(node);
}
return classNode;
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.
the class ClassNode method addStaticInitializerStatements.
public void addStaticInitializerStatements(List<Statement> staticStatements, boolean fieldInit) {
MethodNode method = getOrAddStaticConstructorNode();
BlockStatement block = null;
Statement statement = method.getCode();
if (statement == null) {
block = new BlockStatement();
} else if (statement instanceof BlockStatement) {
block = (BlockStatement) statement;
} else {
block = new BlockStatement();
block.addStatement(statement);
}
// before the other statements
if (!fieldInit) {
block.addStatements(staticStatements);
} else {
List<Statement> blockStatements = block.getStatements();
staticStatements.addAll(blockStatements);
blockStatements.clear();
blockStatements.addAll(staticStatements);
}
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.
the class ClassNode method positionStmtsAfterEnumInitStmts.
public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) {
MethodNode method = getOrAddStaticConstructorNode();
Statement statement = method.getCode();
if (statement instanceof BlockStatement) {
BlockStatement block = (BlockStatement) statement;
// add given statements for explicitly declared static fields just after enum-special fields
// are found - the $VALUES binary expression marks the end of such fields.
List<Statement> blockStatements = block.getStatements();
ListIterator<Statement> litr = blockStatements.listIterator();
while (litr.hasNext()) {
Statement stmt = litr.next();
if (stmt instanceof ExpressionStatement && ((ExpressionStatement) stmt).getExpression() instanceof BinaryExpression) {
BinaryExpression bExp = (BinaryExpression) ((ExpressionStatement) stmt).getExpression();
if (bExp.getLeftExpression() instanceof FieldExpression) {
FieldExpression fExp = (FieldExpression) bExp.getLeftExpression();
if (fExp.getFieldName().equals("$VALUES")) {
for (Statement tmpStmt : staticFieldStatements) {
litr.add(tmpStmt);
}
}
}
}
}
}
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project grails-core by grails.
the class GroovyPageBytecodeOptimizer method performInjection.
public void performInjection(SourceUnit source, GeneratorContext context, ClassNode classNode) {
// search run method in GSP script and get codeblock
MethodNode runMethod = classNode.getMethod(RUN_METHOD, new Parameter[0]);
if (runMethod != null && runMethod.getCode() instanceof BlockStatement) {
BlockStatement block = (BlockStatement) runMethod.getCode();
//scan all MethodExpressionCalls to optimize them
GroovyPageOptimizerVisitor groovyPageVisitor = new GroovyPageOptimizerVisitor(classNode);
groovyPageVisitor.visitBlockStatement(block);
}
}
Aggregations