use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class InnerClassCompletionVisitor method addThisReference.
private void addThisReference(ConstructorNode node) {
if (!shouldHandleImplicitThisForInnerClass(classNode))
return;
Statement code = node.getCode();
// add "this$0" field init
//add this parameter to node
Parameter[] params = node.getParameters();
Parameter[] newParams = new Parameter[params.length + 1];
System.arraycopy(params, 0, newParams, 1, params.length);
String name = getUniqueName(params, node);
Parameter thisPara = new Parameter(classNode.getOuterClass().getPlainNodeReference(), name);
newParams[0] = thisPara;
node.setParameters(newParams);
BlockStatement block = null;
if (code == null) {
block = new BlockStatement();
} else if (!(code instanceof BlockStatement)) {
block = new BlockStatement();
block.addStatement(code);
} else {
block = (BlockStatement) code;
}
BlockStatement newCode = new BlockStatement();
addFieldInit(thisPara, thisField, newCode);
ConstructorCallExpression cce = getFirstIfSpecialConstructorCall(block);
if (cce == null) {
cce = new ConstructorCallExpression(ClassNode.SUPER, new TupleExpression());
block.getStatements().add(0, new ExpressionStatement(cce));
}
if (shouldImplicitlyPassThisPara(cce)) {
// add thisPara to this(...)
TupleExpression args = (TupleExpression) cce.getArguments();
List<Expression> expressions = args.getExpressions();
VariableExpression ve = new VariableExpression(thisPara.getName());
ve.setAccessedVariable(thisPara);
expressions.add(0, ve);
}
if (cce.isSuperCall()) {
// we have a call to super here, so we need to add
// our code after that
block.getStatements().add(1, newCode);
}
node.setCode(block);
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class InnerClassCompletionVisitor method getFirstIfSpecialConstructorCall.
private static ConstructorCallExpression getFirstIfSpecialConstructorCall(BlockStatement code) {
if (code == null)
return null;
final List<Statement> statementList = code.getStatements();
if (statementList.isEmpty())
return null;
final Statement statement = statementList.get(0);
if (!(statement instanceof ExpressionStatement))
return null;
Expression expression = ((ExpressionStatement) statement).getExpression();
if (!(expression instanceof ConstructorCallExpression))
return null;
ConstructorCallExpression cce = (ConstructorCallExpression) expression;
if (cce.isSpecialCall())
return cce;
return null;
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class VariableScopeVisitor method visitConstructorCallExpression.
public void visitConstructorCallExpression(ConstructorCallExpression call) {
isSpecialConstructorCall = call.isSpecialCall();
super.visitConstructorCallExpression(call);
isSpecialConstructorCall = false;
if (!call.isUsingAnonymousInnerClass())
return;
pushState();
InnerClassNode innerClass = (InnerClassNode) call.getType();
innerClass.setVariableScope(currentScope);
for (MethodNode method : innerClass.getMethods()) {
Parameter[] parameters = method.getParameters();
// null means no implicit "it"
if (parameters.length == 0)
parameters = null;
ClosureExpression cl = new ClosureExpression(parameters, method.getCode());
visitClosureExpression(cl);
}
for (FieldNode field : innerClass.getFields()) {
final Expression expression = field.getInitialExpression();
pushState(field.isStatic());
if (expression != null) {
if (expression instanceof VariableExpression) {
VariableExpression vexp = (VariableExpression) expression;
if (vexp.getAccessedVariable() instanceof Parameter) {
// workaround for GROOVY-6834: accessing a parameter which is not yet seen in scope
popState();
continue;
}
}
expression.visit(this);
}
popState();
}
for (Statement statement : innerClass.getObjectInitializerStatements()) {
statement.visit(this);
}
markClosureSharedVariables();
popState();
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class Verifier method visitMethod.
public void visitMethod(MethodNode node) {
//GROOVY-3712 - if it's an MOP method, it's an error as they aren't supposed to exist before ACG is invoked
if (MopWriter.isMopMethod(node.getName())) {
throw new RuntimeParserException("Found unexpected MOP methods in the class node for " + classNode.getName() + "(" + node.getName() + ")", classNode);
}
this.methodNode = node;
adjustTypesIfStaticMainMethod(node);
addReturnIfNeeded(node);
Statement statement;
statement = node.getCode();
if (statement != null)
statement.visit(new VerifierCodeVisitor(this));
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class Verifier method addDefaultParameterConstructors.
protected void addDefaultParameterConstructors(final ClassNode node) {
List methods = new ArrayList(node.getDeclaredConstructors());
addDefaultParameters(methods, new DefaultArgsAction() {
public void call(ArgumentListExpression arguments, Parameter[] newParams, MethodNode method) {
ConstructorNode ctor = (ConstructorNode) method;
ConstructorCallExpression expression = new ConstructorCallExpression(ClassNode.THIS, arguments);
Statement code = new ExpressionStatement(expression);
addConstructor(newParams, ctor, code, node);
}
});
}
Aggregations