How to turn up off source cleanup in a code section? [message #1818412] |
Sat, 14 December 2019 21:01 |
|
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 ?
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
|
|
|
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 |
|
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.
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03305 seconds