Interface with a Builder with a Factory and inheritance

Wang Weijun weijun.wang at oracle.com
Wed Jan 20 13:47:59 UTC 2016


I am working on an Interface with a Builder with a Factory, and involving inheritance. The actual case can be viewed here [1], but I've distilled everything below in a single file.

I've never written something with so many <> symbols. Although the code works, I am not sure if those ?s are used correctly, and there is still a warning 

$ javac -Xlint A1.java
A1.java:4: warning: [unchecked] unchecked cast
        B2<I2,?> BImpl = (B2<I2,?>)f.getB();
                                         ^
  required: B2<I2,?>
  found:    B<CAP#1,CAP#2>
  where CAP#1,CAP#2 are fresh type-variables:
    CAP#1 extends I from capture of ? extends I
    CAP#2 extends B<CAP#1,CAP#2> from capture of ? extends B<?,?>
1 warning
$ java A1
<N,C>
<Nn,Cc>

How can I make this warning going away?

Thanks
Max

--

public class A1 {
    public static void main(String[] args) throws Exception {
        F f = new Impl();
        B2<I2,?> BImpl = (B2<I2,?>)f.getB();
        I2 dad = BImpl.setName("N").setColor("C").build(null);
        I2 son = BImpl.setName("n").setColor("c").build(dad);
        System.out.println(dad);
        System.out.println(son);
    }

    // Base interface and builder
    interface I { String name(); }
    interface B <S extends I, T extends B<S,T>> {
        T setName(String s);
        S build(S parent);
    }

    // Extended interface and builder
    interface I2 extends I { String color(); }
    interface B2<S extends I2, T extends B2<S,T>> extends B<S,T> {
        T setColor(String s);
    }

    // Builder factory interface
    interface F {
        B <? extends I,? extends B<?,?>> getB();
    }

    // Implementation
    static class Impl implements F {
        static class C implements I2 {
            String color, name;

            public String color() {
                return color;
            }

            C(String name, String color) {
                this.color = color;
                this.name = name;
            }

            public String name() {
                return name;
            }

            public String toString() {
                return "<" + name + "," + color + ">";
            }
        }

        static class BImpl implements B2<I2,BImpl> {
            String color;
            String name;

            public BImpl setColor(String s) {
                this.color = s;
                return this;
            }

            public BImpl setName(String s) {
                this.name = s;
                return this;
            }

            public I2 build(I2 parent) {
                if (parent == null) {
                    return new C(name, color);
                } else {
                    return new C(parent.name() + name, parent.color() + color);
                }
            }
        }

        public B <? extends I,? extends B<?,?>> getB() {
            return new BImpl();
        }
    }
}

[1] http://cr.openjdk.java.net/~weijun/8058778/webrev.09/specdiff/java/security/cert/package-summary.html


More information about the compiler-dev mailing list