polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nic...@apache.org
Subject [28/34] zest-java git commit: Fixed the Type resolution so that a Foo<Bar> field will not get a Foo<Blip> as the injected type.
Date Thu, 14 Apr 2016 06:13:34 GMT
Fixed the Type resolution so that a Foo<Bar> field will not get a Foo<Blip> as
the injected type.


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

Branch: refs/heads/develop
Commit: 15f156d50f52a9f334ffdc81f045d93b241522f1
Parents: 9a5c94c
Author: Niclas Hedhman <niclas@hedhman.org>
Authored: Wed Apr 13 19:32:32 2016 +0800
Committer: Niclas Hedhman <niclas@hedhman.org>
Committed: Wed Apr 13 19:32:32 2016 +0800

----------------------------------------------------------------------
 .../zest/api/service/ServiceReference.java      |  7 ++++
 .../runtime/activation/ActivationDelegate.java  |  7 ++++
 .../ImportedServiceReferenceInstance.java       |  7 ++++
 .../service/ServiceReferenceInstance.java       |  8 ++++-
 .../zest/runtime/structure/ModuleInstance.java  | 35 ++++++++++----------
 .../zest/runtime/structure/TypeLookupImpl.java  | 24 ++++++--------
 .../property/ValueNestedBuilderTest.java        | 30 ++++++++---------
 .../runtime/value/NestedValueBuilderTest.java   |  2 ++
 .../runtime/value/ValueCompositeBasicsTest.java |  2 ++
 9 files changed, 73 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/15f156d5/core/api/src/main/java/org/apache/zest/api/service/ServiceReference.java
----------------------------------------------------------------------
diff --git a/core/api/src/main/java/org/apache/zest/api/service/ServiceReference.java b/core/api/src/main/java/org/apache/zest/api/service/ServiceReference.java
index dd17618..3ed1cfd 100644
--- a/core/api/src/main/java/org/apache/zest/api/service/ServiceReference.java
+++ b/core/api/src/main/java/org/apache/zest/api/service/ServiceReference.java
@@ -15,6 +15,7 @@
 package org.apache.zest.api.service;
 
 import org.apache.zest.api.activation.ActivationEventListenerRegistration;
+import org.apache.zest.api.composite.ModelDescriptor;
 import org.apache.zest.api.structure.MetaInfoHolder;
 import org.apache.zest.api.type.HasTypes;
 
@@ -44,4 +45,10 @@ public interface ServiceReference<T>
      * @return TRUE if the service is available, otherwise return FALSE
      */
     boolean isAvailable();
+
+    /** Returns the ServiceModel of the service referenced by this ServiceReference.
+     *
+      * @return
+     */
+     ModelDescriptor model();
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/15f156d5/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java
b/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java
index b69d9a7..8363e18 100644
--- a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java
@@ -24,6 +24,7 @@ import org.apache.zest.api.activation.ActivationEvent;
 import org.apache.zest.api.activation.ActivationEventListener;
 import org.apache.zest.api.activation.ActivationException;
 import org.apache.zest.api.activation.PassivationException;
+import org.apache.zest.api.composite.ModelDescriptor;
 import org.apache.zest.api.service.ServiceReference;
 
 import static org.apache.zest.api.activation.ActivationEvent.EventType.ACTIVATED;
@@ -334,6 +335,12 @@ public final class ActivationDelegate
         }
 
         @Override
+        public ModelDescriptor model()
+        {
+            return reference.model();
+        }
+
+        @Override
         public Stream<Class<?>> types()
         {
             return reference.types();

http://git-wip-us.apache.org/repos/asf/zest-java/blob/15f156d5/core/runtime/src/main/java/org/apache/zest/runtime/service/ImportedServiceReferenceInstance.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/service/ImportedServiceReferenceInstance.java
b/core/runtime/src/main/java/org/apache/zest/runtime/service/ImportedServiceReferenceInstance.java
index b77370a..a2da20c 100644
--- a/core/runtime/src/main/java/org/apache/zest/runtime/service/ImportedServiceReferenceInstance.java
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/service/ImportedServiceReferenceInstance.java
@@ -24,6 +24,7 @@ import org.apache.zest.api.activation.Activation;
 import org.apache.zest.api.activation.ActivationEventListener;
 import org.apache.zest.api.activation.ActivationException;
 import org.apache.zest.api.activation.PassivationException;
+import org.apache.zest.api.composite.ModelDescriptor;
 import org.apache.zest.api.service.ServiceImporterException;
 import org.apache.zest.api.service.ServiceReference;
 import org.apache.zest.api.service.ServiceUnavailableException;
@@ -133,6 +134,12 @@ public final class ImportedServiceReferenceInstance<T>
         }
     }
 
