BoundsTest.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2012, 2015, 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 * @modules jdk.compiler
29 * @build JavacTestingAbstractProcessor BoundsTest
30 * @run main BoundsTest
31 * @summary Assertion check for TypeVariable.getUpperBound() fails
32 */
33
34import com.sun.source.util.*;
35import javax.annotation.processing.*;
36import javax.lang.model.type.*;
37import javax.lang.model.util.ElementFilter;
38import javax.lang.model.element.*;
39import javax.tools.*;
40import java.util.*;
41import java.io.*;
42
43public class BoundsTest {
44
45    private int errors = 0;
46    private static final String Intersection_name = "IntersectionTest.java";
47    private static final String Intersection_contents =
48        "import java.util.List;\n" +
49        "import java.io.Serializable;\t" +
50        "public class IntersectionTest<S extends List & Serializable> {\n" +
51        "  void method(S s) { }\n" +
52        "}";
53    private static final String[] Intersection_bounds = {
54        "java.util.List",
55        "java.io.Serializable"
56    };
57    private static final String[] Intersection_supers = {
58        "java.util.List",
59        "java.io.Serializable"
60    };
61
62    private static final String Single_name = "SingleTest.java";
63    private static final String Single_contents =
64        "import java.util.List;\n" +
65        "public class SingleTest<S extends List> {\n" +
66        "  void method(S s) { }\n" +
67        "}";
68    private static final String[] Single_bounds = {
69        "java.util.List",
70    };
71    private static final String[] Single_supers = {
72        "java.lang.Object",
73        "java.util.Collection"
74    };
75
76    private static final String NoBounds_name = "NoBoundsTest.java";
77    private static final String NoBounds_contents =
78        "public class NoBoundsTest<S> {\n" +
79        "  void method(S s) { }\n" +
80        "}";
81    private static final String[] NoBounds_bounds = {
82        "java.lang.Object",
83    };
84    private static final String[] NoBounds_supers = {};
85
86    private HashSet<String> expected_bounds;
87    private HashSet<String> expected_supers;
88
89    private static final File classesdir = new File("intersectionproperties");
90    private static final JavaCompiler comp =
91        ToolProvider.getSystemJavaCompiler();
92    private static final StandardJavaFileManager fm =
93        comp.getStandardFileManager(null, null, null);
94
95    public void runOne(final String Test_name, final String Test_contents,
96                       final String[] Test_bounds, final String[] Test_supers)
97        throws IOException {
98        System.err.println("Testing " + Test_name);
99        expected_bounds = new HashSet<>(Arrays.asList(Test_bounds));
100        expected_supers = new HashSet<>(Arrays.asList(Test_supers));
101        final Iterable<? extends JavaFileObject> files =
102            fm.getJavaFileObjectsFromFiles(Collections.singleton(writeFile(classesdir, Test_name, Test_contents)));
103        final JavacTask ct =
104            (JavacTask)comp.getTask(null, fm, null, null, null, files);
105        ct.setProcessors(Collections.singleton(new TestProcessor()));
106
107        if (!ct.call()) {
108            System.err.println("Compilation unexpectedly failed");
109            errors++;
110        }
111    }
112
113    public void run() throws IOException {
114        try {
115            runOne(Intersection_name, Intersection_contents,
116                   Intersection_bounds, Intersection_supers);
117            runOne(Single_name, Single_contents,
118                   Single_bounds, Single_supers);
119            runOne(NoBounds_name, NoBounds_contents,
120                   NoBounds_bounds, NoBounds_supers);
121
122            if (0 != errors)
123                throw new RuntimeException(errors + " errors occurred");
124        } finally {
125            fm.close();
126        }
127    }
128
129    public static void main(String... args) throws IOException {
130        new BoundsTest().run();
131    }
132
133    private static File writeFile(File dir, String path, String body)
134        throws IOException {
135        File f = new File(dir, path);
136        f.getParentFile().mkdirs();
137        try (FileWriter out = new FileWriter(f)) {
138            out.write(body);
139        }
140        return f;
141    }
142
143    private class TestProcessor extends JavacTestingAbstractProcessor {
144
145        private Set<? extends Element> rootElements;
146
147        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
148            rootElements = roundEnv.getRootElements();
149            if (!rootElements.isEmpty()) {
150                performCheck();
151            }
152            return true;
153        }
154
155        private void performCheck() {
156            TypeElement typeElement = (TypeElement) rootElements.iterator().next();
157            ExecutableElement method = ElementFilter.methodsIn(typeElement.getEnclosedElements()).get(0);
158            TypeVariable typeVariable = (TypeVariable) method.getParameters().get(0).asType();
159
160            final TypeMirror upperBound = typeVariable.getUpperBound();
161
162            TypeParameterElement typeParameterElement = ((TypeParameterElement) typeVariable.asElement());
163            final List<? extends TypeMirror> bounds = typeParameterElement.getBounds();
164            final List<? extends TypeMirror> supers = processingEnv.getTypeUtils().directSupertypes(upperBound);
165
166            final HashSet<String> actual_bounds = new HashSet<>();
167            final HashSet<String> actual_supers = new HashSet<>();
168
169            for(TypeMirror ty : bounds) {
170                actual_bounds.add(((TypeElement)((DeclaredType)ty).asElement()).getQualifiedName().toString());
171            }
172
173            for(TypeMirror ty : supers) {
174                actual_supers.add(((TypeElement)((DeclaredType)ty).asElement()).getQualifiedName().toString());
175            }
176
177            if (!expected_bounds.equals(actual_bounds)) {
178                System.err.println("Mismatched expected and actual bounds.");
179                System.err.println("Expected:");
180                for(CharSequence tm : expected_bounds)
181                    System.err.println("  " + tm);
182                System.err.println("Actual:");
183                for(CharSequence tm : actual_bounds)
184                    System.err.println("  " + tm);
185                errors++;
186            }
187
188            if (!expected_supers.equals(actual_supers)) {
189                System.err.println("Mismatched expected and actual supers.");
190                System.err.println("Expected:");
191                for(CharSequence tm : expected_supers)
192                    System.err.println("  " + tm);
193                System.err.println("Actual:");
194                for(CharSequence tm : actual_supers)
195                    System.err.println("  " + tm);
196                errors++;
197            }
198        }
199    }
200
201}
202