001: /*******************************************************************************
002: * Copyright (c) 2005 BEA Systems, Inc.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * tyeung@bea.com - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.apt.tests.annotations.readannotation;
011:
012: import java.util.Collection;
013: import java.util.HashSet;
014:
015: import junit.framework.ComparisonFailure;
016: import junit.framework.TestCase;
017:
018: import org.eclipse.jdt.apt.tests.annotations.BaseProcessor;
019: import org.eclipse.jdt.apt.tests.annotations.ProcessorTestStatus;
020:
021: import com.sun.mirror.apt.AnnotationProcessorEnvironment;
022: import com.sun.mirror.declaration.AnnotationMirror;
023: import com.sun.mirror.declaration.FieldDeclaration;
024: import com.sun.mirror.declaration.MethodDeclaration;
025: import com.sun.mirror.declaration.ParameterDeclaration;
026: import com.sun.mirror.declaration.TypeDeclaration;
027:
028: public class ReadAnnotationProcessor extends BaseProcessor {
029: private static final String[] NO_ANNOTATIONS = new String[0];
030:
031: public ReadAnnotationProcessor(AnnotationProcessorEnvironment env) {
032: super (env);
033: }
034:
035: @SuppressWarnings("nls")
036: public void process() {
037: try {
038: TypeDeclaration typeDecl = _env
039: .getTypeDeclaration("question.AnnotationTest");
040: TestCase.assertNotNull(
041: "failed to locate type 'question.AnnotationTest'",
042: typeDecl);
043: if (typeDecl != null) {
044: TestCase.assertEquals("Type name mismatch",
045: "question.AnnotationTest", typeDecl
046: .getQualifiedName());
047:
048: final String[] expectedPkgAnnos = new String[] { "@Deprecated()" };
049: assertAnnotation(expectedPkgAnnos, typeDecl
050: .getPackage().getAnnotationMirrors());
051:
052: final String[] expectedTypeAnnos = new String[] {
053: "@Deprecated()",
054: "@RTVisibleAnno(anno = @SimpleAnnotation(value = test), clazzes = {})",
055: "@RTInvisibleAnno(value = question)" };
056: assertAnnotation(expectedTypeAnnos, typeDecl
057: .getAnnotationMirrors());
058:
059: final Collection<FieldDeclaration> fieldDecls = typeDecl
060: .getFields();
061:
062: int counter = 0;
063: TestCase.assertEquals(5, fieldDecls.size());
064: for (FieldDeclaration fieldDecl : fieldDecls) {
065: final String name = "field" + counter;
066:
067: TestCase.assertEquals("field name mismatch", name,
068: fieldDecl.getSimpleName());
069: final String[] expected;
070: switch (counter) {
071: case 0:
072: expected = new String[] {
073: "@RTVisibleAnno(name = Foundation, boolValue = false, byteValue = 16, charValue = c, doubleValue = 99.0, floatValue = 9.0, intValue = 999, longValue = 3333, shortValue = 3, colors = {question.Color RED, question.Color BLUE}, anno = @SimpleAnnotation(value = core), simpleAnnos = {@SimpleAnnotation(value = org), @SimpleAnnotation(value = eclipse), @SimpleAnnotation(value = jdt)}, clazzes = {Object.class, String.class}, clazz = Object.class)",
074: "@RTInvisibleAnno(value = org.eclipse.jdt.core)",
075: "@Deprecated()" };
076: break;
077: case 1:
078: expected = new String[] { "@Deprecated()" };
079: break;
080: case 2:
081: expected = new String[] {
082: "@RTVisibleAnno(anno = @SimpleAnnotation(value = field), clazzes = {})",
083: "@RTInvisibleAnno(value = 2)" };
084: break;
085: case 3:
086: expected = new String[] { "@RTInvisibleAnno(value = 3)" };
087: break;
088: case 4:
089: expected = new String[] { "@SimpleAnnotation(value = 4)" };
090: break;
091: default:
092: expected = NO_ANNOTATIONS;
093: }
094:
095: assertAnnotation(expected, fieldDecl
096: .getAnnotationMirrors());
097: counter++;
098: }
099:
100: final Collection<? extends MethodDeclaration> methodDecls = typeDecl
101: .getMethods();
102: counter = 0;
103: TestCase.assertEquals(7, methodDecls.size());
104: for (MethodDeclaration methodDecl : methodDecls) {
105: final String name = "method" + counter;
106:
107: TestCase.assertEquals("method name mismatch", name,
108: methodDecl.getSimpleName());
109: final String[] expected;
110: switch (counter) {
111: case 0:
112: expected = new String[] {
113: "@RTVisibleAnno(anno = @SimpleAnnotation(value = method0), clazzes = {})",
114: "@RTInvisibleAnno(value = 0)",
115: "@Deprecated()" };
116: break;
117: case 1:
118: expected = new String[] { "@Deprecated()" };
119: break;
120: case 2:
121: expected = new String[] {
122: "@RTVisibleAnno(anno = @SimpleAnnotation(value = method2), clazzes = {})",
123: "@RTInvisibleAnno(value = 2)" };
124: break;
125: case 3:
126: expected = new String[] { "@RTInvisibleAnno(value = 3)" };
127: break;
128: case 4:
129: expected = new String[] { "@SimpleAnnotation(value = method4)" };
130: break;
131: case 5:
132: case 6:
133: default:
134: expected = NO_ANNOTATIONS;
135: }
136:
137: assertAnnotation(expected, methodDecl
138: .getAnnotationMirrors());
139:
140: if (counter == 5) {
141: Collection<ParameterDeclaration> paramDecls = methodDecl
142: .getParameters();
143: int pCounter = 0;
144: for (ParameterDeclaration paramDecl : paramDecls) {
145: final String[] expectedParamAnnotations;
146: switch (pCounter) {
147: case 1:
148: expectedParamAnnotations = new String[] { "@Deprecated()" };
149: break;
150: case 2:
151: expectedParamAnnotations = new String[] {
152: "@RTVisibleAnno(anno = @SimpleAnnotation(value = param2), clazzes = {})",
153: "@RTInvisibleAnno(value = 2)" };
154: break;
155: default:
156: expectedParamAnnotations = NO_ANNOTATIONS;
157: }
158: assertAnnotation(expectedParamAnnotations,
159: paramDecl.getAnnotationMirrors());
160: pCounter++;
161: }
162:
163: }
164: counter++;
165: }
166: }
167: } catch (ComparisonFailure failure) {
168: if (!ProcessorTestStatus.hasErrors()) {
169: ProcessorTestStatus.failWithoutException(failure
170: .toString());
171: }
172: throw failure;
173: } catch (junit.framework.AssertionFailedError error) {
174: if (!ProcessorTestStatus.hasErrors()) {
175: ProcessorTestStatus.failWithoutException(error
176: .toString());
177: }
178: throw error;
179: }
180: }
181:
182: private void assertAnnotation(final String[] expected,
183: Collection<AnnotationMirror> annotations) {
184: final int expectedLen = expected.length;
185: TestCase
186: .assertEquals(
187: "annotation number mismatch", expected.length, annotations.size()); //$NON-NLS-1$
188:
189: final HashSet<String> expectedSet = new HashSet<String>(
190: expectedLen * 4 / 3 + 1);
191: for (int i = 0; i < expectedLen; i++)
192: expectedSet.add(expected[i]);
193:
194: int counter = 0;
195: for (AnnotationMirror mirror : annotations) {
196: if (counter >= expectedLen)
197: TestCase.assertEquals("", mirror.toString()); //$NON-NLS-1$
198: else {
199: final String mirrorToString = mirror.toString();
200: final boolean contains = expectedSet
201: .contains(mirrorToString);
202: if (!contains) {
203: System.err.println(mirrorToString);
204: System.err.println(expectedSet);
205: }
206: TestCase
207: .assertTrue(
208: "unexpected annotation " + mirrorToString, contains); //$NON-NLS-1$
209: expectedSet.remove(mirrorToString);
210: }
211: counter++;
212: }
213: }
214: }
|