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