Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ReflectionInvoker |
|
| 2.0;2 |
1 | 620 | /******************************************************************************* |
2 | * Copyright (c) 2008 Ketan Padegaonkar and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Ketan Padegaonkar - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.swtbot.swt.finder.utils.internal; | |
12 | ||
13 | import java.lang.reflect.Method; | |
14 | ||
15 | import org.eclipse.swtbot.swt.finder.results.StringResult; | |
16 | ||
17 | ||
18 | /** | |
19 | * This is an object use to invoke a method using reflections. | |
20 | * | |
21 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
22 | */ | |
23 | public final class ReflectionInvoker implements StringResult { | |
24 | /** | |
25 | * The object to invoke a method on. | |
26 | */ | |
27 | private final Object w; | |
28 | /** | |
29 | * The method to invoke. | |
30 | */ | |
31 | private final String methodName; | |
32 | ||
33 | /** | |
34 | * Constructs this object. | |
35 | * | |
36 | * @param o the object to be invoked on. | |
37 | * @param methodName the method to invoke on the object. | |
38 | */ | |
39 | 620 | public ReflectionInvoker(Object o, String methodName) { |
40 | 620 | w = o; |
41 | 620 | this.methodName = methodName; |
42 | 620 | } |
43 | ||
44 | /** | |
45 | * Runs the processing to trigger the method to be invoked. | |
46 | * | |
47 | * @see org.eclipse.swtbot.swt.finder.results.StringResult#run() | |
48 | * @return The results of the invoke. | |
49 | */ | |
50 | public String run() { | |
51 | 620 | String result = ""; //$NON-NLS-1$ |
52 | try { | |
53 | 620 | Method method = w.getClass().getMethod(methodName, new Class[]{}); |
54 | 619 | Object invoke = method.invoke(w, new Object[0]); |
55 | 619 | if (invoke != null) |
56 | 619 | result = invoke.toString(); |
57 | 1 | } catch (Exception e) { |
58 | // do nothing | |
59 | } | |
60 | 620 | return result; |
61 | } | |
62 | } |