如何检测在Java 8中会导致ClassCastException的模糊方法调用? [英] How to detect ambiguous method calls that would cause a ClassCastException in Java 8?

查看:108
本文介绍了如何检测在Java 8中会导致ClassCastException的模糊方法调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在将应用程序从Java 7迁移到Java 8.在修复了一些编译问题之后,我偶然发现了类似于以下问题的问题:。/ b>

总结,这是一个示例代码,它显示了这个问题:

  public class Test {
public static void main(String [ ] args){
System.out.println(String.valueOf(getVal(xxx))); // 7:打印结果8:例外
}

@SuppressWarnings(未选中)
public static< T> T getVal(String param){
//根据参数做一些计算...
return(T)result; //实际返回类型只依赖于param,因此调用者知道期望什么


code $ $ $ $ $ $ $ $ $ $ $ $ b

我们的想法是,我们会强调,调用者知道预期的类型,这将避免他明确的演员(我不认为这是个好主意......)。在很多情况下,调用者只是期望一个 Object ,所以根本没有隐式转换。



As在上面的问题中,在Java 7中, String.valueOf 示例工作正常,因为没有类型推断,因此 Object 被假定。现在在Java 8中,编译器选择最具体的类型(这里 char [] ),这会导致 ClastCastException

问题是我们有大约350次这个 getVal 方法的调用。 是否有办法检测Java 7和Java 8之间会有所不同的重载方法调用? I.E.检测Java 8编译器何时从Java 7编译器中选择不同的方法。

更好的替代方案是:

  public class Test {
public static void main(String [] args){
System.out.println (将String.valueOf(GETVAL( XXX))); // 7:打印结果8:例外
}

@SuppressWarnings(未选中)
public static< T> T getVal(T param){
//根据参数做一些计算...
return param; //实际返回类型只依赖于param,因此调用者知道期望什么


code $ $ $ $ $ $ $ $ $ $ $ $ b

可以在Java 7和Java 8中使用。


We are currently in the process of migrating an application from Java 7 to Java 8. After fixing a some compilation issues, I stumbled upon an issue similar to the following question: ClassCast Error: Java 7 vs Java 8.

To summarise, here is a sample code that shows the issue:

public class Test {
    public static void main(String[] args) {
        System.out.println(String.valueOf(getVal("xxx"))); // 7: prints the result, 8: Exception 
    }

    @SuppressWarnings("unchecked")
    public static <T> T getVal(String param) {
        // do some computation based on param...
        return (T) result; // actual return type only depends on param so the caller knows what to expect
    }
}

The idea was that we would thrust that the caller knows the expected type, and this would avoid him an explicit cast (I don't say this was a good idea…). In a lot of cases, the caller just expects an Object so there was no implicit cast at all.

As stated in the question above, the String.valueOf example worked fine in Java 7 because there was no type inference, hence Object was assumed. Now in Java 8, the compiler chooses the most specific type (here char[]), which causes a ClastCastException at runtime.

The problem is that we have around 350 calls of this getVal method. Is there a way to detect overloaded method calls that would differ between Java 7 and Java 8? I.E. detect when the Java 8 compiler would select a different method from the Java 7 compiler.

解决方案

A better alternative would be:

public class Test {
    public static void main(String[] args) {
        System.out.println(String.valueOf(getVal("xxx"))); // 7: prints the result, 8: Exception
    }

    @SuppressWarnings("unchecked")
    public static <T> T getVal(T param) {
        // do some computation based on param...
        return   param; // actual return type only depends on param so the caller knows what to expect
    }
}

which will work in both Java 7 and Java 8.

这篇关于如何检测在Java 8中会导致ClassCastException的模糊方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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