Maven 和 apache utils 的编译错误不明确 [英] Ambiguous compilation error with Maven and apache utils

查看:20
本文介绍了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.

我该如何解决这个问题?

How can I solve this?

推荐答案

出现问题是因为方法的签名具有可变参数.当一个方法被调用时,有 3 个阶段,在此期间搜索所有适用的方法.在 中搜索带有可变参数的方法阶段 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天全站免登陆