polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nic...@apache.org
Subject [18/50] polygene-java git commit: Reverting the check that Composites are not allowed in @Uses. My prevention was too wide.
Date Mon, 10 Jul 2017 06:53:20 GMT
Reverting the check that Composites are not allowed in @Uses. My prevention was too wide.


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

Branch: refs/heads/es-jooq
Commit: a3e9acd425c7b912223a589b528f4c35af19b0dc
Parents: 517b909
Author: niclas <niclas@hedhman.org>
Authored: Sat Jun 24 11:10:43 2017 +0800
Committer: niclas <niclas@hedhman.org>
Committed: Sat Jun 24 11:10:43 2017 +0800

----------------------------------------------------------------------
 .../runtime/composite/TransientModel.java       | 15 +++-
 .../runtime/composite/UsesInstance.java         | 10 +--
 .../polygene/runtime/entity/EntityModel.java    |  6 ++
 .../polygene/runtime/value/ValueModel.java      | 56 +++++--------
 .../runtime/composite/IsCompositeTest.java      | 55 -------------
 .../runtime/composite/UseCompositeTest.java     | 84 ++++++++++++++++++++
 6 files changed, 125 insertions(+), 101 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a3e9acd4/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TransientModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TransientModel.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TransientModel.java
index 4aedb47..5cf9a71 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TransientModel.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TransientModel.java
@@ -27,6 +27,7 @@ import org.apache.polygene.api.composite.TransientDescriptor;
 import org.apache.polygene.api.constraint.ConstraintViolationException;
 import org.apache.polygene.api.structure.ModuleDescriptor;
 import org.apache.polygene.runtime.injection.InjectionContext;
