langtools/test/tools/javac/processing/errors/EnsureMirroredTypeException/Processor.java
changeset 19507 323f001d6be1
child 36526 3b41f1c69604
equal deleted inserted replaced
19506:9314c30fef42 19507:323f001d6be1
       
     1 /*
       
     2  * Copyright (c) 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 import java.lang.annotation.*;
       
    25 import java.util.*;
       
    26 import javax.annotation.processing.*;
       
    27 import javax.lang.model.element.*;
       
    28 import javax.lang.model.type.DeclaredType;
       
    29 import javax.lang.model.type.MirroredTypeException;
       
    30 import javax.lang.model.type.TypeMirror;
       
    31 import javax.lang.model.type.TypeKind;
       
    32 import javax.tools.*;
       
    33 
       
    34 import com.sun.tools.javac.util.Assert;
       
    35 import com.sun.tools.javac.code.Symbol;
       
    36 import static com.sun.tools.javac.code.Symbol.TypeSymbol;
       
    37 
       
    38 public class Processor extends JavacTestingAbstractProcessor {
       
    39 
       
    40     @Override
       
    41     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
       
    42         for (Element e : roundEnv.getElementsAnnotatedWith(A.class)) {
       
    43             A rtg = e.getAnnotation(A.class);
       
    44 
       
    45             try {
       
    46                 rtg.a();
       
    47                 Assert.check(false); //Should not reach here
       
    48             } catch (MirroredTypeException ex) {
       
    49                 TypeMirror tm = ex.getTypeMirror();
       
    50                 Assert.check(tm.getKind() == TypeKind.ERROR);
       
    51 
       
    52                 TypeElement elm = (TypeElement)((DeclaredType)tm).asElement();
       
    53                 Assert.check(elm.getQualifiedName().toString().
       
    54                         endsWith("some.path.to.SomeUnknownClass$Inner"));
       
    55 
       
    56                 TypeSymbol sym = (TypeSymbol)elm;
       
    57                 Assert.check(sym.name.contentEquals("some.path.to.SomeUnknownClass$Inner"));
       
    58             }
       
    59         }
       
    60         for (Element e : roundEnv.getElementsAnnotatedWith(B.class)) {
       
    61             B rtg = e.getAnnotation(B.class);
       
    62 
       
    63             try {
       
    64                 rtg.a();
       
    65                 Assert.check(false); //Should not reach here
       
    66             } catch (MirroredTypeException ex) {
       
    67                 TypeMirror tm = ex.getTypeMirror();
       
    68                 Assert.check(tm.getKind() == TypeKind.ERROR);
       
    69 
       
    70                 TypeElement elm = (TypeElement)((DeclaredType)tm).asElement();
       
    71                 Assert.check(elm.getQualifiedName().toString().
       
    72                         endsWith("SomeUnknownClass"));
       
    73 
       
    74                 TypeSymbol sym = (TypeSymbol)elm;
       
    75                 Assert.check(sym.name.contentEquals("SomeUnknownClass"));
       
    76             }
       
    77         }
       
    78         for (Element e : roundEnv.getElementsAnnotatedWith(C.class)) {
       
    79             C rtg = e.getAnnotation(C.class);
       
    80 
       
    81             try {
       
    82                 rtg.a();
       
    83                 Assert.check(false); //Should not reach here
       
    84             } catch (AnnotationTypeMismatchException ex) {
       
    85                 ;
       
    86             }
       
    87         }
       
    88         return true;
       
    89     }
       
    90 
       
    91     @interface A {
       
    92         Class<?> a();
       
    93     }
       
    94     @interface B {
       
    95         Class<?> a();
       
    96     }
       
    97     @interface C {
       
    98         Class<?> a();
       
    99     }
       
   100 }