OverEager.java revision 494:fe17a9dbef03
1270114Sse/*
2270114Sse * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
3270114Sse * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4270114Sse *
5270114Sse * This code is free software; you can redistribute it and/or modify it
6270114Sse * under the terms of the GNU General Public License version 2 only, as
7270114Sse * published by the Free Software Foundation.
8270114Sse *
9270114Sse * This code is distributed in the hope that it will be useful, but WITHOUT
10270114Sse * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11270114Sse * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12270114Sse * version 2 for more details (a copy is included in the LICENSE file that
13270114Sse * accompanied this code).
14270114Sse *
15270114Sse * You should have received a copy of the GNU General Public License version
16270114Sse * 2 along with this work; if not, write to the Free Software Foundation,
17270114Sse * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18270114Sse *
19270114Sse * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20270114Sse * CA 95054 USA or visit www.sun.com if you need additional information or
21270114Sse * have any questions.
22270114Sse */
23270114Sse
24270114Sse/*
25270114Sse * @test
26270114Sse * @bug     6362178
27270114Sse * @summary MirroredType[s]Exception shouldn't be created too eagerly
28270114Sse * @author  Scott Seligman
29270114Sse * @compile -g OverEager.java
30270114Sse * @compile -processor OverEager -proc:only OverEager.java
31270114Sse */
32270114Sse
33270114Sseimport java.util.Set;
34270114Sseimport javax.annotation.processing.*;
35270114Sseimport javax.lang.model.SourceVersion;
36270114Sseimport javax.lang.model.element.*;
37270114Sseimport javax.lang.model.type.*;
38270114Sseimport javax.lang.model.util.*;
39270114Sseimport static javax.lang.model.util.ElementFilter.*;
40270114Sse
41270114Sse@SupportedAnnotationTypes("IAm")
42270114Sse@IAm(OverEager.class)
43270114Ssepublic class OverEager extends AbstractProcessor {
44270114Sse
45270114Sse    Elements elements;
46270114Sse    Types types;
47270114Sse
48270114Sse    public void init(ProcessingEnvironment penv) {
49270114Sse        super.init(penv);
50270114Sse        elements = penv.getElementUtils();
51270114Sse        types =  penv.getTypeUtils();
52270114Sse    }
53270114Sse
54270114Sse    public boolean process(Set<? extends TypeElement> annoTypes,
55270114Sse                           RoundEnvironment round) {
56270114Sse        if (!round.processingOver())
57270114Sse            doit(annoTypes, round);
58270114Sse        return true;
59270114Sse    }
60270114Sse
61270114Sse    @Override
62270114Sse    public SourceVersion getSupportedSourceVersion() {
63270114Sse        return SourceVersion.latest();
64270114Sse    }
65270114Sse
66270114Sse    private void doit(Set<? extends TypeElement> annoTypes,
67270114Sse                      RoundEnvironment round) {
68270114Sse        for (TypeElement t : typesIn(round.getRootElements())) {
69270114Sse            IAm anno = t.getAnnotation(IAm.class);
70270114Sse            if (anno != null)
71270114Sse                checkAnno(anno);
72270114Sse        }
73270114Sse    }
74270114Sse
75270114Sse    private void checkAnno(IAm anno) {
76270114Sse        try {
77270114Sse            anno.value();
78270114Sse            throw new AssertionError();
79270114Sse        } catch (MirroredTypeException e) {
80270114Sse            System.out.println("Looking for checkAnno in this stack trace:");
81270114Sse            e.printStackTrace();
82270114Sse            for (StackTraceElement frame : e.getStackTrace()) {
83270114Sse                if (frame.getMethodName() == "checkAnno")
84270114Sse                    return;
85270114Sse            }
86270114Sse            throw new AssertionError();
87270114Sse        }
88270114Sse    }
89270114Sse}
90270114Sse
91270114Sse@interface IAm {
92270114Sse    Class<?> value();
93270114Sse}
94270114Sse