<?xml version="1.0" encoding="utf-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="en">
<title>R&amp;#233;mi Forax&apos;s Blog</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/" />
<modified>2008-04-01T12:48:29Z</modified>
<tagline></tagline>
<id>tag:weblogs.java.net,2008:/blog/forax/343</id>
<generator url="http://www.movabletype.org/" version="3.01D">Movable Type</generator>
<copyright>Copyright (c) 2008, forax</copyright>
<entry>
<title>Closure and groovy builder</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2008/04/closure_and_gro.html" />
<modified>2008-04-01T12:48:29Z</modified>
<issued>2008-04-01T11:47:11Z</issued>
<id>tag:weblogs.java.net,2008:/blog/forax/343.9451</id>
<created>2008-04-01T11:47:11Z</created>
<summary type="text/plain">One think i really like in Groovy, it&apos;s its concept of Builder.
This post explains how to do the same trick using Java 7 closure.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
 One think i really like in Groovy, it's its concept of
 <a href="http://groovy.codehaus.org/Builders">Builder</a>.
<br>
 It allows to simply create tree of objects like XML trees
 using a concise syntax.
</p>
<p>
 An HTML tree in Groovy is defined like that
</p>
<pre>
  html {
      head {
        title "hello groovy builder"
      }
  }
</pre>
<p>
 JavaFX uses a quite 
 <a href="http://jfx.wikia.com/wiki/XML_Framework">similar syntax</a>
 but the syntax is built-in.
 The beauty of the groovy beast is that the builder syntax
 relies on closure.
</p>
<p><b>Closure</b></p>
<p>
 The trick is the following:
 <ol>
   <li>An XML node is a method.
   <li>The content of a XML Node is a closure
       taken as argument of the method
 </ol>
</p>
<p>
 Ok, so let's try to do the same in Java using the
 <a href="http://www.javac.info/closures-v05.html">BGGA
 closure proposal</a>.
 <a href="http://docs.google.com/View?docid=k73_1ggr36h">CICE</a>
 is too verbose for that case and it should work
 seamlessly with <a href="http://docs.google.com/View?docid=ddhp95vd_0f7mcns">FCM</a>+
 <a href="http://docs.google.com/View?docid=ddhp95vd_8f8zkn3">JCA</a>
</p>
<p>
 <b>Proxiiiiiiiiii</b>
</p>
<p>
 But before, how to define all the allowed markups of
 an XML dialect ?
<br>
 Ok, we need one method by markup, but because the code is identical
 for two different markups, instead of writing boilerplate code,
 I will use the class
 <a href="http://download.java.net/jdk7/docs/api/java/lang/reflect/Proxy.html">java.lang.reflect.Proxy</a>.
<br>
 To use the Proxy, we have to define an interface. The one below
 allow us to generate XHTML.
</p>
<pre>
public interface XHTMLBuilder {
  public void html(Object textOrClosure);
  public void head(Object textOrClosure);
  public void title(String text);
  public void body(Object textOrClosure);
  public void h1(Object textOrClosure);
  public void br();
}
</pre>
<p>
 A Proxy acts as a multiplexer a call to any of its
 methods is redirected to a single generic method
 named <tt>invoke</tt>.
</p>
<pre>
public class XMLBuilderFactory {
  final Appendable appendable;
  
  public XMLBuilderFactory(Appendable appendable) {
    this.appendable=appendable;
  }
  
  public void text(CharSequence seq) {
    try {
      appendable.append(seq);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  
  public &lt;T&gt; T createBuilder(Class&lt;T&gt; generatorInterface) {
    return generatorInterface.cast(Proxy.newProxyInstance(
      generatorInterface.getClassLoader(),
      new Class&lt;?&gt;[]{generatorInterface},
      new InvocationHandler() {
        public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
          
          ...
          return null;
        }
      }));
  }
}
</pre>
<p>
 Here <a href="http://download.java.net/jdk7/docs/api/java/lang/Appendable.html">appendable</a>
 acts as a writable stream of characters,
 something on which I can append characters.
<br>
 The method <tt>createBuilder</tt> takes an interface as
 argument and returns a class that implements that interface.
 All methods of thi interface are routed to the method
 <tt>invoke</tt> of the <tt>InvocationHandler</tt>.
</p>
<p>
 <b>Java Builder</b>
</p>
<p>
 Now, suppose that the method <tt>invoke</tt> is written,
 we will see after how to write it.
<br>
 To write an XHTML tree on the standard ouput,
 using the BGGA closure syntax, the code is:
</p>
<pre>
public class Main {
  public static void main(String[] args) {
    XMLBuilderFactory factory=new XMLBuilderFactory(System.out);
    XHTMLBuilder b=factory.createBuilder(XHTMLBuilder.class);
    
    b.html({=&gt;
      b.head({=&gt;
        b.title("hello Java 7 builder");
      });
      
      b.body({=&gt;
        b.h1("Hello");
        factory.text("Greetings from");
        b.br();
        factory.text("Java 7 builder");
      });
    });
  }
}
</pre>
<p>
 It's even more readable if we use the
 <a href="http://www.javac.info/closures-v05.html">control invocation syntax</a>.
</p>
<pre>
    b.html() {
      b.head() {
        b.title("hello Java 7 builder");
      }
      
      b.body() {
        b.h1("Hello");
        factory.text("Greetings from");
        b.br();
        factory.text("Java 7 builder");
      }
    }
</pre>
<p>
 This construct has several advantages.
 I can't produce ill formed XML, markup are opened
 and closed by the same code.
 Because the builder uses an interface I can't create
 markup with the wrong name. Even better, refactoring
 the markup name will works !
</p>
<p>
 <b>How to write the method invoke ?</b>
</p>
<p>
 For each argument of the method, i have
 to determine if it's a closure or any other objects.
 If it's a closure, i have to invoke it.
 If it's another object, i can append it into the
 <tt>Appendable</tt>.
<br>
 The question is how to dynamically test if an object
 is or not a closure.
<br>
 I don't want any closure i just want a closure that takes
 no parameter and returns void, a {=>void}.
</p>
<pre>
  if (arg instanceof {=>void}) {
    (({=>void})arg).invoke();
  }
</pre>
<p>
 There is two poblem with that code.
 First, there are two kinds of closure (function type),
 unrestricted and restricted one.
 A restricted closure is a closure that can't break the control
 flow using break, continue or return.
 I am not sure i want my closure be able to break the
 control flow  but the control invocation syntax creates
 unrestricted closure, so i need unrestricted closure.
 So instead of using '=>' in the function type, i have to use
 '==>'
<br>
 By the way, i think the spec should be revised
 to swap the two notations ('=>' and '==>'),
 '=>' should be used for unrestricted closure.
 If i want a restricted closure which is not the default case,
 i have to take care about that and use the '==>' syntax.
 Append a second '=' is equivalent to say
 don't worry compiler i know what i'am doing. 
<br>
 So currently, the code should be:
</p>
<pre>
  if (arg instanceof {==>void}) {
    (({==>void})arg).invoke();
  }
</pre>
<p>
 But there is another problem, currently,
 {==>void} is a parameterized type and so
 it's illegal to use it in an instanceof.
 Here, i have ask Neal, he says
 he planed to make "types such as this one non-generic".
<br>
 There is a workaround, currently 
 the closure prototype works with 1.4/1.5 VM
 and thus generates interfaces corresponding to
 function types.
 The interface corresponding to the raw type of {==>void} is
 <tt>javax.lang.function.unrestricted.V</tt>.
<br>
 Here is the code that works:
</p>
<pre>
  String name=method.getName();
  if (args==null || args.length==0) {
    appendable.append('<'+name+"/>");
    return null;
  }
          
