POLYGENE-29 Handle mixed method return type auto-unboxing
Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/7fdf9609 Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/7fdf9609 Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/7fdf9609 Branch: refs/heads/develop Commit: 7fdf9609e7d67885627e9e2d5bc33008d6dd0b05 Parents: a5bf65b Author: Paul Merlin <[email protected]> Authored: Mon Apr 10 13:51:02 2017 +0200 Committer: Paul Merlin <[email protected]> Committed: Mon Apr 10 13:51:02 2017 +0200 ---------------------------------------------------------------------- .../polygene/library/scripting/ScriptMixin.java | 52 +++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7fdf9609/libraries/scripting/src/main/java/org/apache/polygene/library/scripting/ScriptMixin.java ---------------------------------------------------------------------- diff --git a/libraries/scripting/src/main/java/org/apache/polygene/library/scripting/ScriptMixin.java b/libraries/scripting/src/main/java/org/apache/polygene/library/scripting/ScriptMixin.java index 61c033b..bca28d3 100644 --- a/libraries/scripting/src/main/java/org/apache/polygene/library/scripting/ScriptMixin.java +++ b/libraries/scripting/src/main/java/org/apache/polygene/library/scripting/ScriptMixin.java @@ -116,7 +116,8 @@ public class ScriptMixin public Object invoke( Object proxy, Method method, Object[] objects ) throws Throwable { - return ( (Invocable) engine ).invokeFunction( method.getName(), objects ); + Object result = ( (Invocable) engine ).invokeFunction( method.getName(), objects ); + return castInvocationResult( method.getReturnType(), result ); } @Override @@ -186,4 +187,53 @@ public class ScriptMixin { engine.getContext().setAttribute( name, value, ScriptContext.GLOBAL_SCOPE ); } + + /** + * Needed to prevent class cast exception between boxed and unboxed types. + * Explicit casting to primitive type, triggers the auto-unboxing compiler trick. + */ + @SuppressWarnings( "RedundantCast" ) + private static Object castInvocationResult( Class<?> returnType, Object result ) + { + if( void.class.equals( returnType ) || Void.class.equals( returnType ) ) + { + return null; + } + if( returnType.isPrimitive() ) + { + if( char.class.equals( returnType ) ) + { + return (char) result; + } + if( boolean.class.equals( returnType ) ) + { + return (boolean) result; + } + if( short.class.equals( returnType ) ) + { + return (short) result; + } + if( int.class.equals( returnType ) ) + { + return (int) result; + } + if( byte.class.equals( returnType ) ) + { + return (byte) result; + } + if( long.class.equals( returnType ) ) + { + return (long) result; + } + if( float.class.equals( returnType ) ) + { + return (float) result; + } + if( double.class.equals( returnType ) ) + { + return (double) result; + } + } + return returnType.cast( result ); + } }
