1 /*
2  * Copyright (c) 2012, 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 8024513
27  * @library /tools/javac/lib
28  * @modules java.compiler
29  *          jdk.compiler
30  * @build JavacTestingAbstractProcessor InheritedAP
31  * @compile -cp . -processor InheritedAP -proc:only InheritedAP.java
32  * @summary NPE in annotation processing
33  */
34 import java.util.*;
35 import javax.annotation.processing.*;
36 import javax.lang.model.element.*;
37 import javax.lang.model.type.*;
38 import javax.lang.model.util.*;
39 import java.lang.annotation.*;
40 import static javax.lang.model.type.TypeKind.*;
41 import static javax.lang.model.SourceVersion.*;
42 import static javax.lang.model.util.ElementFilter.*;
43 
44 @SupportedAnnotationTypes("testclass")
45 public class InheritedAP extends JavacTestingAbstractProcessor {
46     static Types types;
init(ProcessingEnvironment penv)47     public void init(ProcessingEnvironment penv) {super.init(penv);}
getTypes()48     public static Types getTypes() { return types; }
49 
process(Set<? extends TypeElement> typeElementSet,RoundEnvironment renv)50     public boolean process(Set<? extends TypeElement> typeElementSet,RoundEnvironment renv) {
51         if ( renv.errorRaised()) { System.out.println("Error!"); return false; }
52         if ( typeElementSet.size() <=0 && typesIn(renv.getRootElements()).size() <= 0 ) {
53             return true;
54         }
55         types=processingEnv.getTypeUtils();
56         for (TypeElement typeElem: typesIn(renv.getRootElements())) {
57             if (typeElem.getAnnotation(testclass.class) != null) {
58                 new LocalElementScanner( new SimpleTypeMirrorVisitor()).scan(typeElem, null);
59             }
60         }
61         return true ;
62     }
63 }
64 
65 class SimpleTypeMirrorVisitor extends JavacTestingAbstractProcessor.SimpleTypeVisitor<Void, Void> {
defaultAction(TypeMirror mirror, Void p )66     protected Void defaultAction(TypeMirror mirror, Void p ) {
67         try {
68             System.out.println( "InheritedAP.getTypes().directSupertypes( "+mirror.toString()+" );" );
69             InheritedAP.getTypes().directSupertypes(mirror);
70             System.out.println("PASS");
71         }catch(java.lang.IllegalArgumentException iae) {/*stuff*/ }
72         return p;
73     }
74 }
75 
76 class LocalElementScanner <T extends JavacTestingAbstractProcessor.SimpleTypeVisitor<Void, Void> >
77                     extends JavacTestingAbstractProcessor.ElementScanner<Void, Void> {
78     JavacTestingAbstractProcessor.SimpleTypeVisitor<Void, Void> typeVisitor;
79 
LocalElementScanner(T typeVisitor)80     public LocalElementScanner(T typeVisitor) { this.typeVisitor=typeVisitor;}
81 
82     @Override
scan(Element e, Void p)83     public Void scan(Element e, Void p) {
84          if (e instanceof TypeElement ) {
85             TypeElement te = (TypeElement) e;
86             te.getSuperclass().accept(typeVisitor,p);
87         }
88         return p;
89     }
90 }
91 
92 
93 @interface testclass { }
94 
95 @testclass
96 @interface iface { }
97