Skip to content

Method marshaling by list with Boon important for JSON method marshaling

RichardHightower edited this page Feb 26, 2014 · 2 revisions

Boon Home | Boon Source | If you are new to boon, you might want to start here. Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity. A real boon to Java to developers!

Java Boon - Method Marshaling with JSON and Lists

Many languages have support for JSON (Ruby, Groovy and Python). Boon has the most support for JSON for Java or any language.####

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. I have used this for REST servers and clients as well as Websocket and recently used it to create a service tier behind a vertx 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();
        }
    }

}

Thoughts

Thoughts? Write me at richard high tower AT g mail dot c-o-m (Rick Hightower).

Further Reading:

If you are new to boon start here:

Why Boon?

Easily read in files into lines or a giant string with one method call. Works with files, URLs, class-path, etc. Boon IO support will surprise you how easy it is. Boon has Slice notation for dealing with Strings, Lists, primitive arrays, Tree Maps, etc. If you are from Groovy land, Ruby land, Python land, or whatever land, and you have to use Java then Boon might give you some relief from API bloat. If you are like me, and you like to use Java, then Boon is for you too. Boon lets Java be Java, but adds the missing productive APIs from Python, Ruby, and Groovy. Boon may not be Ruby or Groovy, but its a real Boon to Java development.

Core Boon Philosophy

Core Boon will never have any dependencies. It will always be able to run as a single jar. This is not just NIH, but it is partly. My view of what Java needs is more inline with what Python, Ruby and Groovy provide. Boon is an addition on top of the JVM to make up the difference between the harder to use APIs that come with Java and the types of utilities that are built into Ruby, Python, PHP, Groovy etc. Boon is a Java centric view of those libs. The vision of Boon and the current implementation is really far apart.

===

Contact Info

blog|[twitter](https://twitter.com/RickHigh|[infoq]http://www.infoq.com/author/Rick-Hightower|[stackoverflow](http://stackoverflow.com/users/2876739/rickhigh)|[java lobby](http://java.dzone.com/users/rhightower)|Other | richard high tower AT g mail dot c-o-m (Rick Hightower)|work|cloud|nosql

Clone this wiki locally