View Javadoc
1   /*
2    * Copyright (C) 2016 uwe
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.sw4j.tool.annotation.jpa.test.mock.annotation.processing;
18  
19  import java.util.Collections;
20  import java.util.LinkedList;
21  import java.util.List;
22  import javax.annotation.processing.Messager;
23  import javax.lang.model.element.AnnotationMirror;
24  import javax.lang.model.element.AnnotationValue;
25  import javax.lang.model.element.Element;
26  import javax.tools.Diagnostic;
27  
28  /**
29   * A Mock of the Messager for annotation processors.
30   *
31   * @author Uwe Plonus
32   */
33  public class MessagerMock implements Messager {
34  
35      private final List<Message> messages;
36  
37      public MessagerMock() {
38          messages = new LinkedList<>();
39      }
40  
41      public List<Message> getMessages() {
42          return Collections.unmodifiableList(messages);
43      }
44  
45      @Override
46      public void printMessage(final Diagnostic.Kind kind, final CharSequence msg) {
47          messages.add(new Message(kind, msg));
48      }
49  
50      @Override
51      public void printMessage(final Diagnostic.Kind kind, final CharSequence msg, final Element e) {
52          messages.add(new Message(kind, msg, e));
53      }
54  
55      @Override
56      public void printMessage(final Diagnostic.Kind kind, final CharSequence msg, final Element e,
57              final AnnotationMirror a) {
58          messages.add(new Message(kind, msg, e, a));
59      }
60  
61      @Override
62      public void printMessage(final Diagnostic.Kind kind, final CharSequence msg, final Element e,
63              final AnnotationMirror a, final AnnotationValue v) {
64          messages.add(new Message(kind, msg, e, a, v));
65      }
66  
67  
68      public static class Message {
69  
70          private final Diagnostic.Kind kind;
71  
72          private final CharSequence msg;
73  
74          private final Element e;
75  
76          private final AnnotationMirror a;
77  
78          private final AnnotationValue v;
79  
80          public Message(final Diagnostic.Kind kind, final CharSequence msg) {
81              this.kind = kind;
82              this.msg = msg;
83              this.e = null;
84              this.a = null;
85              this.v = null;
86          }
87  
88          public Message(final Diagnostic.Kind kind, final CharSequence msg, final Element e) {
89              this.kind = kind;
90              this.msg = msg;
91              this.e = e;
92              this.a = null;
93              this.v = null;
94          }
95  
96          public Message(final Diagnostic.Kind kind, final CharSequence msg, final Element e, final AnnotationMirror a) {
97              this.kind = kind;
98              this.msg = msg;
99              this.e = e;
100             this.a = a;
101             this.v = null;
102         }
103 
104         public Message(final Diagnostic.Kind kind, final CharSequence msg, final Element e, final AnnotationMirror a,
105                 final AnnotationValue v) {
106             this.kind = kind;
107             this.msg = msg;
108             this.e = e;
109             this.a = a;
110             this.v = v;
111         }
112 
113         public Diagnostic.Kind getKind() {
114             return kind;
115         }
116 
117         public CharSequence getMsg() {
118             return msg;
119         }
120 
121         public Element getE() {
122             return e;
123         }
124 
125         public AnnotationMirror getA() {
126             return a;
127         }
128 
129         public AnnotationValue getV() {
130             return v;
131         }
132 
133     }
134 
135 }