This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Android REPL - add* syntax-sugar not available in constructor



Per Bothner wrote:
> 
> However, it should be doable to use java.lang.reflect.Proxy, as long as
> the
> requested type is an interface (rather than an abstract class).  The 
> idea would be to write a utility class ProcedureInvocationHandler that
> implements
> InvocationHandler by calling applyN on the procedure.
> 

That did the trick, and I now have the following working. It reads like Java
:(


(define-namespace Button android.widget.Button)
(define-namespace Toast android.widget.Toast)
(*activity*:runOnUiThread
   (runnable
     (lambda ()
       (letrec
           ((button  (Button *activity* text: "Push Me"))
            (handler
                (kawa.android.EventHandler:create
                    <android.view.View$OnClickListener> button
                    (lambda (v)
                        (android.util.Log:v "kawa-hello" v)
                        ((Toast:makeText *activity* "Clicked!"
Toast:LENGTH_LONG):show)))))
          (button:setOnClickListener handler)
          (*activity*:setContentView button)))))

// src/android/kawa/hello/EventHandler.java
package kawa.android;

// Similar to java.beans.EventHandler

import java.lang.reflect.Proxy;
import java.lang.reflect.Method;
import gnu.mapping.Procedure;

public class EventHandler implements java.lang.reflect.InvocationHandler
{
    private Procedure procedure;
    public EventHandler(Procedure procedure)
    {
        this.procedure = procedure;
    }

    public static <T> T create(Class<T> listenerInterface,
        Object target, Procedure procedure)
    {
        EventHandler eventHandler = new EventHandler(procedure);

        return (T) Proxy.newProxyInstance(target.getClass()
            .getClassLoader(),
            new Class[] { listenerInterface }, eventHandler);
    }

    public Object invoke(final Object proxy, final Method method,
        final Object[] args) throws Throwable
    {
        return procedure.applyN(args);
    }

}



> It should be possible to have a pre-built MouseAdapter class, and
> somehow tell the compiler to use it.  My preferred way to do this
> would be adding something like Scala's "views": A declaration telling Kawa
> that when it has an expression of type T in a context requiring an
> interface I then giving a recipe (function) for converting T to I.
> But I don't have time for that at this time.
> -- 
> 	--Per Bothner
> per@bothner.com   http://per.bothner.com/
> 
> 

Proxy works just fine.

Thanks Per.
-- 
View this message in context: http://old.nabble.com/Android-REPL---add*-syntax-sugar-not-available-in-constructor-tp33222365p33240947.html
Sent from the Sourceware - kawa list mailing list archive at Nabble.com.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]