• <i id='dKEiq'><tr id='dKEiq'><dt id='dKEiq'><q id='dKEiq'><span id='dKEiq'><b id='dKEiq'><form id='dKEiq'><ins id='dKEiq'></ins><ul id='dKEiq'></ul><sub id='dKEiq'></sub></form><legend id='dKEiq'></legend><bdo id='dKEiq'><pre id='dKEiq'><center id='dKEiq'></center></pre></bdo></b><th id='dKEiq'></th></span></q></dt></tr></i><div id='dKEiq'><tfoot id='dKEiq'></tfoot><dl id='dKEiq'><fieldset id='dKEiq'></fieldset></dl></div>
  • <legend id='dKEiq'><style id='dKEiq'><dir id='dKEiq'><q id='dKEiq'></q></dir></style></legend>
  • <tfoot id='dKEiq'></tfoot>

      <bdo id='dKEiq'></bdo><ul id='dKEiq'></ul>
  • <small id='dKEiq'></small><noframes id='dKEiq'>

        声纳:用方法引用替换这个 lambda

        Sonar : Replace this lambda with a method reference(声纳:用方法引用替换这个 lambda)

          • <bdo id='wiVOJ'></bdo><ul id='wiVOJ'></ul>
          • <small id='wiVOJ'></small><noframes id='wiVOJ'>

              <tbody id='wiVOJ'></tbody>

                <i id='wiVOJ'><tr id='wiVOJ'><dt id='wiVOJ'><q id='wiVOJ'><span id='wiVOJ'><b id='wiVOJ'><form id='wiVOJ'><ins id='wiVOJ'></ins><ul id='wiVOJ'></ul><sub id='wiVOJ'></sub></form><legend id='wiVOJ'></legend><bdo id='wiVOJ'><pre id='wiVOJ'><center id='wiVOJ'></center></pre></bdo></b><th id='wiVOJ'></th></span></q></dt></tr></i><div id='wiVOJ'><tfoot id='wiVOJ'></tfoot><dl id='wiVOJ'><fieldset id='wiVOJ'></fieldset></dl></div>

              1. <tfoot id='wiVOJ'></tfoot>
              2. <legend id='wiVOJ'><style id='wiVOJ'><dir id='wiVOJ'><q id='wiVOJ'></q></dir></style></legend>
                  本文介绍了声纳:用方法引用替换这个 lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  此代码示例

                  Collection<Number> values = transform(
                          getValuatedObjects(),
                          input -> getValueProvider().apply(input).getValue());
                  

                  违反 sonarqube 规则:

                  尽可能用方法引用替换 lambdas

                  Replace lambdas with method references when possible

                  这是声纳错误吗?或者我真的可以使用方法参考吗?

                  is it a sonar bug ? or can i really use a method reference ?

                  推荐答案

                  不能替换 lambda input ->getValueProvider().apply(input).getValue() 带有方法引用而不改变语义.

                  You can’t replace the lambda input -> getValueProvider().apply(input).getValue() with a method reference without changing the semantics.

                  方法引用替换单个方法调用,因此它不能简单地替换由多个方法调用组成的 lambda 表达式.

                  A method reference replace a single method invocation, so it can’t simply replace a lambda expression consisting of more than one method invocation.

                  input -> 形式的 lambda 表达式;getValueProvider().apply(input) 可以被 getValueProvider()::apply 替换当且仅当 getValueProvider() 的评估时间确实没有关系,因为在 lambda 形式中,该方法在每个 lambda 主体评估时调用,而对于方法引用,它只调用一次并捕获结果.

                  A lambda expression of the form input -> getValueProvider().apply(input) could be replaced by getValueProvider()::apply if, and only if, the evaluation time of getValueProvider() does not matter as in the lambda form the method is invoked on each lambda body evaluation while for the method reference it is invoked only once and the result captured.

                  这类似于x->的区别.System.out.println(x)System.out::println 其中读取字段 System.out 的内容发生在不同的时间,但通常是没关系.但你应该知道其中的区别.

                  This is similar to the difference between x -> System.out.println(x) and System.out::println where reading the contents of the field System.out happens at different times but usually it doesn’t matter. But you should be aware of the difference.

                  在您的示例中,调用了第三个方法 getValue().使用方法引用来表达这一点的唯一方法需要像 Function 这样的函数式接口,它具有像 andThen 和/或 compose 这样的方法.但是,Java 8 的工作方式需要将第一个方法引用转换为目标接口以调用组合方法,这绝不比您现在拥有的 lambda 表达式更容易阅读:((Function<X,Y>)getValueProvider()::apply).andThen(Y::getValue) 其中 Y 是类型,apply(input) 返回.

                  In your example, a third method getValue() is invoked. The only way to express that with method references needs a functional interface like Function which has methods like andThen and/or compose. However, the way Java8 works, that would require casting the first method reference to the target interface to invoke the combining method which would be by no way easier to read that the lambda expression you have now: ((Function<X,Y>)getValueProvider()::apply).andThen(Y::getValue) where Y is the type, apply(input) returns.

                  请注意,规则说用方法引用替换 lambdas如果可能"这让你有空间说,好吧,这是不可能的",但是,我不确定你有多少那么可以称之为规则"……

                  Note that the rule says "Replace lambdas with method references when possible" which gives you room to say, "well, here it is impossible", however, I’m not sure how much you can call it a "rule" then…

                  这篇关于声纳:用方法引用替换这个 lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Slf4j LoggerFactory.getLogger and sonarqube(Slf4j LoggerFactory.getLogger 和 sonarqube)
                  Security - Array is stored directly(安全性 - 数组直接存储)
                  SonarQube quot;Class Not Foundquot; during Main AST Scan(SonarQube“找不到类在主 AST 扫描期间)
                  Integrate Spock#39;s test with Sonar(将 Spock 的测试与声纳集成)
                  How do I make Hudson/Jenkins fail if Sonar thresholds are breached?(如果违反声纳阈值,我如何让 Hudson/Jenkins 失败?)
                  automatically add curly brackets to all if/else/for/while etc. in a java code-base(自动将大括号添加到 java 代码库中的所有 if/else/for/while 等)
                  • <bdo id='VQu03'></bdo><ul id='VQu03'></ul>
                      <legend id='VQu03'><style id='VQu03'><dir id='VQu03'><q id='VQu03'></q></dir></style></legend>
                        <tbody id='VQu03'></tbody>

                        <i id='VQu03'><tr id='VQu03'><dt id='VQu03'><q id='VQu03'><span id='VQu03'><b id='VQu03'><form id='VQu03'><ins id='VQu03'></ins><ul id='VQu03'></ul><sub id='VQu03'></sub></form><legend id='VQu03'></legend><bdo id='VQu03'><pre id='VQu03'><center id='VQu03'></center></pre></bdo></b><th id='VQu03'></th></span></q></dt></tr></i><div id='VQu03'><tfoot id='VQu03'></tfoot><dl id='VQu03'><fieldset id='VQu03'></fieldset></dl></div>

                        <small id='VQu03'></small><noframes id='VQu03'>

                          <tfoot id='VQu03'></tfoot>