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

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-site.git


The following commit(s) were added to refs/heads/master by this push:
     new db95fba  Update models docs documenting member visibility allowed for 
the different types of injection (#75)
db95fba is described below

commit db95fba5ff080b631b7ebd676b7be7d0885beaa2
Author: paul-bjorkstrand <[email protected]>
AuthorDate: Wed Dec 15 01:47:46 2021 -0600

    Update models docs documenting member visibility allowed for the different 
types of injection (#75)
---
 .../jbake/content/documentation/bundles/models.md  | 73 +++++++++++++++++++++-
 1 file changed, 72 insertions(+), 1 deletion(-)

diff --git a/src/main/jbake/content/documentation/bundles/models.md 
b/src/main/jbake/content/documentation/bundles/models.md
index c4b0139..c912ea9 100644
--- a/src/main/jbake/content/documentation/bundles/models.md
+++ b/src/main/jbake/content/documentation/bundles/models.md
@@ -20,6 +20,8 @@ Many Sling projects want to be able to create model objects - 
POJOs which are au
 * Work with existing Sling infrastructure (i.e. not require changes to other 
bundles).
 
 # Basic Usage
+## Model Classes
+
 In the simplest case, the class is annotated with `@Model` and the adaptable 
class. Fields which need to be injected are annotated with `@Inject`:
 
     ::java
@@ -30,7 +32,37 @@ In the simplest case, the class is annotated with `@Model` 
and the adaptable cla
         private String propertyName;
     }
 
-In this case, a property named "propertyName" will be looked up from the 
Resource (after first adapting it to a `ValueMap`) and it is injected.
+In this case, a property named "propertyName" will be looked up from the 
Resource (after first adapting it to a `ValueMap`) and it is injected. Fields 
can use any visibility modifier:
+
+    ::java
+    @Model(adaptables=Resource.class)
+    public class PublicFieldModel {
+    
+        @Inject
+        public String publicField;
+    }
+
+    @Model(adaptables=Resource.class)
+    public class ProtectedFieldModel {
+    
+        @Inject
+        protected String protectedField;
+    }
+
+    @Model(adaptables=Resource.class)
+    public class PrivateFieldModel {
+    
+        @Inject
+        private String privateField;
+    }
+
+    @Model(adaptables=Resource.class)
+    public class PackagePrivateFieldModel {
+    
+        @Inject
+        String packagePrivateField;
+    }
+
  
 For an interface, it is similar:
 
@@ -42,6 +74,8 @@ For an interface, it is similar:
            String getPropertyName();
        }
 
+Interface methods must be `public`. Even though private interface methods have 
been available since Java 9, Sling Models uses Dynamic Proxies to instantiate 
the interfaces, which does not work with private interface methods. 
Additionally, while default interface methods will work with interface 
injection, the default implementation (in the interface) is currently not used, 
and will not be executed.
+
 Constructor injection is also supported (as of Sling Models 1.1.0):
 
     ::java
@@ -55,6 +89,43 @@ Constructor injection is also supported (as of Sling Models 
1.1.0):
 
 Because the name of a constructor argument parameter cannot be detected via 
the Java Reflection API a `@Named` annotation is mandatory for injectors that 
require a name for resolving the injection.
 
+Constructors may use any visibility modifier (as of [Sling Models 
1.5.0](https://issues.apache.org/jira/browse/SLING-8069)):
+
+    ::java
+    @Model(adaptables=Resource.class)
+    public class PublicConstructorModel {    
+        @Inject
+        public PublicConstructorModel() {
+          // constructor code
+        }
+    }
+
+    @Model(adaptables=Resource.class)
+    public class ProtectedConstructorModel {    
+        @Inject
+        protected ProtectedConstructorModel() {
+          // constructor code
+        }
+    }
+
+    @Model(adaptables=Resource.class)
+    public class PrivateConstructorModel {    
+        @Inject
+        private PrivateConstructorModel() {
+          // constructor code
+        }
+    }
+
+    @Model(adaptables=Resource.class)
+    public class PackagePrivateConstructorModel {    
+        @Inject
+        PackagePrivateConstructorModel() {
+          // constructor code
+        }
+    }
+
+## Bundle Manifest Configuration
+
 In order for these classes to be picked up, there is a header which must be 
added to the bundle's manifest:
 
        <Sling-Model-Packages>

Reply via email to