use of org.codehaus.groovy.ast.Parameter in project groovy by apache.
the class AntlrParserPlugin method blockExpression.
protected Expression blockExpression(AST node) {
AST codeNode = node.getFirstChild();
if (codeNode == null)
return ConstantExpression.NULL;
if (codeNode.getType() == EXPR && codeNode.getNextSibling() == null) {
// Simplify common case of {expr} to expr.
return expression(codeNode);
}
Parameter[] parameters = Parameter.EMPTY_ARRAY;
Statement code = statementListNoChild(codeNode, node);
ClosureExpression closureExpression = new ClosureExpression(parameters, code);
configureAST(closureExpression, node);
// Call it immediately.
String callName = "call";
Expression noArguments = new ArgumentListExpression();
MethodCallExpression call = new MethodCallExpression(closureExpression, callName, noArguments);
configureAST(call, node);
return call;
}
use of org.codehaus.groovy.ast.Parameter in project groovy by apache.
the class AntlrParserPlugin method forStatement.
protected Statement forStatement(AST forNode) {
AST inNode = forNode.getFirstChild();
Expression collectionExpression;
Parameter forParameter;
if (isType(CLOSURE_LIST, inNode)) {
forStatementBeingDef = true;
ClosureListExpression clist = closureListExpression(inNode);
forStatementBeingDef = false;
int size = clist.getExpressions().size();
if (size != 3) {
throw new ASTRuntimeException(inNode, "3 expressions are required for the classic for loop, you gave " + size);
}
collectionExpression = clist;
forParameter = ForStatement.FOR_LOOP_DUMMY;
} else {
AST variableNode = inNode.getFirstChild();
AST collectionNode = variableNode.getNextSibling();
ClassNode type = ClassHelper.OBJECT_TYPE;
if (isType(VARIABLE_DEF, variableNode)) {
AST node = variableNode.getFirstChild();
// skip the final modifier if it's present
if (isType(MODIFIERS, node)) {
int modifiersMask = modifiers(node, new ArrayList<AnnotationNode>(), 0);
// only final modifier allowed
if ((modifiersMask & ~Opcodes.ACC_FINAL) != 0) {
throw new ASTRuntimeException(node, "Only the 'final' modifier is allowed in front of the for loop variable.");
}
node = node.getNextSibling();
}
type = makeTypeWithArguments(node);
variableNode = node.getNextSibling();
}
String variable = identifier(variableNode);
collectionExpression = expression(collectionNode);
forParameter = new Parameter(type, variable);
configureAST(forParameter, variableNode);
}
final AST node = inNode.getNextSibling();
Statement block;
if (isType(SEMI, node)) {
block = EmptyStatement.INSTANCE;
} else {
block = statement(node);
}
ForStatement forStatement = new ForStatement(forParameter, collectionExpression, block);
configureAST(forStatement, forNode);
return forStatement;
}
use of org.codehaus.groovy.ast.Parameter in project groovy by apache.
the class AntlrParserPlugin method closureExpression.
protected ClosureExpression closureExpression(AST node) {
AST paramNode = node.getFirstChild();
Parameter[] parameters = null;
AST codeNode = paramNode;
if (isType(PARAMETERS, paramNode) || isType(IMPLICIT_PARAMETERS, paramNode)) {
parameters = parameters(paramNode);
codeNode = paramNode.getNextSibling();
}
Statement code = statementListNoChild(codeNode, node);
ClosureExpression closureExpression = new ClosureExpression(parameters, code);
configureAST(closureExpression, node);
return closureExpression;
}
use of org.codehaus.groovy.ast.Parameter in project groovy by apache.
the class AntlrParserPlugin method methodDef.
protected void methodDef(AST methodDef) {
MethodNode oldNode = methodNode;
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST node = methodDef.getFirstChild();
GenericsType[] generics = null;
if (isType(TYPE_PARAMETERS, node)) {
generics = makeGenericsType(node);
node = node.getNextSibling();
}
int modifiers = Opcodes.ACC_PUBLIC;
if (isType(MODIFIERS, node)) {
modifiers = modifiers(node, annotations, modifiers);
checkNoInvalidModifier(methodDef, "Method", modifiers, Opcodes.ACC_VOLATILE, "volatile");
node = node.getNextSibling();
}
if (isAnInterface()) {
modifiers |= Opcodes.ACC_ABSTRACT;
}
ClassNode returnType = null;
if (isType(TYPE, node)) {
returnType = makeTypeWithArguments(node);
node = node.getNextSibling();
}
String name = identifier(node);
if (classNode != null && !classNode.isAnnotationDefinition()) {
if (classNode.getNameWithoutPackage().equals(name)) {
if (isAnInterface()) {
throw new ASTRuntimeException(methodDef, "Constructor not permitted within an interface.");
}
throw new ASTRuntimeException(methodDef, "Invalid constructor format. Remove '" + returnType.getName() + "' as the return type if you want a constructor, or use a different name if you want a method.");
}
}
node = node.getNextSibling();
Parameter[] parameters = Parameter.EMPTY_ARRAY;
ClassNode[] exceptions = ClassNode.EMPTY_ARRAY;
if (classNode == null || !classNode.isAnnotationDefinition()) {
assertNodeType(PARAMETERS, node);
parameters = parameters(node);
if (parameters == null)
parameters = Parameter.EMPTY_ARRAY;
node = node.getNextSibling();
if (isType(LITERAL_throws, node)) {
AST throwsNode = node.getFirstChild();
List<ClassNode> exceptionList = new ArrayList<ClassNode>();
throwsList(throwsNode, exceptionList);
exceptions = exceptionList.toArray(exceptions);
node = node.getNextSibling();
}
}
boolean hasAnnotationDefault = false;
Statement code = null;
boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
modifiers &= ~Opcodes.ACC_SYNTHETIC;
methodNode = new MethodNode(name, modifiers, returnType, parameters, exceptions, code);
if ((modifiers & Opcodes.ACC_ABSTRACT) == 0) {
if (node == null) {
throw new ASTRuntimeException(methodDef, "You defined a method without body. Try adding a body, or declare it abstract.");
}
assertNodeType(SLIST, node);
code = statementList(node);
} else if (node != null && classNode.isAnnotationDefinition()) {
code = statement(node);
hasAnnotationDefault = true;
} else if ((modifiers & Opcodes.ACC_ABSTRACT) > 0) {
if (node != null) {
throw new ASTRuntimeException(methodDef, "Abstract methods do not define a body.");
}
}
methodNode.setCode(code);
methodNode.addAnnotations(annotations);
methodNode.setGenericsTypes(generics);
methodNode.setAnnotationDefault(hasAnnotationDefault);
methodNode.setSyntheticPublic(syntheticPublic);
configureAST(methodNode, methodDef);
if (classNode != null) {
classNode.addMethod(methodNode);
} else {
output.addMethod(methodNode);
}
methodNode = oldNode;
}
use of org.codehaus.groovy.ast.Parameter in project groovy by apache.
the class TupleConstructorASTTransformation method createConstructor.
public static void createConstructor(AbstractASTTransformation xform, ClassNode cNode, boolean includeFields, boolean includeProperties, boolean includeSuperFields, boolean includeSuperProperties, boolean callSuper, boolean force, List<String> excludes, final List<String> includes, boolean useSetters, boolean defaults, boolean allNames, SourceUnit sourceUnit, ClosureExpression pre, ClosureExpression post) {
// no processing if existing constructors found
if (!cNode.getDeclaredConstructors().isEmpty() && !force)
return;
List<FieldNode> superList = new ArrayList<FieldNode>();
if (includeSuperProperties) {
superList.addAll(getSuperPropertyFields(cNode.getSuperClass()));
}
if (includeSuperFields) {
superList.addAll(getSuperNonPropertyFields(cNode.getSuperClass()));
}
List<FieldNode> list = new ArrayList<FieldNode>();
if (includeProperties) {
list.addAll(getInstancePropertyFields(cNode));
}
if (includeFields) {
list.addAll(getInstanceNonPropertyFields(cNode));
}
final List<Parameter> params = new ArrayList<Parameter>();
final List<Expression> superParams = new ArrayList<Expression>();
final BlockStatement preBody = new BlockStatement();
boolean superInPre = false;
if (pre != null) {
superInPre = copyStatementsWithSuperAdjustment(pre, preBody);
if (superInPre && callSuper) {
xform.addError("Error during " + MY_TYPE_NAME + " processing, can't have a super call in 'pre' " + "closure and also 'callSuper' enabled", cNode);
}
}
final BlockStatement body = new BlockStatement();
for (FieldNode fNode : superList) {
String name = fNode.getName();
if (shouldSkipUndefinedAware(name, excludes, includes, allNames))
continue;
params.add(createParam(fNode, name, defaults, xform));
boolean hasSetter = cNode.getProperty(name) != null && !fNode.isFinal();
if (callSuper) {
superParams.add(varX(name));
} else if (!superInPre) {
if (useSetters && hasSetter) {
body.addStatement(stmt(callThisX(getSetterName(name), varX(name))));
} else {
body.addStatement(assignS(propX(varX("this"), name), varX(name)));
}
}
}
if (callSuper) {
body.addStatement(stmt(ctorX(ClassNode.SUPER, args(superParams))));
}
if (!preBody.isEmpty()) {
body.addStatements(preBody.getStatements());
}
for (FieldNode fNode : list) {
String name = fNode.getName();
if (shouldSkipUndefinedAware(name, excludes, includes, allNames))
continue;
Parameter nextParam = createParam(fNode, name, defaults, xform);
params.add(nextParam);
boolean hasSetter = cNode.getProperty(name) != null && !fNode.isFinal();
if (useSetters && hasSetter) {
body.addStatement(stmt(callThisX(getSetterName(name), varX(nextParam))));
} else {
body.addStatement(assignS(propX(varX("this"), name), varX(nextParam)));
}
}
if (post != null) {
body.addStatement(post.getCode());
}
if (includes != null) {
Comparator<Parameter> includeComparator = new Comparator<Parameter>() {
public int compare(Parameter p1, Parameter p2) {
return new Integer(includes.indexOf(p1.getName())).compareTo(includes.indexOf(p2.getName()));
}
};
Collections.sort(params, includeComparator);
}
cNode.addConstructor(new ConstructorNode(ACC_PUBLIC, params.toArray(new Parameter[params.size()]), ClassNode.EMPTY_ARRAY, body));
if (sourceUnit != null && !body.isEmpty()) {
VariableScopeVisitor scopeVisitor = new VariableScopeVisitor(sourceUnit);
scopeVisitor.visitClass(cNode);
}
// or if there is only one Map property (for backwards compatibility)
if (!params.isEmpty() && defaults) {
ClassNode firstParam = params.get(0).getType();
if (params.size() > 1 || firstParam.equals(ClassHelper.OBJECT_TYPE)) {
String message = "The class " + cNode.getName() + " was incorrectly initialized via the map constructor with null.";
if (firstParam.equals(ClassHelper.MAP_TYPE)) {
addMapConstructors(cNode, true, message);
} else {
ClassNode candidate = HMAP_TYPE;
while (candidate != null) {
if (candidate.equals(firstParam)) {
addMapConstructors(cNode, true, message);
break;
}
candidate = candidate.getSuperClass();
}
}
}
}
}
Aggregations