  appendable.append('<'+name+'>');
  try {
    for(Object arg:args) {
      if (arg instanceof javax.lang.function.unrestricted.V) {
        ((javax.lang.function.unrestricted.V)arg).invoke();
      } else {
        appendable.append(arg.toString());
      }
    }
  } finally {
    appendable.append("</"+name+'>');
  }
</pre>
<p>
  A zip containing the wole codes is here:
  <a href="http://weblogs.java.net/blog/forax/archive/closure-builder/closure-builder.zip">closure-builder.zip</a>

 <br>
  You need the
  <a href="http://javac.info/">prototype of the BGGA</a>
  closure too.
</p>
<p>
 Cheers,
<br>
 Rémi
</p>]]>

</content>
</entry>
<entry>
<title>Da Vinci runtime properties</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2008/03/da_vinci_runtim.html" />
<modified>2008-03-23T22:07:42Z</modified>
<issued>2008-03-23T22:07:25Z</issued>
<id>tag:weblogs.java.net,2008:/blog/forax/343.9406</id>
<created>2008-03-23T22:07:25Z</created>
<summary type="text/plain">Use anonymous class of the Da Vinci VM to implement the runtime support of the property spec.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
  After a week without any internet access point surfing the
  snow of the Alp, monday, my fingers was eager to touch the keyboard again.
  Why not finishing my prototype of runtime properties that use
  the Da Vinci VM (i really love that name).
</p>
<p>
  One ugly thing of the
  <a href="http://docs.google.com/View?docid=dfhbvdfw_1f7mzf2">draft
  v3 of the property spec</a>, is a property object
  is implemented by a supplementary class generated by the compiler.
  This means the compiler must create one class by property and so
  bloat the application with lot of stupid code.
 <br>
  Ok, let's try to do better.
  No class at compile time means reflection or
  runtime time generation.
  In fact, it's not a real choice because
  reflection since 1.4.1 generates byte-code when
  a method is often called.
</p>
<p>
 <b>Da Vinci VM</b>
</p>
<p>
  The <a href="http://openjdk.java.net/projects/mlvm/">Da Vinci VM</a>
  is the prototype implementation of
  <a href="http://www.jcp.org/en/jsr/detail?id=292">JSR 292</a>,
  a modified hotspot VM patched with new entry points that help
  to implement dynamic languages on top of the Java platform.
<br>
 The first feature available,
 <a href="http://blogs.sun.com/jrose/entry/anonymous_classes_in_the_vm">anonymous class</a>
 (VM anonymous class not compiler one)
 allows you to create classes with some interesting features:
 <ol>
  <li>The class is not referenced by a classloader, so is GCed
      when there is no more instances of that class
  <li>Class is like an inner-class of an host class and
      therefore can access to members of the host class. 
  <li>In order to create the class you can patch an existing class.
 </ol>
 So i can create property object by patching a 
 template code, the resulting class will be able to call getter and setter
 of the bean class.
<br>
 Great !
<br>
 I've implemented a small library that allows to create
 property object at runtime. It detects if the current VM
 is the Da Vinci VM and uses VM anonymous class or
 uses reflection otherwise.
</p>
<p>
 <b>How property object works</b>
</p>
<p>
 If you are not familiar with properties,
 you can read
 <a href="http://weblogs.java.net/blog/forax/archive/2007/01/property_reload.html">an old blog entry</a>.
 <br>
 A property object stands for a property and permit access
 to the value of the property of any instance of a Class
 that declare that property. 
<br>
 By example, the following code:
</p>
<pre>
public class Bean {
  private int x;
  public int getX() {
    return x;
  }
  public void setX(int x) {
    this.x = x;
  }
  
  private String text;
  public String getText() {
    return text;
  }
  public void setText(String text) {
    this.text = text;
  }
    
  public static void main(String[] args) {
      Bean bean = new Bean();
      Property<Bean> propertyX =
          Property.create(Bean.class, "x");
      bean.setX(0xCAFEBABE);
      System.out.printf(propertyX + " %x\n", propertyX.getValue(bean));
      
      Property<Bean> propertyText = Property.create(Bean.class, "text");
      propertyText.setValue(bean, "hello property");
      System.out.println(bean.getText());
  }
}
</pre>
<p>
 prints:
</p>
<pre>
  int Bean.x cafebabe
  hello property
</pre>
<p>
 <b>How to test it ?</b>
</p>
<p>
  If your OS is Windows, Solaris or MacOs you have to compile the VM
  <a href="http://softonaut.blogspot.com/2008/02/building-openjdk-multi-language-vm-aka.html">by yourself</a>, sorry.
  On linux, you can download the
  <a href="http://download.java.net/jdk7/binaries/">binary of the jdk7 b24</a>
  if you haven't already it, download the following zip
  <a href="http://weblogs.java.net/blog/forax/archive/davinci/davinci.zip">davinci.zip</a>
  and unzip it in your JAVA_HOME/jre/lib/i386/
 <br> 
  It adds a new server JVM lib named 'davinci' that can be launched using
 <pre>
  java -davinci ...
 </pre>
</p>
<p>
 Now download the property runtime support
 <a href="http://weblogs.java.net/blog/forax/archive/davinci/property-runtime.jar">property-runtime.jar</a>,
 because it adds new classes and overrides some existing classes to the JDK
 you must prepend the jar to the bootclasspath.
<br>
 To launch the example above:
<pre>
  java -davinci -Xbootclasspath/p:property.jar Bean
</pre>
</p>
<p>
 The source of the property runtime support are available on the kijaro
 web site here:
 <a href="https://kijaro.dev.java.net/source/browse/kijaro/branches/properties/">https://kijaro.dev.java.net/source/browse/kijaro/branches/properties/</a>.
</p>
<p>
 What's the next step, finish to write a new version of the property spec
 and provide a modified compiler according to that spec.
 I think i've solved most on the corner cases,
 so it's just a matter of time.
<br>
 To be continued...
<br>
 Rémi
</p>]]>

</content>
</entry>
<entry>
<title>Sorry but i&apos;ve toasted your pet</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2008/02/sorry_but_ive_t.html" />
<modified>2008-02-13T13:02:03Z</modified>
<issued>2008-02-11T01:00:27Z</issued>
<id>tag:weblogs.java.net,2008:/blog/forax/343.9135</id>
<created>2008-02-11T01:00:27Z</created>
<summary type="text/plain">Let me introduce banzai a web server that can handle
any text protocols you want. You just have to write a grammar
and a handler to specify the semantics.
Why this server is interresting ?
because it uses Tatoo high performance non blocking parsers.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
 <b>Introduction</b>
</p>
<p>
 The idea is simple. I want to create a calendar service,
 like the server part of Google Calendar.
 I want to create a server that is able
 to parse a specific protocol allowing to query calendars
 and send a response using by example the ical format.
<br>
 This service can be queried by different languages
 so i've decided to use a text based protocol, like HTTP.
<br>
 By example the request to get a calendar named mycalendar
 as user forax between November, the 6th and November, the 7th
 will be something like that.
</p>
<pre>
GET mycalendar forax CAL/1.0
password: AjxHKRFkRwxx3j9lM2HMow==
from: Sun Nov  6 08:49:37 1994
to: Mon Nov  7 12:34:31 1994

</pre>
<p>
 More formally, requests can be described by the following
 grammar using the Tatoo EBNF form.
