Copilot commented on code in PR #2554: URL: https://github.com/apache/groovy/pull/2554#discussion_r3291653286
########## src/test/groovy/groovy/transform/options/PropertyHandlerTest.groovy: ########## @@ -0,0 +1,135 @@ +/* + * 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 groovy.transform.options + +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.Statement +import org.codehaus.groovy.transform.AbstractASTTransformation +import org.junit.jupiter.api.Test + +import static groovy.test.GroovyAssert.assertScript +import static groovy.test.GroovyAssert.shouldFail +import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS +import static org.codehaus.groovy.ast.tools.GeneralUtils.constX +import static org.codehaus.groovy.ast.tools.GeneralUtils.plusX +import static org.codehaus.groovy.ast.tools.GeneralUtils.propX +import static org.codehaus.groovy.ast.tools.GeneralUtils.varX + +/** + * Locks in the public contract of {@link PropertyHandler}: that user-supplied + * handlers wired in via {@code @PropertyOptions(propertyHandler = …)} are + * loaded, validated and consulted at compile time. + */ +final class PropertyHandlerTest { + + @Test + void testCustomHandlerControlsPropertyInitialisation() { + assertScript ''' + import groovy.transform.PropertyOptions + import groovy.transform.TupleConstructor + import groovy.transform.options.PropertyHandlerTest.PrefixingPropertyHandler + + @PropertyOptions(propertyHandler = PrefixingPropertyHandler) + @TupleConstructor + class Foo { + String name + } + + assert new Foo('hello').name == 'X_hello' + ''' + } + + @Test + void testHandlerValidateAttributesCanRejectAnAttribute() { + // Exercises a list-valued attribute (`includes = [...]`); detection relies on + // PropertyHandler.isValidAttribute using AnnotationNode.getMember rather than + // the scalar-only AbstractASTTransformation.getMemberValue. Review Comment: The comment describing `AbstractASTTransformation.getMemberValue` as "scalar-only" is a bit misleading; the key limitation is that it only returns values for `ConstantExpression` members (and returns null for `ListExpression`, closures, etc.). Tweaking the wording will make the test rationale more precise. ########## src/main/java/groovy/transform/options/PropertyHandler.java: ########## @@ -72,15 +82,18 @@ public boolean validateProperties(final AbstractASTTransformation xform, final B } /** - * Create a statement that will initialize the property including any defensive copying. Null if no statement should be added. + * Create a statement that will initialize the property including any defensive copying. + * Return {@code null} to indicate that no initialization statement should be emitted + * for this property; this is distinct from returning an empty block and skips the + * corresponding entry in the generated constructor body altogether. * * @param xform the transform being processed * @param anno the '@ImmutableBase' annotation node * @param cNode the classnode containing the property * @param pNode the property node to initialize - * @param namedArgMap an "args" Map if the property value should come from a named arg map or null if not + * @param namedArgsMap an "args" Map if the property value should come from a named arg map or null if not Review Comment: The `createPropInit` Javadoc still says `anno` is the "'@ImmutableBase' annotation node", but this hook is used by multiple transforms (e.g. `@TupleConstructor`, `@MapConstructor`, `@ImmutableBase`, `@RecordType`). Since this PR is graduating `PropertyHandler` to a stable API, the parameter docs should be accurate/generic to avoid misleading implementers. -- 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]
