Adding injection of '@State StateDescriptor descriptor;'

Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/d8d02107
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/d8d02107
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/d8d02107

Branch: refs/heads/develop
Commit: d8d02107bcf0daab5fe2ff0d21b0be0834c210eb
Parents: e0825fe
Author: niclas <[email protected]>
Authored: Mon Oct 30 13:27:17 2017 +0800
Committer: niclas <[email protected]>
Committed: Mon Oct 30 13:27:17 2017 +0800

----------------------------------------------------------------------
 core/api/src/docs/dependency-injection.txt      |  12 +-
 .../provider/StateInjectionProviderFactory.java |  30 ++++-
 .../injection/StateDescriptorInjectionTest.java | 112 +++++++++++++++++++
 3 files changed, 147 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/d8d02107/core/api/src/docs/dependency-injection.txt
----------------------------------------------------------------------
diff --git a/core/api/src/docs/dependency-injection.txt 
b/core/api/src/docs/dependency-injection.txt
index d9fabca..5de3187 100644
--- a/core/api/src/docs/dependency-injection.txt
+++ b/core/api/src/docs/dependency-injection.txt
@@ -146,9 +146,15 @@ If the ++@Uses++ is ++@Optional++ then no implict object 
creation will take plac
 
 [[core-api-state,@State]]
 == @State ==
-This injection scope can inject either a ++StateHolder++ which allows 
inspection of current state of the Composite,
-or it can inject any declared <<def-property>>, <<def-assocation>>, 
<<def-manyassociation>> or
-<<def-namedassociation>>.
+This injection scope can inject either state related types.
+
+    * ++StateDescriptor++ which holds metainfo about the state of the 
Composite,
+    * ++StateHolder++ which allows inspection of current state of the 
Composite,
+    * <<def-property>> to get a direct reference to a declared ++Property++
+    * <<def-assocation>> to get a direct reference to a declared 
++Association++
+    * <<def-manyassociation>> to get a direct reference to a declared 
++ManyAssociation++
+    * <<def-namedassociation>> to get a direct reference to a declared 
++NamedAssociation++
 
 [[core-api-custom-injection,Custom Injection Scopes]]
 == Custom Injection Scopes ==
+Custom Injection scopes are not yet supported.
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/d8d02107/core/runtime/src/main/java/org/apache/polygene/runtime/injection/provider/StateInjectionProviderFactory.java
----------------------------------------------------------------------
diff --git 
a/core/runtime/src/main/java/org/apache/polygene/runtime/injection/provider/StateInjectionProviderFactory.java
 
b/core/runtime/src/main/java/org/apache/polygene/runtime/injection/provider/StateInjectionProviderFactory.java
index 3a3a80e..de06011 100644
--- 
a/core/runtime/src/main/java/org/apache/polygene/runtime/injection/provider/StateInjectionProviderFactory.java
+++ 
b/core/runtime/src/main/java/org/apache/polygene/runtime/injection/provider/StateInjectionProviderFactory.java
@@ -57,6 +57,11 @@ public final class StateInjectionProviderFactory
             // @State StateHolder properties;
             return new StateInjectionProvider();
         }