<br>
 Tatoo EBNF form use sections:
 <ul> 
  <li>section <tt>tokens</tt> declares terminals and their regex.
  <li>section <tt>blanks</tt> declares the regex whom the lexer
      will not send a terminal to the parser.
  <li>section <tt>productions</tt> declares sequences of terminal
      and non terminal which will be reduced to a non terminal.
 </ul>
 <tt>?</tt> means zero or one, <tt>+</tt> means at least one and 
 <tt>*</tt> means zero or more.
</p>
<pre>
<b>tokens:</b>
  service='GET'
  uri= '([^ ])+'
  user= '([a-z][A-Z])+'
  protocol= 'CAL/1.0'
  colon= ':'
  header_key= '([^ :\r\n])+'
  header_value= '[^ \r\n]([^\r\n])+'
  eoln= '(\r)?\n'
 
<b>blanks</b>:
 space= "( |\t)+"

<b>productions</b>:
 start = request+
       ;
 request = firstline 'eoln' header* 'eoln'
         ;
 firstline = 'service' 'uri' 'user' 'protocol'
           ;
 header = 'header_key' 'colon' 'header_value' 'eoln'
        ;
</pre>
<p>
 Now Tatoo, a parser generator we have developed
 is able to create a non-blocking push lexer/parser
 for that grammar.
<br>
 By non-blocking, i mean a parser that work on NIO buffers,
 and since christmas, Tatoo has a companion named banzai,
 a server using NIO that can embed a Tatoo parser generator.
<br>
 Great, but how to specify the semantics, i.e how to specify
 that the password need to be checked, how the response is computed
 etc. 
</p>
<p>
 <b>The semantics</b>
</p>
<p>
 The semantics is specified by creating a class that inherits
 from <tt>ProtocolHandler</tt>. A <tt>ProtocolHandler</tt>
 is an object called each time a terminal is recognized
 (shifted) or a production is reduced.
 Furthermore, it provides an object that own some methods like
 <tt>asyncWrite()</tt> to send back data to the client
 or <tt>endRequest()</tt> to end the request.
</p>
<pre>
 public class CalendarProtocolHandler extends ProtocolHandler { 
  private String uri;
  private String username;
  private String header_key;
  private String header_value;
  private HashMap<String,String> headerMap=...
  ...

  public void shift(RuleEnum rule,TerminalEnum terminal) {
    switch(terminal) {
      case uri:
        uri=decode();
        return;
      case user:
        user=decode();
        return;
      case header_key:
        header_key=decode();
        return;
      case header_value:
        header_value=decode();
        return;
    }
  }

  public void reduce(ProductionEnum production) {
    if (production==ProductionEnum.header) {
      headerMap.put(header_key,header_value);
      return;
    }
    if (production==ProductionEnum.request) {
      handle();
      return;
    }
  }
  
  private void handle() {
    try {
      String password=headerMap.get("password");
      boolean passwdOK=verifyPassword(username,password);
      if (!passwdOK) {
        putUnauthorizedResponse(outBuffer);
        analyzer.asyncWrite(outBuffer);
        return;
      }
      ...
      Calendar c=...
      putICalendar(outBuffer,c);
      analyzer.asyncWrite(outBuffer);
    
    } catch(IOException e) {
      analyzer.endRequest(e);
    }
  }
}
</pre>
<p>
  Short explanation of the code above:
  <tt>shift()</tt>, if a specific terminal is found, decode
  the buffer to find its value.
  <tt>reduce()</tt> if we reduce a header key/value pair,
  store it into a map, if we readuce a request, call <tt>handle()</tt>.
  <tt>handle()</tt> verify the password and write a response
</p>
<p>
 <b>Banzaï !</b>
</p>
<p>
  I know what you are thinking:
 <br>
  "Ok, it's interresting. But embeding a parser in a webserver
  will damage performance".
 <br>
  No, Tatoo parser is carefully designed to not hurt performance.
 <br>
  I know that you don't believe me. So let me try
  to convince you with a stupid benchmark(tm).
 <br>
  I have written a subset of the HTTP/1.1 grammar
  (a subset because the wole HTTP/1.1 grammar is huge and i'm lenient),
  generated the corresponding parser with Tatoo,
  written a ProtocolHandler corresponding to HTTP and
  beanchmark banzai (the server) with that protocol handler
  and comparing it with Grizzly and Jetty.
</p>
<p>
 <b>Benchmarks</b>
</p>
<p>
 So i've borrowed two DELLs (<a href="http://weblogs.java.net/blog/forax/archive/banzai/banzai-cpuinfo">config</a>) with ethernet Gigabit cards
 and a gigabit switch in my labs, plug them and play.
<br>
 Servers are set up with a Gentoo (2.6.19-gentoo-r5) without major
 modifications. I have just raised the number of descriptors to 65535.
<br>
 Because i want to do a stress test, i've used
 apache bench (<tt>ab</tt>) as client so this is not a real
 scenario, just a stupid benchmark(tm).
<br>
 If you want to reproduce the test, checkout the code here
 <tt>svn checkout https://svnigm.univ-mlv.fr/svn/tatoo/trunk</tt>,
 compile Tatoo using <tt>ant</tt>.
<br>
 Banzai is located in a sample directory named <tt>httpserver</tt>.
 use <tt>ant all compile</tt> to compile banzai,
 and to launch it using
<pre>
 java -server -cp classes:../../lib/tatoo-runtime.jar fr.umlv.tatoo.samples.httpserver.banzai.Main
</pre>
</p>
<p>
 <b>First test: how many requests can be handled by banzai</b>
</p>
<p>
 <img alt="banzai1.png" src="http://weblogs.java.net/blog/forax/archive/banzai/banzai1.png" width="716" height="553" />
</p>
<p>
 Serving files of 4k, 8k, 16k and 32k with different numbers of concurrent
 connections (8, 16, 32 etc.). The value is a mean over 25 runs of
 50 000 requests.
<br>
 First, when i have seen the result of the benchmark I was astonished.
 Wow, linux 2.6 and a Core Duo offers great perf, 
 more than 25 000 requests by second for a 4k file.
<br>
 Furthermore, it seems that banzai have a problem if there are lot of
 concurrent connections. Perhaps because banzai doesn't have a strategy
 like closing keepalive connections if there are lot of connections.
<br>
 Now, a more interesting graphs.
</p>
<p>
 <b>Second test: comparing with the others</b>
</p>
<p>
 <img alt="banzai2.png" src="http://weblogs.java.net/blog/forax/archive/banzai/banzai2.png" width="559" height="492" />
</p><p>
 Serving a 4k with different concurrent connections
<br>
 Yes, banzai performs slightly better than grizzly ...
<br>
 at least if there are not lot of concurrent connections.
<br>
 About Jetty, I think I screw up the conf because the
 slope of the curve is weird and I am not able to explain why.
</p>
<p>
 <b>What's next</b>
</p>
<p>
 I think it's possible to integrate non blocking parsers
 technology directly into grizzly.
<br>
 Another idea is to wrap banzai to use RESTlet API.
<br>
 To end, i have found small improvements to the OpenJDK
 and i will send patches that will benefit to anyone.
</p>
<p>
 Cheers
</p>]]>

</content>
</entry>
<entry>
<title>CICE prototype available and FOSDEM</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2008/02/cice_prototype.html" />
<modified>2008-02-13T08:56:57Z</modified>
<issued>2008-02-07T13:48:58Z</issued>
<id>tag:weblogs.java.net,2008:/blog/forax/343.9157</id>
<created>2008-02-07T13:48:58Z</created>
<summary type="text/plain">I&apos;ve just discover that a CICE prototype is available.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
 It's an old news but i've just discovered that
 <a href="http://markmahieu.blogspot.com/">Mark Mahieu</a> provide an
 <a href="http://slm888.com/javac/">implementation</a> of
 <a href="http://docs.google.com/View?docid=k73_1ggr36h&">CICE</a>
 closure proposal which is an aternative to
 <a href="http://www.javac.info/">BGGA</a> prototype.
