Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ff453d53c54fd5ba7158b2b9eba70aaf3f48de07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*******************************************************************************
 * Copyright (c) 2007, 2018 BEA Systems, Inc.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    wharley@bea.com - initial API and implementation
 *******************************************************************************/

package org.eclipse.jdt.compiler.apt.tests.processors.visitors;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.AbstractAnnotationValueVisitor6;
import javax.lang.model.util.ElementScanner6;

import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor;

/**
 * Processor that tests a variety of Visitors
 */
@SupportedAnnotationTypes({"*"})
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class VisitorProc extends BaseProcessor
{
	/**
	 * This visitor is invoked on the top-level types in resources/targets/model.
	 * We expect to see each of the visitX() methods get hit as a result.
	 */
	@SuppressWarnings("deprecation")
	private static class ElementVisitorTester extends ElementScanner6<Void, Void> {
		
		public enum Visited { TYPE, EXECUTABLE, VARIABLE, TYPEPARAM, PACKAGE, UNKNOWN }
		
		private EnumSet<Visited> _visited = EnumSet.noneOf(Visited.class);
		
		public boolean checkVisits() {
			boolean asExpected = true;
			asExpected &= _visited.contains(Visited.TYPE);
			asExpected &= _visited.contains(Visited.EXECUTABLE);
			asExpected &= _visited.contains(Visited.VARIABLE);
			// TODO: Following two cases not yet implemented:
			//asExpected &= _visited.contains(Visited.TYPEPARAM);
			//asExpected &= _visited.contains(Visited.PACKAGE);
			return asExpected;
		}
		
        /**
         * Check that we can visit types.
         * @return true if all tests passed
         */
        @Override
        public Void visitType(TypeElement e, Void p) {
        	_visited.add(Visited.TYPE);
        	// Scan the type's subtypes, fields, and methods
            return super.visitType(e, p);
        }
        
        /**
         * Check that we can visit methods.
         */
        @Override
        public Void visitExecutable(ExecutableElement e, Void p) {
        	_visited.add(Visited.EXECUTABLE);
        	// Scan the method's parameters
            return super.visitExecutable(e, p);
        }
        
        /**
         * Check that we can visit variables.
         */
        @Override
        public Void visitVariable(VariableElement e, Void p) {
        	_visited.add(Visited.VARIABLE);
            // Variables do not enclose any elements, so no need to call super.
        	return null;
        }

        /**
         * Check that we can visit type parameters.
         */
        @Override
        public Void visitTypeParameter(TypeParameterElement e, Void p) {
        	_visited.add(Visited.TYPEPARAM);
            // Type parameters do not enclose any elements, so no need to call super.
        	return null;
        }
        
        /**
         * Check that we can visit packages.
         */
        @Override
        public Void visitPackage(PackageElement e, Void p) {
        	_visited.add(Visited.PACKAGE);
            // We don't want to scan the package's types here, so don't call super.
        	return null;
        }
        
        /**
         * This should not actually be encountered.
         */
        @Override
        public Void visitUnknown(Element e, Void p) {
        	_visited.add(Visited.UNKNOWN);
        	return null;
        }
        
	}
	
	/*
	 * The specific values checked by this visitor correspond to values in targets.model.pc.TypedAnnos.java
	 */
	@SuppressWarnings("deprecation")
	private static class AnnotationVisitorTester extends AbstractAnnotationValueVisitor6<Void, Void> {

		public enum Visited { ANNOTATION, ARRAY, BOOLEAN, BYTE, CHAR, DOUBLE, ENUMCONSTANT, FLOAT, INT, LONG, SHORT, STRING, TYPE }
		
		private EnumSet<Visited> _visited = EnumSet.noneOf(Visited.class);
		
		public boolean checkVisits() {
			boolean asExpected = true;
			asExpected &= _visited.contains(Visited.ANNOTATION);
			asExpected &= _visited.contains(Visited.ARRAY);
			asExpected &= _visited.contains(Visited.BOOLEAN);
			asExpected &= _visited.contains(Visited.BYTE);
			asExpected &= _visited.contains(Visited.CHAR);
			asExpected &= _visited.contains(Visited.DOUBLE);
			asExpected &= _visited.contains(Visited.ENUMCONSTANT);
			asExpected &= _visited.contains(Visited.FLOAT);
			asExpected &= _visited.contains(Visited.INT);
			asExpected &= _visited.contains(Visited.LONG);
			asExpected &= _visited.contains(Visited.SHORT);
			asExpected &= _visited.contains(Visited.STRING);
			asExpected &= _visited.contains(Visited.TYPE);
			return asExpected;
		}
		
		@Override
		public Void visitAnnotation(AnnotationMirror a, Void p)
		{
			if (a != null && a.getElementValues() != null) {
				_visited.add(Visited.ANNOTATION);
			}
			// we could scan the values of the nested annotation here, but that doesn't help our test case
			return null;
		}

		@Override
		public Void visitArray(List<? extends AnnotationValue> vals, Void p)
		{
			if ( null != vals && vals.size() == 2 ) {
				if ( vals.iterator().next().getValue() instanceof TypeMirror) {
					_visited.add(Visited.ARRAY);
				}
			}
			// we could scan the array values here, but that doesn't help our test case
			return null;
		}

		@Override
		public Void visitBoolean(boolean b, Void p)
		{
			if (b) {
				_visited.add(Visited.BOOLEAN);
			}
			return null;
		}

		@Override
		public Void visitByte(byte b, Void p)
		{
			if (b == 3) {
				_visited.add(Visited.BYTE);
			}
			return null;
		}

		@Override
		public Void visitChar(char c, Void p)
		{
			if (c == 'c') { 
				_visited.add(Visited.CHAR);
			}
			return null;
		}

		@Override
		public Void visitDouble(double d, Void p)
		{
			if (d == 6.3) {
				_visited.add(Visited.DOUBLE);
			}
			return null;
		}

		@Override
		public Void visitEnumConstant(VariableElement c, Void p)
		{
			if (c.getKind() == ElementKind.ENUM_CONSTANT) {
				if ("A".equals(c.getSimpleName().toString())) {
					_visited.add(Visited.ENUMCONSTANT);
				}
			}
			return null;
		}

		@Override
		public Void visitFloat(float f, Void p)
		{
			if (f == 26.7F) {
				_visited.add(Visited.FLOAT);
			}
			return null;
		}

		@Override
		public Void visitInt(int i, Void p)
		{
			if (i == 19) {
				_visited.add(Visited.INT);
			}
			return null;
		}

		@Override
		public Void visitLong(long i, Void p)
		{
			if (i == 300L) {
				_visited.add(Visited.LONG);
			}
			return null;
		}

		@Override
		public Void visitShort(short s, Void p)
		{
			if (s == 289) {
				_visited.add(Visited.SHORT);
			}
			return null;
		}

		@Override
		public Void visitString(String s, Void p)
		{
			if ("foo".equals(s)) {
				_visited.add(Visited.STRING);
			}
			return null;
		}

		@Override
		public Void visitType(TypeMirror t, Void p)
		{
			if ("java.lang.Exception".equals(t.toString())) {
				_visited.add(Visited.TYPE);
			}
			return null;
		}
		
	}
	
	@Override
	public synchronized void init(ProcessingEnvironment processingEnv)
	{
		super.init(processingEnv);
	}

	@Override
	public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
	{
		if (roundEnv.processingOver()) {
			return false;
		}
		Map<String, String> options = processingEnv.getOptions();
		if (!options.containsKey(this.getClass().getName())) {
			// Disable this processor unless we are intentionally performing the test.
			return false;
		}
		ElementVisitorTester elementVisitor = new ElementVisitorTester();
		elementVisitor.scan(roundEnv.getRootElements(), null);
		if (!elementVisitor.checkVisits()) {
			reportError("Element visitor was not visited as expected");
			return false;
		}
		
		AnnotationVisitorTester annoValVisitor = new AnnotationVisitorTester();
		TypeElement typedAnnosDecl = _elementUtils.getTypeElement("org.eclipse.jdt.compiler.apt.tests.annotations.TypedAnnos");
		if (null == typedAnnosDecl) {
			reportError("Couldn't find targets.model.pc.AnnotatedWithManyTypes");
			return false;
		}
		for (TypeElement anno : annotations) {
			if (typedAnnosDecl.equals(anno.getEnclosingElement())) {
				for (Element elem : roundEnv.getElementsAnnotatedWith(anno)) {
					for (AnnotationMirror annoMirror : elem.getAnnotationMirrors()) {
						if (anno.equals(annoMirror.getAnnotationType().asElement())) {
							Map<? extends ExecutableElement, ? extends AnnotationValue> values = annoMirror.getElementValues();
							for (AnnotationValue val : values.values()) {
								val.accept(annoValVisitor, null);
							}
						}
					}
				}
			}
		}
		if (!annoValVisitor.checkVisits()) {
			reportError("Annotation value visitor was not visited as expected");
			return false;
		}

		reportSuccess();
		return false;
	}

}

Back to the top