Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 56df81d8d5cb9bf10072a707a7b33d9b85623540 (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
/*******************************************************************************
 * Copyright (c) 2009, 2011 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.tests.ccvs.ui;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.eclipse.core.runtime.Assert;
import org.eclipse.team.tests.ccvs.core.EclipseTest;

public class ReflectionUtils {

	public static Object construct(String className, ClassLoader classLoader,
			Class<?>[] constructorTypes, Object[] constructorArgs) {
		Class<?> clazz = null;
		try {
			clazz = Class.forName(className, true, classLoader);
		} catch (ClassNotFoundException e) {
			EclipseTest.fail(e.getMessage());
		} catch (ExceptionInInitializerError e) {
			EclipseTest.fail(e.getMessage());
		}
		Constructor<?> constructor = null;
		try {
			constructor = clazz.getDeclaredConstructor(constructorTypes);
		} catch (SecurityException e) {
			EclipseTest.fail(e.getMessage());
		} catch (NoSuchMethodException e) {
			EclipseTest.fail(e.getMessage());
		}
		Assert.isNotNull(constructor);
		constructor.setAccessible(true);
		try {
			return constructor.newInstance(constructorArgs);
		} catch (IllegalArgumentException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
			EclipseTest.fail(e.getMessage());
		}
		return null;
	}

	public static Object callMethod(Object object, String name, Class<?> types[],
			Object args[]) {
		try {
			Method method = null;
			Class<?> clazz = object.getClass();
			NoSuchMethodException ex = null;
			while (method == null && clazz != null) {
				try {
					method = clazz.getDeclaredMethod(name, types);
				} catch (NoSuchMethodException e) {
					if (ex == null) {
						ex = e;
					}
					clazz = clazz.getSuperclass();
				}
			}
			if (method == null) {
				throw ex;
			}
			method.setAccessible(true);
			Object ret = method.invoke(object, args);
			return ret;
		} catch (IllegalArgumentException | IllegalAccessException | SecurityException | NoSuchMethodException | InvocationTargetException e) {
			EclipseTest.fail(e.getMessage());
		}
		return null;
	}

	public static Object callMethod(Object object, String name, Object args[]) {
		Class<?> types[] = new Class[args.length];
		for (int i = 0; i < args.length; i++) {
			types[i] = args[i].getClass();
		}
		return callMethod(object, name, types, args);
	}

	public static Object getField(Object object, String name) {
		try {
			Field field = null;
			Class<?> clazz = object.getClass();
			NoSuchFieldException ex = null;
			while (field == null && clazz != null) {
				try {
					field = clazz.getDeclaredField(name);
				} catch (NoSuchFieldException e) {
					if (ex == null) {
						ex = e;
					}
					clazz = clazz.getSuperclass();
				}
			}
			if (field == null) {
				throw ex;
			}
			field.setAccessible(true);
			Object ret = field.get(object);
			return ret;
		} catch (IllegalArgumentException | IllegalAccessException | SecurityException | NoSuchFieldException e) {
			EclipseTest.fail(e.getMessage());
		}
		return null;
	}

	public static void setField(Object object, String name, Object value) {
		try {
			Field field = null;
			Class<?> clazz = object.getClass();
			NoSuchFieldException ex = null;
			while (field == null && clazz != null) {
				try {
					field = clazz.getDeclaredField(name);
				} catch (NoSuchFieldException e) {
					if (ex == null) {
						ex = e;
					}
					clazz = clazz.getSuperclass();
				}
			}
			if (field == null) {
				throw ex;
			}
			field.setAccessible(true);
			field.set(object, value);
		} catch (IllegalArgumentException | IllegalAccessException | SecurityException | NoSuchFieldException e) {
			EclipseTest.fail(e.getMessage());
		}
	}

}

Back to the top