Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to turn up off source cleanup in a code section?
How to turn up off source cleanup in a code section? [message #1818412] Sat, 14 December 2019 21:01 Go to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6515
Registered: July 2009
Senior Member
I'd like to enable the Editor Save Action that converts to lambdas where possible. At my first attempts I noticed that the conversion happens even in cases where it's apparently not possible. Examples:

1) Fields in a class that are initialized with a lambda that accesses other fields which are defined later in the class. In my case most often listeners.

2) Calls to methods that take a Runnable argument and have an overloaded sibling method with a Callable argument. I can't easily describe what's going on, so here's a snippet from my code:

public interface Transaction
{
  public void syncExec(Runnable runnable);
  public <V> V syncExec(Callable<V> callable) throws Exception;
}

public class SomeOtherClass
{
  private Transaction transaction;
  private boolean showMerges;

  public void close()
  {
    transaction.syncExec(new Runnable()
    {
      @Override
      public void run()
      {
        transaction.removeTransactionHandler(transactionHandler);
        transaction.removeListener(transactionListener);
      }
    });
  }

  public final void setShowMerges(final boolean showMerges)
  {
    transaction.syncExec(new Runnable()
    {
      @Override
      public void run()
      {
        SomeOtherClass.this.showMerges = showMerges;
      }
    });
  }
}


In the SomeOtherClass.close() method the new Runnable(){...} is converted into a lambda that compiles without errors. But the conversion in the SomeOtherClass.setShowMerges() method leads to the compile error "Unhandled exception type Exception". I have no idea what exactly is the problem with that second case.

I tend to think that these are bugs in the "Use lambdas where possible" conversion because I interpret "where possible" as "if the result compiles without errors and works like before".

I could work around the problem with "accesses other fields which are defined later in the class", but it turned out that it's really not worth the effort. It just clutters my code and its Git history. I don't know how to work around the "Unhandled exception type Exception" problem.

Can you please give me advice on how to deal with these problems? Am I doing something wrong? Are they bugs and should be fixed? Is there a magic comment token to turn the cleanup off for a code region, similar to @formatter:off ?



Re: How to turn up off source cleanup in a code section? [message #1818416 is a reply to message #1818412] Sun, 15 December 2019 07:09 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6515
Registered: July 2009
Senior Member
I played some more with the automatic lambda conversion and came to the conclusion that it's too dangerous to use. Even when all resulting compile errors are fixed by reverting the particular changes the resulting code does not behave identical to the original code without the lambdas. To give an example:

public class Notifier
{
  protected final void fireEvent(Event event)
  {
    for (Class<?> i : event.getClass().getInterfaces())
    {
      System.out.println(i);
    }
  }

  public interface Event
  {
    public Notifier getSource();
  }
}

public class SpecialNotifier extends Notifier
{
  public static void main(String[] args)
  {
    new SpecialNotifier().run();
  }

  public void run()
  {
    fireEvent(new SpecialEvent()
    {
      @Override
      public SpecialNotifier getSource()
      {
        return SpecialNotifier.this;
      }
    });
  }

  public interface SpecialEvent extends Event
  {
    @Override
    public SpecialNotifier getSource();
  }
}


When you run the SpecialNotifier.main() method it correctly prints:
interface events.SpecialNotifier$SpecialEvent


After the lambda conversion the SpecialNotifier.run() method looks like this:
  public void run()
  {
    fireEvent(() -> SpecialNotifier.this);
  }


And, you guess it, the program output changes to:
interface events.Notifier$Event


This means that listeners which check "event instanceof SpecialEvent " silently stop working. Ouch.


Re: How to turn up off source cleanup in a code section? [message #1818417 is a reply to message #1818416] Sun, 15 December 2019 07:19 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6515
Registered: July 2009
Senior Member
One more note regarding the "silent type change" problem (SpecialEvent->Event, Runnable->Callable, ...). In https://monospacedmonologues.com/2016/01/casting-lambdas-in-java/ I just learned that, of course, it is possible to cast the lambda to the desired type, e.g.:
fireEvent((SpecialEvent)() -> SpecialNotifier.this);


Do you think it would be possible to detect and correctly handle cases like these?


Re: How to turn up off source cleanup in a code section? [message #1818506 is a reply to message #1818417] Tue, 17 December 2019 21:17 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1828
Registered: July 2009
Senior Member
The case of SpecialNotifier.this changing its meaning should be in the set of pre-conditions of the conversion. Please file a bug (are add your example if one already exists ;p ).

OTOH, I totally agree with your conclusion: to-lambda-conversion is more complex than what you'd typically want cleaned-up automatically. I think the only reason to provide this as a cleanup in the first place was: help people migrate to the new style by *attempting* a mass conversion. Still each individual lambda should be reviewed before committing.
Talk to IHOP SURVEY [message #1818509 is a reply to message #1818506] Wed, 18 December 2019 04:34 Go to previous messageGo to next message
logan joyadan is currently offline logan joyadanFriend
Messages: 1
Registered: December 2019
Junior Member
And you can also make generating those comments around a block of code ... Once you have Off/On Tags enabled, just enclose a section of code in ... This only works for formatting rules and not for other Save Actions such as cleaning up ... would exist on "Organize Imports" and "Clean Up" source options.
Re: Talk to IHOP SURVEY [message #1820989 is a reply to message #1818509] Tue, 04 February 2020 08:17 Go to previous messageGo to next message
Bhondawe Patil is currently offline Bhondawe PatilFriend
Messages: 23
Registered: October 2019
Junior Member
Thanks for the valuable code.
Re: How to turn up off source cleanup in a code section? [message #1821217 is a reply to message #1818506] Fri, 07 February 2020 10:07 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6515
Registered: July 2009
Senior Member
Late, but now I submitted https://bugs.eclipse.org/bugs/show_bug.cgi?id=559910 :

Automatic conversion to lambdas silently changes the meaning of "this"


Re: Talk to IHOP SURVEY [message #1821218 is a reply to message #1818509] Fri, 07 February 2020 10:13 Go to previous message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6515
Registered: July 2009
Senior Member
And I also submitted https://bugs.eclipse.org/bugs/show_bug.cgi?id=559914 :

Support Off/On tags for Source Cleanup


Previous Topic:How To "Turn Off" A Plugin And Project Using The "Module Path"?
Next Topic:Using Javadoc on Processing Sketches
Goto Forum:
  


Current Time: Wed Oct 21 00:12:41 GMT 2020

Powered by FUDForum. Page generated in 0.01956 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top