Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b2339363df5fbd6035af4229a3a437541c27365a (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
/*******************************************************************************
 * Copyright (c) 2005, 2013 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:
 *    tyeung@bea.com - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.apt.tests.annotations.readannotation;

import java.util.Collection;
import java.util.HashSet;

import junit.framework.ComparisonFailure;

import org.eclipse.jdt.apt.tests.annotations.BaseProcessor;
import org.eclipse.jdt.apt.tests.annotations.ProcessorTestStatus;
import org.eclipse.jdt.apt.tests.annotations.ProcessorUtil;

import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.declaration.AnnotationMirror;
import com.sun.mirror.declaration.FieldDeclaration;
import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.declaration.ParameterDeclaration;
import com.sun.mirror.declaration.TypeDeclaration;

public class ReadAnnotationProcessor extends BaseProcessor
{
	private static final String[] NO_ANNOTATIONS = new String[0];

	public ReadAnnotationProcessor(AnnotationProcessorEnvironment env)
	{
		super(env);
	}

	public void process()
	{
		ProcessorTestStatus.setProcessorRan();
		try{
			TypeDeclaration typeDecl = _env.getTypeDeclaration("question.AnnotationTest");
			junit.framework.TestCase.assertNotNull("failed to locate type 'question.AnnotationTest'", typeDecl);
			if( typeDecl != null){
				junit.framework.TestCase.assertEquals("Type name mismatch", "question.AnnotationTest", typeDecl.getQualifiedName());

				final String[] expectedPkgAnnos = new String[]{ "@Deprecated()" };
				assertAnnotation(expectedPkgAnnos, typeDecl.getPackage().getAnnotationMirrors() );
				assertAnnotation(expectedPkgAnnos, _env.getPackage("question").getAnnotationMirrors() );

				final String[] expectedNoTypeAnnos = new String[]{ "@SimpleAnnotation(value = foo)" };
				assertAnnotation(expectedNoTypeAnnos, _env.getPackage("notypes").getAnnotationMirrors());

				final String[] expectedTypeAnnos = new String[]{ "@Deprecated()",
														  	     "@RTVisibleAnno(anno = @SimpleAnnotation(value = test), clazzes = {})",
															     "@RTInvisibleAnno(value = question)" };
				assertAnnotation(expectedTypeAnnos, typeDecl.getAnnotationMirrors());

				final Collection<FieldDeclaration> fieldDecls = typeDecl.getFields();

				int counter = 0;
				junit.framework.TestCase.assertEquals(5, fieldDecls.size());
				for(FieldDeclaration fieldDecl : fieldDecls ){
					final String name = "field" + counter;

					junit.framework.TestCase.assertEquals("field name mismatch", name, fieldDecl.getSimpleName());
					final String[] expected;
					switch(counter){
					case 0:
						expected = new String[] { "@RTVisibleAnno(name = Foundation, boolValue = false, byteValue = 16, charValue = c, doubleValue = 99.0, floatValue = 9.0, intValue = 999, longValue = 3333, shortValue = 3, colors = {RED, BLUE}, anno = @SimpleAnnotation(value = core), simpleAnnos = {@SimpleAnnotation(value = org), @SimpleAnnotation(value = eclipse), @SimpleAnnotation(value = jdt)}, clazzes = {java.lang.Object, java.lang.String}, clazz = java.lang.Object)",
										          "@RTInvisibleAnno(value = org.eclipse.jdt.core)",
										          "@Deprecated()" };
						break;
					case 1:
						expected = new String[] { "@Deprecated()" };
						break;
					case 2:
						expected = new String[] { "@RTVisibleAnno(anno = @SimpleAnnotation(value = field), clazzes = {})",
												  "@RTInvisibleAnno(value = 2)" };
						break;
					case 3:
						expected = new String[] { "@RTInvisibleAnno(value = 3)" };
						break;
					case 4:
						expected = new String[] { "@SimpleAnnotation(value = 4)" };
						break;
					default:
						expected = NO_ANNOTATIONS;
					}

					assertAnnotation(expected, fieldDecl.getAnnotationMirrors());
					counter ++;
				}

				final Collection<? extends MethodDeclaration> methodDecls = typeDecl.getMethods();
				counter = 0;
				junit.framework.TestCase.assertEquals(8, methodDecls.size());
				for(MethodDeclaration methodDecl : methodDecls ){
					final String name = "method" + counter;

					junit.framework.TestCase.assertEquals("method name mismatch", name, methodDecl.getSimpleName());
					final String[] expected;
					switch(counter)
					{
					case 0:
						expected = new String[] { "@RTVisibleAnno(anno = @SimpleAnnotation(value = method0), clazzes = {})",
							                      "@RTInvisibleAnno(value = 0)",
							                      "@Deprecated()" };
						break;
					case 1:
						expected = new String[] { "@Deprecated()" };
						break;
					case 2:
						expected = new String[] { "@RTVisibleAnno(anno = @SimpleAnnotation(value = method2), clazzes = {})",
												  "@RTInvisibleAnno(value = 2)" };
						break;
					case 3:
						expected = new String[] { "@RTInvisibleAnno(value = 3)" };
						break;
					case 4:
						expected = new String[] { "@SimpleAnnotation(value = method4)" };
						break;
					case 5:
					case 6:
						expected = NO_ANNOTATIONS;
						break;
					case 7:
						expected = new String[] { "@RTVisibleAnno(name = I'm \"special\": 	\\\n, charValue = ', clazzes = {}, anno = @SimpleAnnotation(value = ))" };
						break;
					default:
						expected = NO_ANNOTATIONS;
					}

					assertAnnotation(expected, methodDecl.getAnnotationMirrors());

					if( counter == 5 ){
						Collection<ParameterDeclaration> paramDecls = methodDecl.getParameters();
						int pCounter = 0;
						for( ParameterDeclaration paramDecl : paramDecls ){
							final String[] expectedParamAnnotations;
							switch( pCounter )
							{
							case 1:
								expectedParamAnnotations = new String[] { "@Deprecated()" };
								break;
							case 2:
								expectedParamAnnotations = new String[] { "@RTVisibleAnno(anno = @SimpleAnnotation(value = param2), clazzes = {})",
																	      "@RTInvisibleAnno(value = 2)" };
								break;
							default:
								expectedParamAnnotations = NO_ANNOTATIONS;
							}
							assertAnnotation(expectedParamAnnotations, paramDecl.getAnnotationMirrors());
							pCounter ++;
						}

					}
					counter ++;
				}
			}
		}
		catch( ComparisonFailure failure ){
			if (!ProcessorTestStatus.hasErrors()) {
				ProcessorTestStatus.failWithoutException(failure.toString());
			}
			throw failure;
		}
		catch( junit.framework.AssertionFailedError error ){
			if (!ProcessorTestStatus.hasErrors()) {
				ProcessorTestStatus.failWithoutException(error.toString());
			}
			throw error;
		}
	}

	private void assertAnnotation(final String[] expected, Collection<AnnotationMirror> annotations)
	{
		final int expectedLen = expected.length;
		junit.framework.TestCase.assertEquals("annotation number mismatch", expected.length, annotations.size()); //$NON-NLS-1$

		final HashSet<String> expectedSet = new HashSet<String>(expectedLen * 4 / 3 + 1);
		for( int i=0; i<expectedLen; i++ )
			expectedSet.add(expected[i]);

		int counter = 0;
		for( AnnotationMirror mirror : annotations ){
			String mirrorString = ProcessorUtil.annoMirrorToString(mirror);
			if( counter >= expectedLen )
				junit.framework.TestCase.assertEquals("", mirrorString); //$NON-NLS-1$
			else{
				final boolean contains = expectedSet.contains(mirrorString);
				if( !contains ){
					System.err.println("found unexpected: " + mirrorString);
					System.err.println("expected set: " + expectedSet);
				}
				junit.framework.TestCase.assertTrue("unexpected annotation " + mirrorString, contains); //$NON-NLS-1$
				expectedSet.remove(mirrorString);
			}
			counter ++;
		}
	}
}

Back to the top