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