Skip to content

Commit a5258c2

Browse files
committed
Use a single HelperFunction for invoking methods
Earlier there were separate helperfunction methods for invoking methdods with and without arguments, but since the method that will be invoked is now retrieved using MethodUtils.getMatchingAccessibleMethod, these can be combined into a single helperfunction.
1 parent 5e2f6c3 commit a5258c2

File tree

4 files changed

+12
-56
lines changed

4 files changed

+12
-56
lines changed

src/main/java/javafxlibrary/keywords/AdditionalKeywords/ConvenienceKeywords.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,12 @@ public void bringStageToFront(Stage stage) {
110110
+ "| ${node text}= | Call Object Method | ${node} | getText | \n")
111111
@ArgumentNames({ "object", "method", "*arguments=" })
112112
public Object callObjectMethod(Object object, String method, Object... arguments) {
113-
/* Workaround for overloading the keyword, Javalib Core seems to have a bug which causes overloaded keywords that
114-
take varargs throw IllegalArgumentException occasionally. Some of the calls for the base keyword get directed
115-
to the overloaded keyword, so the method invocation fails because of incorrect arguments. */
116-
117113
/* Javalib Core changes all parameters to Strings after runKeywords automatic argument replacement, so arguments
118114
are replaced with objects from objectMap here instead. */
119115
object = HelperFunctions.useMappedObject(object);
120116
Object[] tempArgs = HelperFunctions.checkMethodArguments(arguments);
121117
Object[] finalArgs = HelperFunctions.useMappedObjects(tempArgs);
122-
123-
Object result;
124-
125-
if (finalArgs.length == 0)
126-
result = callMethod(object, method, false);
127-
else
128-
result = callMethod(object, method, finalArgs, false);
118+
Object result = callMethod(object, method, finalArgs, false);
129119

130120
if (result != null)
131121
return mapObject(result);
@@ -140,15 +130,11 @@ public Object callObjectMethod(Object object, String method, Object... arguments
140130
+ "| Call Object Method In Fx Application Thread | ${node} | maxHeight | (boolean)false | \n")
141131
@ArgumentNames({ "object", "method", "*arguments=" })
142132
public void callObjectMethodInFxApplicationThread(Object object, String method, Object... arguments) {
143-
// Check callObjectMethod for info about argument replacing and overloading in these keywords.
133+
// Check callObjectMethod for info about argument replacing.
144134
object = HelperFunctions.useMappedObject(object);
145135
Object[] tempArgs = HelperFunctions.checkMethodArguments(arguments);
146136
Object[] finalArgs = HelperFunctions.useMappedObjects(tempArgs);
147-
148-
if (finalArgs.length == 0)
149-
callMethod(object, method, true);
150-
else
151-
callMethod(object, method, finalArgs, true);
137+
callMethod(object, method, finalArgs, true);
152138
}
153139

154140
@Deprecated

src/main/java/javafxlibrary/utils/HelperFunctions.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -179,36 +179,6 @@ public static List<Object> mapObjects(Iterable objects) {
179179
return keys;
180180
}
181181

182-
public static Object callMethod(Object o, String method, boolean runLater) {
183-
RobotLog.info("Calling method " + method + " of object " + o);
184-
Class<?> c = o.getClass();
185-
186-
try {
187-
Method m = c.getMethod(method, null);
188-
189-
if (!runLater) {
190-
return m.invoke(o, null);
191-
} else {
192-
Platform.runLater(() -> {
193-
try {
194-
m.invoke(o, null);
195-
} catch (InvocationTargetException | IllegalAccessException e) {
196-
throw new JavaFXLibraryNonFatalException("Couldn't execute Call Method: " + e.getCause().getMessage());
197-
}
198-
});
199-
}
200-
} catch (InvocationTargetException | IllegalAccessException e) {
201-
throw new JavaFXLibraryNonFatalException("Couldn't execute Call Method: " + e.getCause().getMessage());
202-
} catch (NoSuchMethodException e) {
203-
throw new JavaFXLibraryNonFatalException(c + " has no method \"" + method + "()\"");
204-
} catch (JavaFXLibraryNonFatalException e) {
205-
throw e;
206-
} catch (Exception e) {
207-
throw new JavaFXLibraryNonFatalException("Couldn't execute Call Method: " + e.getMessage());
208-
}
209-
return null;
210-
}
211-
212182
public static Object callMethod(Object o, String method, Object[] arguments, boolean runLater) {
213183
RobotLog.info("Calling method \"" + method + "\" of object \"" + o + "\" with arguments \""
214184
+ Arrays.toString(arguments) + "\"");
@@ -232,16 +202,16 @@ public static Object callMethod(Object o, String method, Object[] arguments, boo
232202
try {
233203
m.invoke(o, arguments);
234204
} catch (IllegalAccessException | InvocationTargetException e) {
235-
throw new JavaFXLibraryNonFatalException("Couldn't execute Call Method: " + e.getMessage());
205+
throw new JavaFXLibraryNonFatalException("Couldn't execute Call Method: " + e.getCause().getMessage());
236206
}
237207
});
238208
}
239209
} catch (IllegalAccessException | InvocationTargetException e) {
240-
throw new JavaFXLibraryNonFatalException("Couldn't execute Call Method: " + e.getMessage());
210+
throw new JavaFXLibraryNonFatalException("Couldn't execute Call Method: " + e.getCause().getMessage());
241211
} catch (JavaFXLibraryNonFatalException e) {
242212
throw e;
243213
} catch (Exception e) {
244-
throw new JavaFXLibraryNonFatalException("Couldn't execute Call Method: " + e.getMessage(), e);
214+
throw new JavaFXLibraryNonFatalException("Couldn't execute Call Method: " + e.getCause().getMessage(), e);
245215
}
246216
return null;
247217
}

