Solved

Private class members in reflected class

Posted on 2012-04-12
3
285 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 87
servlet  URL Rewriting 1 28
Should localization be done inside spring controller 5 12
servlet example 17 10
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

861 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now