DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Calling Private Methods Publicly?

Calling Private Methods Publicly?

Ronald Daniel user avatar by
Ronald Daniel
·
Jun. 12, 12 · Java Zone · Interview
Like (0)
Save
Tweet
11.34K Views

Join the DZone community and get the full member experience.

Join For Free

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 :)

Java (programming language) Hack (falconry) dev Inheritance (object-oriented programming) IT

Published at DZone with permission of Ronald Daniel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Unit Vs Integration Testing: What's the Difference?
  • The Evolution of Configuration Management: IaC vs. GitOps
  • 6 Things Startups Can Do to Avoid Tech Debt
  • Stupid Things Orgs Do That Kill Productivity w/ Netflix, FloSports & Refactoring.club

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo