Generics and Java Warning

I know that this topic has already been discussed at the moment (for example, Java generic warnings on java.util.Collections , java generics, unchecked warnings , etc.), but I came across a situation for which I can not find an explanation.

I have a Predicate class defined as

public interface Predicate<T> {
    boolean holds(T o) ; 
}

Then I have a utility class (PredicateUtils) for predicting. Sample method there

public static <T> Predicate join(final Predicate<T> p1, final Predicate<T> p2) {
    return new Predicate<T>() {

        @Override
        public boolean holds(T o) {
            return (p1.holds(o) && p2.holds(o)) ;
        }
    } ;
}

However, when I call the join method, for example, passing two instances of Predicate, I get the following error from the jdk compiler (javac 1.7.0_51):

warning: [unchecked] unchecked conversion
        return PredicateUtils.join(p1, p2) 
  required: Predicate<Integer>
  found:    Predicate

To simplify the discussion, you can define the method below (dummy code) in this class:

public static Predicate<Integer> test() {
    Predicate<Integer> p1 = new Predicate<Integer>() {
        public boolean holds(Integer o) { return true ; }
    };
    Predicate<Integer> p2 = new Predicate<Integer>() {
        public boolean holds(Integer o) { return true ; }
    };
    return join(p1, p2) ;
}

and it will see that when compiling the associated class, javac will issue the same warning.

+4
2

join:

public static <T> Predicate join(final Predicate<T> p1, final Predicate<T> p2)

, , . :

public static <T> Predicate<T> join(final Predicate<T> p1, final Predicate<T> p2)
+8

, Predicate, Predicate<T>.

.

, .

+6

Source: https://habr.com/ru/post/1530685/


All Articles