Copilot commented on code in PR #2503: URL: https://github.com/apache/groovy/pull/2503#discussion_r3166866282
########## src/main/java/org/codehaus/groovy/transform/TupleConstructorASTStubber.java: ########## @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.codehaus.groovy.transform; + +import groovy.transform.DefaultsMode; +import groovy.transform.TupleConstructor; +import org.codehaus.groovy.ast.ASTNode; +import org.codehaus.groovy.ast.AnnotatedNode; +import org.codehaus.groovy.ast.AnnotationNode; +import org.codehaus.groovy.ast.ClassNode; +import org.codehaus.groovy.ast.Parameter; +import org.codehaus.groovy.ast.PropertyNode; +import org.codehaus.groovy.ast.stmt.EmptyStatement; +import org.codehaus.groovy.control.CompilePhase; +import org.codehaus.groovy.control.SourceUnit; + +import java.util.ArrayList; +import java.util.List; + +import static org.codehaus.groovy.ast.ClassHelper.MAP_TYPE; +import static org.codehaus.groovy.ast.ClassHelper.make; +import static org.codehaus.groovy.ast.tools.GeneralUtils.param; +import static org.codehaus.groovy.ast.tools.GenericsUtils.newClass; +import static org.codehaus.groovy.transform.StubberSupport.addStubConstructor; +import static org.codehaus.groovy.transform.TupleConstructorASTTransformation.resolveDefaultsMode; +import static org.objectweb.asm.Opcodes.ACC_PUBLIC; + +/** + * Adds a placeholder tuple-style constructor surface for {@link TupleConstructor} + * classes during joint compilation. The stubber can only see properties that + * are directly declared on the class — trait-injected properties, super-class + * properties, and ones pulled in by {@code includeFields} or {@code includes} + * referencing names not yet present are NOT visible at this phase. The stub + * is therefore a SUBSET of the runtime constructor surface. + * + * <p>When {@code defaults != false} (the default), the runtime exposes the + * full prefix-overload chain via {@code Verifier}. The stubber mirrors that + * chain so Java consumers can drop trailing arguments. When {@code defaults + * = false} only the maximal-arg constructor is emitted, matching the runtime. + * + * <p>Implication for joint compilation: Java consumers can construct instances + * using the directly-declared property prefix; calls that depend on properties + * the stubber could not see will not compile. + */ +@GroovyASTTransformation(phase = CompilePhase.CONVERSION) +public class TupleConstructorASTStubber extends AbstractASTTransformation { + + private static final ClassNode MY_TYPE = make(TupleConstructor.class); + + @Override + public void visit(ASTNode[] nodes, SourceUnit source) { + init(nodes, source); + AnnotationNode annotation = (AnnotationNode) nodes[0]; + AnnotatedNode parent = (AnnotatedNode) nodes[1]; + if (!MY_TYPE.equals(annotation.getClassNode())) return; + if (!(parent instanceof ClassNode classNode) || classNode.isInterface()) return; + + // Honour user-declared parameterless construction intent only when no other + // constructor was hand-written. Skip silently in any ambiguous case so the + // full transform at CANONICALIZATION remains authoritative. + // + // Filter out stubber-tagged constructors: when @TupleConstructor and + // @MapConstructor are composed (e.g. via @Immutable), each stubber + // would otherwise see the OTHER stubber's placeholder and silently + // bail out. Only genuine user-declared constructors should suppress + // emission. + if (classNode.getDeclaredConstructors().stream().anyMatch(c -> !StubberSupport.isStub(c))) return; + + List<Parameter> params = new ArrayList<>(); + for (PropertyNode property : classNode.getProperties()) { + if (property.isStatic()) continue; + params.add(param(newClass(property.getType()), property.getName())); + } Review Comment: TupleConstructorASTStubber currently derives constructor parameters from all non-static properties on the class and ignores key `@TupleConstructor` attributes like includeProperties/includeFields, includes/excludes/allNames, and super/field inclusion flags. Because the full TupleConstructorASTTransformation builds its constructor signature using these flags (and filters properties via shouldSkipUndefinedAware), the stubber can emit constructor signatures that will NOT exist at runtime (e.g. excludes/includes or includeProperties=false or includeFields=true + defaultsMode OFF), breaking the “stub is a strict subset of runtime” invariant and causing Java code to compile against the stub but fail at runtime. Recommendation: either (a) mirror the full transform’s parameter-selection logic for directly-declared members (respect includeProperties/includeFields and includes/excludes/allNames), or (b) conservatively skip emitting stub constructors whenever any non-default attributes that can change the signature are present, so the stub can’t advertise non-existent runtime ctors. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
