Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2e0aa609c9d219d1146c8b1ff35eaabab7a28ed7 (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*******************************************************************************
 * Copyright (c) 2006 BEA Systems, Inc. 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    sbandow@bea.com - initial API and implementation
 *    
 *******************************************************************************/

package org.eclipse.jdt.apt.tests;

import java.util.ArrayList;
import java.util.Collection;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.apt.tests.annotations.generic.AbstractGenericProcessor;
import org.eclipse.jdt.apt.tests.annotations.generic.GenericFactory;

import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
import com.sun.mirror.declaration.ClassDeclaration;
import com.sun.mirror.declaration.ConstructorDeclaration;
import com.sun.mirror.declaration.Declaration;
import com.sun.mirror.declaration.EnumConstantDeclaration;
import com.sun.mirror.declaration.EnumDeclaration;
import com.sun.mirror.declaration.ExecutableDeclaration;
import com.sun.mirror.declaration.FieldDeclaration;
import com.sun.mirror.declaration.InterfaceDeclaration;
import com.sun.mirror.declaration.MemberDeclaration;
import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.declaration.PackageDeclaration;
import com.sun.mirror.declaration.ParameterDeclaration;
import com.sun.mirror.declaration.TypeDeclaration;
import com.sun.mirror.declaration.TypeParameterDeclaration;
import com.sun.mirror.util.DeclarationVisitor;

/**
 * Tests for the JDT-APT implementation of Declaration Visitors
 */
public class DeclarationVisitorTests extends APTTestBase {
	
	public DeclarationVisitorTests(final String name) {
		super(name);
	}

	public static Test suite() {
		return new TestSuite(DeclarationVisitorTests.class);
	}
	
	public void testPackageDeclarationVisitor() {
		testCaseIdentifier = Cases.PackageDeclaration;
		runProcessorBasedTest();
	}

	public void testClassDeclarationVisitor() {
		testCaseIdentifier = Cases.ClassDeclaration;
		runProcessorBasedTest();
	}

	public void testEnumDeclarationVisitor() {
		testCaseIdentifier = Cases.EnumDeclaration;
		runProcessorBasedTest();
	}
	
	public void testInterfaceDeclarationVisitor() {
		testCaseIdentifier = Cases.InterfaceDeclaration;
		runProcessorBasedTest();
	}

	public void testAnnotationTypeDeclarationVisitor() {
		testCaseIdentifier = Cases.AnnotationTypeDeclaration;
		runProcessorBasedTest();
	}

	public void testFieldDeclarationVisitor() {
		testCaseIdentifier = Cases.FieldDeclaration;
		runProcessorBasedTest();
	}

	public void testEnumConstantDeclarationVisitor() {
		testCaseIdentifier = Cases.EnumConstantDeclaration;
		runProcessorBasedTest();
	}

	public void testConstructorDeclarationVisitor() {
		testCaseIdentifier = Cases.ConstructorDeclaration;
		runProcessorBasedTest();
	}

	public void testMethodDeclarationVisitor() {
		testCaseIdentifier = Cases.MethodDeclaration;
		runProcessorBasedTest();
	}

	public void testAnnotationTypeElementDeclarationVisitor() {
		testCaseIdentifier = Cases.AnnotationTypeElementDeclaration;
		runProcessorBasedTest();
	}

	public void testParameterDeclarationVisitor() {
		testCaseIdentifier = Cases.ParameterDeclaration;
		runProcessorBasedTest();
	}

	public void testTypeParameterDeclarationVisitor() {
		testCaseIdentifier = Cases.TypeParameterDeclaration;
		runProcessorBasedTest();
	}
	
	
	/**
	 * Instantiate the AnnotationProcessor to run the actual tests
	 */
	void runProcessorBasedTest() {
		DeclarationVisitorProc p = new DeclarationVisitorProc();
		GenericFactory.PROCESSOR = p;
		
		IProject project = env.getProject(getProjectName());
		IPath srcRoot = getSourcePath();

		env.addClass(srcRoot, "test", "Test", code);		
		
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		
		assertTrue("Processor not invoked", p.called);
	}

	
	/**
	 * Annotation Processor containing the actual tests
	 */
	class DeclarationVisitorProc extends AbstractGenericProcessor {
		boolean called;
		
		public void _process() {
		
			called = true;
			assertTrue(decls.size() == 1);
			
			initDeclVisitList();

			TypeDeclaration typeDecl = env.getTypeDeclarations().iterator().next();
			Collection<TypeDeclaration> nestedTypes = typeDecl.getNestedTypes();
			ClassDeclaration classDecl = null;
			EnumDeclaration enumDecl = null;
			InterfaceDeclaration interfaceDecl = null;
			AnnotationTypeDeclaration annoTypeDecl = null;
			EnumConstantDeclaration enumConstantDecl = null;
			MethodDeclaration methodDecl = null;
			
			switch (testCaseIdentifier) {

			case PackageDeclaration : 
				PackageDeclaration packageDecl = typeDecl.getPackage();
				packageDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected PackageDeclaration visitor", "PackageDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;

			case ClassDeclaration :
				for(TypeDeclaration tempDecl : nestedTypes) {
					if(tempDecl.getSimpleName().equals("C")) {
						classDecl = (ClassDeclaration)tempDecl;
					}
				}
				classDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected ClassDeclaration visitor", "ClassDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;
			
			case EnumDeclaration :
				for(TypeDeclaration tempDecl : nestedTypes) {
					if(tempDecl.getSimpleName().equals("E")) {
						enumDecl = (EnumDeclaration)tempDecl;
					}
				}
				enumDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected EnumDeclaration visitor", "EnumDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;
				
			case InterfaceDeclaration :
				for(TypeDeclaration tempDecl : nestedTypes) {
					if(tempDecl.getSimpleName().equals("I")) {
						interfaceDecl = (InterfaceDeclaration)tempDecl;
					}
				}
				interfaceDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected InterfaceDeclaration visitor", "InterfaceDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;
				
			case AnnotationTypeDeclaration :
				for(TypeDeclaration tempDecl : nestedTypes) {
					if(tempDecl.getSimpleName().equals("A")) {
						annoTypeDecl = (AnnotationTypeDeclaration)tempDecl;
					}
				}
				annoTypeDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected AnnotationTypeDeclaration visitor", "AnnotationDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;
				
			case FieldDeclaration :
				FieldDeclaration fieldDecl = typeDecl.getFields().iterator().next();
				fieldDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected FieldDeclaration visitor", "FieldDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;
				
			case EnumConstantDeclaration :
				for(TypeDeclaration tempDecl : nestedTypes) {
					if(tempDecl.getSimpleName().equals("E")) {
						enumDecl = (EnumDeclaration)tempDecl;
					}
				}
				enumConstantDecl = enumDecl.getEnumConstants().iterator().next();
				enumConstantDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected EnumConstantDeclaration visitor", "EnumConstantDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;
				
			case ConstructorDeclaration :
				for(TypeDeclaration tempDecl : nestedTypes) {
					if(tempDecl.getSimpleName().equals("C")) {
						classDecl = (ClassDeclaration)tempDecl;
					}
				}
				ConstructorDeclaration constructorDecl = classDecl.getConstructors().iterator().next();
				constructorDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected ConstructorDeclaration visitor", "ConstructorDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;
				
			case MethodDeclaration :
				methodDecl = typeDecl.getMethods().iterator().next();
				methodDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected MethodDeclaration visitor", "MethodDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;
				
			case AnnotationTypeElementDeclaration :
				for(TypeDeclaration tempDecl : nestedTypes) {
					if(tempDecl.getSimpleName().equals("A")) {
						annoTypeDecl = (AnnotationTypeDeclaration)tempDecl;
					}
				}
				AnnotationTypeElementDeclaration annoTypeElementDecl = annoTypeDecl.getMethods().iterator().next();
				annoTypeElementDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected AnnotationTypeElementDeclaration visitor", "AnnotationElementDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;
				
			case ParameterDeclaration :
				methodDecl = typeDecl.getMethods().iterator().next();
				ParameterDeclaration paramDecl = methodDecl.getParameters().iterator().next();
				paramDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected ParameterDeclaration visitor", "SourceParameterDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;
				
			case TypeParameterDeclaration :
				for(TypeDeclaration tempDecl : nestedTypes) {
					if(tempDecl.getSimpleName().equals("P")) {
						classDecl = (ClassDeclaration)tempDecl;
					}
				}
				TypeParameterDeclaration typeParamDecl = classDecl.getFormalTypeParameters().iterator().next();
				typeParamDecl.accept(new DeclarationVisitorImpl());
				assertEquals("Expect one visitor", 1, declarationsVisited.size());
				assertEquals("Expected TypeParameterDeclaration visitor", "TypeParameterDeclarationImpl", declarationsVisited.get(0).getClass().getSimpleName());
				break;
			}
		
		}
		
	}
	
	
	/**
	 * DeclarationVisitor implementation for the purposes of these tests
	 */
	class DeclarationVisitorImpl implements DeclarationVisitor {

		public void visitDeclaration(Declaration d) {
			fail("Should never visit a Declaration, only a subclass: " + d);
		}

		public void visitPackageDeclaration(PackageDeclaration d) {
			declarationVisited(d);
		}

		public void visitMemberDeclaration(MemberDeclaration d) {
			fail("Should never visit a Member, only a subclass: " + d);
		}

		public void visitTypeDeclaration(TypeDeclaration d) {
			fail("Should never visit a Type, only a subclass: " + d);
		}

		public void visitClassDeclaration(ClassDeclaration d) {
			declarationVisited(d);
		}

		public void visitEnumDeclaration(EnumDeclaration d) {
			declarationVisited(d);
		}

		public void visitInterfaceDeclaration(InterfaceDeclaration d) {
			declarationVisited(d);
		}

		public void visitAnnotationTypeDeclaration(AnnotationTypeDeclaration d) {
			declarationVisited(d);
		}

		public void visitFieldDeclaration(FieldDeclaration d) {
			declarationVisited(d);
		}

		public void visitEnumConstantDeclaration(EnumConstantDeclaration d) {
			declarationVisited(d);
		}

		public void visitExecutableDeclaration(ExecutableDeclaration d) {
			fail("Should never visit an ExecutableDeclaration, only a subclass: " + d);
		}

		public void visitConstructorDeclaration(ConstructorDeclaration d) {
			declarationVisited(d);
		}

		public void visitMethodDeclaration(MethodDeclaration d) {
			declarationVisited(d);
		}

		public void visitAnnotationTypeElementDeclaration(AnnotationTypeElementDeclaration d) {
			declarationVisited(d);
		}

		public void visitParameterDeclaration(ParameterDeclaration d) {
			declarationVisited(d);
		}

		public void visitTypeParameterDeclaration(TypeParameterDeclaration d) {
			declarationVisited(d);
		}
	}
	

	/*
	 * Utilities for running the DeclarationVisitor tests
	 */
	
	enum Cases {
		PackageDeclaration, 
		ClassDeclaration, 
		EnumDeclaration, 
		InterfaceDeclaration,
		AnnotationTypeDeclaration, 
		FieldDeclaration, 
		EnumConstantDeclaration, 
		ConstructorDeclaration,
		MethodDeclaration, 
		AnnotationTypeElementDeclaration, 
		ParameterDeclaration, 
		TypeParameterDeclaration
	}
	
	Cases testCaseIdentifier;
	
	ArrayList<Declaration> declarationsVisited = new ArrayList<Declaration>();
	
	void declarationVisited(Declaration d) {
			declarationsVisited.add(d);
	}
	
	void initDeclVisitList() {
		if(declarationsVisited.size() > 0) {
			declarationsVisited.clear();
		}
	}
	
	final String code =
		"package test;" + "\n" +
		"import org.eclipse.jdt.apt.tests.annotations.generic.*;" + "\n" +
		"@GenericAnnotation public class Test" + "\n" +
		"{" + "\n" +
		"    Test() {}" + "\n" +
		"    String s;" + "\n" +
		"    class C {}" + "\n" +
		"    class P<T> {}" + "\n" +
		"    interface I {}" + "\n" +
		"    void m(int i) {}" + "\n" +
		"    enum E { elephant }" + "\n" +
		"    @interface A { String strValue() default \"\"; }" + "\n" +
		"}";
}

Back to the top