use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class ImmutableASTTransformation method createConstructorOrdered.
private static void createConstructorOrdered(ClassNode cNode, List<PropertyNode> list) {
final MapExpression argMap = new MapExpression();
final Parameter[] orderedParams = new Parameter[list.size()];
int index = 0;
for (PropertyNode pNode : list) {
Parameter param = new Parameter(pNode.getField().getType(), pNode.getField().getName());
orderedParams[index++] = param;
argMap.addMapEntryExpression(constX(pNode.getName()), varX(pNode.getName()));
}
final BlockStatement orderedBody = new BlockStatement();
orderedBody.addStatement(stmt(ctorX(ClassNode.THIS, args(castX(HASHMAP_TYPE, argMap)))));
doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, orderedParams, ClassNode.EMPTY_ARRAY, orderedBody));
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class ImmutableASTTransformation method createConstructorMapCommon.
private static void createConstructorMapCommon(ClassNode cNode, BlockStatement body) {
final List<FieldNode> fList = cNode.getFields();
for (FieldNode fNode : fList) {
// public fields will be rejected elsewhere
if (fNode.isPublic())
continue;
// a property
if (cNode.getProperty(fNode.getName()) != null)
continue;
if (fNode.isFinal() && fNode.isStatic())
continue;
// internal field
if (fNode.getName().contains("$") || fNode.isSynthetic())
continue;
if (fNode.isFinal() && fNode.getInitialExpression() != null)
body.addStatement(checkFinalArgNotOverridden(cNode, fNode));
body.addStatement(createConstructorStatementDefault(fNode));
}
doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, params(new Parameter(HASHMAP_TYPE, "args")), ClassNode.EMPTY_ARRAY, body));
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class InheritConstructorsASTTransformation method processClass.
private void processClass(ClassNode cNode, AnnotationNode node) {
if (cNode.isInterface()) {
addError("Error processing interface '" + cNode.getName() + "'. " + MY_TYPE_NAME + " only allowed for classes.", cNode);
return;
}
boolean copyConstructorAnnotations = memberHasValue(node, "constructorAnnotations", true);
boolean copyParameterAnnotations = memberHasValue(node, "parameterAnnotations", true);
ClassNode sNode = cNode.getSuperClass();
List<AnnotationNode> superAnnotations = sNode.getAnnotations(MY_TYPE);
if (superAnnotations.size() == 1) {
// We need @InheritConstructors from parent classes processed first
// so force that order here. The transformation is benign on an already
// processed node so processing twice in any order won't matter bar
// a very small time penalty.
processClass(sNode, node);
}
for (ConstructorNode cn : sNode.getDeclaredConstructors()) {
addConstructorUnlessAlreadyExisting(cNode, cn, copyConstructorAnnotations, copyParameterAnnotations);
}
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class InheritConstructorsASTTransformation method addConstructorUnlessAlreadyExisting.
private void addConstructorUnlessAlreadyExisting(ClassNode classNode, ConstructorNode consNode, boolean copyConstructorAnnotations, boolean copyParameterAnnotations) {
Parameter[] origParams = consNode.getParameters();
if (consNode.isPrivate())
return;
Parameter[] params = new Parameter[origParams.length];
Map<String, ClassNode> genericsSpec = createGenericsSpec(classNode);
extractSuperClassGenerics(classNode, classNode.getSuperClass(), genericsSpec);
List<Expression> theArgs = buildParams(origParams, params, genericsSpec, copyParameterAnnotations);
if (isExisting(classNode, params))
return;
ConstructorNode added = classNode.addConstructor(consNode.getModifiers(), params, consNode.getExceptions(), block(ctorSuperS(args(theArgs))));
if (copyConstructorAnnotations) {
added.addAnnotations(copyAnnotatedNodeAnnotations(consNode, MY_TYPE_NAME));
}
}
use of org.codehaus.groovy.ast.ConstructorNode in project gcontracts by andresteingress.
the class EnsuresAnnotationProcessor method process.
@Override
public void process(ProcessingContextInformation processingContextInformation, Contract contract, ClassNode classNode, MethodNode methodNode, BlockStatement blockStatement, BooleanExpression booleanExpression) {
if (!processingContextInformation.isPostconditionsEnabled())
return;
if (booleanExpression == null)
return;
final List<ConstructorNode> declaredConstructors = classNode.getDeclaredConstructors();
contract.postconditions().and(methodNode, new Postcondition(blockStatement, booleanExpression, declaredConstructors.contains(methodNode)));
}
Aggregations