BAEL-4384: Invoke a private method using reflection (#10470)
* - initial commit of sample code * BAEL-4209: Moving the localized exception module into core-java-exceptions-3. * BAEL-4209: Removed the old files for localizing exception messages. * Added test case to explain invoking a private method. * Made the code example more useful so we don't look like we're hacking another framework. * Forgot to make the test class and method package-private. * Incorporated Spring ReflectionTestUtils into the code sample. * Updated the Spring Test version to match the article.
This commit is contained in:
		
							parent
							
								
									99373625b2
								
							
						
					
					
						commit
						6a117f9f3a
					
				| @ -14,6 +14,15 @@ | ||||
|         <relativePath>../</relativePath> | ||||
|     </parent> | ||||
|      | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework</groupId> | ||||
|             <artifactId>spring-test</artifactId> | ||||
|             <version>${spring.version}</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
|      | ||||
|     <build> | ||||
|         <finalName>core-java-reflection-2</finalName> | ||||
|         <resources> | ||||
| @ -40,5 +49,6 @@ | ||||
|         <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version> | ||||
|         <source.version>1.8</source.version> | ||||
|         <target.version>1.8</target.version> | ||||
|         <spring.version>5.3.4</spring.version> | ||||
|     </properties> | ||||
| </project> | ||||
| @ -0,0 +1,18 @@ | ||||
| package com.baeldung.reflection.access.privatemethods; | ||||
| 
 | ||||
| public class LongArrayUtil { | ||||
| 
 | ||||
|     public static int indexOf(long[] array, long target) { | ||||
|         return indexOf(array, target, 0, array.length); | ||||
|     } | ||||
| 
 | ||||
|     private static int indexOf(long[] array, long target, int start, int end) { | ||||
|         for (int i = start; i < end; i++) { | ||||
|             if (array[i] == target) { | ||||
|                 return i; | ||||
|             } | ||||
|         } | ||||
|         return -1; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,28 @@ | ||||
| package com.baeldung.reflection.access.privatemethods; | ||||
| 
 | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.springframework.test.util.ReflectionTestUtils; | ||||
| 
 | ||||
| import java.lang.reflect.Method; | ||||
| 
 | ||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
| 
 | ||||
| class InvokePrivateMethodsUnitTest { | ||||
| 
 | ||||
|     private final long[] someLongArray = new long[] { 1L, 2L, 1L, 4L, 2L }; | ||||
| 
 | ||||
|     @Test | ||||
|     void whenSearchingForLongValueInSubsequenceUsingReflection_thenTheCorrectIndexOfTheValueIsReturned() throws Exception { | ||||
|         Method indexOfMethod = LongArrayUtil.class.getDeclaredMethod("indexOf", long[].class, long.class, int.class, int.class); | ||||
|         indexOfMethod.setAccessible(true); | ||||
| 
 | ||||
|         assertEquals(2, indexOfMethod.invoke(LongArrayUtil.class, someLongArray, 1L, 1, someLongArray.length), "The index should be 2."); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     void whenSearchingForLongValueInSubsequenceUsingSpring_thenTheCorrectIndexOfTheValueIsReturned() throws Exception { | ||||
|         int indexOfSearchTarget = ReflectionTestUtils.invokeMethod(LongArrayUtil.class, "indexOf", someLongArray, 1L, 1, someLongArray.length); | ||||
|         assertEquals(2, indexOfSearchTarget, "The index should be 2."); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user