Java Programming/Reflection/Accessing Private Features With Reflection Learning | Java Programming/Reflection/Accessing Private Features With Reflection Facts and Resources | DefaultLogic For Business

Java Programming/Reflection/Accessing Private Features With Reflection

All features of a class can be obtained via reflection, including access to private methods & variables. But not always see [1]. Let us look at the following example:

Computer code Code listing 10.3:
1 public class Secret {
2   private String secretCode = "It's a secret";
4   private String getSecretCode {
5     return secretCode;     
6   }
7 }

Although the field and method are marked private, the following class shows that it is possible to access the private features of a class:

Computer code Code listing 10.4:
 1 import java.lang.reflect.Field;
 2 import java.lang.reflect.InvocationTargetException;
 3 import java.lang.reflect.Method;
 5 public class Hacker {
 7    private static final Object[] EMPTY = {};
 9    public void reflect throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
10      Secret instance = new Secret;
11      Class<?> secretClass = instance.getClass;
13      // Print all the method names & execution result
14      Method methods[] = secretClass.getDeclaredMethods;
15      System.out.println("Access all the methods");
16      for (Method method : methods) {
17         System.out.println("Method Name: " + method.getName);
18         System.out.println("Return type: " + method.getReturnType);
19         method.setAccessible(true);
20         System.out.println(method.invoke(instance, EMPTY) + "\n");
21      }
23      // Print all the field names & values
24      Field fields[] = secretClass.getDeclaredFields;
25      System.out.println("Access all the fields");
26      for (Field field : fields) {
27         System.out.println("Field Name: " + field.getName);
28         field.setAccessible(true);
29         System.out.println(field.get(instance) + "\n");
30      }
31   }
33   public static void main(String[] args) {
34     Hacker newHacker = new Hacker;
36     try {
37       newHacker.reflect;
38     } catch (Exception e) {
39       e.printStackTrace;
40     }
41   }
42 }
Standard input or output 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

To do:
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 habit[1];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-time[2].

  1. ? What's the best way of unit testing private methods?, March 7, 2011
  2. ? Reflection API injected at compile-time

To do:
Add some exercises like the ones in Variables

  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.



Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.

Manage research, learning and skills at Create an account using LinkedIn to manage and organize your omni-channel knowledge. is like a shopping cart for information -- helping you to save, discuss and share.

  Contact Us