+    @Override
+    public ModelDescriptor model()
+    {
+        return serviceModel;
+    }
+
     public ModuleDescriptor module()
     {
         return module;

http://git-wip-us.apache.org/repos/asf/zest-java/blob/15f156d5/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceReferenceInstance.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceReferenceInstance.java
b/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceReferenceInstance.java
index 97928c5..1abe3d5 100644
--- a/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceReferenceInstance.java
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceReferenceInstance.java
@@ -58,7 +58,7 @@ public final class ServiceReferenceInstance<T>
     private final ActivationDelegate activation = new ActivationDelegate( this );
     private boolean active = false;
 
-    public ServiceReferenceInstance( ServiceModel serviceModel, ModuleDescriptor module )
+    ServiceReferenceInstance( ServiceModel serviceModel, ModuleDescriptor module )
     {
         this.module = module;
         this.serviceModel = serviceModel;
@@ -102,6 +102,12 @@ public final class ServiceReferenceInstance<T>
         return getInstance().isAvailable();
     }
 
+    @Override
+    public ModelDescriptor model()
+    {
+        return serviceModel;
+    }
+
     public ModuleDescriptor module()
     {
         return module;

http://git-wip-us.apache.org/repos/asf/zest-java/blob/15f156d5/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java
b/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java
index a24e768..ce458c9 100644
--- a/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java
@@ -24,6 +24,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.Optional;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 import org.apache.zest.api.activation.Activation;
@@ -370,20 +371,7 @@ public class ModuleInstance
         {
             throw new NoSuchServiceException( serviceType.getTypeName(), name() );
         }
-        ModuleInstance serviceLocation = (ModuleInstance) serviceModel.module().instance();
-        try
-        {
-            //noinspection unchecked
-            return serviceLocation.services
-                .references()
-                .filter( ref -> ref.hasType( serviceType ) )
-                .map( ref -> (ServiceReference<T>) ref )
-                .findAny().get();
-        }
-        catch( NoSuchElementException e )
-        {
-            throw new NoSuchServiceException( serviceType.getTypeName(), name() );
-        }
+        return findServiceReferenceInstance( serviceModel );
     }
 
     @Override
@@ -402,14 +390,27 @@ public class ModuleInstance
         }
         //noinspection unchecked
         return serviceModels.stream()
-            .flatMap(
-                model -> ( (ModuleInstance) model.module().instance() ).services.references()
-            )
+            .map( this::findServiceReferenceInstance )
+            .filter( ref -> ref != null )
             .filter( ref -> ref.hasType( serviceType ) )
             .map( ref -> (ServiceReference<T>) ref )
             .collect( Collectors.toList() );
     }
 
+    private <T> ServiceReference<T> findServiceReferenceInstance( ModelDescriptor
model )
+    {
+        ModuleInstance moduleInstanceOfModel = (ModuleInstance) model.module().instance();
+        Optional<ServiceReference<?>> candidate = moduleInstanceOfModel.services.references()
+            .filter( ref -> ref.model().equals( model ) )
+            .findAny();
+        if( candidate.isPresent() )
+        {
+            ServiceReference<?> serviceReference = candidate.get();
+            return (ServiceReference<T>) serviceReference;
+        }
+        return null;
+    }
+
     // Implementation of Activation
     @Override
     @SuppressWarnings( "unchecked" )

http://git-wip-us.apache.org/repos/asf/zest-java/blob/15f156d5/core/runtime/src/main/java/org/apache/zest/runtime/structure/TypeLookupImpl.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/structure/TypeLookupImpl.java
b/core/runtime/src/main/java/org/apache/zest/runtime/structure/TypeLookupImpl.java
index 0d3c740..aba9b22 100755
--- a/core/runtime/src/main/java/org/apache/zest/runtime/structure/TypeLookupImpl.java
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/structure/TypeLookupImpl.java
@@ -47,7 +47,7 @@ import static org.apache.zest.functional.Iterables.first;
 /**
  * Central place for Composite Type lookups.
  */
