Solved

Private class members in reflected class

Posted on 2012-04-12
3
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

724 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