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