Skip to main content
summaryrefslogtreecommitdiffstats
blob: d210cb0f5f7e9ce8df40b54200268a897e1087ab (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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*******************************************************************************
 * Copyright (c) 2005, 2010 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Vector;

import junit.framework.TestCase;

import org.eclipse.jpt.common.utility.internal.CollectionTools;
import org.eclipse.jpt.common.utility.internal.ReflectionTools;
import org.eclipse.jpt.common.utility.internal.iterables.TransformationIterable;

@SuppressWarnings("nls")
public class ReflectionToolsTests extends TestCase {

	private static String testStaticField;

	public ReflectionToolsTests(String name) {
		super(name);
	}

// this is no longer true - it appears the JLS now defines the generated names...
//	/**
//	 * Return the compiler-generated class name. The Eclipse compiler generates
//	 * "local" classes with names in the form "com.foo.Outer$1$Local"; while the
//	 * JDK compiler generates "com.foo.Outer$1Local". There might be other
//	 * differences.... ~bjv
//	 */
//	public static String compilerDependentClassNameFor(String className) {
//		int index = className.indexOf("$1$");
//		if (index == -1) {
//			return className;
//		}
//		try {
//			Class.forName(className);
//		} catch (ClassNotFoundException ex) {
//			return className.substring(0, index + 2) + className.substring(index + 3);
//		}
//		return className;
//	}
//
//	private static String munge(String className) {
//		return compilerDependentClassNameFor(className);
//	}

	public void testAllFields() {
		int fieldCount = 0;
		fieldCount += java.util.Vector.class.getDeclaredFields().length;
		fieldCount += java.util.AbstractList.class.getDeclaredFields().length;
		fieldCount += java.util.AbstractCollection.class.getDeclaredFields().length;
		fieldCount += java.lang.Object.class.getDeclaredFields().length;
		Iterable<Field> fields = ReflectionTools.getAllFields(java.util.Vector.class);
		assertEquals(fieldCount, CollectionTools.size(fields));
		assertTrue(CollectionTools.contains(this.fieldNames(fields), "modCount"));
		assertTrue(CollectionTools.contains(this.fieldNames(fields), "serialVersionUID"));
		assertTrue(CollectionTools.contains(this.fieldNames(fields), "capacityIncrement"));
		assertTrue(CollectionTools.contains(this.fieldNames(fields), "elementCount"));
		assertTrue(CollectionTools.contains(this.fieldNames(fields), "elementData"));
		assertTrue(fields.iterator().next().isAccessible());
	}

	public void testAllMethods() {
		int methodCount = 0;
		methodCount += java.util.Vector.class.getDeclaredMethods().length;
		methodCount += java.util.AbstractList.class.getDeclaredMethods().length;
		methodCount += java.util.AbstractCollection.class.getDeclaredMethods().length;
		methodCount += java.lang.Object.class.getDeclaredMethods().length;
		Iterable<Method> methods = ReflectionTools.getAllMethods(java.util.Vector.class);
		assertEquals(methodCount, CollectionTools.size(methods));
		assertTrue(CollectionTools.contains(this.methodNames(methods), "wait"));
		assertTrue(CollectionTools.contains(this.methodNames(methods), "addElement"));
		assertTrue(methods.iterator().next().isAccessible());
	}

	public void testNewInstanceClass() {
		Vector<?> v = ReflectionTools.newInstance(java.util.Vector.class);
		assertNotNull(v);
		assertEquals(0, v.size());
	}

	public void testNewInstanceClassClassObject() {
		int initialCapacity = 200;
		Vector<?> v = ReflectionTools.newInstance(java.util.Vector.class, int.class, new Integer(initialCapacity));
		assertNotNull(v);
		assertEquals(0, v.size());
		Object[] elementData = (Object[]) ReflectionTools.getFieldValue(v, "elementData");
		assertEquals(initialCapacity, elementData.length);
	}

	public void testNewInstanceClassClassArrayObjectArray() {
		int initialCapacity = 200;
		Class<?>[] parmTypes = new Class[1];
		parmTypes[0] = int.class;
		Object[] parms = new Object[1];
		parms[0] = new Integer(initialCapacity);
		Vector<?> v = ReflectionTools.newInstance(java.util.Vector.class, parmTypes, parms);
		assertNotNull(v);
		assertEquals(0, v.size());
		Object[] elementData = (Object[]) ReflectionTools.getFieldValue(v, "elementData");
		assertEquals(initialCapacity, elementData.length);

		parms[0] = new Integer(-1);
		boolean exCaught = false;
		try {
			v = ReflectionTools.newInstance(java.util.Vector.class, parmTypes, parms);
		} catch (RuntimeException ex) {
			exCaught = true;
		}
		assertTrue("RuntimeException not thrown", exCaught);

		parmTypes[0] = java.lang.String.class;
		parms[0] = "foo";
		exCaught = false;
		try {
			v = ReflectionTools.newInstance(java.util.Vector.class, parmTypes, parms);
		} catch (RuntimeException ex) {
			if (ex.getCause() instanceof NoSuchMethodException) {
				exCaught = true;
			}
		}
		assertTrue("NoSuchMethodException not thrown", exCaught);
	}

	public void testFieldValue() {
		int initialCapacity = 200;
		Vector<?> v = new Vector<Object>(initialCapacity);
		Object[] elementData = (Object[]) ReflectionTools.getFieldValue(v, "elementData");
		assertEquals(initialCapacity, elementData.length);

		// test inherited field
		Integer modCountInteger = (Integer) ReflectionTools.getFieldValue(v, "modCount");
		int modCount = modCountInteger.intValue();
		assertEquals(0, modCount);

		boolean exCaught = false;
		Object bogusFieldValue = null;
		try {
			bogusFieldValue = ReflectionTools.getFieldValue(v, "bogusField");
		} catch (RuntimeException ex) {
			if (ex.getCause() instanceof NoSuchFieldException) {
				exCaught = true;
			}
		}
		assertTrue("NoSuchFieldException not thrown: " + bogusFieldValue, exCaught);
	}

	public void testExecuteMethodObjectString() {
		Vector<String> v = new Vector<String>();
		int size = ((Integer) ReflectionTools.executeMethod(v, "size")).intValue();
		assertEquals(0, size);

		v.addElement("foo");
		size = ((Integer) ReflectionTools.executeMethod(v, "size")).intValue();
		assertEquals(1, size);
	}

	public void testExecuteMethodObjectStringClassObject() {
		Vector<String> v = new Vector<String>();
		boolean booleanResult = ((Boolean) ReflectionTools.executeMethod(v, "add", Object.class, "foo")).booleanValue();
		assertTrue(booleanResult);
		assertTrue(v.contains("foo"));
		Object voidResult = ReflectionTools.executeMethod(v, "addElement", Object.class, "bar");
		assertNull(voidResult);
	}

	public void testExecuteMethodObjectStringClassArrayObjectArray() {
		Vector<String> v = new Vector<String>();
		Class<?>[] parmTypes = new Class[1];
		parmTypes[0] = java.lang.Object.class;
		Object[] parms = new Object[1];
		parms[0] = "foo";
		boolean booleanResult = ((Boolean) ReflectionTools.executeMethod(v, "add", parmTypes, parms)).booleanValue();
		assertTrue(booleanResult);
		assertTrue(v.contains("foo"));

		boolean exCaught = false;
		Object bogusMethodReturnValue = null;
		try {
			bogusMethodReturnValue = ReflectionTools.executeMethod(v, "bogusMethod", parmTypes, parms);
		} catch (RuntimeException ex) {
			if (ex.getCause() instanceof NoSuchMethodException) {
				exCaught = true;
			}
		}
		assertTrue("NoSuchMethodException not thrown: " + bogusMethodReturnValue, exCaught);
	}

	public void testExecuteStaticMethodClassString() {
		Double randomObject = (Double) ReflectionTools.executeStaticMethod(java.lang.Math.class, "random");
		assertNotNull(randomObject);
		double random = randomObject.doubleValue();
		assertTrue(random >= 0);
		assertTrue(random < 1);
	}

	public void testExecuteStaticMethodClassStringClassObject() {
		String s = (String) ReflectionTools.executeStaticMethod(java.lang.String.class, "valueOf", boolean.class, Boolean.TRUE);
		assertNotNull(s);
		assertEquals("true", s);
	}

	public void testExecuteStaticMethodClassStringClassArrayObjectArray() {
		Class<?>[] parmTypes = new Class[1];
		parmTypes[0] = boolean.class;
		Object[] parms = new Object[1];
		parms[0] = Boolean.TRUE;
		String s = (String) ReflectionTools.executeStaticMethod(java.lang.String.class, "valueOf", parmTypes, parms);
		assertNotNull(s);
		assertEquals("true", s);

		boolean exCaught = false;
		Object bogusStaticMethodReturnValue = null;
		try {
			bogusStaticMethodReturnValue = ReflectionTools.executeStaticMethod(java.lang.String.class, "bogusStaticMethod", parmTypes, parms);
		} catch (RuntimeException ex) {
			if (ex.getCause() instanceof NoSuchMethodException) {
				exCaught = true;
			}
		}
		assertTrue("NoSuchMethodException not thrown: " + bogusStaticMethodReturnValue, exCaught);

		// test non-static method
		exCaught = false;
		try {
			bogusStaticMethodReturnValue = ReflectionTools.executeStaticMethod(java.lang.String.class, "toString");
		} catch (RuntimeException ex) {
			if (ex.getCause() instanceof NoSuchMethodException) {
				exCaught = true;
			}
		}
		assertTrue("NoSuchMethodException not thrown: " + bogusStaticMethodReturnValue, exCaught);
	}

	public void testSetFieldValue() {
		Vector<String> v = new Vector<String>();
		Object[] newElementData = new Object[5];
		newElementData[0] = "foo";
		ReflectionTools.setFieldValue(v, "elementData", newElementData);
		ReflectionTools.setFieldValue(v, "elementCount", new Integer(1));
		// test inherited field
		ReflectionTools.setFieldValue(v, "modCount", new Integer(1));
		assertTrue(v.contains("foo"));

		boolean exCaught = false;
		try {
			ReflectionTools.setFieldValue(v, "bogusField", "foo");
		} catch (RuntimeException ex) {
			if (ex.getCause() instanceof NoSuchFieldException) {
				exCaught = true;
			}
		}
		assertTrue("NoSuchFieldException not thrown", exCaught);
	}

	public void testSetStaticFieldValue() {
		ReflectionTools.setStaticFieldValue(this.getClass(), "testStaticField", "new value");
		assertEquals(testStaticField, "new value");

		boolean exCaught = false;
		try {
			ReflectionTools.setStaticFieldValue(this.getClass(), "bogusStaticField", "new value");
		} catch (RuntimeException ex) {
			if (ex.getCause() instanceof NoSuchFieldException) {
				exCaught = true;
			}
		}
		assertTrue("NoSuchFieldException not thrown", exCaught);
	}

	public void testSimpleName() {
		assertEquals("Vector", java.util.Vector.class.getSimpleName());
		assertEquals("Entry", java.util.Map.Entry.class.getSimpleName());
		assertEquals("int", int.class.getSimpleName());
		assertEquals("int[]", int[].class.getSimpleName());
		assertEquals("int[][]", int[][].class.getSimpleName());
		assertEquals("void", void.class.getSimpleName());
	}

	public void testPackageName() {
		assertEquals("java.util", java.util.Vector.class.getPackage().getName());
		assertEquals("java.util", java.util.Map.Entry.class.getPackage().getName());
	}

	public void testArrayDepthFor() {
		assertEquals(0, ReflectionTools.getArrayDepth(java.util.Vector.class));
		assertEquals(0, ReflectionTools.getArrayDepth(int.class));
		assertEquals(0, ReflectionTools.getArrayDepth(void.class));
		assertEquals(1, ReflectionTools.getArrayDepth(java.util.Vector[].class));
		assertEquals(1, ReflectionTools.getArrayDepth(int[].class));
		assertEquals(3, ReflectionTools.getArrayDepth(java.util.Vector[][][].class));
		assertEquals(3, ReflectionTools.getArrayDepth(int[][][].class));
	}

	public void testElementTypeFor() {
		assertEquals(java.util.Vector.class, ReflectionTools.getElementType(java.util.Vector.class));
		assertEquals(int.class, ReflectionTools.getElementType(int.class));
		assertEquals(void.class, ReflectionTools.getElementType(void.class));
		assertEquals(java.util.Vector.class, ReflectionTools.getElementType(java.util.Vector[].class));
		assertEquals(int.class, ReflectionTools.getElementType(int[].class));
		assertEquals(java.util.Vector.class, ReflectionTools.getElementType(java.util.Vector[][][].class));
		assertEquals(int.class, ReflectionTools.getElementType(int[][][].class));
	}

	public void testClassIsPrimitiveWrapperClass() {
		assertTrue(ReflectionTools.classIsPrimitiveWrapper(java.lang.Void.class));
		assertTrue(ReflectionTools.classIsPrimitiveWrapper(java.lang.Boolean.class));
		assertTrue(ReflectionTools.classIsPrimitiveWrapper(java.lang.Integer.class));
		assertTrue(ReflectionTools.classIsPrimitiveWrapper(java.lang.Float.class));

		assertFalse(ReflectionTools.classIsPrimitiveWrapper(java.lang.String.class));
		assertFalse(ReflectionTools.classIsPrimitiveWrapper(void.class));
		assertFalse(ReflectionTools.classIsPrimitiveWrapper(int.class));
	}

	public void testClassIsVariablePrimitiveWrapperClass() {
		assertFalse(ReflectionTools.classIsVariablePrimitiveWrapper(java.lang.Void.class));

		assertTrue(ReflectionTools.classIsVariablePrimitiveWrapper(java.lang.Boolean.class));
		assertTrue(ReflectionTools.classIsVariablePrimitiveWrapper(java.lang.Integer.class));
		assertTrue(ReflectionTools.classIsVariablePrimitiveWrapper(java.lang.Float.class));

		assertFalse(ReflectionTools.classIsVariablePrimitiveWrapper(java.lang.String.class));
		assertFalse(ReflectionTools.classIsVariablePrimitiveWrapper(void.class));
		assertFalse(ReflectionTools.classIsVariablePrimitiveWrapper(int.class));
	}

	public void testWrapperClass() {
		assertEquals(java.lang.Void.class, ReflectionTools.getWrapperClass(void.class));
		assertEquals(java.lang.Integer.class, ReflectionTools.getWrapperClass(int.class));
		assertEquals(java.lang.Float.class, ReflectionTools.getWrapperClass(float.class));
		assertEquals(java.lang.Boolean.class, ReflectionTools.getWrapperClass(boolean.class));

		assertNull(ReflectionTools.getWrapperClass(java.lang.String.class));
	}

	public void testClassForTypeDeclarationStringInt() throws Exception {
		assertEquals(int.class, ReflectionTools.getClassForTypeDeclaration("int", 0));
		assertEquals(int[].class, ReflectionTools.getClassForTypeDeclaration("int", 1));
		assertEquals(int[][][].class, ReflectionTools.getClassForTypeDeclaration("int", 3));

		assertEquals(Object.class, ReflectionTools.getClassForTypeDeclaration("java.lang.Object", 0));
		assertEquals(Object[][][].class, ReflectionTools.getClassForTypeDeclaration("java.lang.Object", 3));

		assertEquals(void.class, ReflectionTools.getClassForTypeDeclaration("void", 0));
		try {
			ReflectionTools.getClassForTypeDeclaration(void.class.getName(), 1);
			fail("should not get here...");
		} catch (RuntimeException ex) {
			// expected
		}
	}

	public void testCodeForClass() {
		assertEquals('I', ReflectionTools.getCodeForClass(int.class));
		assertEquals('B', ReflectionTools.getCodeForClass(byte.class));
	}

	public void testClassNameForTypeDeclarationString() throws Exception {
		assertEquals("int", ReflectionTools.getClassNameForTypeDeclaration("int"));
		assertEquals("[I", ReflectionTools.getClassNameForTypeDeclaration("int[]"));
		assertEquals("[[I", ReflectionTools.getClassNameForTypeDeclaration("int [ ] [ ]"));

		assertEquals("java.lang.Object", ReflectionTools.getClassNameForTypeDeclaration("java.lang.Object"));
		assertEquals("[Ljava.lang.Object;", ReflectionTools.getClassNameForTypeDeclaration("java.lang.Object\t[]"));
		assertEquals("[[Ljava.lang.Object;", ReflectionTools.getClassNameForTypeDeclaration("java.lang.Object\t[]\t[]"));
	}

	public void testArrayDepthForTypeDeclarationString() throws Exception {
		assertEquals(0, ReflectionTools.getArrayDepthForTypeDeclaration("java.lang.Object"));
		assertEquals(1, ReflectionTools.getArrayDepthForTypeDeclaration("java.lang.Object[]"));
		assertEquals(3, ReflectionTools.getArrayDepthForTypeDeclaration("java.lang.Object[][][]"));

		assertEquals(0, ReflectionTools.getArrayDepthForTypeDeclaration("int"));
		assertEquals(1, ReflectionTools.getArrayDepthForTypeDeclaration("int[]"));
		assertEquals(3, ReflectionTools.getArrayDepthForTypeDeclaration("int[][][]"));

		assertEquals(0, ReflectionTools.getArrayDepthForTypeDeclaration("float"));
		assertEquals(1, ReflectionTools.getArrayDepthForTypeDeclaration("float [ ]"));
		assertEquals(3, ReflectionTools.getArrayDepthForTypeDeclaration("float[] [] []"));
	}

	public void testElementTypeNameForTypeDeclarationString() throws Exception {
		assertEquals("java.lang.Object", ReflectionTools.getElementTypeNameForTypeDeclaration("java.lang.Object"));
		assertEquals("java.lang.Object", ReflectionTools.getElementTypeNameForTypeDeclaration("java.lang.Object[]"));
		assertEquals("java.lang.Object", ReflectionTools.getElementTypeNameForTypeDeclaration("java.lang.Object[][][]"));

		assertEquals("int", ReflectionTools.getElementTypeNameForTypeDeclaration("int"));
		assertEquals("int", ReflectionTools.getElementTypeNameForTypeDeclaration("int[]"));
		assertEquals("int", ReflectionTools.getElementTypeNameForTypeDeclaration("int[][][]"));

		assertEquals("float", ReflectionTools.getElementTypeNameForTypeDeclaration("float"));
		assertEquals("float", ReflectionTools.getElementTypeNameForTypeDeclaration("float [ ]"));
		assertEquals("float", ReflectionTools.getElementTypeNameForTypeDeclaration("float[] [] []"));
	}

	public void testClassNameForTypeDeclarationStringInt() throws Exception {
		assertEquals(int.class.getName(), ReflectionTools.getClassNameForTypeDeclaration("int", 0));
		assertEquals(int[].class.getName(), ReflectionTools.getClassNameForTypeDeclaration("int", 1));
		assertEquals(int[][][].class.getName(), ReflectionTools.getClassNameForTypeDeclaration("int", 3));

		assertEquals(Object.class.getName(), ReflectionTools.getClassNameForTypeDeclaration("java.lang.Object", 0));
		assertEquals(Object[][][].class.getName(), ReflectionTools.getClassNameForTypeDeclaration("java.lang.Object", 3));

		assertEquals(void.class.getName(), ReflectionTools.getClassNameForTypeDeclaration("void", 0));
		try {
			ReflectionTools.getClassNameForTypeDeclaration(void.class.getName(), 1);
			fail("should not get here...");
		} catch (IllegalArgumentException ex) {
			// expected
		}
	}

	private Iterable<String> fieldNames(Iterable<Field> fields) {
		return new TransformationIterable<Field, String>(fields) {
			@Override
			protected String transform(Field field) {
				return field.getName();
			}
		};
	}

	private Iterable<String> methodNames(Iterable<Method> methods) {
		return new TransformationIterable<Method, String>(methods) {
			@Override
			protected String transform(Method method) {
				return method.getName();
			}
		};
	}

}

Back to the top