src/test/java/javafxlibrary/utils/HelperFunctionsTests/CallMethodTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public class CallMethodTest extends TestFxAdapterTest {
2323
@Test
2424
public void callMethod_InSameThread_NoArgs_WithReturnValue() {
2525
String name = "JavaFXLibrary";
26-
String result = (String) HelperFunctions.callMethod(name, "toUpperCase", false);
26+
String result = (String) HelperFunctions.callMethod(name, "toUpperCase", new Object[]{}, false);
2727
Assert.assertEquals("JAVAFXLIBRARY", result);
2828
}
2929

3030
@Test
3131
public void callMethod_InSameThread_NoArgs_NoReturnValue() {
3232
TestPoint testPoint = new TestPoint(0, 0);
33-
HelperFunctions.callMethod(testPoint, "setLocationTo2017", false);
33+
HelperFunctions.callMethod(testPoint, "setLocationTo2017", new Object[]{}, false);
3434
Assert.assertEquals(20, testPoint.getX(), 0);
3535
Assert.assertEquals(17, testPoint.getY(), 0);
3636
}
@@ -71,7 +71,7 @@ public void callMethod_InJavaFXThread_WithArgs() {
7171
public void callMethod_InJavaFXThread_NoArgs() {
7272
Stage stage = setupStageInJavaFXThread();
7373
Assert.assertFalse(stage.isShowing());
74-
HelperFunctions.callMethod(stage, "show", true);
74+
HelperFunctions.callMethod(stage, "show", new Object[]{}, true);
7575
waitForEventsInJavaFXThread();
7676
Assert.assertTrue(stage.isShowing());
7777
Platform.runLater(() -> stage.close());
@@ -82,7 +82,7 @@ public void callMethod_InWrongThread() {
8282
Stage stage = setupStageInJavaFXThread();
8383
thrown.expect(JavaFXLibraryNonFatalException.class);
8484
thrown.expectMessage("Couldn't execute Call Method: Not on FX application thread; currentThread = main");
85-
HelperFunctions.callMethod(stage, "show", false);
85+
HelperFunctions.callMethod(stage, "show", new Object[]{}, false);
8686
}
8787

8888
@Test

src/test/robotframework/acceptance/MiscTests.robot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Call Method That Does Not Exist
3535
Set Test Application javafxlibrary.testapps.TestBoundsLocation
3636
${NODE} Find \#green
3737
${MSG} Run Keyword And Expect Error * Call Object Method ${NODE} fakeMethod
38-
Should Be Equal ${MSG} class javafx.scene.shape.Rectangle has no method "fakeMethod()"
38+
Should Be Equal ${MSG} class javafx.scene.shape.Rectangle has no method "fakeMethod" with arguments []
3939

4040
Call Method With Wrong Types
4141
[Tags] negative smoke
@@ -49,7 +49,7 @@ Call Method That Does Not Exist In Fx Application Thread
4949
Set Test Application javafxlibrary.testapps.TestBoundsLocation
5050
${NODE} Find \#green
5151
${MSG} Run Keyword And Expect Error * Call Object Method In Fx Application Thread ${NODE} fakeMethod
52-
Should Be Equal ${MSG} class javafx.scene.shape.Rectangle has no method "fakeMethod()"
52+
Should Be Equal ${MSG} class javafx.scene.shape.Rectangle has no method "fakeMethod" with arguments []
5353

5454
Call Method With Wrong Types In Fx Application Thread
5555
[Tags] negative smoke

0 commit comments

Comments
 (0)