简体   繁体   English

如何使用Apache Commons BooleanUtils.and方法?

[英]How to use apache commons BooleanUtils.and method?

Apache commons-lang has two overloaded BooleanUtils.and methods. Apache commons-lang有两个重载的BooleanUtils.and方法。

public static boolean and(final boolean... array) {

public static Boolean and(final Boolean... array) {

When calling BooleanUtils.and method, ambiguous method call error is thrown. 调用BooleanUtils.and方法时,将引发模棱两可的方法调用错误。

java: reference to and is ambiguous
  both method and(boolean...) in org.apache.commons.lang3.BooleanUtils and method and(java.lang.Boolean...) in org.apache.commons.lang3.BooleanUtils match

It can be called using following syntax. 可以使用以下语法来调用它。

BooleanUtils.and(new Boolean[]{Boolean.TRUE, Boolean.TRUE});

But, as per the method's javadoc, the usage detail is different. 但是,根据方法的javadoc,用法细节有所不同。

Java文档

文字布尔值

包装布尔值

调用BooleanUtils.and的有效方法

This is because overloading a varargs method doesn't work for the primitive type and its object wrapper type . 这是因为重载varargs方法不适用于原始类型及其对象包装器类型 Its nothing to blame about the apache-commons-lang3. apache-commons-lang3没什么可怪的。

How varags do work ? varags如何工作?

During the compile time varags method signatures are replaces as Array . 在编译期间,将varags方法签名替换为Array Here the BooleanUtils.and methods will convert as 在这里, BooleanUtils.and方法将转换为

public static boolean and(final boolean[] array) { ... 
}

public static boolean and(final boolean[] array) { ... 
}

And the parameters you pass to them get replaced to an Array . 传递给它们的参数将替换为Array In this case you will get this 在这种情况下,您会得到这个

BooleanUtils.and(new boolean[]{true, true}) 
BooleanUtils.and(new Boolean[]{Boolean.TRUE, Boolean.TRUE})

Why Ambigious Method call ? 为什么调用歧义方法?

You can find that the your converted method parameter is an Array type and it matches the both methods against this type. 您会发现您转换后的方法参数是Array类型,并且两个方法都与此类型相匹配。 So compiler finds neither one is more appropriate than the other one by itself. 因此,编译器本身发现哪一个都不比另一个更合适。 It can't decide which method is the most-specific to call. 它无法确定哪个方法是最特定的调用。

But when you yourself declare BooleanUtils.and(new Boolean[]{Boolean.TRUE, Boolean.TRUE}) or BooleanUtils.and(new boolean[]{true, true}) that exposes your intention to compiler and method is chosen without boxing or autoboxing. 但是,当您自己声明BooleanUtils.and(new Boolean[]{Boolean.TRUE, Boolean.TRUE})BooleanUtils.and(new boolean[]{true, true}) ,无需编译即可选择暴露给编译器和方法的意图或自动装箱。

And this is how compiler identify applicable methods n 3 phases. 这就是编译器在三个阶段中确定适用方法的方式。 See details about Choosing the Most Specific Method 查看有关选择最具体方法的详细信息

The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion , or the use of variable arity method invocation. 第一阶段(第15.12.2.2节)执行重载解析, 而不允许装箱或拆箱转换 ,也不允许使用可变arity方法调用。 If no applicable method is found during this phase then processing continues to the second phase. 如果在此阶段未找到适用的方法,则处理将继续进行到第二阶段。

The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing , but still precludes the use of variable arity method invocation. 第二阶段(第15.12.2.3节) 在允许装箱和拆箱的同时执行重载解析,但仍排除使用可变arity方法调用。 If no applicable method is found during this phase then processing continues to the third phase. 如果在此阶段未找到适用的方法,则处理将继续进行到第三阶段。

The third phase (§15.12.2.4) allows overloading to be combined with variable arity methods, boxing, and unboxing. 第三阶段(第15.12.2.4节)允许将重载与可变arity方法,装箱和拆箱相结合。

Such compilation errors appeared in JDK8. 这种编译错误出现在JDK8中。 I believe, that commons-lang's javadoc was written in past. 我相信,commons-lang的javadoc是以前编写的。 (when JDK7 was a latest SDK). (当JDK7是最新的SDK时)。 Seems, this is a side-effect for one of the features that was released with JDK8 (probably lambas ). 似乎,这是JDK8(可能是lambas )发布的功能之一的lambas

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM