Feature Request: use of inner / nested classes and lambda expressions
See original GitHub issueToday, I tested a workflow using a lambda expression:
@FunctionalInterface
public interface MyFuncInterface<V> {
V call() throws Interrupt;
}
MyFuncInterface test = () -> {
logger.info("Before wait in lambda");
wait(WaitMode.FIRST, 15 * 1000, UUID.randomUUID().toString());
logger.info("After wait in lambda");
return "hello";
};
test.call();
Works fine.
Actually you can’t do
Stream.of(1,2,3).forEach(elem -> {
// ....
wait(WaitMode.FIRST, 10*1000, UUID.randomUUID().toString());
//...
}
by default.
Possible solutions, as suggested by theoDiefenthal:
- Redeclare
Interrupt
to be of type RuntimeException. This could potentially break clients in really nasty ways, e.g. if they have a global try … catch (RuntimeException) in their code, their workflows would suddenly stop working in the expected way. - Can we manipulate something in bytecode here? For COPPER to work, we have to manipulate the bytecode anyways. So could we trick the java compiler so that e.g.
Callable
would not just be declared asthrows Exception
but alsoInterrupt
. The problem here is that the code wouldn’t even compile so we couldn’t use ASM which works on the compiled code and does its manipulations in there. - We just live with the way it is as there are no better solutions?!
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
When to Use Nested Classes, Local Classes, Anonymous ...
Use it if you are encapsulating a single unit of behavior that you want to pass to other code. For example, you would...
Read more >Difference between Anonymous Inner Class and Lambda ...
Inside Anonymous inner class, “this” always refers to current anonymous inner class object but not to outer object. Inside Lambda Expression, “ ...
Read more >Java Nested Classes and Lambda Expressions
Inner class should be used if access to an enclosing instance's non-public fields and methods are required. Static class should be used if...
Read more >Interfaces, Lambda Expressions, and Inner Classes in Java
However, the Java inner classes have an additional feature that makes them richer and more useful than nested classes in C++.
Read more >15.4 Inner Class with Lambda Expression - YouTube
Check out our website: http://www.telusko.com Follow Telusko on Twitter: https://twitter.com/navinreddy20 Follow on Facebook: Telusko ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Would it be an option to make
Interrupt
extend fromRuntimeException
but to modify the Workflow bytecode in a way that we alter through each try…catch block in order to assert our special handling? We’d just would need to transform fromto
We’d also lose the restriction to “define any method which uses
wait
” withthrows Interrupt
in it’s signature. And as we modify the workflow bytecode heavily anyway, do that trick on all try catch blocks won’t harm any further. Or would other problems arise?And for those people like me who didn’t get the trick from @siordache direcly, here is a small manual on how it works: https://www.baeldung.com/java-sneaky-throws
From that I found another alternate solution: “The compiler sees the signature with the throws T inferred to a RuntimeException type, so it allows the unchecked exception to propagate. The Java Runtime doesn’t see any type in the throws as all throws are the same a simple throw e.” and “It’s possible to throw a checked exception using bytecode manipulation, or Thread.stop(Throwable), but it’s messy and not recommended.” => So it should be possible to keep “Interrupt” as a subtype of Throwable and we just remove the
throws Interrupt
from the signature ofwait
in a workflow?! We then keep on requiring only that the workflowmain
method has a signature ofthrows Interrupt
so that the COPPER processor is able to catch that checked Interrupt exception explicitly. If this works, this would in my opinion be the best solution.Another option is to provide a helper class as shown below:
With the appropriate
import static ...
you can then write: