rxjava2-jdbc icon indicating copy to clipboard operation
rxjava2-jdbc copied to clipboard

Java 9 TODO Automapped interface

Open sorinpepelea opened this issue 7 years ago • 5 comments

This is not an issue, but had no idea where to drop suggestions

Automapped interface with default methods currentluy does not work in Java 9

In order to work in Java 9, folowing changes should be made :

in org.davidmoten.rx.jdbc.Util#ProxyService<T>

class add additional method

@SuppressWarnings("unchecked") public <T> T newInstance(ResultSet rs, Class<T> cls) { return (T) Proxy.newProxyInstance(cls.getClassLoader(), new Class[] { cls }, new ProxyInstance<T>(cls, values())); }

in org.davidmoten.rx.jdbc.Util class

alter original method that works with java8

static <T> T autoMap(ResultSet rs, Class<T> cls, ProxyService<T> proxyService) { return proxyService.newInstance(); }

to a modified version that works with java 9

static <T> T autoMap(ResultSet rs, Class<T> cls, ProxyService<T> proxyService) { return proxyService.newInstance(rs, cls); }

Testing example that worked in Java 9

Database .test() .select(Person.class) .get() .map(Person::name) .blockingForEach(System.out::println);

Not related to this example - another suggestion - make annotation work with sql field aliases I would be happy to share more ideas for this great great api.

sorinpepelea avatar Mar 08 '18 10:03 sorinpepelea

Great, thanks! I'll have a look soon.

davidmoten avatar Mar 19 '18 00:03 davidmoten

I tried this and did not work for me but I would be happy to review a PR if you can make one. One of the current tests is below. Once your fix is in place uncomment the version check in the test and we'll see if it works!

    @Test
    public void testAutomappedObjectsWhenDefaultMethodInvoked() {
        // only run test if java 8
      if (System.getProperty("java.version").startsWith("1.8.")) {
            PersonWithDefaultMethod p = Database.test() //
                    .select("select name, score from person where name=?") //
                    .parameters("FRED") //
                    .autoMap(PersonWithDefaultMethod.class) //
                    .blockingFirst();
            assertEquals("fred", p.nameLower());
      }
    }

davidmoten avatar Mar 20 '18 00:03 davidmoten

Sorry for delay. I have no java 8 installed, and my project is set to java 9.04 - source/binary, compile .. i will post a project that works on java9 with all the changes i made.

sorinpepelea avatar Mar 24 '18 12:03 sorinpepelea

Here is one of my prj . cropped to minimum for testing. All you need to do is to adjust setting.ini file to fill your need adjust query line in com.atlas.sql.Q.java interface and run com.atlas.java9.DBTEST.java

I use postgresql database .. works like a charm ..but i tested also with mysql, mssql, oracle, gridx.zip

sorinpepelea avatar Mar 24 '18 15:03 sorinpepelea

Hi, if you can make a PR on this with unit test it would be very welcome.

davidmoten avatar Nov 20 '18 22:11 davidmoten