IntersectionPropertiesTest.java revision 2049:09301757bb32
1/*
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6499673
27 * @library /tools/javac/lib
28 * @build JavacTestingAbstractProcessor IntersectionPropertiesTest
29 * @run main IntersectionPropertiesTest
30 * @summary Assertion check for TypeVariable.getUpperBound() fails
31 */
32
33import com.sun.source.util.*;
34import com.sun.tools.javac.api.*;
35import com.sun.tools.javac.file.*;
36import javax.annotation.processing.*;
37import javax.lang.model.SourceVersion;
38import javax.lang.model.type.*;
39import javax.lang.model.util.ElementFilter;
40import javax.lang.model.element.*;
41import javax.tools.*;
42import java.util.*;
43import java.io.*;
44
45public class IntersectionPropertiesTest {
46
47    private int errors = 0;
48    private static final String Intersection_name = "IntersectionTest.java";
49    private static final String Intersection_contents =
50        "import java.util.List;\n" +
51        "import java.io.Serializable;\t" +
52        "public class IntersectionTest<S extends List & Serializable> {\n" +
53        "  void method(S s) { }\n" +
54        "}";
55
56    private static final File classesdir = new File("intersectionproperties");
57    private static final JavaCompiler comp =
58        ToolProvider.getSystemJavaCompiler();
59    private static final StandardJavaFileManager fm =
60        comp.getStandardFileManager(null, null, null);
61
62    public void runOne(final String Test_name, final String Test_contents)
63        throws IOException {
64        System.err.println("Testing " + Test_name);
65        final Iterable<? extends JavaFileObject> files =
66            fm.getJavaFileObjectsFromFiles(Collections.singleton(writeFile(classesdir, Test_name, Test_contents)));
67        final JavacTask ct =
68            (JavacTask)comp.getTask(null, fm, null, null, null, files);
69        ct.setProcessors(Collections.singleton(new TestProcessor()));
70
71        if (!ct.call()) {
72            System.err.println("Compilation unexpectedly failed");
73            errors++;
74        }
75    }
76
77    public void run() throws IOException {
78        runOne(Intersection_name, Intersection_contents);
79
80        if (0 != errors)
81            throw new RuntimeException(errors + " errors occurred");
82    }
83
84    public static void main(String... args) throws IOException {
85        new IntersectionPropertiesTest().run();
86    }
87
88    private static File writeFile(File dir, String path, String body)
89        throws IOException {
90        File f = new File(dir, path);
91        f.getParentFile().mkdirs();
92        try (FileWriter out = new FileWriter(f)) {
93            out.write(body);
94        }
95        return f;
96    }
97
98    private class TestProcessor extends JavacTestingAbstractProcessor {
99
100        private Set<? extends Element> rootElements;
101
102        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
103            rootElements = roundEnv.getRootElements();
104            if (!rootElements.isEmpty()) {
105                performCheck();
106            }
107            return true;
108        }
109
110        private void performCheck() {
111            TypeElement typeElement = (TypeElement) rootElements.iterator().next();
112            ExecutableElement method = ElementFilter.methodsIn(typeElement.getEnclosedElements()).get(0);
113            TypeVariable typeVariable = (TypeVariable) method.getParameters().get(0).asType();
114
115            final TypeMirror upperBound = typeVariable.getUpperBound();
116
117            TypeParameterElement typeParameterElement = ((TypeParameterElement) typeVariable.asElement());
118            final List<? extends TypeMirror> bounds = typeParameterElement.getBounds();
119            final HashSet<TypeMirror> actual = new HashSet<TypeMirror>(processingEnv.getTypeUtils().directSupertypes(upperBound));
120            final HashSet<TypeMirror> expected = new HashSet<TypeMirror>(bounds);
121            if (!expected.equals(actual)) {
122                System.err.println("Mismatched expected and actual bounds.");
123                System.err.println("Expected:");
124                for(TypeMirror tm : expected)
125                    System.err.println("  " + tm);
126                System.err.println("Actual:");
127                for(TypeMirror tm : actual)
128                    System.err.println("  " + tm);
129                errors++;
130            }
131        }
132
133    }
134
135}
136