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