001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 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.valueconversion;
011:
012: import java.util.Collection;
013: import java.util.List;
014: import java.util.Map;
015:
016: import junit.framework.TestCase;
017:
018: import org.eclipse.jdt.apt.tests.annotations.BaseProcessor;
019:
020: import com.sun.mirror.apt.AnnotationProcessorEnvironment;
021: import com.sun.mirror.apt.Messager;
022: import com.sun.mirror.declaration.AnnotationMirror;
023: import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
024: import com.sun.mirror.declaration.AnnotationValue;
025: import com.sun.mirror.declaration.TypeDeclaration;
026: import com.sun.mirror.type.AnnotationType;
027:
028: public class ValueConversionProcessor extends BaseProcessor {
029:
030: public static final Byte BYTE_49 = 49;
031: public static final Byte BYTE_50 = 50;
032: public static final Character CHAR_ONE = '1';
033: public static final Character CHAR_TWO = '2';
034: public static final Double DOUBLE_49 = 49d;
035: public static final Double DOUBLE_50 = 50d;
036: public static final Float FLOAT_49 = 49f;
037: public static final Float FLOAT_50 = 50f;
038: public static final Integer INTEGER_49 = 49;
039: public static final Integer INTEGER_50 = 50;
040: public static final Long LONG_49 = 49l;
041: public static final Long LONG_50 = 50l;
042: public static final Short SHORT_49 = 49;
043: public static final Short SHORT_50 = 50;
044:
045: public ValueConversionProcessor(AnnotationProcessorEnvironment env) {
046: super (env);
047: }
048:
049: @SuppressWarnings("nls")
050: public void process() {
051: final TypeDeclaration test = _env
052: .getTypeDeclaration("sample.Test");
053: if (test == null)
054: TestCase.assertNotNull(
055: "failed to locate type 'sample.Test'", test);
056:
057: testCompilerAPIPath(test);
058: testReflectionPath(test);
059: }
060:
061: private void testCompilerAPIPath(TypeDeclaration test) {
062: final Collection<AnnotationMirror> annotations = test
063: .getAnnotationMirrors();
064: final int numAnnotations = annotations == null ? 0
065: : annotations.size();
066: TestCase.assertEquals("annotation number mismatch", 1,
067: numAnnotations);
068:
069: final AnnotationMirror annotation = annotations.iterator()
070: .next();
071: final AnnotationType annotationType = annotation
072: .getAnnotationType();
073: final String annoTypeName = annotationType.getDeclaration()
074: .getQualifiedName();
075: if (!Annotation.class.getName().equals(annoTypeName)
076: && !AnnotationWithArray.class.getName().equals(
077: annoTypeName))
078: return;
079:
080: final Map<AnnotationTypeElementDeclaration, AnnotationValue> elementValues = annotation
081: .getElementValues();
082:
083: for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry : elementValues
084: .entrySet()) {
085: AnnotationTypeElementDeclaration elementDecl = entry
086: .getKey();
087: final String name = elementDecl.getSimpleName();
088: final AnnotationValue value = entry.getValue();
089: compare(name, value.getValue());
090: }
091: }
092:
093: private void testReflectionPath(TypeDeclaration test) {
094: final RefAnnotation refAnno = test
095: .getAnnotation(RefAnnotation.class);
096: if (refAnno != null) {
097: assertValueMatch("z", refAnno.z(), true);
098: assertValueMatch("b", refAnno.b(), (byte) 49);
099: assertValueMatch("c", refAnno.c(), '1');
100: assertValueMatch("d", refAnno.d(), 49);
101: assertValueMatch("f", refAnno.f(), 49);
102: assertValueMatch("i", refAnno.i(), 49);
103: assertValueMatch("l", refAnno.l(), 49);
104: assertValueMatch("s", refAnno.s(), (short) 49);
105: }
106: final RefAnnotationWithArray refAnnoArray = test
107: .getAnnotation(RefAnnotationWithArray.class);
108: if (refAnnoArray != null) {
109: assertArrayValueMatch("booleans", refAnnoArray.booleans(),
110: new boolean[] { true, true });
111: assertArrayValueMatch("bytes", refAnnoArray.bytes(),
112: new byte[] { 49, 50 });
113: assertArrayValueMatch("chars", refAnnoArray.chars(),
114: new char[] { '1', '2' });
115: assertArrayValueMatch("doubles", refAnnoArray.doubles(),
116: new double[] { 49d, 50d });
117: assertArrayValueMatch("floats", refAnnoArray.floats(),
118: new float[] { 49f, 50f });
119: assertArrayValueMatch("ints", refAnnoArray.ints(),
120: new int[] { 49, 50 });
121: assertArrayValueMatch("longs", refAnnoArray.longs(),
122: new long[] { 49l, 50l });
123: assertArrayValueMatch("shorts", refAnnoArray.shorts(),
124: new short[] { 49, 50 });
125: compare("str", refAnnoArray.str());
126: }
127: }
128:
129: private void compare(final String name, final Object actualValue) {
130: if (name.length() == 1) {
131: final Class<?> expectedType;
132: final Object expectedValue;
133: switch (name.charAt(0)) {
134: case 'z':
135: expectedType = Boolean.class;
136: expectedValue = Boolean.TRUE;
137: break;
138: case 'b':
139: expectedType = Byte.class;
140: expectedValue = BYTE_49;
141: break;
142: case 'c':
143: expectedType = Character.class;
144: expectedValue = CHAR_ONE;
145: break;
146: case 's':
147: expectedType = Short.class;
148: expectedValue = SHORT_49;
149: break;
150: case 'i':
151: expectedType = Integer.class;
152: expectedValue = INTEGER_49;
153: break;
154: case 'l':
155: expectedType = Long.class;
156: expectedValue = LONG_49;
157: break;
158: case 'f':
159: expectedType = Float.class;
160: expectedValue = FLOAT_49;
161: break;
162: case 'd':
163: expectedType = Double.class;
164: expectedValue = DOUBLE_49;
165: break;
166: default:
167: TestCase.assertNotNull("unexpected member " + name,
168: null);
169: throw new IllegalStateException(); // won't get here.
170: }
171: assertValueTypeMatch(name, actualValue, expectedType,
172: expectedValue);
173: } else {
174: final Class<?> expectedElementType;
175: final Object[] expectedElementValues;
176: if ("booleans".equals(name)) {
177: expectedElementType = Boolean.class;
178: expectedElementValues = new Object[] { Boolean.TRUE,
179: Boolean.TRUE };
180: } else if ("chars".equals(name)) {
181: expectedElementType = Character.class;
182: expectedElementValues = new Object[] { CHAR_ONE,
183: CHAR_TWO };
184: } else if ("bytes".equals(name)) {
185: expectedElementType = Byte.class;
186: expectedElementValues = new Object[] { BYTE_49, BYTE_50 };
187: } else if ("shorts".equals(name)) {
188: expectedElementType = Short.class;
189: expectedElementValues = new Object[] { SHORT_49,
190: SHORT_50 };
191: } else if ("ints".equals(name)) {
192: expectedElementType = Integer.class;
193: expectedElementValues = new Object[] { INTEGER_49,
194: INTEGER_50 };
195: } else if ("longs".equals(name)) {
196: expectedElementType = Long.class;
197: expectedElementValues = new Object[] { LONG_49, LONG_50 };
198: } else if ("floats".equals(name)) {
199: expectedElementType = Float.class;
200: expectedElementValues = new Object[] { FLOAT_49,
201: FLOAT_50 };
202:
203: } else if ("doubles".equals(name)) {
204: expectedElementType = Double.class;
205: expectedElementValues = new Object[] { DOUBLE_49,
206: DOUBLE_50 };
207: } else if ("str".equals(name)) {
208: assertValueTypeMatch(name, actualValue, String.class,
209: "string");
210: return;
211: } else {
212: TestCase.assertNotNull("unexpected member " + name,
213: null);
214: throw new IllegalStateException(); // won't get here.
215: }
216: @SuppressWarnings("unchecked")
217: List<AnnotationValue> actualList = (List<AnnotationValue>) actualValue;
218: assertArrayValueTypeMatch(name, actualList,
219: expectedElementType, expectedElementValues);
220: }
221: }
222:
223: private void assertValueTypeMatch(final String name,
224: final Object actualValue, final Class<?> expectedType,
225: final Object expectedValue) {
226: if (actualValue != null
227: && expectedType != actualValue.getClass()) {
228: final Messager msgr = _env.getMessager();
229: msgr.printError("type mismatch for member " + name
230: + " expected " + expectedType.getName()
231: + " but got " + actualValue.getClass().getName());
232: } else if (!expectedValue.equals(actualValue)) {
233: final Messager msgr = _env.getMessager();
234: msgr.printError("value mismatch for member " + name
235: + " expected " + expectedValue + " but got "
236: + actualValue);
237: }
238: }
239:
240: private void assertArrayValueTypeMatch(final String name,
241: final List<AnnotationValue> actualValues,
242: final Class<?> expectedElementType,
243: final Object[] expectedValues) {
244: int i = 0;
245: for (AnnotationValue av : actualValues) {
246: assertValueTypeMatch(name, av.getValue(),
247: expectedElementType, expectedValues[i]);
248: i++;
249: }
250: }
251:
252: private void assertValueMatch(final String name,
253: final boolean actual, final boolean expected) {
254:
255: if (actual != expected) {
256: final Messager msgr = _env.getMessager();
257: msgr.printError("value mismatch for member " + name
258: + " expected " + expected + " but got " + actual);
259: }
260: }
261:
262: private void assertValueMatch(final String name, final byte actual,
263: final byte expected) {
264:
265: if (actual != expected) {
266: final Messager msgr = _env.getMessager();
267: msgr.printError("value mismatch for member " + name
268: + " expected " + expected + " but got " + actual);
269: }
270: }
271:
272: private void assertValueMatch(final String name, final char actual,
273: final char expected) {
274:
275: if (actual != expected) {
276: final Messager msgr = _env.getMessager();
277: msgr.printError("value mismatch for member " + name
278: + " expected " + expected + " but got " + actual);
279: }
280: }
281:
282: private void assertValueMatch(final String name,
283: final double actual, final double expected) {
284:
285: if (actual != expected) {
286: final Messager msgr = _env.getMessager();
287: msgr.printError("value mismatch for member " + name
288: + " expected " + expected + " but got " + actual);
289: }
290: }
291:
292: private void assertValueMatch(final String name,
293: final float actual, final float expected) {
294:
295: if (actual != expected) {
296: final Messager msgr = _env.getMessager();
297: msgr.printError("value mismatch for member " + name
298: + " expected " + expected + " but got " + actual);
299: }
300: }
301:
302: private void assertValueMatch(final String name, final int actual,
303: final int expected) {
304:
305: if (actual != expected) {
306: final Messager msgr = _env.getMessager();
307: msgr.printError("value mismatch for member " + name
308: + " expected " + expected + " but got " + actual);
309: }
310: }
311:
312: private void assertValueMatch(final String name, final long actual,
313: final long expected) {
314:
315: if (actual != expected) {
316: final Messager msgr = _env.getMessager();
317: msgr.printError("value mismatch for member " + name
318: + " expected " + expected + " but got " + actual);
319: }
320: }
321:
322: private void assertValueMatch(final String name,
323: final short actual, final short expected) {
324:
325: if (actual != expected) {
326: final Messager msgr = _env.getMessager();
327: msgr.printError("value mismatch for member " + name
328: + " expected " + expected + " but got " + actual);
329: }
330: }
331:
332: private void assertArrayValueMatch(final String name,
333: final boolean[] actual, final boolean[] expected) {
334:
335: int i = 0;
336: final Messager msgr = _env.getMessager();
337: for (boolean a : actual) {
338: if (a != expected[i]) {
339: msgr.printError("value mismatch for member " + name
340: + " expected " + expected[i] + " but got " + a);
341: }
342: i++;
343: }
344: }
345:
346: private void assertArrayValueMatch(final String name,
347: final byte[] actual, final byte[] expected) {
348:
349: int i = 0;
350: final Messager msgr = _env.getMessager();
351: for (byte a : actual) {
352: if (a != expected[i]) {
353: msgr.printError("value mismatch for member " + name
354: + " expected " + expected[i] + " but got " + a);
355: }
356: i++;
357: }
358: }
359:
360: private void assertArrayValueMatch(final String name,
361: final char[] actual, final char[] expected) {
362:
363: int i = 0;
364: final Messager msgr = _env.getMessager();
365: for (char a : actual) {
366: if (a != expected[i]) {
367: msgr.printError("value mismatch for member " + name
368: + " expected " + expected[i] + " but got " + a);
369: }
370: i++;
371: }
372: }
373:
374: private void assertArrayValueMatch(final String name,
375: final double[] actual, final double[] expected) {
376:
377: int i = 0;
378: final Messager msgr = _env.getMessager();
379: for (double a : actual) {
380: if (a != expected[i]) {
381: msgr.printError("value mismatch for member " + name
382: + " expected " + expected[i] + " but got " + a);
383: }
384: i++;
385: }
386: }
387:
388: private void assertArrayValueMatch(final String name,
389: final float[] actual, final float[] expected) {
390:
391: int i = 0;
392: final Messager msgr = _env.getMessager();
393: for (float a : actual) {
394: if (a != expected[i]) {
395: msgr.printError("value mismatch for member " + name
396: + " expected " + expected[i] + " but got " + a);
397: }
398: i++;
399: }
400: }
401:
402: private void assertArrayValueMatch(final String name,
403: final int[] actual, final int[] expected) {
404:
405: int i = 0;
406: final Messager msgr = _env.getMessager();
407: for (int a : actual) {
408: if (a != expected[i]) {
409: msgr.printError("value mismatch for member " + name
410: + " expected " + expected[i] + " but got " + a);
411: }
412: i++;
413: }
414: }
415:
416: private void assertArrayValueMatch(final String name,
417: final long[] actual, final long[] expected) {
418:
419: int i = 0;
420: final Messager msgr = _env.getMessager();
421: for (long a : actual) {
422: if (a != expected[i]) {
423: msgr.printError("value mismatch for member " + name
424: + " expected " + expected[i] + " but got " + a);
425: }
426: i++;
427: }
428: }
429:
430: private void assertArrayValueMatch(final String name,
431: final short[] actual, final short[] expected) {
432:
433: int i = 0;
434: final Messager msgr = _env.getMessager();
435: for (short a : actual) {
436: if (a != expected[i]) {
437: msgr.printError("value mismatch for member " + name
438: + " expected " + expected[i] + " but got " + a);
439: }
440: i++;
441: }
442: }
443: }
|