Maven和apache utils的模棱两可的编译错误 [英] Ambiguous compilation error with Maven and apache utils

查看:102
本文介绍了Maven和apache utils的模棱两可的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在commons-lang3(版本3.1)中使用org.apache.commons.lang3.BooleanUtils. 当我尝试编译下一行代码

I'm using org.apache.commons.lang3.BooleanUtils in the commons-lang3 (version 3.1). When I try to compile next line of code

BooleanUtils.xor(true, true);

使用maven-compiler-plugin(版本3.3),我收到一条编译失败消息:

using maven-compiler-plugin (version 3.3), I'm getting a compilation failure message:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project exchange: Compilation failure
[ERROR] MyClass.java:[33,34] reference to xor is ambiguous, both method xor(boolean...) in org.apache.commons.lang3.BooleanUtils and method xor(java.lang.Boolean...) in org.apache.commons.lang3.BooleanUtils match

我使用Java 1.7.0_55进行编译.

I use Java 1.7.0_55 to compile.

我该如何解决?

推荐答案

之所以会出现此问题,是因为该方法的签名具有可变参数.调用方法时,在三个阶段中搜索所有适用的方法.在中搜索具有可变参数的方法阶段3 ,也可以进行装箱和拆箱.

The problem happens because the signature of the method has variable arguments. When a method is invoked, there are 3 phases during which all applicable methods are searched. Methods with variable arguments are searched in phase 3 where boxing, and unboxing is also allowed.

因此xor(boolean...)xor(Boolean...)均适用于此处,因为考虑了装箱.当有多种方法适用时,仅调用最具体的方法.但是在这种情况下,无法比较booleanBoolean,因此没有其他更具体的方法,因此会出现编译器错误:两种方法都匹配.

So both xor(boolean...) and xor(Boolean...) are applicable here because boxing is taken into account. When multiple methods are applicable, only the most specific is invoked. But in this case, boolean and Boolean can't be compared, so there is no more specific method, hence the compiler error: both methods match.

一种解决方法是创建一个显式数组:

A workaround is to create an explicit array:

public static void main(String[] args) {
    xor(new boolean[] { true, false }); // will call the primitive xor
    xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }); // will call the non-primitive xor
}

private static Boolean xor(Boolean... booleans) {
    System.out.println("Boolean...");
    return Boolean.TRUE;
}

private static boolean xor(boolean... booleans) {
    System.out.println("boolean...");
    return true;
}

这篇关于Maven和apache utils的模棱两可的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