Solved

Private class members in reflected class

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 79
Safari, Firefox, IE, Java, Plugin, Java Security error 13 41
Java and GPO 11 47
Problem to start Neon 20 54
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 This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

746 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

11 Experts available now in Live!

Get 1:1 Help Now