File: AmbiguousCast.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 65,172 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (32 lines) | stat: -rw-r--r-- 987 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * @test /nodynamiccopyright/
 * @bug 4897892
 * @summary cast to parameterized type is accepted although it should be rejected
 * @author gafter
 *
 * @compile/ref=AmbiguousCast.out -XDrawDiagnostics -Xlint:unchecked AmbiguousCast.java
 */

class Test {

    private static class GenericWrapper<Elem> {
        private Elem theObject;
        public GenericWrapper(Elem arg) {
            theObject = arg;
        }
        public <T extends Elem> GenericWrapper (GenericWrapper<T> other) {
            this.theObject = other.theObject;
        }
        public String toString() {
            return theObject.toString();
        }
    }
    private static GenericWrapper<String> method (Object wrappedString) {
        return (GenericWrapper<String>) wrappedString;
    }

    public static void main(String[] args) {
        System.out.println(method(new GenericWrapper<String>("abc")));
        System.out.println(method(new GenericWrapper<Exception>(new Exception())));
    }
}