01: /*******************************************************************************
02: * Copyright (c) 2007 BEA Systems, Inc.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * wharley@bea.com - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.apt.pluggable.tests.processors.message6;
11:
12: import java.util.Set;
13:
14: import javax.annotation.processing.AbstractProcessor;
15: import javax.annotation.processing.Messager;
16: import javax.annotation.processing.ProcessingEnvironment;
17: import javax.annotation.processing.RoundEnvironment;
18: import javax.annotation.processing.SupportedAnnotationTypes;
19: import javax.annotation.processing.SupportedOptions;
20: import javax.annotation.processing.SupportedSourceVersion;
21: import javax.lang.model.SourceVersion;
22: import javax.lang.model.element.Element;
23: import javax.lang.model.element.TypeElement;
24: import javax.tools.Diagnostic;
25:
26: import org.eclipse.jdt.apt.pluggable.tests.annotations.Message6;
27:
28: /**
29: * A processor that reads the Message6 annotation and sends output via the Messager API
30: */
31: @SupportedAnnotationTypes({"org.eclipse.jdt.apt.pluggable.tests.annotations.Message6"})
32: @SupportedSourceVersion(SourceVersion.RELEASE_6)
33: @SupportedOptions({})
34: public class Message6Proc extends AbstractProcessor {
35:
36: private ProcessingEnvironment _processingEnv;
37: private Messager _messager;
38:
39: /* (non-Javadoc)
40: * @see javax.annotation.processing.AbstractProcessor#init(javax.annotation.processing.ProcessingEnvironment)
41: */
42: @Override
43: public synchronized void init(ProcessingEnvironment processingEnv) {
44: super .init(processingEnv);
45: _processingEnv = processingEnv;
46: _messager = _processingEnv.getMessager();
47: }
48:
49: /* (non-Javadoc)
50: * @see javax.annotation.processing.AbstractProcessor#process(java.util.Set, javax.annotation.processing.RoundEnvironment)
51: */
52: @Override
53: public boolean process(Set<? extends TypeElement> annotations,
54: RoundEnvironment roundEnv) {
55: if (!annotations.isEmpty()) {
56: round(annotations, roundEnv);
57: }
58: return true;
59: }
60:
61: /**
62: * Perform a round of processing
63: */
64: private void round(Set<? extends TypeElement> annotations,
65: RoundEnvironment roundEnv) {
66: TypeElement genClassAnno = annotations.iterator().next();
67: Set<? extends Element> annotatedEls = roundEnv
68: .getElementsAnnotatedWith(genClassAnno);
69: for (Element annotatedEl : annotatedEls) {
70: Message6 messageMirror = annotatedEl
71: .getAnnotation(Message6.class);
72: Diagnostic.Kind kind = Diagnostic.Kind.OTHER;
73: String text = null;
74: try {
75: kind = messageMirror.value();
76: text = messageMirror.text();
77: } catch (Exception e) {
78: // Do nothing: compiler will have put up a syntax error on the annotation already
79: return;
80: }
81: if (kind != Diagnostic.Kind.OTHER) {
82: _messager.printMessage(kind, text, annotatedEl);
83: }
84: }
85: }
86:
87: }
|