Accessing Private Variable

Reflection in java:
Reflection is the mechanism by which Java exposes the features of a class during runtime, allowing Java programs to enumerate and access a class’ methods, fields, and constructors as objects. In other words, there are object based mirrors which reflect the Java object model, and you can use these objects to access an object’s features using runtime API constructs instead of compile time language constructs.

Each object instance has a getClass() method, inherited from java.lang.Object, which returns an object with the runtime representation of that object’s class; this object is an instance of the java.lang.Class.This object in turn has methods which return the fields, methods, constructors, superclass, and other properties of that class.

You can use these reflection objects to access fields, invoke methods, or instantiate instances, all without having compile time dependencies on those features. The Java runtime provides the corresponding classes for reflection. Most of the Java classes which support reflection are in the java.lang.reflect package.

Accessing Private Features with Reflection:
All features of a class can be obtained via Reflection, including Access to private methods & variables.Setting the Accessible flag in a reflected object of java.lang.reflect.AccessibleObject class allows us to read/modify a private variable that we normally wouldn’t have permission to. However, the access check can only be supressed if approved by installed SecurityManager.
The Classes java.lang.reflect.Field, java.lang.reflect.Method and java.lang.reflect.Constructor can inherit this behavior from java.lang.reflect.AccessibleObject class. The following example demonstrates read/modify access to a private variable which is normally inaccessible.

SimpleKeyPair.java

package com;

public class SimpleKeyPair {

private String privateKey = “Welcome SimpleKeyPair “; // private field

}

PrivateMemberAccessTest.java

package com;

import java.lang.reflect.Field;

public class PrivateMemberAccessTest {

public static void main(String[] args) throws Exception {

SimpleKeyPair keyPair = new SimpleKeyPair();

Class c = keyPair.getClass();

// get the reflected object

Field field = c.getDeclaredField(”privateKey”);

// set accessible true

field.setAccessible(true);

System.out.println(”Value of privateKey: ” + field.get(keyPair));

/* prints “Welcome SimpleKeyPair */

// modify the member varaible

field.set(keyPair, “Welcome PrivateMemberAccessTest”);

System.out.println(”Value of privateKey: ” + field.get(keyPair));

/* prints “PrivateMemberAccessTest” */

}

}

Output

=====

Value of privateKey: Welcome SimpleKeyPair

Value of privateKey: PrivateMemberAccessTest

Popularity: 12% [?]

Related posts:

  1. Dot Net Interview Questions – Part 3
  2. Dot Net Interview Questions – Part 1
  3. set focus on textbox value at the end
  4. Mail Merge Using C#
You can leave a response, or trackback from your own site.

One Response to “Accessing Private Variable”

  1. Dorothy says:

    And this is why U read http://www.fordevs.com. Love the post.

Leave a Reply

Designed by: Business Web Hosting | Thanks to Buy Icons, travel tips and Used Cars