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