Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e770897978e6b275ba58f58c3cfda152052e0130 (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
/*******************************************************************************
 * Copyright (c) 2005, 2012 BEA Systems, Inc. and others
 *
 * 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:
 *    sbandow@bea.com - initial API and implementation
 *    IBM Corporation - Added test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=352949
 *******************************************************************************/

package org.eclipse.jdt.apt.tests;

import java.util.Collection;
import java.util.Map;

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.core.env.EnvironmentFactory;
import org.eclipse.jdt.apt.tests.annotations.ProcessorTestStatus;
import org.eclipse.jdt.apt.tests.annotations.generic.AbstractGenericProcessor;
import org.eclipse.jdt.apt.tests.annotations.generic.GenericFactory;
import org.eclipse.jdt.apt.tests.annotations.mirrortest.MirrorDeclarationCodeExample;
import org.eclipse.jdt.apt.tests.annotations.mirrortest.SourceMirrorCodeExample;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.declaration.AnnotationMirror;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
import com.sun.mirror.declaration.AnnotationValue;
import com.sun.mirror.declaration.Declaration;
import com.sun.mirror.declaration.TypeDeclaration;
import com.sun.mirror.util.SourcePosition;

public class MirrorDeclarationTests extends APTTestBase {

	public MirrorDeclarationTests(final String name)
	{
		super(name);
	}

	public static Test suite()
	{
		return new TestSuite(MirrorDeclarationTests.class);
	}
	
	public void setUp() throws Exception
	{
		super.setUp();
		
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		String code = MirrorDeclarationCodeExample.CODE;
		env.addClass(srcRoot, MirrorDeclarationCodeExample.CODE_PACKAGE, MirrorDeclarationCodeExample.CODE_CLASS_NAME, code);
		fullBuild( project.getFullPath() );
		expectingNoProblems();
	}
	
	public void testMirrorDeclaration() throws Exception
	{
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}
	
	public void testFieldConstant() throws Exception 
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		String codeTrigger =
			"package test;\n" +
			"public @interface Trigger{}";
		
		env.addClass(srcRoot, "test", "Trigger", codeTrigger);
		
		String codeEntryPoint = "package test;\n" +
								"@Trigger\n" +
								"public class EntryPoint {\n" +
								"    ClassWithNestedAnnotation nestedAnno;\n}";
		
		env.addClass(srcRoot, "test", "EntryPoint", codeEntryPoint);

		String codeClassWithNestedAnnotation = 
			"package test; \n" +
			"public class ClassWithNestedAnnotation {\n" +
			"	public final int FOUR = 4; \n " +
			"}";
		
		env.addClass(srcRoot, "test", "ClassWithNestedAnnotation", codeClassWithNestedAnnotation);
		fullBuild( project.getFullPath() );
		expectingNoProblems();
	}
	
	// TODO: Disabled due to Bugzilla 124388 -theodora
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=124388
	public void DISABLED_testDefault() throws Exception
	{	
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		String codeTrigger =
			"package test;\n" +
			"public @interface Trigger{}";
		
		env.addClass(srcRoot, "test", "Trigger", codeTrigger);
		
		String codeEntryPoint = "package test;\n" +
								"@Trigger\n" +
								"public class EntryPoint {\n" +
								"    ClassWithNestedAnnotation nestedAnno;\n}";
		
		env.addClass(srcRoot, "test", "EntryPoint", codeEntryPoint);	
		
		String codeClassWithNestedAnnotation = 
			"package test; \n" +
			"public class ClassWithNestedAnnotation {\n" +
			"	public @interface NestedAnnotation{\n" +
			"		public enum Character{ \n" +
			"			Winnie, Tiger, Piglet, Eore; \n" +
			"		}\n"+
			"		Character value() default Character.Eore; \n" +
			"	}\n" +
			"}";
		
		env.addClass(srcRoot, "test", "ClassWithNestedAnnotation", codeClassWithNestedAnnotation);
		fullBuild( project.getFullPath() );
		expectingNoProblems();
	}
	
	/**
	 * Test AST based mirror implementation and binding based mirror implementation.
	 * Specifically,
	 *   (i) method declaration with unresolvable return type.
	 *  (ii) constructor declaration with unresolvable parameter
	 * (iii) field declaration with unresolvable type.
	 * 
	 * This test focus on declarations from file in context.
	 * 
	 * @throws Exception
	 */
	public void testUnresolvableDeclarations0()
		throws Exception 
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		String declAnno =
			"package test;\n" +
			"public @interface DeclarationAnno{}";
		
		env.addClass(srcRoot, "test", "DeclarationAnno", declAnno);
		
		String codeFoo = 
			"package test;\n" +
			"@DeclarationAnno\n" +
			"public class Foo {\n" +
			"    int field0;\n " +
			"    UnknownType field1;\n " +
			"    public Foo(UnknownType type){} \n" +
			"    public void voidMethod(){} \n " +
			"    public UnknownType getType(){}\n " +
			"    public class Inner{} \n" +
			"}";
		
		final IPath fooPath = env.addClass(srcRoot, "test", "Foo", codeFoo);
		fullBuild( project.getFullPath() );
		
		expectingOnlySpecificProblemsFor(fooPath, new ExpectedProblem[]{
				new ExpectedProblem("", "UnknownType cannot be resolved to a type", fooPath),
				new ExpectedProblem("", "UnknownType cannot be resolved to a type", fooPath),
				new ExpectedProblem("", "UnknownType cannot be resolved to a type", fooPath)}
		);
	}
	
	/**
	 * Test AST based mirror implementation and binding based mirror implementation.
	 * Specifically,
	 *   (i) method declaration with unresolvable return type.
	 *  (ii) constructor declaration with unresolvable parameter
	 * (iii) field declaration with unresolvable type.
	 * 
	 * This test focus on declarations from file outside of processor
	 * environment context.
	 * 
	 * @throws Exception
	 */
	public void testUnresolvableDeclarations1()
		throws Exception 
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		String declAnno =
			"package test;\n" +
			"public @interface DeclarationAnno{}";
		
		env.addClass(srcRoot, "test", "DeclarationAnno", declAnno);		
		
		String codeBar = 
			"package test;\n" +
			"@DeclarationAnno\n" +
			"public class Bar {}";		
		env.addClass(srcRoot, "test", "Bar", codeBar);
		
		String codeFoo = 
			"package test;\n" +
			"@DeclarationAnno\n" +
			"public class Foo {\n" +
			"    int field0;\n " +
			"    UnknownType field1;\n " +
			"    public Foo(UnknownType type){} \n" +
			"    public void voidMethod(){} \n " +
			"    public UnknownType getType(){}\n " +
			"    public class Inner{} \n" +
			"}";
		
		final IPath fooPath = env.addClass(srcRoot, "test", "Foo", codeFoo);
		
		fullBuild( project.getFullPath() );
		expectingOnlySpecificProblemsFor(fooPath, new ExpectedProblem[]{
				new ExpectedProblem("", "UnknownType cannot be resolved to a type", fooPath),
				new ExpectedProblem("", "UnknownType cannot be resolved to a type", fooPath),
				new ExpectedProblem("", "UnknownType cannot be resolved to a type", fooPath)}
		);
	}
	
	public void testLocation() {

		TestLocationProc p = new TestLocationProc();
		GenericFactory.setProcessor(p);
		
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		String x =
			"package test;\n" +
			"import org.eclipse.jdt.apt.tests.annotations.generic.*;\n" +
			"@GenericAnnotation public class X {}";
		
		env.addClass(srcRoot, "test", "X", x);		
		
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		
		assertTrue("Processor not invoked", p.called);
	}
	
	public void testSourceMirror() {
		
		TestSourceMirrorProc p = new TestSourceMirrorProc();
		GenericFactory.setProcessor(p);
		
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		env.addClass(srcRoot, SourceMirrorCodeExample.CODE_PACKAGE, SourceMirrorCodeExample.ANNO_CODE_CLASS_NAME, SourceMirrorCodeExample.ANNO_CODE);
		env.addClass(srcRoot, SourceMirrorCodeExample.CODE_PACKAGE, SourceMirrorCodeExample.CODE_CLASS_NAME, SourceMirrorCodeExample.CODE);
		
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		
		assertTrue("Processor not invoked", p.called);
	}
	
	public void testPackageInfo() {
		PackageInfoProc p = new PackageInfoProc(); 
		GenericFactory.setProcessor(p);
		
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		IPath pkg = env.addPackage(srcRoot, "pkg");
		String contents = "@PkgAnnotation\n" +
						  "@GenericAnnotation\n" +
						  "package pkg;\n" +
						  "import org.eclipse.jdt.apt.tests.annotations.generic.*;";
		env.addFile(pkg, "package-info.java", contents);
		String annContents =
				"package pkg;\n" +
				"\n" +
				"@interface PkgAnnotation {\n" +
				"    String value() default \"def\";\n" +
				"}\n";
		env.addClass(srcRoot, "pkg", "pkgAnnotation", annContents);
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		
		assertTrue("Processor not invoked", p.called);
	}
	
	static class TestLocationProc extends AbstractGenericProcessor {

		boolean called;
		
		public void _process() {
			called = true;
			assertTrue(decls.size() == 1);
			
			Declaration d = decls.iterator().next();
			SourcePosition p = d.getPosition();

			assertTrue(p.column() == 32);
			assertTrue(p.line() == 3);
		}
	}
	
	static class TestSourceMirrorProc extends AbstractGenericProcessor {
		
		boolean called;
		
		public void _process() {
			called = true;
			TypeDeclaration tdCode = env.getTypeDeclaration(SourceMirrorCodeExample.CODE_PACKAGE + "." + SourceMirrorCodeExample.CODE_CLASS_NAME);
			for (AnnotationMirror am : tdCode.getAnnotationMirrors()) {
				if ("GenericAnnotation".equals(am.getAnnotationType().getDeclaration().getSimpleName())) {
					continue;
				}
				assertTrue(null != am.getPosition());
				AnnotationTypeDeclaration atd = am.getAnnotationType().getDeclaration();
				assertTrue(null != atd.getPosition());
				for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry : am.getElementValues().entrySet()) {
					assertNotNull(entry.getKey().getPosition());
					assertNotNull(entry.getKey().getDefaultValue().getPosition());
				}
			}
		}
	}
	
	static class PackageInfoProc extends AbstractGenericProcessor {
		
		boolean called;
		
		public void _process() {
			called = true;
			AnnotationTypeDeclaration annoDecl = (AnnotationTypeDeclaration)env.getTypeDeclaration("pkg.PkgAnnotation");
			assertTrue(null != annoDecl);			
			// get the annotated declarations
			Collection<Declaration> annotatedDecls = env.getDeclarationsAnnotatedWith(annoDecl);
			// don't return the package declaration - well, apt is doing that.. 
			assertTrue(annotatedDecls == null  || annotatedDecls.size() == 0); 
			
			//TODO: test to support package annotation
			// PackageDeclaration pdCode = env.getPackage("pkg");
			// for (AnnotationMirror am : pdCode.getAnnotationMirrors()) {
			//	if ("GenericAnnotation".equals(am.getAnnotationType().getDeclaration().getSimpleName())) {
			//		continue;
			//	}
			//	assertTrue(null != am.getPosition());
			//	AnnotationTypeDeclaration atd = am.getAnnotationType().getDeclaration();
			//	assertTrue(null != atd.getPosition());
			//	for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry : am.getElementValues().entrySet()) {
			//		assertNotNull(entry.getKey().getPosition());
			//		assertNotNull(entry.getKey().getDefaultValue().getPosition());
			//	}
			// }
		}
	}
	
	public void testEnvFactory() throws JavaModelException {

		IProject project = env.getProject(getProjectName());
		IPath srcRoot = getSourcePath();
		String x = "package test;\n" + "import org.eclipse.jdt.apt.tests.annotations.generic.*;\n"
				+ "@GenericAnnotation public class X {}";

		IPath path = env.addClass(srcRoot, "test", "X", x);
		IPath tail = path.removeFirstSegments(2);
		IJavaProject p = JavaCore.create(project);
		ICompilationUnit cu = (ICompilationUnit) p.findElement(tail);
		assertTrue("Could not find cu", cu != null);
		
		AnnotationProcessorEnvironment env = EnvironmentFactory.getEnvironment(cu, p);
		TypeDeclaration t = env.getTypeDeclaration("test.X");

		SourcePosition pos = t.getPosition();
		
		assertTrue(pos.column() == 32);
		assertTrue(pos.line() == 3);
		
		// Bad type should gracefully return null
		TypeDeclaration tBad = env.getTypeDeclaration("test.XYZ");
		assertTrue(null == tBad);
	}

}

Back to the top