in src/main/java/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java [176:321]
private static void createConstructor(final AbstractASTTransformation xform, final AnnotationNode anno, final ClassNode cNode, final boolean includeFields,
final boolean includeProperties, final boolean includeSuperFields, final boolean includeSuperProperties,
final List<String> excludes, final List<String> includes, final boolean allNames, final boolean allProperties,
final SourceUnit sourceUnit, final PropertyHandler handler, final ClosureExpression pre, final ClosureExpression post) {
boolean namedVariant = xform.memberHasValue(anno, "namedVariant", Boolean.TRUE);
boolean callSuper = xform.memberHasValue(anno, "callSuper", Boolean.TRUE);
DefaultsMode defaultsMode = maybeDefaultsMode(anno, "defaultsMode");
if (defaultsMode == null) {
boolean defaults = anno.getMember("defaults") == null
|| !xform.memberHasValue(anno, "defaults", Boolean.FALSE);
defaultsMode = defaults ? ON : OFF;
}
boolean force = xform.memberHasValue(anno, "force", Boolean.TRUE);
boolean makeImmutable = makeImmutable(cNode);
// no processing if explicit constructor(s) found, unless forced or ImmutableBase is in play
if (!force && !makeImmutable && hasExplicitConstructor(null, cNode)) return;
boolean includePseudoGetters = false, includePseudoSetters = allProperties, skipReadOnly = true;
Set<String> names = new HashSet<>();
List<PropertyNode> superList;
if (includeSuperProperties || includeSuperFields) {
superList = getAllProperties(names, cNode.getSuperClass(), includeSuperProperties, includeSuperFields, includePseudoGetters, includePseudoSetters, /*super*/true, skipReadOnly);
} else {
superList = new ArrayList<>();
}
List<PropertyNode> list = getAllProperties(names, cNode, includeProperties, includeFields, includePseudoGetters, includePseudoSetters, /*super*/false, skipReadOnly);
List<Parameter> params = new ArrayList<>();
List<Expression> superParams = new ArrayList<>();
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);
}
}
BlockStatement body = new BlockStatement();
if (!handler.validateProperties(xform, body, cNode, plus(list, superList))) {
return;
}
boolean specialNamedArgCase = (superList.isEmpty() && ImmutableASTTransformation.isSpecialNamedArgCase(list, defaultsMode == OFF))
|| (list.isEmpty() && ImmutableASTTransformation.isSpecialNamedArgCase(superList, defaultsMode == OFF));
for (PropertyNode pNode : superList) {
String name = pNode.getName();
FieldNode fNode = pNode.getField();
if (!shouldSkipUndefinedAware(name, excludes, includes, allNames)) {
params.add(createParam(fNode, name, defaultsMode, xform, makeImmutable));
if (callSuper) {
superParams.add(varX(name));
} else if (!superInPre && !specialNamedArgCase) {
Statement propInit = handler.createPropInit(xform, anno, cNode, pNode, null);
if (propInit != null) {
body.addStatement(propInit);
}
}
}
}
if (callSuper) {
body.addStatement(stmt(ctorX(ClassNode.SUPER, args(superParams))));
}
if (!preBody.isEmpty()) {
body.addStatements(preBody.getStatements());
}
for (PropertyNode pNode : list) {
String name = pNode.getName();
if (shouldSkipUndefinedAware(name, excludes, includes, allNames)) continue;
Statement propInit = handler.createPropInit(xform, anno, cNode, pNode, null);
if (propInit != null) {
body.addStatement(propInit);
}
FieldNode fNode = pNode.getField();
Parameter param = createParam(fNode, name, defaultsMode, xform, makeImmutable);
if (cNode.getNodeMetaData("_RECORD_HEADER") != null) {
param.addAnnotations(pNode.getAnnotations());
param.putNodeMetaData("_SKIPPABLE_ANNOTATIONS", Boolean.TRUE);
fNode.putNodeMetaData("_SKIPPABLE_ANNOTATIONS", Boolean.TRUE);
}
params.add(param);
}
if (post != null) {
body.addStatement(post.getCode());
}
if (includes != null) {
params.sort(Comparator.comparingInt(p -> includes.indexOf(p.getName())));
}
int modifiers = getVisibility(anno, cNode, ConstructorNode.class, ACC_PUBLIC);
Parameter[] signature = params.toArray(Parameter.EMPTY_ARRAY);
if (cNode.getDeclaredConstructor(signature) != null) {
if (sourceUnit != null) {
String warning = String.format(
"%s specifies duplicate constructor: %s(%s)",
xform.getAnnotationName(), cNode.getNameWithoutPackage(),
params.stream().map(Parameter::getType).map(ClassNodeUtils::formatTypeName).collect(joining(",")));
sourceUnit.addWarning(warning, anno.getLineNumber() > 0 ? anno : cNode);
}
} else {
// add main tuple constructor; if any parameters have default values, then Verifier will generate the variants
ConstructorNode tupleCtor = addGeneratedConstructor(cNode, modifiers, signature, ClassNode.EMPTY_ARRAY, body);
if (cNode.getNodeMetaData("_RECORD_HEADER") != null) {
tupleCtor.addAnnotations(cNode.getAnnotations());
tupleCtor.putNodeMetaData("_SKIPPABLE_ANNOTATIONS", Boolean.TRUE);
}
if (namedVariant) {
BlockStatement inner = new BlockStatement();
Parameter mapParam = param(ClassHelper.MAP_TYPE.getPlainNodeReference(), NAMED_ARGS);
List<Parameter> genParams = new ArrayList<>();
genParams.add(mapParam);
ArgumentListExpression args = new ArgumentListExpression();
List<String> propNames = new ArrayList<>();
Map<Parameter, Expression> seen = new HashMap<>();
for (Parameter p : params) {
if (!processImplicitNamedParam(xform, tupleCtor, mapParam, inner, args, propNames, p, false, seen)) return;
}
NamedVariantASTTransformation.createMapVariant(xform, tupleCtor, anno, mapParam, genParams, cNode, inner, args, propNames);
}
if (sourceUnit != null && !body.isEmpty()) {
new VariableScopeVisitor(sourceUnit).visitClass(cNode);
}
if (body.isEmpty()) { // GROOVY-8868: retain empty constructor
body.addStatement(stmt(ConstantExpression.EMPTY_EXPRESSION));
}
}
// If the first param is def or a Map, named args might not work as expected so we add a hard-coded map constructor in this case
// we don't do it for LinkedHashMap for now (would lead to duplicate signature)
// or if there is only one Map property (for backwards compatibility)
// or if there is already a @MapConstructor annotation
if (!params.isEmpty() && defaultsMode != OFF && specialNamedArgCase
&& !AnnotatedNodeUtils.hasAnnotation(cNode, MapConstructorASTTransformation.MY_TYPE)) {
ClassNode firstParamType = params.get(0).getType();
if (params.size() > 1 || ClassHelper.isObjectType(firstParamType)) {
String message = "The class " + cNode.getName() + " was incorrectly initialized via the map constructor with null.";
addSpecialMapConstructors(modifiers, cNode, message, false);
}
}
}