T8142931.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2015, 2016, 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 8142931
27 * @summary java compiler: type erasure doesn't work since 9-b28
28 * @modules jdk.compiler/com.sun.tools.javac.api
29 *          jdk.compiler/com.sun.tools.javac.file
30 */
31
32import java.io.*;
33import java.util.*;
34import javax.annotation.processing.*;
35import javax.lang.model.*;
36import javax.lang.model.element.*;
37import javax.lang.model.util.ElementFilter;
38import javax.lang.model.type.TypeMirror;
39import javax.lang.model.type.DeclaredType;
40import javax.tools.*;
41import com.sun.source.util.*;
42import com.sun.tools.javac.api.*;
43
44@SupportedAnnotationTypes("*")
45public class T8142931 extends AbstractProcessor {
46
47    public java.util.List<? extends javax.xml.namespace.QName> f0;
48
49    public static void main(String... args) throws IOException {
50        String testSrc = System.getProperty("test.src", ".");
51        String testClasses = System.getProperty("test.classes");
52        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
53        MyDiagListener dl = new MyDiagListener();
54        try (StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null)) {
55            Iterable<? extends JavaFileObject> files =
56                fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T8142931.class.getName()+".java")));
57            Iterable<String> opts = Arrays.asList("-XaddExports:" +
58                                                  "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
59                                                  "-XDaccessInternalAPI",
60                                                  "-proc:only",
61                                                  "-processor", "T8142931",
62                                                  "-processorpath", testClasses);
63            StringWriter out = new StringWriter();
64            JavacTask task = (JavacTask)tool.getTask(out, fm, dl, opts, null, files);
65            task.call();
66            String s = out.toString();
67            System.err.print(s);
68            System.err.println(dl.count + " diagnostics; " + s.length() + " characters");
69            if (dl.count != 0 || s.length() != 0)
70                throw new AssertionError("unexpected output from compiler");
71        }
72    }
73
74    @Override
75    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
76        Set<? extends Element> set = roundEnv.getRootElements();
77        for (Element element : set) {
78            Collection<VariableElement> fields = ElementFilter.fieldsIn(((TypeElement) element).getEnclosedElements());
79            for (VariableElement field : fields) {
80                TypeMirror listType = field.asType();
81                List<? extends TypeMirror> typeArgs = ((DeclaredType) listType).getTypeArguments();
82                TypeMirror arg = typeArgs.get(0);
83                String erasure = processingEnv.getTypeUtils().erasure(arg).toString();
84                if (!erasure.equals("javax.xml.namespace.QName"))
85                    throw new AssertionError("Wrong Erasure: " + erasure);
86            }
87        }
88        return false;
89    }
90
91    @Override
92    public SourceVersion getSupportedSourceVersion() {
93        return SourceVersion.latest();
94    }
95
96    static class MyDiagListener implements DiagnosticListener {
97        public void report(Diagnostic d) {
98            System.err.println(d);
99            count++;
100        }
101
102        public int count;
103    }
104}
105
106