Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 9E24E200C77 for ; Mon, 10 Apr 2017 13:54:43 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 9CF22160B85; Mon, 10 Apr 2017 11:54:43 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 99982160BAE for ; Mon, 10 Apr 2017 13:54:42 +0200 (CEST) Received: (qmail 79884 invoked by uid 500); 10 Apr 2017 11:54:41 -0000 Mailing-List: contact commits-help@polygene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@polygene.apache.org Delivered-To: mailing list commits@polygene.apache.org Received: (qmail 79847 invoked by uid 99); 10 Apr 2017 11:54:41 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Apr 2017 11:54:41 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 80E8BE961F; Mon, 10 Apr 2017 11:54:41 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: paulmerlin@apache.org To: commits@polygene.apache.org Date: Mon, 10 Apr 2017 11:54:48 -0000 Message-Id: <2a34f862d31e43d385c18ac8fdc05d81@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [8/9] polygene-java git commit: POLYGENE-29 Handle mixed method return type auto-unboxing archived-at: Mon, 10 Apr 2017 11:54:43 -0000 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 Authored: Mon Apr 10 13:51:02 2017 +0200 Committer: Paul Merlin 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 ); + } }