Rick

Rick
Rick

Friday, February 21, 2014

Added respondsTo, invoke from Ruby to Java (via Boon), and added Go Programming style interface support, and dynamic JSON, list, map marshaling


Wrote some code to speed up injection container construction and then went ahead and wrote some code to speed up the toList biz. Then wrote some additional code to speed up dynamic invoke

Then I've always wanted to add some nice reflection utilities to easily invoke/marshal to Java objects.  I wrote a Ruby style respondsTo method (cached methods, should be fairly fast). I also added JSON, List, and Map marshaling to Java method calls. 

The respondsTo works with no args, class as args parameter types and objects as arguments. 

Then on top of respondsTo I wrote Go Programming style interface support so you can have an interface and as long as you support all the methods in that interface, you object does not have to implement it. This is so you don't have to have 100 jar file dependencies for design by interface. Read up on Go Programming to see what I am talking about because I am not giving it justice. 

Considering writing a respondsTo that works with JSON, maps and lists like the Invoker/Marshaler (seen at bottom of this post). I am also considering adding the ability for invoke to calling   missingMethod(String methodName, Object... args):Object if the method can't be found and there is a missingMethod defined in the instance. :)

Please take a look at the Ruby style respondsTo in Java.


Added Ruby style respondsTo to Java

We can invoke methods if they are supported. (Duck typing in Java)
        FileObject file = new FileObject();
        StringReaderThing reader = new StringReaderThing(lines("Hi mom", "how are you?"));
        List<Object> list = list(file, reader);


        for (Object object : list) {
            if ( respondsTo(object, "open", String.class) ) invoke(object, "open", "hi");

            if ( respondsTo(object, "add", int.class, int.class) ) puts ("add", invoke(object, "add", 1, 2));

            if ( respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if ( respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if ( respondsTo(object, "close" ) ) invoke(object, "close");
        }

The above invokes openaddreadLine and close methods if they are found. Above we pass types to see if there is a match. You don't have to pass types, respondsTo can also just check to see if the method exists. The invoke operation should be fairly fast in a tight loop.
You have the option of invoking by argument type match without passing around types (classes) as follows:
        FileObject file = new FileObject();
        StringReaderThing reader = new StringReaderThing(lines("Hi mom", "how are you?"));
        List<Object> list = list(file, reader);


        for (Object object : list) {
            if (respondsTo(object, "open", "hi") ) invoke(object, "open", "hi");

            if (respondsTo(object, "add", 1, 2) ) puts ("add", invoke(object, "add", 1, 2));


            if (respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if (respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if (respondsTo(object, "close" ) ) invoke(object, "close");
        }
You can also invoke with a java.util.List (invoke and respondsTo take var-arg Object... by default for arguments) as follows:
        FileObject file = new FileObject();
        StringReaderThing reader = new StringReaderThing(lines("Hi mom", "how are you?"));
        List<Object> list = list(file, reader);



        List <?> openList = list("hi");
        List <?> addList = list(1, 2);

        for (Object object : list) {
            if (respondsTo(object, "open", openList) ) invoke(object, "open", openList);

            if (respondsTo(object, "add", addList) ) puts ("add", invoke(object, "add", addList));


            if (respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if (respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if (respondsTo(object, "close" ) ) invoke(object, "close");
        }

        boolean ok = file.closeCalled && file.readLine && file.openCalled && file.addCalled || die();


I also added the ability to invoke by interface. This is a loosely typed interface concept like the Go Programming language in that the class does not have to implement the interface. It only has to implement the methods in the interface. This allows loosely coupled (no jar dependencies) contracts like Go, or so I hope.
        FileObject file = new FileObject();
        StringReaderThing reader = new StringReaderThing(lines("Hi mom", "how are you?"));
        List<Object> list = list(file, reader);



        List <?> openList = list("hi");
        List <?> addList = list(1, 2);

        for (Object object : list) {
            if ( handles(object, FileInterface.class) ) invoke(object, "open", openList);

            if ( respondsTo(object, "add", addList) ) puts ("add", invoke(object, "add", addList));


            if ( handles(object, FileInterface.class) ) {
                puts ( invoke(object, "readLine") );
                puts ( invoke(object, "readLine") );
                invoke(object, "close");
            }
        }


Here is the complete code listing for handles and respondsTo:
package org.boon.core.reflection;


import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import static org.boon.Boon.puts;
import static org.boon.Exceptions.die;
import static org.boon.Lists.list;
import static org.boon.Str.lines;
import static org.boon.core.reflection.Reflection.handles;
import static org.boon.core.reflection.Reflection.invoke;
import static org.boon.core.reflection.Reflection.respondsTo;

public class RespondsTo {

    public static interface FileInterface {
        void open(String fileName);

        String readLine();

        void close();
    }


    public static class FileObject {

        boolean readLine;
        boolean openCalled;
        boolean closeCalled;

        boolean addCalled;

        public void open(String fileName) {

            openCalled = true;
            puts("Open ", fileName);
        }

        public String readLine() {
            readLine = true;
            return "read line";
        }

        public int add(int i, int f) {
           addCalled = true;
           return i + f;
        }

        public void close() {
            closeCalled = true;
        }


    }

    public static class StringReaderThing {

        BufferedReader reader = null;

        String contents;

        public StringReaderThing(String contents) {
            this.contents = contents;
        }

        public void open(String fileName) {

            reader = new BufferedReader(new StringReader(contents));

        }

        public String readLine() throws IOException {
            return reader.readLine();
        }

        public void close() throws IOException {
            reader.close();
        }

    }



    @Test
    public void test() {
        FileObject file = new FileObject();
        StringReaderThing reader = new StringReaderThing(lines("Hi mom", "how are you?"));
        List<Object> list = list(file, reader);


        for (Object object : list) {
            if ( respondsTo(object, "open", String.class) ) invoke(object, "open", "hi");

            if ( respondsTo(object, "add", int.class, int.class) ) puts ("add", invoke(object, "add", 1, 2));

            if ( respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if ( respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if ( respondsTo(object, "close" ) ) invoke(object, "close");
        }

        boolean ok = file.closeCalled && file.readLine && file.openCalled && file.addCalled || die();


    }



    @Test
    public void testInvoke() {
        FileObject file = new FileObject();
        StringReaderThing reader = new StringReaderThing(lines("Hi mom", "how are you?"));
        List<Object> list = list(file, reader);


        for (Object object : list) {
            if (respondsTo(object, "open", "hi") ) invoke(object, "open", "hi");

            if (respondsTo(object, "add", 1, 2) ) puts ("add", invoke(object, "add", 1, 2));


            if (respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if (respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if (respondsTo(object, "close" ) ) invoke(object, "close");
        }

        boolean ok = file.closeCalled && file.readLine && file.openCalled && file.addCalled || die();



    }


    @Test
    public void testInvokeByList() {
        FileObject file = new FileObject();
        StringReaderThing reader = new StringReaderThing(lines("Hi mom", "how are you?"));
        List<Object> list = list(file, reader);



        List <?> openList = list("hi");
        List <?> addList = list(1, 2);

        for (Object object : list) {
            if (respondsTo(object, "open", openList) ) invoke(object, "open", openList);

            if (respondsTo(object, "add", addList) ) puts ("add", invoke(object, "add", addList));


            if (respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if (respondsTo(object, "readLine") ) puts ( invoke(object, "readLine") );

            if (respondsTo(object, "close" ) ) invoke(object, "close");
        }

        boolean ok = file.closeCalled && file.readLine && file.openCalled && file.addCalled || die();



    }



    @Test
    public void testHandles() {
        FileObject file = new FileObject();
        StringReaderThing reader = new StringReaderThing(lines("Hi mom", "how are you?"));
        List<Object> list = list(file, reader);



        List <?> openList = list("hi");
        List <?> addList = list(1, 2);

        for (Object object : list) {
            if ( handles(object, FileInterface.class) ) invoke(object, "open", openList);

            if ( respondsTo(object, "add", addList) ) puts ("add", invoke(object, "add", addList));


            if ( handles(object, FileInterface.class) ) {
                puts ( invoke(object, "readLine") );
                puts ( invoke(object, "readLine") );
                invoke(object, "close");
            }
        }

        boolean ok = file.closeCalled && file.readLine && file.openCalled && file.addCalled || die();



    }
}

Dynamic invocation via list, map, Json Map, and Json List

I added the ability to dynamic dispatch to a method from a map or list or JSON. This is important for REST, Websocket, Service and JSON messaging.
Take a look and see if you can figure out where I am going, then let me know because sometimes I forget.
package org.boon.core.reflection;

import org.boon.Lists;
import org.boon.Maps;
import org.junit.Test;

import javax.annotation.PostConstruct;


import static org.boon.Boon.puts;
import static org.boon.Boon.sputs;
import static org.boon.Exceptions.die;

/**
 * Created by Richard on 2/17/14.
 */
public class InvokerTest {


    public static class HelloWorldArg  {
         int i = 0;
         String hello = "null";

        public HelloWorldArg( int i ) {
            this.i = i;
        }

        public HelloWorldArg( int i, String hello ) {
            this.i = i;
            this.hello = hello;
        }


        public HelloWorldArg(  String hello ) {
            this.hello = hello;
        }

        @Override
        public boolean equals( Object o ) {
            if ( this == o ) return true;
            if ( o == null || getClass() != o.getClass() ) return false;

            HelloWorldArg that = ( HelloWorldArg ) o;

            if ( i != that.i ) return false;
            if ( hello != null ? !hello.equals( that.hello ) : that.hello != null ) return false;

            return true;
        }



        @Override
        public int hashCode() {
            int result = i;
            result = 31 * result + ( hello != null ? hello.hashCode() : 0 );
            return result;
        }
    }

    public static class HelloWorld  {

        boolean initCalled;

        boolean called1st;
        boolean called2nd;



        private HelloWorldArg sayArg(HelloWorldArg hi, int i) {
            return hi;
        }



        private HelloWorldArg sayArg2 (HelloWorldArg hi) {
            return hi;
        }

        private void sayHi(String hi) {
            puts ( "hi ", hi );

        }



        private String say(String hi, int i) {
            return sputs( "hi ", hi, i );

        }



        private void sayHi2(String hi) {
            called1st = true;
            puts ( "hi ", hi );

        }

        private void sayHi2(String hi, int i) {
            called2nd = true;

            puts ( "hi ", hi );

        }


        @PostConstruct
        private void init() {
            initCalled = true;

        }



    }

    @Test
    public void test() {
        Invoker.invoke( new HelloWorld(), "sayHi", "Rick" );
    }



    @Test
    public void testPostConstruct() {
        HelloWorld hw = new HelloWorld();
        hw.initCalled = false;
        Invoker.invokeMethodWithAnnotationNoReturn( hw, "postConstruct" );
        if (!hw.initCalled) {
            die("Post construct not called");
        }
    }


    @Test
    public void testPostConstruct2() {
        HelloWorld hw = new HelloWorld();
        hw.initCalled = false;
        Invoker.invokeMethodWithAnnotationNoReturn( hw, "PostConstruct" );
        if (!hw.initCalled) {
            die("Post construct not called");
        }
    }


    @Test
    public void testPostConstruct3() {
        HelloWorld hw = new HelloWorld();
        hw.initCalled = false;
        Invoker.invokeMethodWithAnnotationNoReturn( hw, "javax.annotation.PostConstruct" );
        if (!hw.initCalled) {
            die("Post construct not called");
        }
    }


    @Test
    public void testNoOverloads() {
        try {
            Invoker.invoke( new HelloWorld(), "sayHi2", "Rick" );
            die("can't get this far");
        } catch (Exception ex) {

        }
    }




    @Test
    public void testAllowOverloads() {
        HelloWorld hw = new HelloWorld();
        hw.called1st = false;
        hw.called2nd = false;

        Invoker.invokeOverloaded( hw, "sayHi2", "Rick" );

        if ((!hw.called1st)) {
            die("");
        }


        if ((hw.called2nd)) {
            die("");
        }
    }




    @Test
    public void testAllowOverloads3() {
        HelloWorld hw = new HelloWorld();
        hw.called1st = false;
        hw.called2nd = false;

        Invoker.invokeOverloadedFromList( hw, "sayHi2", Lists.list("Rick") );

        if ((!hw.called1st)) {
            die("");
        }


        if ((hw.called2nd)) {
            die("");
        }
    }



    @Test
    public void testAllowOverloads2() {
        HelloWorld hw = new HelloWorld();
        hw.called1st = false;
        hw.called2nd = false;

        Invoker.invokeOverloaded( hw, "sayHi2", "Rick", 1 );

        if ((hw.called1st)) {
            die("");
        }


        if ((!hw.called2nd)) {
            die("");
        }
    }



    @Test
    public void testAllowOverloads4() {
        HelloWorld hw = new HelloWorld();
        hw.called1st = false;
        hw.called2nd = false;

        Invoker.invokeOverloadedFromList( hw, "sayHi2", Lists.list("Rick", "1") );

        if ((hw.called1st)) {
            die("");
        }


        if ((!hw.called2nd)) {
            die("");
        }
    }



    @Test
    public void testWithListSimple() {
        Invoker.invokeFromList( new HelloWorld(), "sayHi", Lists.list( "Rick" ) );
    }


    @Test
    public void testWithListSimple2() {
        String message = (String) Invoker.invokeFromList( new HelloWorld(), "say", Lists.list( "Rick", 1 ) );
        puts (message);

        if (!message.equals( "hi  Rick 1\n" )) die(message);
    }


    @Test
    public void testWithListSimpleWithConversion() {
        String message = (String) Invoker.invokeFromList( new HelloWorld(), "say", Lists.list( "Rick", "1" ) );
        puts (message);
        if (!message.equals( "hi  Rick 1\n" )) die(message);
    }



    @Test
    public void testComplex() {
        HelloWorldArg message = (HelloWorldArg) Invoker.invokeFromList( new HelloWorld(), "sayArg",
                Lists.list( Lists.list( "1", "Hello" ), 1 ) );


        if (!message.equals( new HelloWorldArg( 1, "Hello" ) )) {
            die();
        }
    }



    @Test
    public void testComplex2() {
        HelloWorldArg message = (HelloWorldArg) Invoker.invokeFromObject( new HelloWorld(), "sayArg2",
                Lists.list( "1", "Hello" ) );


        if (!message.equals( new HelloWorldArg( 1, "Hello" ) )) {
            die();
        }
    }



    @Test
    public void testComplex3() {
        HelloWorldArg message = (HelloWorldArg) Invoker.invokeFromList( new HelloWorld(), "sayArg2",
                Lists.list( (Object)Lists.list( "1", "Hello" ) ) );


        if (!message.equals( new HelloWorldArg( 1, "Hello" ) )) {
            die();
        }
    }



    @Test
    public void testComplex4() {
        HelloWorldArg message = (HelloWorldArg) Invoker.invokeFromList( new HelloWorld(), "sayArg2",
                Lists.list( Maps.map( "i", "1", "hello", "Hello" )));


        if (!message.equals( new HelloWorldArg( 1, "Hello" ) )) {
            die();
        }
    }



    @Test
    public void testComplex5() {
        HelloWorldArg message = (HelloWorldArg) Invoker.invokeFromList( new HelloWorld(), "sayArg2",
                Lists.list( Maps.map( "i", "1", "hello", "Hello" )));


        if (!message.equals( new HelloWorldArg( 1, "Hello" ) )) {
            die();
        }
    }

}

Kafka and Cassandra support, training for AWS EC2 Cassandra 3.0 Training