Solved

Private class members in reflected class

Posted on 2012-04-12
3
287 Views
Last Modified: 2012-04-16
Here:
http://docs.oracle.com/javase/tutorial/reflect/index.html
is a discussion of reflection. One part says:
Exposure of Internals
    Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.

How and why does this happen? Why do class members lose their access modifier definition?
0
Comment
Question by:allelopath
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
ramazanyich earned 350 total points
ID: 37841541
IT is due to setAccessible API which is available in reflection APi.
lets say you have class:
public class PrivateObject {

  private String privateString = null;

  public PrivateObject(String privateString) {
    this.privateString = privateString;
  }
}

by usin reflection API you can get privateString value

Field privateStringField = PrivateObject.class.
            getDeclaredField("privateString");

privateStringField.setAccessible(true);

String fieldValue = (String) privateStringField.get(privateObject);
System.out.println("fieldValue = " + fieldValue);
0
 
LVL 19

Expert Comment

by:ramazanyich
ID: 37841543
same setAccesible API is applicable for Method
0
 
LVL 12

Assisted Solution

by:Sharon Seth
Sharon Seth earned 150 total points
ID: 37841972
Reflective code breaks abstractions

Say the following is a property in your class and you have a setter for this property:

private List  items;

public void setItems(Map items)
{
   this.items = items;
}      

Here you have declared an abstraction over the underlying collection for items . The actual collection used can be any of the List implementations

When you go for reflection and try to access the setter on items , but items was assigned  an ArrayList , then the reflection mechanism would throw  NoSuchMethodException , clearly breaking the abstraction you had defined .
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question