BAEL-870 How to Call a Method During Runtime Using Reflection (#1806)

This commit is contained in:
Raúl Juárez 2017-05-07 19:54:23 -05:00 committed by KevinGilmore
parent 46d9022879
commit dcc4045371
3 changed files with 11 additions and 11 deletions

View File

@ -2,19 +2,19 @@ package com.baeldung.java.reflection;
public class Operations { public class Operations {
public double sum(int a, double b) { public double publicSum(int a, double b) {
return a + b; return a + b;
} }
public static double multiply(float a, long b) { public static double publicStaticMultiply(float a, long b) {
return a * b; return a * b;
} }
private boolean and(boolean a, boolean b) { private boolean privateAnd(boolean a, boolean b) {
return a && b; return a && b;
} }
protected int max(int a, int b) { protected int protectedMax(int a, int b) {
return a > b ? a : b; return a > b ? a : b;
} }

View File

@ -16,7 +16,7 @@ public class OperationsUnitTest {
@Test(expected = IllegalAccessException.class) @Test(expected = IllegalAccessException.class)
public void givenObject_whenInvokePrivateMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void givenObject_whenInvokePrivateMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method andPrivateMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class); Method andPrivateMethod = Operations.class.getDeclaredMethod("privateAnd", boolean.class, boolean.class);
Operations operationsInstance = new Operations(); Operations operationsInstance = new Operations();
Boolean result = (Boolean) andPrivateMethod.invoke(operationsInstance, true, false); Boolean result = (Boolean) andPrivateMethod.invoke(operationsInstance, true, false);
@ -26,7 +26,7 @@ public class OperationsUnitTest {
@Test @Test
public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception { public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception {
Method andPrivatedMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class); Method andPrivatedMethod = Operations.class.getDeclaredMethod("privateAnd", boolean.class, boolean.class);
andPrivatedMethod.setAccessible(true); andPrivatedMethod.setAccessible(true);
Operations operationsInstance = new Operations(); Operations operationsInstance = new Operations();
@ -37,7 +37,7 @@ public class OperationsUnitTest {
@Test @Test
public void givenObject_whenInvokePublicMethod_thenCorrect() throws Exception { public void givenObject_whenInvokePublicMethod_thenCorrect() throws Exception {
Method sumInstanceMethod = Operations.class.getMethod("sum", int.class, double.class); Method sumInstanceMethod = Operations.class.getMethod("publicSum", int.class, double.class);
Operations operationsInstance = new Operations(); Operations operationsInstance = new Operations();
Double result = (Double) sumInstanceMethod.invoke(operationsInstance, 1, 3); Double result = (Double) sumInstanceMethod.invoke(operationsInstance, 1, 3);
@ -47,7 +47,7 @@ public class OperationsUnitTest {
@Test @Test
public void givenObject_whenInvokeStaticMethod_thenCorrect() throws Exception { public void givenObject_whenInvokeStaticMethod_thenCorrect() throws Exception {
Method multiplyStaticMethod = Operations.class.getDeclaredMethod("multiply", float.class, long.class); Method multiplyStaticMethod = Operations.class.getDeclaredMethod("publicStaticMultiply", float.class, long.class);
Double result = (Double) multiplyStaticMethod.invoke(null, 3.5f, 2); Double result = (Double) multiplyStaticMethod.invoke(null, 3.5f, 2);

View File

@ -16,17 +16,17 @@ public class MoreOperationsUnitTest {
@Test(expected = IllegalAccessException.class) @Test(expected = IllegalAccessException.class)
public void givenObject_whenInvokeProtectedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void givenObject_whenInvokeProtectedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method maxProtectedMethod = Operations.class.getDeclaredMethod("max", int.class, int.class); Method maxProtectedMethod = Operations.class.getDeclaredMethod("protectedMax", int.class, int.class);
Operations operationsInstance = new Operations(); Operations operationsInstance = new Operations();
Integer result = (Integer) maxProtectedMethod.invoke(operationsInstance, 2, 4); Integer result = (Integer) maxProtectedMethod.invoke(operationsInstance, 2, 4);
System.out.println("result = " + result);
assertThat(result, equalTo(4)); assertThat(result, equalTo(4));
} }
@Test @Test
public void givenObject_whenInvokeProtectedMethod_thenCorrect() throws Exception { public void givenObject_whenInvokeProtectedMethod_thenCorrect() throws Exception {
Method maxProtectedMethod = Operations.class.getDeclaredMethod("max", int.class, int.class); Method maxProtectedMethod = Operations.class.getDeclaredMethod("protectedMax", int.class, int.class);
maxProtectedMethod.setAccessible(true); maxProtectedMethod.setAccessible(true);
Operations operationsInstance = new Operations(); Operations operationsInstance = new Operations();