+import org.apache.polygene.runtime.property.PropertyModel;
 
 /**
  * Model for Transient Composites
@@ -67,8 +68,18 @@ public class TransientModel extends CompositeModel
     public void checkConstraints( TransientStateInstance instanceState )
         throws ConstraintViolationException
     {
-        stateModel.properties().forEach( propertyModel ->
-            propertyModel.checkConstraints( instanceState.propertyFor( propertyModel.accessor()
).get() )
+        stateModel.properties().forEach( ( PropertyModel propertyModel ) ->
+                                         {
+                                             try
+                                             {
+                                                 propertyModel.checkConstraints( instanceState.propertyFor(
propertyModel.accessor() ).get() );
+                                             }
+                                             catch( ConstraintViolationException e )
+                                             {
+                                                 e.setCompositeDescriptor( this );
+                                                 throw e;
+                                             }
+                                         }
         );
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a3e9acd4/core/runtime/src/main/java/org/apache/polygene/runtime/composite/UsesInstance.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/UsesInstance.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/UsesInstance.java
index 9d9632b..79ce9fa 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/UsesInstance.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/UsesInstance.java
@@ -47,15 +47,7 @@ public final class UsesInstance
 
     public UsesInstance use( Object... objects )
     {
-        // Validate that there are no Composites in the list, since they are possibly not
fully initialized yet.
-        // TODO: The reason for that is that Composites may not be fully initialized when
reaching here, and the hashCode() call later will cause an NPE.
-        for( Object obj : objects )
-        {
-            if( PolygeneRuntimeImpl.isCompositeType( obj ) )
-            {
-                throw new ConstructionException( "Composites are not allowed as @Uses arguments:
" + obj.toString() );
-            }
-        }
+        // There is some case where we get here with only partially initialized composite
as "objects". That fails with NPE in useObjects.addAll() below. Should be figured out when
this happens and prevent it.
         HashSet<Object> useObjects = new HashSet<>();
         if( !uses.isEmpty() )
         {

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a3e9acd4/core/runtime/src/main/java/org/apache/polygene/runtime/entity/EntityModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/entity/EntityModel.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/entity/EntityModel.java
index 5fc00f5..bc2390e 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/entity/EntityModel.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/entity/EntityModel.java
@@ -134,6 +134,12 @@ public final class EntityModel extends CompositeModel
         {
             throw new ConstructionException( "Could not create new entity in store", e );
         }
+        catch( ConstraintViolationException e )
+        {
+            e.setCompositeDescriptor( this );
+            e.setIdentity( reference.identity() );
+            throw e;
+        }
     }
 
     public void initState( ModuleDescriptor module, EntityState entityState )

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a3e9acd4/core/runtime/src/main/java/org/apache/polygene/runtime/value/ValueModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/value/ValueModel.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/value/ValueModel.java
index 9338deb..496e503 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/value/ValueModel.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/value/ValueModel.java
@@ -25,8 +25,8 @@ import java.util.List;
 import org.apache.polygene.api.association.AssociationDescriptor;
 import org.apache.polygene.api.common.MetaInfo;
 import org.apache.polygene.api.common.Visibility;
-import org.apache.polygene.api.constraint.ValueConstraintViolation;
 import org.apache.polygene.api.constraint.ConstraintViolationException;
+import org.apache.polygene.api.constraint.ValueConstraintViolation;
 import org.apache.polygene.api.entity.EntityDescriptor;
 import org.apache.polygene.api.identity.HasIdentity;
 import org.apache.polygene.api.identity.Identity;
@@ -114,16 +114,7 @@ public final class ValueModel extends CompositeModel
                 }
                 catch( ConstraintViolationException e )
                 {
-                    try
-                    {
-                        PropertyInstance<Identity> identityProperty = state.propertyFor(
HasIdentity.IDENTITY_METHOD );
-                        e.setIdentity( identityProperty.get() );
-                    }
-                    catch( IllegalArgumentException e1 )
-                    {
-                        // ignore. Is not a HasIdentity instance
-                    }
-                    throw e;
+                    violations.addAll( e.constraintViolations() );
                 }
             }
                                                                );
@@ -137,17 +128,7 @@ public final class ValueModel extends CompositeModel
                 }
                 catch( ConstraintViolationException e )
                 {
-                    try
-                    {
-                        PropertyInstance<Identity> identityProperty = state.propertyFor(
HasIdentity.IDENTITY_METHOD );
-                        e.setIdentity( identityProperty.get() );
-                    }
-                    catch( IllegalArgumentException e1 )
-                    {
-                        // ignore. is not a HasIdentity value
-                    }
-                    throw e;
-
+                    violations.addAll( e.constraintViolations() );
                 }
             }
                                                                    );
@@ -161,28 +142,33 @@ public final class ValueModel extends CompositeModel
                 }
                 catch( ConstraintViolationException e )
                 {
-                    PropertyInstance<Identity> propertyInstance = state.propertyFor(
HasIdentity.IDENTITY_METHOD );
-                    throw e;
-
+                    violations.addAll( e.constraintViolations() );
                 }
             }
                                                                     );
-        if( ! violations.isEmpty() )
+        if( !violations.isEmpty() )
         {
             ConstraintViolationException exception = new ConstraintViolationException( violations
);
-            try
-            {
-                PropertyInstance<Identity> identityProperty = state.propertyFor( HasIdentity.IDENTITY_METHOD
);
-                exception.setIdentity(identityProperty.get());
-            }
-            catch( IllegalArgumentException e )
-            {
-                // ignore, there is no Identity.
-            }
+            exception.setCompositeDescriptor( this );
+            exception.setIdentity( extractIdentity( state, exception ) );
             throw exception;
         }
     }
 
+    private Identity extractIdentity( ValueStateInstance state, ConstraintViolationException
e )
+    {
+        try
+        {
+            PropertyInstance<Identity> identityProperty = state.propertyFor( HasIdentity.IDENTITY_METHOD
);
+            return identityProperty.get();
+        }
+        catch( IllegalArgumentException e1 )
+        {
+            // ignore. is not a HasIdentity value
+        }
+        return null;
+    }
+
     public ValueInstance newValueInstance( ValueStateInstance state )
     {
         Object[] mixins = mixinsModel.newMixinHolder();

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a3e9acd4/core/runtime/src/test/java/org/apache/polygene/runtime/composite/IsCompositeTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/polygene/runtime/composite/IsCompositeTest.java
b/core/runtime/src/test/java/org/apache/polygene/runtime/composite/IsCompositeTest.java
deleted file mode 100644
index a7fe462..0000000
--- a/core/runtime/src/test/java/org/apache/polygene/runtime/composite/IsCompositeTest.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- *  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.composite;
-
-import org.apache.polygene.api.common.ConstructionException;
-import org.apache.polygene.api.injection.scope.Uses;
-import org.apache.polygene.bootstrap.AssemblyException;
-import org.apache.polygene.bootstrap.ModuleAssembly;
-import org.apache.polygene.test.AbstractPolygeneTest;
-import org.junit.Test;
-
-public class IsCompositeTest extends AbstractPolygeneTest
-{
-
-    @Override
-    public void assemble( ModuleAssembly module )
-        throws AssemblyException
-    {
-        module.transients( MyTransient.class );
-        module.objects( MyObject.class );
-    }
-
-    @Test( expected = ConstructionException.class )
-    public void givenCompositeToUsesWhenInstantiatingExpectException()
-    {
-        MyTransient myTransient = transientBuilderFactory.newTransient( MyTransient.class
);
-        objectFactory.newObject( MyObject.class, myTransient );
-    }
-
-    public interface MyTransient
-    {}
-
-    public static class MyObject
-    {
-        @Uses
-        MyTransient my;
-    }
-}

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a3e9acd4/core/runtime/src/test/java/org/apache/polygene/runtime/composite/UseCompositeTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/polygene/runtime/composite/UseCompositeTest.java
b/core/runtime/src/test/java/org/apache/polygene/runtime/composite/UseCompositeTest.java
new file mode 100644
index 0000000..16cb948
--- /dev/null
+++ b/core/runtime/src/test/java/org/apache/polygene/runtime/composite/UseCompositeTest.java
@@ -0,0 +1,84 @@
+/*
+ *  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.composite;
+
+import org.apache.polygene.api.common.Optional;
+import org.apache.polygene.api.injection.scope.Uses;
+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.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+public class UseCompositeTest extends AbstractPolygeneTest
+{
+
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        module.transients( Hello.class );
+        module.objects( Speaker.class );
+    }
+
+    @Test
+    public void givenCompositeToUsesWhenInstantiatingExpectException()
+    {
+        Hello hello = transientBuilderFactory.newTransient( Hello.class );
+        hello.name().set( "World" );
+        Speaker speaker = objectFactory.newObject( Speaker.class, hello );
+        assertThat( speaker.speak(), equalTo( "Hello, World!" ) );
+    }
+
+    @Mixins( HelloMixin.class )
+    public interface Hello
+    {
+        String sayHello();
+
+        @Optional
+        Property<String> name();
+    }
+
+    public static abstract class HelloMixin
+        implements Hello
+    {
+
+        @Override
+        public String sayHello()
+        {
+            return "Hello, " + name().get();
+        }
+    }
+
+    public static class Speaker
+    {
+        @Uses
+        Hello hello;
+
+        String speak()
+        {
+            return hello.sayHello() + "!";
+        }
+    }
+}


Mime
View raw message