My favorites | Sign in
Logo
                
People details
Project owners:
  e.lucash

Eclipse JDT extension to visually collapse (fold) anonymous inner classes to concise lambda expressions (closures) in Java Editor.

Author believe that main problem of anonymous inner classes is not their a somewhat limited semantic (yeah, they aren't real full blown closures!) but verbosity of declaration. While it is not difficult to write anonymous inner classes using completion and/or templates in java editor, reading of such structures can be really hurting.

Unlike real language change proposals (CICE and BGGA),

Uncollapsed
Collapsed
Functional

Lambda4jdt uses hacked jface projection infrastructure to visually hide uninteresting parts of specified anonymous class declaration. To specify function-like folding one must use special comment /* => */ right after parameter list of single method definition. Folding engine will not recognize declaration without such comment or with more than one method declared. See WhyMagicCommentAsMarker and JFaceProjectionHack wiki pages.

Since v0.3, there is alternative folding provider "Lambda4jdt JavaStyle closures" to collapse ALL single method anonymous inner classes without required comment marker (but also without fancy => syntax)

In attempt to modify collapsed text of closure expression will be expanded back to anonymous inner class (I have tried to add limited in place editing but it is not working properly yet or ever will). It can be collapsed again manually (using (-) icon in editor ruler column) or by resetting folding structure.

Appearance of collapsed function expressions resembles syntax used by Scala or C#. Also folding engine collapses anonymous inner class declaration to clause statement (like custom control methods in BGGA). Such folding only take place when function method body consist of more than one expression and appears to be single parameter of higher order method invocation, which is followed by statement terminator (semicolon). In particular it is triggered by char '(' before inner class and by sequence ');' after declaration. For now, if you need to supply more than one parameter to higher order method and still want clause-statement you can use currying =).

Executor e =...
e.execute {
   Object a = getSomething();
   doSomething(a);
}

// for one line it is folded like simple lambda expression
final int i = 1;//you still need final to access i in function
e.execute(=> doSomethingWithIn(i));

// or we can add extra empty statement
e.execute {
  doSomethingWithIn(i);;
}

Nested closure constructs are supported to some degree

interface Provider<T> {
  T get(Object context);
}
//Consider such code
public Provider<Provider<Provider<String>>> myProvider() {
  return new Provider<Provider<Provider<String>>>() {
    public Provider<Provider<String>> get(Object c)/* => */{
      return new Provider<Provider<String>>() {
        public Provider<String> get(Object c)/* => */{
          return new Provider<String>() {
            public String get(Object c)/* => */{
              return "MyFavoriteStringFactory" + c;
            }
          };
        }
      };
    }
  };
}
//....
// Which may be collapsed to..
public Provider<Provider<Provider<String>>> myProvider() {
  return c => c => c => "MyFavoriteStringFactory" + c;
}

Lambda4jdt is in early development phase and some bugs are known. Sometimes inappropriately partially hidden text chunks can occur when some modification by quick fix, refactoring or toggle comment (and such) overlaps with collapsed code in a unexpected manner. Quirks can be defeated by resetting folding structure or reopening editor. I'm going to fix some of bugs. There are other limitation. Folding structure provider extension supply no preference page and therefore folding defaults are biased. Preferences block can be added later.

Lambda4jdt is experimental proof-of-concept project not intended for broad use, but rather a toy to play with and evaluate use of closures in Java (as we wont see closures support in java language for a long time).

Solution targets an Eclipse Platform + JDT version 3.4.x or 3.5.x and JavaSE-1.5 (and up) Download consist of "java folding structure provider" extension in separate bundle com.googlecode.lambda4jdt and slightly modified jface text projection components residing in substitution bundle org.eclipse.jface.text with special version, higher then bundled with eclipse

Downloads contains archived install sites with Lambda4jdt feature. For details see HowToMakeItWork page.

Non-archived Galileo sites:http://lambda4jdt.googlecode.com/svn/trunk/com.googlecode.lambda4jdt.site









Hosted by Google Code