-public class TypeLookupImpl
+class TypeLookupImpl
     implements TypeLookup
 {
 
@@ -250,19 +250,15 @@ public class TypeLookupImpl
     {
         return servicesReferences.computeIfAbsent( type1, type ->
         {
-            List<ModelDescriptor> models =
-                allServices().filter(
-                    new ExactTypeMatching<>( type ).or( new AssignableFromTypeMatching<>(
type ) )
-                )
-                    .distinct()
-                    .collect( Collectors.toList() );
-
-            // TODO: Needed??
-//            List<T> result = new ArrayList<>();
-//            //noinspection unchecked
-//            serviceRefs.forEach( descriptor -> result.add( (T) descriptor ) );
-
-            return models;
+            // There is a requirement that "exact match" services must be returned before
"assignable match"
+            // services, hence the dual streams instead of a OR filter.
+            return Stream.concat(
+                allServices()
+                    .filter( new ExactTypeMatching<>( type ) ),
+                allServices()
+                    .filter( new AssignableFromTypeMatching<>( type ) ) )
+                .distinct()
+                .collect( Collectors.toList() );
         } );
     }
 

http://git-wip-us.apache.org/repos/asf/zest-java/blob/15f156d5/core/runtime/src/test/java/org/apache/zest/runtime/property/ValueNestedBuilderTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/zest/runtime/property/ValueNestedBuilderTest.java
b/core/runtime/src/test/java/org/apache/zest/runtime/property/ValueNestedBuilderTest.java
index e23af89..69dfe02 100644
--- a/core/runtime/src/test/java/org/apache/zest/runtime/property/ValueNestedBuilderTest.java
+++ b/core/runtime/src/test/java/org/apache/zest/runtime/property/ValueNestedBuilderTest.java
@@ -17,7 +17,6 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import org.junit.Test;
 import org.apache.zest.api.common.UseDefaults;
 import org.apache.zest.api.property.Property;
 import org.apache.zest.api.value.ValueBuilder;
@@ -25,23 +24,23 @@ import org.apache.zest.api.value.ValueComposite;
 import org.apache.zest.bootstrap.AssemblyException;
 import org.apache.zest.bootstrap.ModuleAssembly;
 import org.apache.zest.test.AbstractZestTest;
+import org.apache.zest.valueserialization.orgjson.OrgJsonValueSerializationService;
+import org.junit.Test;
 
 public class ValueNestedBuilderTest
-        extends AbstractZestTest
+    extends AbstractZestTest
 {
 
-    static interface InnerValue
-            extends ValueComposite
+    private interface InnerValue
+        extends ValueComposite
     {
-
         Property<List<String>> listProp();
 
         Property<Map<String, String>> mapProp();
-
     }
 
-    static interface InnerDefaultedValue
-            extends ValueComposite
+    private interface InnerDefaultedValue
+        extends ValueComposite
     {
 
         @UseDefaults
@@ -49,30 +48,28 @@ public class ValueNestedBuilderTest
 
         @UseDefaults
         Property<Map<String, String>> mapPropDefault();
-
     }
 
-    static interface OuterValue
-            extends ValueComposite
+    private interface OuterValue
+        extends ValueComposite
     {
 
         Property<List<InnerValue>> innerListProp();
-
     }
 
-    static interface OuterDefaultedValue
-            extends ValueComposite
+    private interface OuterDefaultedValue
+        extends ValueComposite
     {
 
         @UseDefaults
         Property<List<InnerDefaultedValue>> innerListPropDefault();
-
     }
 
     @Override
     public void assemble( ModuleAssembly module )
-            throws AssemblyException
+        throws AssemblyException
     {
+        module.services( OrgJsonValueSerializationService.class );
         module.values( InnerValue.class, InnerDefaultedValue.class, OuterValue.class, OuterDefaultedValue.class
);
     }
 
@@ -127,5 +124,4 @@ public class ValueNestedBuilderTest
         System.out.println( outer.toString() );
         // If we reach this point, value creation went well
     }
-
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/15f156d5/core/runtime/src/test/java/org/apache/zest/runtime/value/NestedValueBuilderTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/zest/runtime/value/NestedValueBuilderTest.java
b/core/runtime/src/test/java/org/apache/zest/runtime/value/NestedValueBuilderTest.java
index a0c2715..f20fa6e 100644
--- a/core/runtime/src/test/java/org/apache/zest/runtime/value/NestedValueBuilderTest.java
+++ b/core/runtime/src/test/java/org/apache/zest/runtime/value/NestedValueBuilderTest.java
@@ -17,6 +17,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import org.apache.zest.valueserialization.orgjson.OrgJsonValueSerializationService;
 import org.junit.Test;
 import org.apache.zest.api.common.UseDefaults;
 import org.apache.zest.api.property.Property;
@@ -71,6 +72,7 @@ public class NestedValueBuilderTest
     public void assemble( ModuleAssembly module )
         throws AssemblyException
     {
+        module.services( OrgJsonValueSerializationService.class );
         module.values( InnerValue.class, InnerDefaultedValue.class, OuterValue.class, OuterDefaultedValue.class
);
     }
 

http://git-wip-us.apache.org/repos/asf/zest-java/blob/15f156d5/core/runtime/src/test/java/org/apache/zest/runtime/value/ValueCompositeBasicsTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/zest/runtime/value/ValueCompositeBasicsTest.java
b/core/runtime/src/test/java/org/apache/zest/runtime/value/ValueCompositeBasicsTest.java
index 9174699..f5dabb5 100644
--- a/core/runtime/src/test/java/org/apache/zest/runtime/value/ValueCompositeBasicsTest.java
+++ b/core/runtime/src/test/java/org/apache/zest/runtime/value/ValueCompositeBasicsTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.zest.runtime.value;
 
+import org.apache.zest.valueserialization.orgjson.OrgJsonValueSerializationService;
 import org.junit.Test;
 import org.apache.zest.api.injection.scope.This;
 import org.apache.zest.api.mixin.Mixins;
@@ -37,6 +38,7 @@ public class ValueCompositeBasicsTest
     public void assemble( ModuleAssembly module )
         throws AssemblyException
     {
+        module.services( OrgJsonValueSerializationService.class );
         module.values( SomeValue.class );
     }
 


Mime
View raw message