[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Private class members in reflected class

Posted on 2012-04-12
3
Medium Priority
?
298 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 1400 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 600 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

831 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