+        else if( StateDescriptor.class.isAssignableFrom( 
dependencyModel.rawInjectionType() ) )
+        {
+            StateDescriptor descriptor = ( (StatefulCompositeDescriptor) 
resolution.model() ).state();
+            return new StateDescriptorInjectionProvider( descriptor );
+        }
         else if( UnitOfWork.class.isAssignableFrom( 
dependencyModel.rawInjectionType() ) )
         {
             if( !( resolution.model() instanceof EntityDescriptor ) )
@@ -181,7 +186,7 @@ public final class StateInjectionProviderFactory
             throws InjectionProviderException
         {
             AbstractAssociation abstractAssociation = ( 
(AssociationStateHolder) context.state() ).
-                associationFor( associationDescriptor.accessor() );
+                                                                               
                       associationFor( associationDescriptor.accessor() );
             if( abstractAssociation != null )
             {
                 return abstractAssociation;
@@ -208,7 +213,7 @@ public final class StateInjectionProviderFactory
             throws InjectionProviderException
         {
             ManyAssociation<?> abstractAssociation = ( 
(AssociationStateHolder) context.state() ).
-                manyAssociationFor( manyAssociationDescriptor.accessor() );
+                                                                               
                      manyAssociationFor( manyAssociationDescriptor.accessor() 
);
             if( abstractAssociation != null )
             {
                 return abstractAssociation;
@@ -235,7 +240,7 @@ public final class StateInjectionProviderFactory
             throws InjectionProviderException
         {
             NamedAssociation<?> abstractAssociation = ( 
(AssociationStateHolder) context.state() ).
-                namedAssociationFor( namedAssociationDescriptor.accessor() );
+                                                                               
                       namedAssociationFor( 
namedAssociationDescriptor.accessor() );
             if( abstractAssociation != null )
             {
                 return abstractAssociation;
@@ -258,6 +263,24 @@ public final class StateInjectionProviderFactory
         }
     }
 
+    static private class StateDescriptorInjectionProvider
+        implements InjectionProvider
+    {
+        private StateDescriptor descriptor;
+
+        public StateDescriptorInjectionProvider( StateDescriptor descriptor )
+        {
+            this.descriptor = descriptor;
+        }
+
+        @Override
+        public Object provideInjection( InjectionContext context )
+            throws InjectionProviderException
+        {
+            return descriptor;
+        }
+    }
+
     static private class UnitOfWorkInjectionProvider
         implements InjectionProvider
     {
@@ -269,5 +292,4 @@ public final class StateInjectionProviderFactory
             return ( (EntityInstance) context.compositeInstance() 
).unitOfWork();
         }
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/d8d02107/core/runtime/src/test/java/org/apache/polygene/runtime/injection/StateDescriptorInjectionTest.java
----------------------------------------------------------------------
diff --git 
a/core/runtime/src/test/java/org/apache/polygene/runtime/injection/StateDescriptorInjectionTest.java
 
b/core/runtime/src/test/java/org/apache/polygene/runtime/injection/StateDescriptorInjectionTest.java
new file mode 100644
index 0000000..593ffb8
--- /dev/null
+++ 
b/core/runtime/src/test/java/org/apache/polygene/runtime/injection/StateDescriptorInjectionTest.java
@@ -0,0 +1,112 @@
+/*
+ *  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.polygene.runtime.injection;
+
+import org.apache.polygene.api.common.Optional;
+import org.apache.polygene.api.composite.StateDescriptor;
+import org.apache.polygene.api.composite.TransientBuilder;
+import org.apache.polygene.api.composite.TransientComposite;
+import org.apache.polygene.api.injection.scope.State;
+import org.apache.polygene.api.mixin.Mixins;
+import org.apache.polygene.api.property.Property;
+import org.apache.polygene.bootstrap.AssemblyException;
+import org.apache.polygene.bootstrap.ModuleAssembly;
+import org.apache.polygene.test.AbstractPolygeneTest;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test the @State annotation with StateDescriptor type
+ */
+public class StateDescriptorInjectionTest
+    extends AbstractPolygeneTest
+{
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        module.transients( 
StateDescriptorInjectionTest.PropertyFieldInjectionComposite.class );
+    }
+
+    /**
+     * Tests that a mixin is injected into member variables annotated with 
{@link @PropertyField}.
+     *
+     * @throws Exception re-thrown
+     */
+    @Test
+    public void mixinIsInjectedForMemberVariablesAnnotatedWithPropertyField()
+        throws Exception
+    {
+        TransientBuilder<PropertyFieldInjectionComposite> pficBuilder =
+            transientBuilderFactory.newTransientBuilder( 
StateDescriptorInjectionTest.PropertyFieldInjectionComposite.class );
+        pficBuilder.prototype().testField().set( "X" );
+        PropertyFieldInjectionComposite pfic = pficBuilder.newInstance();
+        assertThat( "Test field", pfic.testField().get(), is( equalTo( "X" ) ) 
);
+        assertThat( "Named fieldX", pfic.namedField().get(), is( equalTo( "X" 
) ) );
+        assertThat( "State", pfic.getDescriptor()
+                                 .findPropertyModelByName( "testField" )
+                                 .type(),
+                    is( equalTo( String.class ) ) );
+    }
+
+    @Mixins( PropertyFieldInjectionMixin.class )
+    public interface PropertyFieldInjectionComposite
+        extends TransientComposite
+    {
+        @Optional
+        Property<String> testField();
+
+        @Optional
+        Property<String> namedField();
+
+        StateDescriptor getDescriptor();
+    }
+
+    public abstract static class PropertyFieldInjectionMixin
+        implements PropertyFieldInjectionComposite
+    {
+        @State
+        Property<String> testField;
+
+        @State( "testField" )
+        Property<String> namedField;
+
+        @State
+        StateDescriptor descriptor;
+
+        public StateDescriptor getDescriptor()
+        {
+            return descriptor;
+        }
+
+        public Property<String> testField()
+        {
+            return testField;
+        }
+
+        public Property<String> namedField()
+        {
+            return namedField;
+        }
+    }
+}

Reply via email to