SLING-3716 SLING-3718 - adding support for constructor injection and self injection based on a patch from Stefan Seifert

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1619007 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/models/annotations/Default.java b/src/main/java/org/apache/sling/models/annotations/Default.java
index aa0cc25..963a480 100644
--- a/src/main/java/org/apache/sling/models/annotations/Default.java
+++ b/src/main/java/org/apache/sling/models/annotations/Default.java
@@ -24,7 +24,7 @@
 /**
  * Default value for an injection.
  */
-@Target({ ElementType.FIELD, ElementType.METHOD })
+@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Default {
 
diff --git a/src/main/java/org/apache/sling/models/annotations/Filter.java b/src/main/java/org/apache/sling/models/annotations/Filter.java
index 355314c..c91f296 100644
--- a/src/main/java/org/apache/sling/models/annotations/Filter.java
+++ b/src/main/java/org/apache/sling/models/annotations/Filter.java
@@ -26,7 +26,7 @@
 /**
  * Provide a filter on an @Inject.
  */
-@Target({ ElementType.FIELD, ElementType.METHOD })
+@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
 @Retention(RetentionPolicy.RUNTIME)
 @Source("osgi-services")
 @Qualifier
diff --git a/src/main/java/org/apache/sling/models/annotations/Optional.java b/src/main/java/org/apache/sling/models/annotations/Optional.java
index 5743495..39e068a 100644
--- a/src/main/java/org/apache/sling/models/annotations/Optional.java
+++ b/src/main/java/org/apache/sling/models/annotations/Optional.java
@@ -24,7 +24,7 @@
 /**
  * Marker annotation for optional injections.
  */
-@Target({ ElementType.FIELD, ElementType.METHOD })
+@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Optional {
 
diff --git a/src/main/java/org/apache/sling/models/annotations/Required.java b/src/main/java/org/apache/sling/models/annotations/Required.java
index 4bd87e7..8992377 100644
--- a/src/main/java/org/apache/sling/models/annotations/Required.java
+++ b/src/main/java/org/apache/sling/models/annotations/Required.java
@@ -24,7 +24,7 @@
 /**
  * Marker annotation for required injections.
  */
-@Target({ ElementType.FIELD, ElementType.METHOD })
+@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Required {
 
diff --git a/src/main/java/org/apache/sling/models/annotations/Source.java b/src/main/java/org/apache/sling/models/annotations/Source.java
index 0a23c0d..403b4ea 100644
--- a/src/main/java/org/apache/sling/models/annotations/Source.java
+++ b/src/main/java/org/apache/sling/models/annotations/Source.java
@@ -28,7 +28,7 @@
  * Can also be used as a meta-annotation to declare that some other annotation implies
  * a source.
  */
-@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
+@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE })
 @Retention(RetentionPolicy.RUNTIME)
 @Qualifier
 public @interface Source {
diff --git a/src/main/java/org/apache/sling/models/annotations/Via.java b/src/main/java/org/apache/sling/models/annotations/Via.java
index 75683f7..e8b5b07 100644
--- a/src/main/java/org/apache/sling/models/annotations/Via.java
+++ b/src/main/java/org/apache/sling/models/annotations/Via.java
@@ -25,7 +25,7 @@
  * Indicate that this injection point should be handled using a projected
  * property of the adaptable.
  */
-@Target({ ElementType.FIELD, ElementType.METHOD })
+@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Via {
 
diff --git a/src/main/java/org/apache/sling/models/annotations/injectorspecific/ChildResource.java b/src/main/java/org/apache/sling/models/annotations/injectorspecific/ChildResource.java
index bce2cc2..26f32ce 100644
--- a/src/main/java/org/apache/sling/models/annotations/injectorspecific/ChildResource.java
+++ b/src/main/java/org/apache/sling/models/annotations/injectorspecific/ChildResource.java
@@ -18,6 +18,7 @@
 
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
 import java.lang.annotation.Retention;
@@ -27,10 +28,10 @@
 import org.apache.sling.models.spi.injectorspecific.InjectAnnotation;
 
 /**
- * Annotation to be used on either methods or fields to let Sling Models inject a child resource
+ * Annotation to be used on either methods, fields or constructor parameters to let Sling Models inject a child resource
  *
  */
-@Target({ METHOD, FIELD })
+@Target({ METHOD, FIELD, PARAMETER })
 @Retention(RUNTIME)
 @InjectAnnotation
 @Source("child-resources")
diff --git a/src/main/java/org/apache/sling/models/annotations/injectorspecific/OSGiService.java b/src/main/java/org/apache/sling/models/annotations/injectorspecific/OSGiService.java
index 92b31bd..a4363c9 100644
--- a/src/main/java/org/apache/sling/models/annotations/injectorspecific/OSGiService.java
+++ b/src/main/java/org/apache/sling/models/annotations/injectorspecific/OSGiService.java
@@ -18,6 +18,7 @@
 
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
 import java.lang.annotation.Retention;
@@ -27,10 +28,10 @@
 import org.apache.sling.models.spi.injectorspecific.InjectAnnotation;
 
 /**
- * Annotation to be used on either methods or fields to let Sling Models inject an OSGi service
+ * Annotation to be used on either methods, fields or constructor parameters to let Sling Models inject an OSGi service
  *
  */
-@Target({ METHOD, FIELD })
+@Target({ METHOD, FIELD, PARAMETER })
 @Retention(RUNTIME)
 @InjectAnnotation
 @Source("osgi-services")
diff --git a/src/main/java/org/apache/sling/models/annotations/injectorspecific/RequestAttribute.java b/src/main/java/org/apache/sling/models/annotations/injectorspecific/RequestAttribute.java
index 6b4d1a6..07626d4 100644
--- a/src/main/java/org/apache/sling/models/annotations/injectorspecific/RequestAttribute.java
+++ b/src/main/java/org/apache/sling/models/annotations/injectorspecific/RequestAttribute.java
@@ -18,6 +18,7 @@
 
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
 import java.lang.annotation.Retention;
@@ -27,11 +28,11 @@
 import org.apache.sling.models.spi.injectorspecific.InjectAnnotation;
 
 /**
- * Annotation to be used on either methods or fields to let Sling Models inject a
+ * Annotation to be used on either methods, fields or constructor parameters to let Sling Models inject a
  * request attribute.
  *
  */
-@Target({ METHOD, FIELD })
+@Target({ METHOD, FIELD, PARAMETER })
 @Retention(RUNTIME)
 @InjectAnnotation
 @Source("request-attributes")
diff --git a/src/main/java/org/apache/sling/models/annotations/injectorspecific/ScriptVariable.java b/src/main/java/org/apache/sling/models/annotations/injectorspecific/ScriptVariable.java
index b08cece..d25c0e3 100644
--- a/src/main/java/org/apache/sling/models/annotations/injectorspecific/ScriptVariable.java
+++ b/src/main/java/org/apache/sling/models/annotations/injectorspecific/ScriptVariable.java
@@ -18,6 +18,7 @@
 
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
 import java.lang.annotation.Retention;
@@ -27,11 +28,11 @@
 import org.apache.sling.models.spi.injectorspecific.InjectAnnotation;
 
 /**
- * Annotation to be used on either methods or fields to let Sling Models inject a
+ * Annotation to be used on either methods, fields or constructor parameters to let Sling Models inject a
  * script variable (from the {@link org.apache.sling.api.scripting.SlingBindings})
  *
  */
-@Target({ METHOD, FIELD })
+@Target({ METHOD, FIELD, PARAMETER })
 @Retention(RUNTIME)
 @InjectAnnotation
 @Source("script-bindings")
diff --git a/src/main/java/org/apache/sling/models/annotations/injectorspecific/Self.java b/src/main/java/org/apache/sling/models/annotations/injectorspecific/Self.java
new file mode 100644
index 0000000..b452a76
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/annotations/injectorspecific/Self.java
@@ -0,0 +1,46 @@
+/*
+ * 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.apache.sling.models.annotations.injectorspecific;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import org.apache.sling.models.annotations.Source;
+import org.apache.sling.models.spi.injectorspecific.InjectAnnotation;
+
+/**
+ * Annotation to be used on either methods, fields or constructor parameters to let Sling Models
+ * inject the adaptable itself, or an object that can be adapted from it. 
+ */
+@Target({ METHOD, FIELD, PARAMETER })
+@Retention(RUNTIME)
+@InjectAnnotation
+@Source("self")
+public @interface Self {
+
+    /**
+     * If set to true, the model can be instantiated even if there is no object that can be adapted from the adaptable itself. 
+     * Default = false.
+     */
+    public boolean optional() default false;
+
+}
diff --git a/src/main/java/org/apache/sling/models/annotations/injectorspecific/ValueMapValue.java b/src/main/java/org/apache/sling/models/annotations/injectorspecific/ValueMapValue.java
index bbdaf80..4511d59 100644
--- a/src/main/java/org/apache/sling/models/annotations/injectorspecific/ValueMapValue.java
+++ b/src/main/java/org/apache/sling/models/annotations/injectorspecific/ValueMapValue.java
@@ -18,6 +18,7 @@
 
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
 import java.lang.annotation.Retention;
@@ -27,10 +28,11 @@
 import org.apache.sling.models.spi.injectorspecific.InjectAnnotation;
 
 /**
- * Annotation to be used on either methods or fields to let Sling Models inject a value from the ValueMap of the current resource.
+ * Annotation to be used on either methods, fields or constructor parameter to let Sling Models
+ * inject a value from the ValueMap of the current resource.
  *
  */
-@Target({ METHOD, FIELD })
+@Target({ METHOD, FIELD, PARAMETER })
 @Retention(RUNTIME)
 @InjectAnnotation
 @Source("valuemap")
diff --git a/src/main/java/org/apache/sling/models/annotations/injectorspecific/package-info.java b/src/main/java/org/apache/sling/models/annotations/injectorspecific/package-info.java
index da4fcdb..52eee68 100644
--- a/src/main/java/org/apache/sling/models/annotations/injectorspecific/package-info.java
+++ b/src/main/java/org/apache/sling/models/annotations/injectorspecific/package-info.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-@Version("1.0.0")
+@Version("1.0.2")
 package org.apache.sling.models.annotations.injectorspecific;
 
 import aQute.bnd.annotation.Version;
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/models/annotations/package-info.java b/src/main/java/org/apache/sling/models/annotations/package-info.java
index 1413f05..59b91d4 100644
--- a/src/main/java/org/apache/sling/models/annotations/package-info.java
+++ b/src/main/java/org/apache/sling/models/annotations/package-info.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-@Version("1.1.0")
+@Version("1.1.2")
 package org.apache.sling.models.annotations;
 
 import aQute.bnd.annotation.Version;
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/models/spi/AcceptsNullName.java b/src/main/java/org/apache/sling/models/spi/AcceptsNullName.java
new file mode 100644
index 0000000..3f0e80e
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/spi/AcceptsNullName.java
@@ -0,0 +1,27 @@
+/*
+ * 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.apache.sling.models.spi;
+
+/**
+ * Marker interface for an injector to declare that it ignores the name
+ * parameter.
+ *
+ */
+public interface AcceptsNullName {
+}
diff --git a/src/main/java/org/apache/sling/models/spi/package-info.java b/src/main/java/org/apache/sling/models/spi/package-info.java
index e63fa96..93327e5 100644
--- a/src/main/java/org/apache/sling/models/spi/package-info.java
+++ b/src/main/java/org/apache/sling/models/spi/package-info.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-@Version("1.0.0")
+@Version("1.0.2")
 package org.apache.sling.models.spi;
 
 import aQute.bnd.annotation.Version;
\ No newline at end of file