Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Java Programming/Reflection/Accessing Private Features with Reflection

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Accessing Private Features with Reflection __NOTOC__ All features of a class can be obtained via reflection, including access to methods & variables. But not always see . Let us look at the following example: Code listing 10.3: Secret.java public class Secret { private String secretCode = "It's a secret"; private String getSecretCode() { return secretCode; } } Although the field and method are marked , the following class shows that it is possible to access the features of a class: Code listing 10.4: Hacker.java import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Hacker { private static final Object[] EMPTY = {}; public void reflect() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { Secret instance = new Secret(); Class<?> secretClass = instance.getClass(); // Print all the method names & execution result Method methods[] = secretClass.getDeclaredMethods(); System.out.println("Access all the methods"); for (Method method : methods) { System.out.println("Method Name: " + method.getName()); System.out.println("Return type: " + method.getReturnType()); method.setAccessible(true); System.out.println(method.invoke(instance, EMPTY) + "\n"); } // Print all the field names & values Field fields[] = secretClass.getDeclaredFields(); System.out.println("Access all the fields"); for (Field field : fields) { System.out.println("Field Name: " + field.getName()); field.setAccessible(true); System.out.println(field.get(instance) + "\n"); } } public static void main(String[] args) { Hacker newHacker = new Hacker(); try { newHacker.reflect(); } catch (Exception e) { e.printStackTrace(); } } }Console for Code listing 10.4 Access all the methods Method Name: getSecretCode Return type: class java.lang.String It's a secret Access all the fields Field Name: secretCode It's a secret We need to add some explanation of what is going on here. JUnit - Test Private methods JUnit's are unit test cases, used to test the Java programs. Now you know how to test a private method using Reflection in JUnit. There's a long-standing debate on whether testing private members is a good habitWhat's the best way of unit testing private methods?, March 7, 2011;There are cases where you want to make sure a class exhibited the right behavior while not making the fields that need checking to assert that public (as it's generally considered bad practice to create accessors to a class just for the sake of a unit test). There are also cases when you can greatly simplify a test case by using reflection to test all smaller private methods (and their various branches), then test the main function. With dp4j it is possible to test private members without directly using the Reflection API but simply accessing them as if they were accessible from the testing method; dp4j injects the needed Reflection code at compile-timeReflection API injected at compile-time . Add some exercises like the ones in Variables 50%