use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class AntlrParserPlugin method enumConstantDef.
protected void enumConstantDef(AST node) {
enumConstantBeingDef = true;
assertNodeType(ENUM_CONSTANT_DEF, node);
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST element = node.getFirstChild();
if (isType(ANNOTATIONS, element)) {
processAnnotations(annotations, element);
element = element.getNextSibling();
}
String identifier = identifier(element);
Expression init = null;
element = element.getNextSibling();
if (element != null) {
init = expression(element);
ClassNode innerClass;
if (element.getNextSibling() == null) {
innerClass = getAnonymousInnerClassNode(init);
if (innerClass != null) {
init = null;
}
} else {
element = element.getNextSibling();
Expression next = expression(element);
innerClass = getAnonymousInnerClassNode(next);
}
if (innerClass != null) {
// we have to handle an enum constant with a class overriding
// a method in which case we need to configure the inner class
innerClass.setSuperClass(classNode.getPlainNodeReference());
innerClass.setModifiers(classNode.getModifiers() | Opcodes.ACC_FINAL);
// we use a ClassExpression for transportation to EnumVisitor
Expression inner = new ClassExpression(innerClass);
if (init == null) {
ListExpression le = new ListExpression();
le.addExpression(inner);
init = le;
} else {
if (init instanceof ListExpression) {
((ListExpression) init).addExpression(inner);
} else {
ListExpression le = new ListExpression();
le.addExpression(init);
le.addExpression(inner);
init = le;
}
}
// and remove the final modifier from classNode to allow the sub class
classNode.setModifiers(classNode.getModifiers() & ~Opcodes.ACC_FINAL);
} else if (isType(ELIST, element)) {
if (init instanceof ListExpression && !((ListExpression) init).isWrapped()) {
ListExpression le = new ListExpression();
le.addExpression(init);
init = le;
}
}
}
FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
enumField.addAnnotations(annotations);
configureAST(enumField, node);
enumConstantBeingDef = false;
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class InitializerStrategy method initializeFields.
private static void initializeFields(List<FieldNode> fields, BlockStatement body, boolean useSetters) {
for (FieldNode field : fields) {
String name = field.getName();
body.addStatement(stmt(useSetters && !field.isFinal() ? callThisX(getSetterName(name), varX(param(field.getType(), name))) : assignX(propX(varX("this"), field.getName()), varX(param(field.getType(), name)))));
}
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class InitializerStrategy method createBuilderForAnnotatedMethod.
private void createBuilderForAnnotatedMethod(BuilderASTTransformation transform, MethodNode mNode, AnnotationNode anno, boolean useSetters) {
if (transform.getMemberValue(anno, "includes") != null || transform.getMemberValue(anno, "excludes") != null) {
transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME + " processing: includes/excludes only allowed on classes", anno);
}
if (mNode instanceof ConstructorNode) {
mNode.setModifiers(ACC_PRIVATE | ACC_SYNTHETIC);
} else {
if ((mNode.getModifiers() & ACC_STATIC) == 0) {
transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME + " processing: method builders only allowed on static methods", anno);
}
mNode.setModifiers(ACC_PRIVATE | ACC_SYNTHETIC | ACC_STATIC);
}
ClassNode buildee = mNode.getDeclaringClass();
Parameter[] parameters = mNode.getParameters();
if (parameters.length == 0) {
transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME + " processing: at least one parameter is required for this strategy", anno);
}
ClassNode builder = createInnerHelperClass(buildee, getBuilderClassName(buildee, anno), parameters.length);
List<FieldNode> convertedFields = convertParamsToFields(builder, parameters);
buildCommon(buildee, anno, convertedFields, builder);
if (mNode instanceof ConstructorNode) {
createBuildeeConstructors(transform, buildee, builder, convertedFields, false, useSetters);
} else {
createBuildeeMethods(buildee, mNode, builder, convertedFields);
}
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class InitializerStrategy method createInitializerConstructor.
private static ConstructorNode createInitializerConstructor(ClassNode buildee, ClassNode builder, List<FieldNode> fields) {
ClassNode paramType = makeClassSafeWithGenerics(builder, setGenTypes(fields.size()));
List<Expression> argsList = new ArrayList<Expression>();
Parameter initParam = param(paramType, "initializer");
for (FieldNode fieldNode : fields) {
argsList.add(propX(varX(initParam), fieldNode.getName()));
}
return buildee.addConstructor(ACC_PUBLIC, params(param(paramType, "initializer")), NO_EXCEPTIONS, block(ctorThisS(args(argsList))));
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class InitializerStrategy method getParams.
private static Parameter[] getParams(List<FieldNode> fields, ClassNode cNode) {
Parameter[] parameters = new Parameter[fields.size()];
for (int i = 0; i < parameters.length; i++) {
FieldNode fNode = fields.get(i);
Map<String, ClassNode> genericsSpec = createGenericsSpec(fNode.getDeclaringClass());
extractSuperClassGenerics(fNode.getType(), cNode, genericsSpec);
ClassNode correctedType = correctToGenericsSpecRecurse(genericsSpec, fNode.getType());
parameters[i] = new Parameter(correctedType, fNode.getName());
}
return parameters;
}
Aggregations