</p>
<p>
 By the way, i will be at <a
 href="http://www.fosdem.org/2008/">FOSDEM'08</a>,
 if you want to meet me, i will try to attend to all 
 <a href="http://wiki.debian.org/Java/DevJam/2008/Fosdem">Free
 Java Meetings</a>.
</p><p>
 I have decided to finish this entry
 a la <a href="http://weblogs.java.net/blog/campbell/">Chris
 Campbell</a>.
<br>
 In my ears: Morcheeba, "Dive Deep"
<br>
 In my eyes: Some cryptic codes like always.
</p>]]>

</content>
</entry>
<entry>
<title>Yet another closure proposal</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2008/01/yet_another_clo_1.html" />
<modified>2008-02-13T08:56:29Z</modified>
<issued>2008-01-16T11:39:54Z</issued>
<id>tag:weblogs.java.net,2008:/blog/forax/343.8998</id>
<created>2008-01-16T11:39:54Z</created>
<summary type="text/plain">I have carefully checked my archive and doesn&apos;t find any closure
proposal. It seems that be uptodate nowadays
you have to propose your own closure proposal.
So here is mine.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
 Everybody comes with its own closure proposal, why not me :)
</p>
<p>
 Unlike BGGA, CICE, or FCM (not entirely sure about FCM),
 i don't like the fact a closure is an instance of a class
 that implement a method.
 I prefer the
 <a href="http://blogs.sun.com/jrose/entry/notes_on_an_architecture_for">John Rose's vision</a>,
 closure are at runtime method handler on autonomous
 block of code.
</p>
<p>
 There is another thing I don't like in the current BGGA proposal:
 its syntax of function type,
 <tt>{int,int=>int}</tt> is not enough Javaish for me.
 My proposal comes with a new way to declare a function type
</p>
<p>
 I've separated my proposal in three parts:
 <ol>
  <li>function type declaration
  <li>how to use function type
  <li>closure syntax
 </ol>
 and because i'am lenient, i will only provide example and not
 a real spec.
</p>
<p>
 <b>Function types declaration</b>
</p>
<p>
 A function type is declared using the 'almost'
 keyword <b>function</b>. 
</p>
<pre>
// function types declaration
class Ops {

  // function are basically like typedef
  // top-level functions are illegal
  function &lt;T&gt; int comparator(T value1,T value2);

  // here function is not a real keyword, the compiler
  // recognize it as a keyword only in this construction
  function &lt;E&gt; void block(E element);

  // in the function below, E can be instantiated to any
  // object types or any primitive types,
  // it works because function are only declaration with no code. 

  // another function
  function &lt;R&gt; R task();
}
</pre>
<p>
 <b>How to use function type</b>
</p>
<p>
 Function types are types so they can be used anywhere
 a type can be used except as bound of a type variable
 or type argument of a type variable.
</p>
<pre>
// usage
import static Ops.*;

class Utils {
  // note: comparator&lt;?&gt; is illegal
  // (wildcard are prohibited for generics functions because
  // they are not needed).
  static &lt;T&gt; void sort(List&lt;T&gt; list,comparator&lt;T&gt; comparator) {
    ...
  }

  static &lt;T&gt; void forEach(Collection&lt;T&gt; c,block&lt;T&gt; block) {
    for(T t:c) {
      // invoke is a magic method that execute the block of code,
      // a special rule in the compiler is able to infer its
      // parameter type.
      block.invoke(t);
    }
  }
}
</pre>
<p>
 <b>Closure syntax</b>
</p>
<p>
 Unlike function type syntax of BGGA, i like its closure
 syntax so basically my proposed closure syntax is identical.
 I have borrow method references from FCM even if
 we need to add a special lookup rule (because methods and fields
 are not in the same lookup space)
 for that in the compiler.
<br>
 Like BGGA, my proposal include non local transfer
 and access to local variable (read and write) 
 but only for closures and not for any anonymous classes,
 a closure is not an anonymous class.
 Like any proposal, my proposal allow interface conversion
 of function to interropt with legacy codes
 but only if closure use final local variable.
<br>
 At last, <b>return</b> is not allowed in a closure to avoid
 stupid puzzlers but a replacement syntax exists.
</p>
<pre>
// at calling site
class Main {
  static int f(Object o1,Object o2) {
    ...
  }

  void g(int value) {
    ...
  }

  public static void main(String[] args) {
    Ops.comparator&lt;Object&gt; c=Main.f;

    // using a primitive type here is legal,
    // it creates a block of code that call g()
    Ops.block&lt;int&gt; b=new Main().g; 

    List&lt;String&gt; l=Arrays.asList(args);
    Utils.sort(l,c); // ok contravariance

    // closure syntax, this syntax allow to specify the block of code
    Utils.sort(l,{Charsequence c1, CharSequence c2 =&gt;
      c1.toString().compareTo(c1.toString());
    });

    // function to interface conversion
    java.util.Comparator&lt;Object&gt; javaUtilComparator=c;

    // it's roughly equivalent to
    final Ops.comparator&lt;Object&gt; tempc=c;
    java.util.Comparator&lt;Object&gt; javaUtilComparator=new java.util.Comparator&lt;Object&gt;() {
       public int compare(Object o1,Object o2) {
         return tempc.invoke(o1,o2);
       }
    };
    
    // closure to interface conversion
    java.util.Comparator&lt;String&gt; javaUtilComparator2={String s1, String s2 =&gt;
      -s1.compareTo(s2);
    }); 
  }
</pre>
<p>
  Examples of non-local transfer and local variable mutation.
</p>
<pre>
  // this is not allowed in contrast to BGGA.
  class NotAllowed implements Ops.block {
    ...
  }

  static int counter(Collection&lt;Integer&gt; c) {
    int count=0;
    // bloc of code (closure) can mutate local variable,
    // a function that returns an int is a subtype of a
    // function that returns void.
    Utils.forEach(c, {Integer i=&gt;
      count+=i;
    });
    return count;
  }

  static boolean startsWith(Collection&lt;String&gt; c, String prefix) {
    // closure with non local tranfer,
    // moreover prefix doesn't need to be final
    // because this is not an interface conversion
    Utils.forEach(c, {String s=&gt;
      if (s.startsWith(prefix) {
        return true; // doesn't compile ambiguous
        contains.return true; // ok
      } 
    });
  }
}
</pre>
<p>
 I wait your comments,
 <br>
 Rémi
</p>]]>

</content>
</entry>
<entry>
<title>Java 7 - Extension methods</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2007/11/java_7_extensio.html" />
<modified>2008-02-13T08:56:16Z</modified>
<issued>2007-11-29T11:15:49Z</issued>
<id>tag:weblogs.java.net,2007:/blog/forax/343.8730</id>
<created>2007-11-29T11:15:49Z</created>
<summary type="text/plain">My two cents on extension methods or why i hate use-site extension methods.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
 Recently,
 <a href="http://gafter.blogspot.com/2007/11/closures-prototype-update-and-extension.html">Neals</a>,
 <a href="http://digital-sushi.org/entry/declaration-site-extension-methods/">Peter</a> and
 <a href="http://www.jroller.com/scolebourne/entry/java_7_extension_methods">Stephen</a>
 blog about extension methods.
</p>
<p>
 <b>Why i hate (yes hate) use-site extension ?</b>
</p>
<p>
 What i love (yes love) with Java is the fact that i
 can take a look to a screen over one of my student
 shoulder and be able to say is the snippet i see
 is correct or not.
