Ronald Djunaedi

About Ronald Djunaedi

Ronald is a senior Java Developer, specialization in J2EE Programming and its technologies and frameworks. Been a trainer for a while, specialization in training subjects: Java Fundamentals, OOP and J2EE.

Calling private Java methods publicly?

We Java developers, known 4 access modifiers in Java: private, protected, public, and package. Well, except for the private, the last three, can be called from outside of the class by inheritance, same package or from the instance.

Now, the common question, can private be called publicly (from outside class)? well the answer is NO and YES. No when you use ‘usual’ way to access it, and YES when you ‘hack’ into it using the Reflection API provided by Java itself.

Well okay, now just write the code that we will hack into. I called it as “TheVictim

package com.namex.hack;

public class TheVictim {
 private void hackTest() {
  System.out.println("hackTest called");
 }

 private static void hackTestStatic() {
  System.out.println("hackTestStatic called");
 }

}

Now after that, just follow my code and try to run it. I guarantee that if you followed it right, you will get TheVictim to call both of the hackTest and hackTestStatic. And you can see the output on your screen.

package com.namex.hack;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class HackTest {
 public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

  Class c = TheVictim.class;

  Method[] ms = c.getDeclaredMethods();

  for (Method each : ms) {
   String methodName = each.getName();
   each.setAccessible(true); // this is the key
   if (Modifier.isPrivate(each.getModifiers())) {
    
    if (Modifier.isStatic(each.getModifiers())) {
     // static doesnt require the instance to call it.
     each.invoke(TheVictim.class, new Object[] {});
    } else {
     each.invoke(new TheVictim(), new Object[] {});
    }
   }
  }

 }
}

Output example:

hackTestStatic called
hackTest called

Okay, this tutorial has met its purpose. Now you know the Reflection API of java is very powerful feature of programming language. And it’s all up to you to modify or even extend it for your own purpose. Have fun with Java :)

Reference: Calling private methods publicly ? from our JCG partner Ronald Djunaedi at the Naming Exception blog.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


three × 7 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books