it matches the (E), (V) and (L) requirements, but it needs at least some comments to explain, why there is a execCode( AutoCloseable code ) { ... code.close() ; ... } although the code does not have to do anything with a semantic closing context :)
(S.C): using java.util.function.Consumer
does not support (E)
see also this answer* (S.X): using java.lang.reflect.Method
发布评论
评论(1)
简而言之,最佳找到的策略:
更多详细信息:
解决方案部分在其他答案中找到及其缺点:
java.util.concurrent.Callable
(上述解决方案)java.util.Function
void foo() { execCode( (i) -> doBar() ) ... void execCode( Functioncode ) { ... code.apply( null ) 。 .. }
接口
(类)来完成它接口代码{abstract void run() throws Exception; }
this::doBar
execCode( Callable
java.lang.Runnable
RuntimeException
,但不允许抛出异常java.lang.AutoCloseable
>execCode( AutoCloseable code ) { ... code.close() ; ... }
尽管代码不必对语义结束上下文执行任何操作:)java.util.function.Consumer< /代码>
java.lang.reflect.Method
best found strategy in short:
more details:
solutions partially found in other answers and their disadvantages for above:
java.util.concurrent.Callable
(above solution)java.util.Function
void foo() { execCode( (i) -> doBar() ) ... void execCode( Function< Void, Void > code ) { ... code.apply( null ) ... }
interface
(class) to accomplish itinterface Code { abstract void run() throws Exception; }
this::doBar
execCode( Callable<Object> ... ) { ... }
execCode( Runnable ... ) { ... }
java.lang.Runnable
RuntimeException
but no checked onesjava.lang.AutoCloseable
execCode( AutoCloseable code ) { ... code.close() ; ... }
although the code does not have to do anything with a semantic closing context :)java.util.function.Consumer
java.lang.reflect.Method