<br>
 With the use-site extension
 <a href="http://gafter.blogspot.com/2007/11/closures-prototype-update-and-extension.html">proposed by Neal</a>,
 the behavior of a code depends on some static imports
 that are located in the beginning of the file.
 So you can't understand a code without browsing
 the entire file.
</p>
<p>
 Demonstration. Do you understand this code ?
</p>
<pre>
 ...
 String text=...
 for(int i=0;i&lt;text.size();i++) {
   System.out.println(text.charAt(i));
 }
</pre>
<p>
 Hum, i have forget to say that the begining of
 the file contains that declaration:
 <tt>import static Strings.</tt> and that
 the class <tt>Strings</tt> is defined like that:
</p>
<pre>
 public class Strings {
   public static int size(String s) {
     return s.trim().length();
   }
 }
</pre>
<p>
 I'am sure i will hate that.
<br>
 It will remember me the the mess of C++ member functions:
 "where is the f***ing def of this method".
</p>
<p>
 Furthermore, import static has already some limitations,
 the code below doesn't compile. This case is currently
 rare but with extension methods it will be more frequent.
</p>
<pre>
  import static java.util.Arrays.*;

  public class StaticImport {
    public static void main(String[] args) {
      toString(args);
    }
  }
</pre>
<p>
 <b>Why i prefer declaration-site extension methods ?</b>
</p>
<p>
 It's definitely a good property of a language
 to be able to understand 
 a code that deals with some <tt>java.util.List</tt>s,
 just by knowing the declaration of this type.
 If i don't know how to use it, i can just to
 browse its code.
 After that, because i have a good brain storage device,
 i will be able to understand ALL codes that deals
 with <tt>java.util.List</tt>.
</p>
<p>
 <b>Another proposed syntax</b>
</p>
<p>
 I think there is some problems with the
 <a href="http://digital-sushi.org/entry/declaration-site-extension-methods/">syntax proposed by Peter</a>.
 <pre>
  package java.util;
  public interface List&lt;E&gt; … {
  …
    void sort() import static java.util.Collections.sort;
  …
  }
 </pre>
 <ol>
  <li>A new syntax, again !
  <li>The syntax let a developer think that this
      method can be overriden but without a modified VM
      it's not possible.
  <li>The constraint on the elements of the list
      (must be comparable)
      are not visible.
 </ol>
</p>
<p>
 Here is my proposed syntax, allow to declare static method
 in interfaces (in the same time, we close
 one the top 25 RFE <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4093687">Bug 4093687</a>)
 and add some sugar in the compiler.
</p>
<pre>
  package java.util;
  public interface List&lt;E&gt; … {
  …
    @ExtensionMethod
    static &lt;T extends Comparable&lt;? super T&gt;&gt; void sort(List&lt;T&gt; list) {
      java.util.Collections.sort(list);
    }
  …
  }
</pre>
<p>
 Like other proposed syntax, the compiler
 is modified to transform an instance call
 to a static call. The following code
 <pre>
  List&lt;String&gt; list=...
  list.sort();
 </pre>
 is rewritten to
 <pre>
  List&lt;String&gt; list=...
  List.sort(list);
 </pre>
</p>
<p>
 <tt>@ExtensionMethod</tt> (borrowed from
 <a href="http://www.jroller.com/scolebourne/entry/java_7_extension_methods">Stephen blog</a>)
 like <tt>@Override</tt> is not
 mandatory and tells the compiler to verify
 that the first parameter of the static
 method is the same type that the declaring interface.
</p>
<p>
 I wait your comments,<br>
 Rémi
</p>]]>

</content>
</entry>
<entry>
<title>Netbeanize Tatoo: the weirdest error, i have ever seen</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2007/10/netbeanize_tato.html" />
<modified>2008-02-13T08:55:51Z</modified>
<issued>2007-10-25T13:47:58Z</issued>
<id>tag:weblogs.java.net,2007:/blog/forax/343.8490</id>
<created>2007-10-25T13:47:58Z</created>
<summary type="text/plain">It is no so simple to take a project with a complex build script
that works with eclipse and try to convert it to works with netbeans.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>

<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
 Yesterday, one of the user of
 <a href="http://gforgeigm.univ-mlv.fr/projects/tatoo/">Tatoo</a>
 (a compiler compiler) email me because
 he wants to contribute but he wasn't able to compile Tatoo
 in Netbeans without any errors.
</p>
<p>
 My first thought was something like "you want to hack Tatoo
 but you are not able to configure your IDE, hoho"
</p>
<p>
 Tatoo currently compiles with an Ant script or using Eclipse,
 why not try to compile it using Netbeans by myself,
 after all, reading some recent
 <a href="http://weblogs.java.net/blog/joshy/archive/2007/10/netbeans_6_beta.html">blogs</a>,
 it seems that Netbeans is
 a good competitor of my "old buddy" Eclipse.
 And better, there is a netbeans plugin that can import an
 eclipse project in Netbeans. Cool, nothing to do.
</p>
<p>
 The Ant script of Tatoo is a little bit complex,
 some parts are bootstraped, the Tatoo grammar compiler
 is created using Tatoo. So I was prepared to
 have to add classpath and other gory details by hands.
</p>
<p>
 So I tried the import assistant, super, no problem,
 the import works. But after one or two seconds
 the Netbeans editor raises the weirdest error I have ever seen.
 I take a shoot.
</p>
<img alt="netbeans-tatoo.png" src="http://weblogs.java.net/blog/forax/archive/netbeans-tatoo.png" width="991" height="214" />
<p>
 If you don't see the problem, 
 the compiler emits an error because
 it doesn't find a class that is in the same package,
 strange, is it ?
 Moreover, at the same location, it emits a warning
 saying that the class is in the same package
 so I don't need to import it. duh ??
</p>
<p>
 So i've reverted my opinion about the contributor
 that use netbeans every day, he is able to hack Tatoo :)
</p>
<p>
 cheers,
<br>
 Rémi
</p>
<p>
 PS: The last sentence is voluntarily provocative.
</p>]]>

</content>
</entry>
<entry>
<title>Resurrect dolphin</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2007/10/resurrect_dolph.html" />
<modified>2008-02-13T08:55:21Z</modified>
<issued>2007-10-14T14:19:29Z</issued>
<id>tag:weblogs.java.net,2007:/blog/forax/343.8426</id>
<created>2007-10-14T14:19:29Z</created>
<summary type="text/plain">In this entry, the author advocates to raise the dead or at least just a little doplhin.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
  This morning is was in my shower, thinking about life, sea etc,
  when the word 'dolphin' pop in my mind.
<br>
  It instantly remember that day, when SUN management decide to
  <a href="http://weblogs.java.net/blog/ray_gans/archive/2006/08/mustang_and_dol.html">stop using codenames</a>.
</p>
<p>
  I am certainly a geek but i was shocked and, for me,
  this day will always be a day in gray.
  Afterall, SUN decide to kill dolphin.
 <br>
  I always think it's a little bit easier to try
  to explain to anyone that you have spent a whole night on ladybird
  than on jdk7 :)
</p>
<p>
  I think it's time to resurrect the dolphin. 
  Now, the openjdk is an opensource project, lead by a community
  not by some managers and lot of opensource softwares have a codename
  like
  <a href="http://wiki.mozilla.org/Firefox3/Gran_Paradiso">Gran Paradiso</a>,
  <a href="http://www.debian.org/">Etch</a>,
  <a href="http://fedoraproject.org/">Moonshine</a>,
   etc.
</p>
<p>
 So i officialy request to be able to use dolphin
 as codename for openjdk7.
</p>
<p>
  I wait your comment and hope your support.
</p>
<p>
 Rémi
</p>]]>

</content>
</entry>
<entry>
<title>Mixing property language support and bean bindings</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2007/09/mixing_property_1.html" />
<modified>2007-10-05T10:11:06Z</modified>
<issued>2007-09-24T23:14:23Z</issued>
<id>tag:weblogs.java.net,2007:/blog/forax/343.8307</id>
<created>2007-09-24T23:14:23Z</created>
<summary type="text/plain"> Recently, Shannon posts version 1.0 of beanbindings, even if this version is not ready for production use, it is stable enough to create a small demo mixing property language support and beanbindings. The idea is to create a bean...</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
 Recently, Shannon posts version 1.0 of beanbindings, even
 if this version is not ready for production use,
 it is stable enough to create a small demo mixing
 <a href="http://weblogs.java.net/blog/forax/archive/2007/05/java_property_d.html">property language support</a> and
 <a href="http://weblogs.java.net/blog/shan_man/archive/2007/09/beans_binding_1.html">beanbindings</a>.
</p>
<p>
 The idea is to create a bean (here <tt>MyBean</tt>) with a <b>bound</b>
 property (<tt>label</tt>) and to bind this property to two different
 textfields.
</p>
<img alt="properties-meet-bindings.png" src="http://weblogs.java.net/blog/forax/archive/properties-meet-bindings.png" width="373" height="200" />
<h3>Defining a bean</h3>
<p>
 Because i use the property support directly integrated in the language,
 defining such bean is easy. <tt>AbstractBean</tt> provides the
 basic support for managing property change listeners.
</p>
<pre>
 public static class MyBean extends AbstractBean {
    property String label bound;
    
    private &lt;B,T&gt; void propertyChanged(java.lang.Property&lt;B,T&gt; property,Object oldValue,Object newValue) {
      firePropertyChange(property,oldValue,newValue);
    }
    
    @Override
    public String toString() {
      return "label "+label;
    }
  }
</pre>
<h3>Wrap the property to someting understandable</h3>
<p>
 By default bean bindings only supports two kinds of property
 bean property that uses bean introspector and
 EL property that uses EL script but
 the framework is easily extensible and let you create
 your own property implementation.
</p>
<p>
 So i've created a new kind of property (here <tt>LangProperty</tt>)
 that exposes a <tt>java.lang.Property</tt> as a property understandable
 by the bindings framework.
</p>
<h3>And mix</h3>
<p>
 And now mixing the language support and
 the API together is as simple as that:
</p>
<pre>
    LangProperty&lt;MyBean,String&gt; labelProperty =
        LangProperty.create(MyBean#label);
    
    JLabel label = new JLabel("Label:");
    JTextField field = new JTextField(20);
    Bindings.createAutoBinding(READ_WRITE, 
        bean, labelProperty,
        field, BeanProperty.create("text")).bind();
    
    JLabel anotherLabel = new JLabel("Another label:");
    JTextField anotherField = new JTextField(20);
    Bindings.createAutoBinding(READ_WRITE, 
        bean, labelProperty,
        anotherField, BeanProperty.create("text")).bind();
</pre>
<p>
  The notation sharp ('<tt>#</tt>') is used to get an objet
  typed <tt>java.lang.Property</tt> that allow to get and set
  the value of the property.
</p>
<h3>Want to test it</h3>
<p>
 You want to test it by yourself, ok, here a zip containing
 the code, the beanbindings jar and the javac.jar patched to
 understand properties.
<br>
 Download <a href="http://weblogs.java.net/blog/forax/archive/properties-meets-bindings.zip"> properties-meets-bindings.zip</a>
</p>
<p>
 Use this line to compile
</p>
<pre>
 java -Xbootclasspath/p:javac.jar com.sun.tools.javac.Main -XDallowProperty -cp beansbinding-1.0.jar *.java
</pre>
<p>
 And this one to execute
</p>
<pre>
 java -Xbootclasspath/p:javac.jar -cp .:beansbinding-1.0.jar Sample
</pre>
<p>
 You must include <tt>javac.jar</tt> at runtime only because it
 contains the class <tt>java.lang.Property</tt>
 and i am too lazy to create two jars.
</p>
<p>
 WARNING, i have re-written the bound property support in the patched
 compiler last night so i think it will not work
 on another example :)
</p>
<p>
 Cheers,
<br>
 Rémi
</p>]]>

</content>
</entry>
<entry>
<title>Java kernel in jdk6 update 4</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2007/08/java_kernel_in.html" />
<modified>2008-02-13T08:54:34Z</modified>
<issued>2007-08-20T21:22:31Z</issued>
<id>tag:weblogs.java.net,2007:/blog/forax/343.8062</id>
<created>2007-08-20T21:22:31Z</created>
<summary type="text/plain"> From bug report Bug 6585322 it seems that Java Browser Edition, Java Kernel will be available in jdk6 update 4. Cheers, Rémi...</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
 From bug report
  <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6585322">Bug  
  6585322</a> it seems that <s>Java Browser Edition</s>,
  <a href="http://weblogs.java.net/blog/enicholas/archive/2006/09/java_browser_ed.html">Java Kernel</a>
  will be available in jdk6 update 4.
</p>
<p>
 Cheers,
<br>
 Rémi
</p>]]>

</content>
</entry>
<entry>
<title>Beansbinding goes to the wrong direction</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2007/06/beansbinding_go.html" />
<modified>2008-02-13T08:54:10Z</modified>
<issued>2007-06-23T22:33:16Z</issued>
<id>tag:weblogs.java.net,2007:/blog/forax/343.7710</id>
<created>2007-06-23T22:33:16Z</created>
<summary type="text/plain">After reading the Shannon Hickey&apos;s Blog about the recent release
of beanbinding 0.6, i&apos;ve decided to take a look to beanbing
and i think that the API doesn&apos;t guide the user enough.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
 Shannon Hickey recently post <a href="http://weblogs.java.net/blog/shan_man/archive/2007/06/beans_binding_0.html">a blog entry</a> about
 a new release of Beans Binding project at
 <a href="http://beansbinding.dev.java.net">http://beansbinding.dev.java.net</a>
<br>
 I have take a look to the documentation and my first belief is
 that the API doesn't guide the user enough.
<br>
 But let me first introduce the concept of bindings.
</p>
<p>
 <b>What is a binding ?</b>
</p>
<p>
 A binding is a two-way connection between two
 properties (javabean property). When you change the value of one,
 automatically the value of the other one is changed.
<br>
 The concept is not new, i have used JGoodies binding
 two years ago and the framework was mature at that time.
 And this concept is heavily used in web tier like Tapestry,
 JSF etc.
</p>
<p>
 <b>Why do we need bindings ?</b>
</p>
<p>
 Bindings are very elegant solution when you want to
 cleanly separate the presentation logic
 to the presentation view of a form.
<br>
 The presentation view is a panel or a dialog with
 its components and the presentation logic is a bean.
 A binding allows you to connect a component
 (textfield, combo, etc) property
 to its corresponding bean's property.
<br>
 So by example if you decide to change a textfield by a combo,
 you only update the presentation view, and doesn't change
 the presentation logic.
</p>
<p>
 Furthermore bindings are great because:
 <ol>
  <li>it provides a unified concept that hide the complexity
      of dealing with different swing listeners,
      by example, the selected value of a list is not a bound
      property, a textfield can't be changed when it
      fire a modification event, etc.
  <li>if you set a validator to a binding,
      you can do validation (on the fly), and provide a feedback
      to the user by changing the component visual aspect.
      Futhermore if you have a way to
      group bindings you can easily find the list of all unvalidated bindings.
  <li>they can manage conversions
      (by example between text and primitive type,
      date etc) because a bean property exports its type.
  <li>dealing with swing listeners often leads to memory leaks,
      with a binding you can easily remove listeners by unbinding it or
      better by unbinding a group of bindings.
 </ol>
