You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polygene.apache.org by ni...@apache.org on 2017/10/26 07:34:38 UTC

[08/33] polygene-java git commit: Better error message for missing return types need to be public classes or interfaces

Better error message for missing return types need to be public classes or interfaces

Signed-off-by: niclas <ni...@hedhman.org>


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

Branch: refs/heads/develop
Commit: 0f58a7d570d25ad8caa448e3f1fb2a2a06e14057
Parents: 60d2eeb
Author: niclas <ni...@hedhman.org>
Authored: Fri Jun 9 12:37:41 2017 +0800
Committer: niclas <ni...@hedhman.org>
Committed: Fri Jun 9 12:37:41 2017 +0800

----------------------------------------------------------------------
 .../runtime/composite/ConstructorModel.java     |  5 ++-
 .../TypedModifierInvocationHandler.java         | 17 +++++++-
 .../runtime/methods/AccessibleTest.java         | 41 +++++++++++++++-----
 3 files changed, 50 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/0f58a7d5/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorModel.java b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorModel.java
index 427cff0..026a4e1 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorModel.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorModel.java
@@ -27,13 +27,14 @@ import java.util.stream.Stream;
 import org.apache.polygene.api.common.ConstructionException;
 import org.apache.polygene.api.composite.ConstructorDescriptor;
 import org.apache.polygene.api.composite.InvalidCompositeException;
-import org.apache.polygene.api.util.AccessibleObjects;
 import org.apache.polygene.api.util.HierarchicalVisitor;
 import org.apache.polygene.api.util.VisitableHierarchy;
 import org.apache.polygene.runtime.injection.DependencyModel;
 import org.apache.polygene.runtime.injection.InjectedParametersModel;
 import org.apache.polygene.runtime.injection.InjectionContext;
 
+import static org.apache.polygene.api.util.AccessibleObjects.accessible;
+
 /**
  * JAVADOC
  */
@@ -46,7 +47,7 @@ public final class ConstructorModel
 
     public ConstructorModel( Constructor<?> constructor, InjectedParametersModel parameters )
     {
-        this.constructor = AccessibleObjects.accessible( constructor );
+        this.constructor = accessible( constructor );
         this.parameters = parameters;
     }
 

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/0f58a7d5/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TypedModifierInvocationHandler.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TypedModifierInvocationHandler.java b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TypedModifierInvocationHandler.java
index 7d96abf..bf750e9 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TypedModifierInvocationHandler.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TypedModifierInvocationHandler.java
@@ -21,6 +21,7 @@ package org.apache.polygene.runtime.composite;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 
 /**
  * JAVADOC
@@ -38,7 +39,21 @@ public final class TypedModifierInvocationHandler
         }
         catch( InvocationTargetException e )
         {
-            throw cleanStackTrace( e.getTargetException(), proxy, method );
+            Throwable targetException = e.getTargetException();
+            if( targetException instanceof IllegalAccessError )
+            {
+                // We get here if any of the return types or parameters are not public. This is probably due to
+                // the _Stub class ends up in a different classpace than the original mixin. We intend to fix this in
+                // 3.1 or 3.2
+                if( !Modifier.isPublic( method.getReturnType().getModifiers() ) )
+                {
+                    String message = "Return types must be public: " + method.getReturnType().getName();
+                    IllegalAccessException illegalAccessException = new IllegalAccessException( message );
+                    illegalAccessException.initCause( e.getTargetException() );
+                    throw cleanStackTrace( illegalAccessException, proxy, method );
+                }
+            }
+            throw cleanStackTrace( targetException, proxy, method );
         }
         catch( Throwable e )
         {

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/0f58a7d5/core/runtime/src/test/java/org/apache/polygene/runtime/methods/AccessibleTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/polygene/runtime/methods/AccessibleTest.java b/core/runtime/src/test/java/org/apache/polygene/runtime/methods/AccessibleTest.java
index 0d3ccb4..f420ef7 100644
--- a/core/runtime/src/test/java/org/apache/polygene/runtime/methods/AccessibleTest.java
+++ b/core/runtime/src/test/java/org/apache/polygene/runtime/methods/AccessibleTest.java
@@ -19,6 +19,7 @@
  */
 package org.apache.polygene.runtime.methods;
 
+import java.lang.reflect.UndeclaredThrowableException;
 import org.apache.polygene.api.injection.scope.This;
 import org.apache.polygene.api.mixin.Mixins;
 import org.apache.polygene.bootstrap.AssemblyException;
@@ -26,8 +27,9 @@ 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.hamcrest.core.IsInstanceOf.instanceOf;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
 
 public class AccessibleTest extends AbstractPolygeneTest
 {
@@ -40,10 +42,18 @@ public class AccessibleTest extends AbstractPolygeneTest
     }
 
     @Test
-    public void givenPrivateMixinWhenCallingFromWithinExpectSuccess()
+    public void givenPackageReturnTypeWhenCallingFromWithinExpectSuccess()
     {
         MyComposite myComposite = transientBuilderFactory.newTransient( MyComposite.class );
-        assertThat(myComposite.doSomething(), equalTo("Hello"));
+        try
+        {
+            myComposite.doSomething();
+            fail("Should have gotten an IllegalAccessException");
+        } catch( UndeclaredThrowableException e )
+        {
+            Throwable thrown = e.getUndeclaredThrowable();
+            assertThat( thrown, instanceOf(IllegalAccessException.class));
+        }
     }
 
     @Mixins( MyCompositeMixin.class)
@@ -55,10 +65,9 @@ public class AccessibleTest extends AbstractPolygeneTest
     @Mixins( MyFunctionMixin.class )
     public interface MyFunction
     {
-        String doSomething();
+        MyString doSomething();
     }
 
-
     public class MyCompositeMixin
         implements MyComposite
     {
@@ -70,19 +79,20 @@ public class AccessibleTest extends AbstractPolygeneTest
         {
             return new MyObject( function ).doSomething() + " ---- " + getClass().getClassLoader();
         }
+
     }
 
-    public class MyFunctionMixin
+    public static class MyFunctionMixin
         implements MyFunction
     {
         @Override
-        public String doSomething()
+        public MyString doSomething()
         {
-            return "Hello " + getClass().getClassLoader();
+            return new MyString( "Hello " );
         }
     }
 
-    public class MyObject
+    public static class MyObject
     {
         private MyFunction fn;
 
@@ -93,7 +103,18 @@ public class AccessibleTest extends AbstractPolygeneTest
 
         String doSomething()
         {
-            return fn.doSomething();
+            return fn.doSomething().mine;
         }
     }
+
+    static class MyString
+    {
+        String mine;
+
+        public MyString( String mine )
+        {
+            this.mine = mine;
+        }
+    }
+
 }