Changes between Version 162 and Version 163 of j2p


Ignore:
Timestamp:
11/14/24 09:54:41 (5 months ago)
Author:
wouter
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • j2p

    v162 v163  
    128128* For the non-static inner class: pass the enclosing class as argument to the child class constructor. You may have to use more tricks to get around cyclic dependencies.
    129129
    130 ===== Workaround
     130==== Example workaround
     131Java has the arrow notation (x,y) -> methodcall(x,y) to write lambda expressions.
     132This notation can also be translated. This feature is especially useful when pulling inner classes out of a parent class, to pass parent class functions over to the pulled-out class.
     133For example, suppose you have a class like this
     134
     135{{{
     136class Parent {
     137  public void f() {...}
     138  public String g(int x) {...}
     139
     140
     141  public X h() {
     142    ....
     143    return new X{
     144      f()
     145      g(1)
     146    }
     147}
     148}}}
     149
     150The "new X" call has to be converted into an explicit class, but somehow it needs to be able to call f and g. We suggest to do it like this:
     151
     152{{{
     153class MyX extends X {
     154  public MyX(Runnable f, Function<Integer,String> g) {
     155    f.run();
     156    g.apply(1);
     157  }
     158}
     159
     160class Parent {
     161  public void f() {...}
     162  public String g(int x) {...}
     163
     164
     165  public X h() {
     166    ....
     167    return new MyX{()->f(),(x)->g(x));
     168}
     169}}}
     170
     171===== Special cases
    131172If some particular inner classes, particularly constants, are used in very specific translators, these translators may opt to attempt to recognise these and process them separately (thus avoiding calling the translator for them). One example is the JsonDeserializer (jackson-t package) that recognises use of "com.fasterxml.jackson.databind.JsonDeserializer.None".
    132173
     
    444485
    445486
    446 == Use of lambda notation, inner class extraction
    447 Java has the arrow notation (x,y) -> methodcall(x,y) to write lambda expressions.
    448 This notation can also be translated. This feature is especially useful when pulling inner classes out of a parent class, to pass parent class functions over to the pulled-out class.
    449 For example, suppose you have a class like this
    450 
    451 {{{
    452 class Parent {
    453   public void f() {...}
    454   public String g(int x) {...}
    455 
    456 
    457   public X h() {
    458     ....
    459     return new X{
    460       f()
    461       g(1)
    462     }
    463 }
    464 }}}
    465 
    466 The "new X" call has to be converted into an explicit class, but somehow it needs to be able to call f and g. We suggest to do it like this:
    467 
    468 {{{
    469 class MyX extends X {
    470   public MyX(Runnable f, Function<Integer,String> g) {
    471     f.run();
    472     g.apply(1);
    473   }
    474 }
    475 
    476 class Parent {
    477   public void f() {...}
    478   public String g(int x) {...}
    479 
    480 
    481   public X h() {
    482     ....
    483     return new MyX{()->f(),(x)->g(x));
    484 }
    485 }}}
    486487
    487488== FAQs