T6421111.java revision 2689:f839b50088bc
1/*
2 * Copyright (c) 2006, 2014, 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     6421111
27 * @summary NullPointerException thrown when retrieving bounds for the type parameter
28 * @author  Peter von der Ah\u00e9
29 * @library ../lib
30 * @build ToolTester
31 * @compile -Xlint:all T6421111.java
32 * @run main T6421111
33 */
34
35import java.io.File;
36import java.io.IOException;
37import java.net.URI;
38import java.util.Arrays;
39import java.util.Collections;
40import java.util.Set;
41import javax.annotation.processing.AbstractProcessor;
42import javax.annotation.processing.RoundEnvironment;
43import javax.annotation.processing.SupportedAnnotationTypes;
44import javax.annotation.processing.SupportedSourceVersion;
45import javax.lang.model.SourceVersion;
46import javax.lang.model.element.TypeElement;
47import javax.lang.model.element.TypeParameterElement;
48import javax.lang.model.type.DeclaredType;
49import javax.lang.model.type.TypeVariable;
50import javax.tools.SimpleJavaFileObject;
51
52import static javax.tools.JavaFileObject.Kind.SOURCE;
53
54public class T6421111 extends ToolTester {
55    void test(String... args) {
56        class Test1 extends SimpleJavaFileObject {
57            Test1() {
58                super(URI.create("myfo:///Test1.java"), SOURCE);
59            }
60            @Override
61            public String getCharContent(boolean ignoreEncodingErrors) {
62                return "class Test1<T extends Thread & Runnable> {}";
63            }
64        }
65        class Test2 extends SimpleJavaFileObject {
66            Test2() {
67                super(URI.create("myfo:///Test2.java"), SOURCE);
68            }
69            @Override
70            public String getCharContent(boolean ignoreEncodingErrors) {
71                return "class Test2<T extends Test2<T> & Runnable> {}";
72            }
73        }
74        task = tool.getTask(null, fm, null, Collections.singleton("-Xlint:all"), null,
75                            Arrays.asList(new Test1(), new Test2()));
76        task.setProcessors(Collections.singleton(new MyProcessor()));
77        if (!task.call())
78            throw new AssertionError("Annotation processor failed");
79    }
80    @SupportedAnnotationTypes("*")
81    static class MyProcessor extends AbstractProcessor {
82        void test(TypeElement element, boolean fbound) {
83            TypeParameterElement tpe = element.getTypeParameters().iterator().next();
84            tpe.getBounds().getClass();
85            if (fbound) {
86                DeclaredType type = (DeclaredType)tpe.getBounds().get(0);
87                if (type.asElement() != element)
88                    throw error("%s != %s", type.asElement(), element);
89                TypeVariable tv = (TypeVariable)type.getTypeArguments().get(0);
90                if (tv.asElement() != tpe)
91                    throw error("%s != %s", tv.asElement(), tpe);
92            }
93        }
94        public boolean process(Set<? extends TypeElement> annotations,
95                               RoundEnvironment roundEnv) {
96            test(processingEnv.getElementUtils().getTypeElement("Test1"), false);
97            test(processingEnv.getElementUtils().getTypeElement("Test2"), true);
98            return false;
99        }
100        @Override
101        public SourceVersion getSupportedSourceVersion() {
102            return SourceVersion.latest();
103        }
104    }
105    public static void main(String... args) throws IOException {
106        try (T6421111 t = new T6421111()) {
107            t.test(args);
108        }
109    }
110    public static AssertionError error(String format, Object... args) {
111        return new AssertionError(String.format(format, args));
112    }
113}
114