You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/04/01 07:11:11 UTC

[groovy] branch master updated: GROOVY-8659: Clarification of property accessor method generation in the presence on inherited final methods

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new c32d71b  GROOVY-8659: Clarification of property accessor method generation in the presence on inherited final methods
c32d71b is described below

commit c32d71bbe54e9da6ead16fe72ad83094ba9fa532
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Apr 1 17:11:03 2021 +1000

    GROOVY-8659: Clarification of property accessor method generation in the presence on inherited final methods
---
 src/spec/doc/core-object-orientation.adoc | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/src/spec/doc/core-object-orientation.adoc b/src/spec/doc/core-object-orientation.adoc
index e822b5c..60e7740 100644
--- a/src/spec/doc/core-object-orientation.adoc
+++ b/src/spec/doc/core-object-orientation.adoc
@@ -611,7 +611,7 @@ Using an explicit checked exception declaration is illustrated in the following
 include::../test/objectorientation/MethodsTest.groovy[tags=checked_method_declaration,indent=0]
 ----
 
-=== Fields and properties
+=== Fields and Properties
 
 [[fields]]
 ==== Fields
@@ -763,6 +763,18 @@ e.g. you can have `aProp` and `AProp`, or `pNAME` and `PNAME`. The getters would
 and `getpNAME` and `getPNAME` respectively.
 ====
 
+===== Modifiers on a property
+
+We have already seen that properties are defined by omitting the visibility modifier.
+In general, any other modifiers, e.g. `transient` would be copied across to the field.
+Two special cases are worth noting:
+
+* `final`, which we saw earlier is for read-only properties, is copied onto the backing field but also causes no setter to be defined
+* `static` is copied onto the backing field but also causes the accessor methods to be static
+
+If you wish to have a modifier like `final` also carried over to the accessor methods,
+you can write your properties long hand or consider using a <<split-properties,split property definition>>.
+
 ===== Annotations on a property
 
 Annotations, including those associated with AST transforms,
@@ -778,6 +790,7 @@ include::../test/ClassTest.groovy[tags=annotated_properties,indent=0]
 <2> Normal property access
 <3> Confirms initialization upon property access
 
+[[split-properties]]
 ===== Split property definition with an explicit backing field
 
 Groovy's property syntax is a convenient shorthand when your class design
@@ -832,6 +845,15 @@ class HasPropertyWithSynchronizedAccessorMethods {
 <1> Backing field for name property
 <2> Declare name property with annotation for setter/getter
 
+===== Explicit accessor methods
+
+The automatic generation of accessor methods doesn't occur if there
+is an explicit definition of the getter or setter in the class.
+This allows you to modify the normal behavior of such a getter or setter if needed.
+Inherited accessor methods aren't normally considered but if an inherited
+accessor method is marked final, that will also cause no generation of an
+additional accessor method to honor the `final` requirement of no subclassing of such methods.
+
 === Annotation
 
 [[ann-definition]]