<p>
 If you want more info, take a look to an <a href="http://www.javalobby.org/java/forums/t17672">introduction to
 jgoodies binding</a> by R.J. Lorimer.
</p>
<p>
 <b>Simplifying the MVC</b>
</p>
<p>
 beansbinding spec/implementation allow not only to
 define bindings between simple components but
 to define bindings on MVC components like JList, JTable, JTree,
 i.e on components that display multiple objects.
 This provide a more simpler way to define component's model.
</p>
<p>
 Let me take an example, i have a list of employees and i want
 to display their names in a JList. 
 Moreover, if i modify the employee list or if i change the name of one
 employee, i want the JList to reflect that change.
<br>
 A JList (like JTree or JTable) has 3 kinds of events,
 added, removed or changed. So my list need to be able to
 signal addition or removal and employee objet need to
 signal when its name changed.
<br>
 Employee name modification is signaled using a classical binding
 <pre>
  List<Employee> list=...
  new Binding(list, null, "${name}");
 </pre>
 Furthermore beanbindings introduces the concept of ObservableList which
 is a List with listeners that report additions and removals.
 It is not new, <a href="">glazed list</a> already have the same concept,
 but its is the first time i see the two concepts merged.
</p>
<p>
 Why do we need the Expression Language ?
</p>
<p>
 beansbinding let you specify the bound property 
 using a enhanced version of the Expression Language (EL)
 script already use in JSP or JSF.
<br>
 Suppose that now i want to display the list of street name of
 my employee. I can create this kind of binding
 <pre>
  List<Employee> list=...
  new Binding(list, null, "${address.streetname}");
 </pre>
 EL is used here to indicate the path to the property
 and all properties along that path are listened.
 You need to do that because if the address is changed
 the street name may changed too. 
</p>
<p>
 <b>So if binding are is a great concept why don't you like beansbinding ?</b>
</p>
<p>
 I just don't like some parts of the current implementation of bean bindings:
</p>
 <ol>
  <li>javax.beans.binding.Binding is not an abstract class,
      so it's not a concept. I can't create a binding
      on a user defined component that have a specific listener.
  <li>beansbinding allows to define a tree of binding but i think
      it's not necessary, a path of bindings
      (or a collection/map of binding path) is sufficient.
  <li>you can define a binding and forget it, this will lead
      to memory leaks,
      All bindings need to appear in a group of bindings to be
      unbound easily.
  <li>name a binding (new in 0.6) is not a good solution(tm),
      an object is always more powerfull than a string,
      if you want a reference to a binding, store it
      in a field.
  <li>there is no simple way to get the list of bindings that
      have validation errors.
      Furthermore, pratically convertions and validations
      are often the same operation in Java,
      by example there is no method Integer.isAnInteger(),
      Integer.parseInt() do the convertion
      or throw an exception. 
  <li>there is no way to set a collection of possible conversions
      for a group of bindings.
  <li>beanbindings use EL script so goodbye type safety.
      There is a real risk of creating a desktop applications
      that will work or not depending on inputs (by example, of
      data of a database)
      Java really needs something like C# expression trees,
      a part of (D)LINQ.
  <li>the way to define bindings for MVC component is clumsy,
      by example you can set the parameter of a tree to
      a binding that bind a table.
  <li>there is no support for dynamic bean. Roughtly,
      a dynamic bean is a hashmap of String/value with
      a property change listener mecanism.
      Swing's AbstractAction is that kind of bean,
      so there is currently no way to bind the name of an
      action to a text of a textfield.
 </ol>
<p>
 <b>Proposed new Design</b>
</p>
<p>
 Ok, i ve tried to summarize all my remarks in a new design:
</p>
<p>
 <pre>
  public abstract class Binding {
    Object getSource();
    Object getTarget();
    Binding setValidator(Validator validator);
    void bind();
    void unbind();
    //etc...
  }

  //BindingContext renamed
  public class BindingGroup {
    List<Binding> bindingList() {...}
    void bindAll() {...}
    void unbindAll() {...}
    // list of all bindings that are not validated
    List<Binding> unvalidatedBindingList() {..}
    void addValidationListener(ValidationListener l) {...}
    void removeValidationListener(ValidationListener l) {...}
    // the mapper find a converter by its source and target types
    void setConverterMapper(ConverterMapper mapper)
  }

  public interface ValidationListener extends java.util.EventListener {
     void validationFailed(Binding binding, ValidationResult result);
     // allows to remove visual effect on component
     void validationOk(Binding binding);
  }

  // i have tried to separate bindingroup concept from 
  // the binding factory, not sure that is a good idea
  public class BeanBinder {
    public BeanBinder(BindingGroup group) {...}
    public BeanBinder() { this(new BindingGroup()); }
    public BindingGroup getBindingGroup() {...}
    public Binding addBinding(Object source,String sourceExpr,Object target,Object targetPath) {...}
  }
  
  // add swing bindings for some specific components
  public class SwingBinder extends BeanBinder {
    public Binding addSelectedBinding(JTextCompmonent text, Object target,Object targetPath) {...}
    public Binding addSelectedBinding(AbstractButton button, Object target,Object targetPath) {...}
    public Binding addBinding(AbstractAction action String sourceExpr, Object target,Object targetPath) {...}
    public Binding addSelectedBinding(JList list, Object target,Object targetPath) {...}
    //etc.

    ListBinding addListBinding(JList list, ObservableList<?> list) {...}
    ListBinding addListBinding(JList list, Object source, String sourceExpr) {...}

    TableBinding addTableBinding(JTable table, ObservableList<?> list) {...}
    TableBinding addTableBinding(JTable table, Object source, String sourceExpr)
{...}    
    TreeBinding addTreeBinding(JTree tree, Object root) {...}
    //etc.  
  }
  
  // list specific binding
  abstract class ListBinding extends Binding {
    JList getSource() {...}
    ListModel getModel() {...}
    Binding addElementBinding(String elementPath) {...}
  }

  abstract class TableBinding extends Binding {
    JTable getSource() {...}
    TableModel getModel() {...}
    Binding addColumnBinding(int index,String elementPath) {...}
  }

  abstract class TreeBinding extends Binding {
    JTree getSource() {...}
    TreeModel getModel() {...}
    Binding addNodeBinding(Class<?> nodeClass,String elementPath) {...}
  }
 </pre>
</p>
<p>
 What do you think about that ? i wait your answers.
</p>
<p>
 Cheers,
 <br>Rémi
</p>]]>

</content>
</entry>
<entry>
<title>Java property (draft 3)</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2007/05/java_property_d.html" />
<modified>2008-02-13T08:53:44Z</modified>
<issued>2007-05-13T15:34:49Z</issued>
<id>tag:weblogs.java.net,2007:/blog/forax/343.7146</id>
<created>2007-05-13T15:34:49Z</created>
<summary type="text/plain">News about the property spec.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
  This is a new step in my quest (or curse) to
  provide properties to Java.
 <br>  
  I've written a new version of the proposal (the third draft)
  of the property spec, available as a google doc
 <br>
  <a href="http://docs.google.com/Doc?id=dfhbvdfw_1f7mzf2">property draft (v3)</a>
