This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 242856 - Wrong hint "Unnecessary cast to T" ("Typecast is too strong") for overloaded methods (leads to ambiguous calls)
Summary: Wrong hint "Unnecessary cast to T" ("Typecast is too strong") for overloaded ...
Status: RESOLVED DUPLICATE of bug 250627
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0
Hardware: PC Windows 8
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-13 11:21 UTC by xehpuk
Modified: 2015-04-09 12:30 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description xehpuk 2014-03-13 11:21:32 UTC
Product Version = NetBeans IDE 8.0 RC1 (Build 201402242200)
Operating System = Windows 8 version 6.2 running on amd64
Java; VM; Vendor = 1.8.0
Runtime = Java HotSpot(TM) 64-Bit Server VM 25.0-b70

Consider this utility class:

import java.util.function.*;

public final class Utils {
    public static <T> T require(final T t, final Predicate<T> predicate) {
        return require(t, predicate, (String) null); // "Unnecessary cast to String"
    }
    
    public static <T> T require(final T t, final Predicate<T> predicate, final String message) {
        if (!predicate.test(t)) {
            throw new IllegalArgumentException(message);
        }
        return t;
    }
    
    public static <T> T require(final T t, final Predicate<T> predicate, final Supplier<String> messageSupplier) {
        if (!predicate.test(t)) {
            throw new IllegalArgumentException(messageSupplier.get());
        }
        return t;
    }
    
    private Utils() {}
}

NetBeans raises a warning in the commented line. Removing the cast leads to the compiler error "reference to require is ambiguous".
Comment 1 bugmenot1 2014-12-19 21:58:42 UTC
Also happens with constructors:

public class LoginWindow extends JDialog {

    public LoginWindow() {
        super((Frame) null); // "Unnecessary cast to Frame" (NetBeans 8.0.2)
    }
}

Also, after removing the cast, the error message suggests there are only two matching constructors ("both constructor JDialog(Frame) in JDialog and constructor JDialog(Dialog) in JDialog match") while there are actually three (the third one being JDialog(Window)), so maybe the error message could be extended.

As for the error, it would be nice if there would be a hint with fixing options, e. g. "cast to Frame", "cast to Dialog", "cast to Window" so one can just click the desired type.
Comment 2 Svata Dedic 2015-04-09 12:30:18 UTC
Duplicate -- method call resolution now detects ambiguity of the result.

*** This bug has been marked as a duplicate of bug 250627 ***