T8142931.java revision 3109:500d36bee375
1/*
2 * Copyright (c) 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 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        JavacTool tool = JavacTool.create();
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("-proc:only",
58                                                  "-processor", "T8142931",
59                                                  "-processorpath", testClasses);
60            StringWriter out = new StringWriter();
61            JavacTask task = tool.getTask(out, fm, dl, opts, null, files);
62            task.call();
63            String s = out.toString();
64            System.err.print(s);
65            System.err.println(dl.count + " diagnostics; " + s.length() + " characters");
66            if (dl.count != 0 || s.length() != 0)
67                throw new AssertionError("unexpected output from compiler");
68        }
69    }
70
71    @Override
72    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
73        Set<? extends Element> set = roundEnv.getRootElements();
74        for (Element element : set) {
75            Collection<VariableElement> fields = ElementFilter.fieldsIn(((TypeElement) element).getEnclosedElements());
76            for (VariableElement field : fields) {
77                TypeMirror listType = field.asType();
78                List<? extends TypeMirror> typeArgs = ((DeclaredType) listType).getTypeArguments();
79                TypeMirror arg = typeArgs.get(0);
80                String erasure = processingEnv.getTypeUtils().erasure(arg).toString();
81                if (!erasure.equals("javax.xml.namespace.QName"))
82                    throw new AssertionError("Wrong Erasure: " + erasure);
83            }
84        }
85        return false;
86    }
87
88    @Override
89    public SourceVersion getSupportedSourceVersion() {
90        return SourceVersion.latest();
91    }
92
93    static class MyDiagListener implements DiagnosticListener {
94        public void report(Diagnostic d) {
95            System.err.println(d);
96            count++;
97        }
98
99        public int count;
100    }
101}