</p>
<p>
  During the last month, i've written a new prototype
  from scratch that follows the specification.
  The prototype is not available yet, mostly because
  i change the spec too frequently, but i hope to
  post a blog about its availability in one or two weeks.
</p>
<p>
  If you are concerned by the implementation
  details of the prototype, i will soon post messages
  that describe its implementation
  to <a href="https://ksl.dev.java.net/">Kitchen Sink
  Language</a> mailing list.
</p>
<p>
  I wait your comment about the spec.
</p>
<p>
  Cheers,
 <br>
  Rémi
</p>]]>

</content>
</entry>
<entry>
<title>@nnotation type or type @nnotation</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2007/03/annotation_type.html" />
<modified>2008-02-13T08:53:18Z</modified>
<issued>2007-03-18T15:42:09Z</issued>
<id>tag:weblogs.java.net,2007:/blog/forax/343.6480</id>
<created>2007-03-18T15:42:09Z</created>
<summary type="text/plain">What is the best syntax to allow anootation on types ?
I will not answer to that question but you can try
to convince me.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
  The purpose of <a href="">JSR 308</a> is to allow to
  define annotation on types.
 <br> 
  Currently, the JLS 3 only allows to annotate
  language elements than accept modifiers so
  it's not possible to annotate types.
</p>
<p>
  <b>Why allowing this is a good idea ?</b>
</p>
<p>
  There is real interest to allow annotation on Java types,
  it enables to write safer code by performing
  static source code analysis. Imagine annotations like:
</p>
<ol>
  <li>JetBrain's IDEA <a href="http://www.jetbrains.com/idea/documentation/howto.html">@NonNull, @Nullable</a>
      to avoid NullPointerException and helps database mapper
  <li><a href="http://research.aonix.com/jsc/static.safety.9-04.pdf">@Scoped</a> to denote objects allocated in scoped memory of
<a href="http://jcp.org/en/jsr/detail?id=1">RTSJ</a>

  <li><a href="http://jcip.net/">Java Concurrent In Practice</a> <a href="http://www.javaconcurrencyinpractice.com/annotations/doc/index.html">@Immutable or @ThreadSafe</a> that allows to write
      safer code in a multi-threaded environment.
</ol>
 If you want to see more examples, the
 current draft of the spec is available
 <a href="http://pag.csail.mit.edu/jsr308/java-annotation-design.pdf">here</a>.
</p>
<p>
 There are two possible syntax, the first one is to allow
 annotation of type at the left of the type like
 any other Java annotations. But it introduce an ambiguity
 because a reader doesn't see clearly if an annotation belong to
 a variable, a method etc. or its type.
 In the following sample, <tt>@NonNull</tt> refers to <tt>String</tt>
 and <tt>@Column</tt> refers to <tt>name</tt>.
</p>
<pre>
  @Local
  class MyBean {
      @NonNull @Column(name="_NAME") String name;
      @Deprecated @NonNull Dimension getSize() { ... }
  }
</pre>
<p>
 The second one is to allow annotation on type at the
 right of a type, in that case there is no ambiguity.
</p>
<pre>
  @Local
  class MyBean {
      @Column(name="_NAME") String @NonNull name;
      @Deprecated Dimension @NonNull getSize() { ... }
  }
</pre>
<p>
  The JSR308 expert group seems to think that the first syntax is
  better, more Java-like even if it introduce an ambiguity.
 <br>
  I am not totally convinced.
</p>
<p>
  What do you think ?
</p>
<p>
 Cheers, Rémi
</p>]]>

</content>
</entry>
<entry>
<title>Closure Litteral and Method Reference</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2007/02/closure_littera.html" />
<modified>2007-03-18T14:56:43Z</modified>
<issued>2007-02-27T11:46:24Z</issued>
<id>tag:weblogs.java.net,2007:/blog/forax/343.6696</id>
<created>2007-02-27T11:46:24Z</created>
<summary type="text/plain">Recently, Stephen Colebourne and Stefan Schulz post another closure
like proposal.
I am not a big fan of it but one syntax element of their proposal
is quite interesting.
</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
 Recently, <a href="http://jroller.com/page/scolebourne?entry=first_class_methods_java_style">Stephen Colebourne</a> and <a href="http://jroller.com/page/jadda">Stefan Schulz</a> post another closure like <a href="http://docs.google.com/Doc?id=ddhp95vd_0f7mcns">proposal</a>, yes, yet another one.
</p>
<p>
 They propose another syntax for describing a closure
 which, in my opinion, is more a simpler way
 to declare an inner-class.
<br>
 I am not a big fan of this proposal,
 i definitively prefer the
 <a href="http://www.javac.info/">closure syntax</a>
 decribed by Neal Gafter.
<br>
 But there is something that i like in their proposal,
 the fact that you can create invocable method references.
</p>
<p>
 <b>Invocable Method Reference</b>
</p>
<p>
 We need a way to easily create a reference to a method,
 an object that allow to call a method.
 Currently a method is not an Object
 and if you want something like that
 you have to use reflection (java.lang.reflect.Method)
 which is not type-safe, uses lot of checked exceptions
 and performs primitive boxing and array boxing.
</p>
<p>
 So having a way to create a method reference is a good think.
 But unlike Stephen and Stefan, i don't think that
 a method reference is a java.lang.reflect.Method correctly typed
 by the compiler but instead a java.function object (a closure)
 of Neal.
<br>
 In my opinion method reference is a kind of closure litteral,
 so this code snippet:
</p>
<pre>
  public void init() {
    JButton button = ...;
    button.addActionListener(this#handleAction(ActionEvent));
  }
  public void handleAction(ActionEvent ev) {
    // handle event
  }
</pre>
<p>
 is a short syntax for:
</p>
<pre>
  public void init() {
    JButton button = ...;
    button.addActionListener({ActionEvent e=>
      handleAction(e);
    });
  }
  public void handleAction(ActionEvent ev) {
    // handle event
  }
</pre>
<p>
 Because a method reference is a closure litteral,
 the type of a method reference is a function type.
</p>
<pre>
 {ActionEvent=>void} handle = this#handleAction(ActionEvent);
</pre>
<p>
 I think that using # is a good idea.
 Perhaps because it's the syntax i choose to use
 to create property litteral (property reference)
 in my next property proposal that
 i will post in few days.
</p>
<p>
 Cheers, Rémi
</p>]]>

</content>
</entry>
<entry>
<title>Meet me at FOSDEM</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/forax/archive/2007/02/meet_me_at_fosd.html" />
<modified>2007-03-18T14:56:23Z</modified>
<issued>2007-02-20T18:57:43Z</issued>
<id>tag:weblogs.java.net,2007:/blog/forax/343.6647</id>
<created>2007-02-20T18:57:43Z</created>
<summary type="text/plain">I will be saturday and sunday at FOSDEM.</summary>
<author>
<name>forax</name>

<email>forax@univ-mlv.fr</email>
</author>
<dc:subject>Community: JDK</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/forax/">
<![CDATA[<p>
 If you want to talk about: 
</p>
<ol>
 <li>Why the JLS doesn't allow
     <a href="http://weblogs.java.net/blog/forax/archive/2006/11/why_array_of_pa.html">array of parametrized type<a/>.
 <li><a href="http://blogs.sun.com/ahe/entry/ksl_open">Kitchen Sink Language</a>.
 <li><a href="http://weblogs.java.net/blog/forax/archive/2007/01/property_reload.html">Property syntax</a>.
 <li>Why beers are so great in Belgium ?
</ol>
<p>
 I will be saturday and sunday at
 <a href="http://www.fosdem.org/2007/">FOSDEM</a>.
</p>
<p>
 Rémi
</p>]]>

</content>
</entry>

</feed>