Skip to main content
summaryrefslogtreecommitdiffstats
blob: 79564e9a0db29c6ade35744e1d78da03bad72c0c (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
/*******************************************************************************
 * Copyright (c) 2012 Obeo.
 * 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:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.ide.ui.internal.contentmergeviewer.util;

import static com.google.common.base.Throwables.propagate;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

/**
 * Utility class to access (R/W) field in super class hierarchy.
 * <p>
 * It has decent performance as it LRU-caches reflective call.
 * 
 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
 */
public class DynamicObject {

	private final LoadingCache<String, Field> fFieldCache;

	private final LoadingCache<MethodKey, Method> fMethodCache;

	private final Object fTarget;

	private final Class<?> fTargetClass;

	/**
	 * 
	 */
	public DynamicObject(Object target) {
		fTarget = target;
		fTargetClass = target.getClass();
		fFieldCache = CacheBuilder.newBuilder().maximumSize(16).build(new CacheLoader<String, Field>() {
			@Override
			public Field load(String key) throws Exception {
				return getField(fTargetClass, key);
			}
		});
		fMethodCache = CacheBuilder.newBuilder().maximumSize(16).build(new CacheLoader<MethodKey, Method>() {
			@Override
			public Method load(MethodKey key) throws Exception {
				return getMethod(fTargetClass, key.methodName, key.parameterTypes);
			}
		});
	}

	public Object get(String fieldName) {
		Object ret = null;
		try {
			Field field = fFieldCache.get(fieldName);
			ret = field.get(fTarget);
		} catch (Exception e) {
			handleException(e);
		}
		return ret;
	}

	public void set(String fieldName, Object value) {
		try {
			Field field = fFieldCache.get(fieldName);
			field.set(fTarget, value);
		} catch (Exception e) {
			handleException(e);
		}
	}

	public double getDouble(String fieldName) {
		double ret = Double.NaN;
		try {
			Field field = fFieldCache.get(fieldName);
			ret = field.getDouble(fTarget);
		} catch (Exception e) {
			handleException(e);
		}
		return ret;
	}

	public void setDouble(String fieldName, double value) {
		try {
			Field field = fFieldCache.get(fieldName);
			field.setDouble(fTarget, value);
		} catch (Exception e) {
			handleException(e);
		}
	}

	public int getInt(String fieldName) {
		int ret = 0;
		try {
			Field field = fFieldCache.get(fieldName);
			ret = field.getInt(fTarget);
		} catch (Exception e) {
			handleException(e);
		}
		return ret;
	}

	public void setInt(String fieldName, int value) {
		try {
			Field field = fFieldCache.get(fieldName);
			field.setInt(fTarget, value);
		} catch (Exception e) {
			handleException(e);
		}
	}

	public Object invoke(String methodName, Class<?>[] parameterTypes, Object... args) {
		Object ret = null;
		try {
			Method method = fMethodCache.get(MethodKey.create(methodName, parameterTypes));
			ret = method.invoke(fTarget, args);
		} catch (Exception e) {
			handleException(e);
		}
		return ret;
	}

	/**
	 * @param cause
	 */
	protected void handleException(Throwable cause) {
		propagate(cause);
	}

	private static Field getField(Class<?> clazz, String fieldName) {
		try {
			Field declaredField = clazz.getDeclaredField(fieldName);
			makeAccessible(declaredField);
			return declaredField;
		} catch (NoSuchFieldException e) {
			Class<?> superClass = clazz.getSuperclass();
			if (superClass == null) {
				propagate(e);
			} else {
				return getField(superClass, fieldName);
			}
		}
		return null;
	}

	private static Method getMethod(Class<?> clazz, String name, Class<?>[] parameterTypes) {
		Method method = null;
		try {
			method = clazz.getDeclaredMethod(name, parameterTypes);
		} catch (NoSuchMethodException nsme) {
			if (!clazz.isInterface()) {
				Class<?> superClass = clazz.getSuperclass();
				if (superClass != null) {
					method = getMethod(superClass, name, parameterTypes);
				}
			}

			if (method == null) {
				for (Class<?> superInterface : clazz.getInterfaces()) {
					method = getMethod(superInterface, name, parameterTypes);
					if (method != null) {
						break;
					}
				}
			}
		} catch (Exception e) {
			propagate(e);
		}

		if (method != null && !method.isAccessible()) {
			makeAccessible(method);
		}

		return method;
	}

	private static void makeAccessible(Member member) {
		final boolean instanceOfAccesibleAndNotAccessible = member instanceof AccessibleObject
				&& !((AccessibleObject)member).isAccessible();
		if (instanceOfAccesibleAndNotAccessible || !Modifier.isPublic(member.getModifiers())
				|| !Modifier.isPublic(member.getDeclaringClass().getModifiers())) {
			if (member instanceof AccessibleObject) {
				((AccessibleObject)member).setAccessible(true);
			} else {
				throw new RuntimeException("Can not set accessible " + member);
			}
		}
	}

	private static class MethodKey {
		final String methodName;

		final Class<?>[] parameterTypes;

		private MethodKey(String methodName, Class<?>[] parameterTypes) {
			this.methodName = methodName;
			this.parameterTypes = parameterTypes;
		}

		static MethodKey create(String methodName, Class<?>[] parameterTypes) {
			return new MethodKey(methodName, parameterTypes);
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((methodName == null) ? 0 : methodName.hashCode());
			result = prime * result + Arrays.hashCode(parameterTypes);
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj) {
				return true;
			}
			if (obj == null) {
				return false;
			}
			if (getClass() != obj.getClass()) {
				return false;
			}
			MethodKey other = (MethodKey)obj;
			if (methodName == null) {
				if (other.methodName != null) {
					return false;
				}
			} else if (!methodName.equals(other.methodName)) {
				return false;
			}
			if (!Arrays.equals(parameterTypes, other.parameterTypes)) {
				return false;
			}
			return true;
		}

	}
}

Back to the top