?
Solved

java reflection element to a list by using reflection

Posted on 2014-02-26
10
Medium Priority
?
1,026 Views
Last Modified: 2014-04-03
Hi,

I am reading below link
http://tutorials.jenkov.com/java-reflection/index.html

I have not clearly understood about below lines
For instance, when mapping objects to tables in a database at runtime, like Butterfly Persistence does. Or, when mapping the statements in a script language to method calls on real objects at runtime, like Butterfly Container does when parsing its configuration scripts.



Is java reflection only from j2se6 onwards. Where to find which version has which new features at a glance and their simple explanation.

How is propertyutils used in reflection  in adding element to a list by using reflection. http://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/PropertyUtils.html#getPropertyDescriptor(java.lang.Object

Any detailed example on that.

I also referred below link
http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful

I did not understand below statements
va's static typing system isn't really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called 'doSomething' and then call it if you want to.

So, to give you a code example of this in Java (imagine the object in question is foo) :

Method method = foo.getClass().getMethod("doSomething", null);
method.invoke(foo, null);One very common use case in Java is the usage with annotations. JUnit 4, for example, will use reflection to look through your classes for methods tagged with the @Test annotation, and will then call them when running the unit test.


please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
[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
  • 6
  • 4
10 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 2000 total points
ID: 39889428
Is java reflection only from j2se6 onwards.

Java has had reflection since its very first version. New features that have been added since the first version have a "Since:" field in their Javadocs. For example, Method.getTypeParameters() was added in Java 5, to support generics.

How is propertyutils used in reflection  in adding element to a list by using reflection.

I'm not sure PropertyUtils is related to lists, but I could be misunderstanding your question.

I did not understand below statements

I can't think of a well-designed piece of code where developers would need reflection to see if a method with a certain name were present. There could be a time where one would need to call a static method in a class, without knowing the exact class name ahead of time, but that's pretty loose design and I've never liked it much.

Using reflection for debugging and testing, as JUnit does, is a pretty nice benefit, because the code doing the hard work can stay contained in your JUnit tests, instead of polluting your main code.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39890390
I did not get what it mean by 'knowing object information at runtime' can you please explain what and why and how, when we do it.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 39890531
When you're developing code, you can see all the methods and variables that are available in classes you're using. Like, you know that the method String.trim() is available to you when you're working with an instance of the String class.

Reflection is used when your code does not know what kind of objects it's working with. This happens when the type of object your code has to work with is determined at runtime. This especially happens when writing libraries.

You're usually safer when you design your code in a way that avoids reflection, for the most part. Basically, unless you're forced to use it, try not to.
0
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 
LVL 7

Author Comment

by:gudii9
ID: 39891893
Reflection is used when your code does not know what kind of objects it's working with. This happens when the type of object your code has to work with is determined at runtime. This especially happens when writing libraries.

What would be scenario when to use this in practical world. I do not write libraries.

I see code as below

    PropertyUtils.getPropertyDescriptor(obj, fieldName).getWriteMethod().invoke(obj, list);

What is the meaning of above code while parsing a text document which has not only primitives but also list kind of objects before sending to one other system.
please advise
0
 
LVL 14

Expert Comment

by:CPColin
ID: 39892343
What would be scenario when to use this in practical world. I do not write libraries.

I mentioned this already in my previous comment. You would use reflection when you don't know what kind of object you're dealing with until your code actually runs. If you have no situations in your code where that applies, then you don't need reflection.

What is the meaning of above code...

You will have to read the Javadocs for that class; I am not very familiar with it. You may also want to take that part of the discussion over to your "PropertyUtils java" question.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39954543
What are these ButerFly Component and ButterFly Persistence mentioned in the link. Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39954553
How the reflection on class, method, field are different. Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39954563
Any practical example to understand this reflection concept end to end better with set up on eclipse. please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39976528
Using reflection for debugging and testing, as JUnit does, is a pretty nice benefit, because the code doing the hard work can stay contained in your JUnit tests, instead of polluting your main code.

Can you please elaborate on this.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 39976545
Not really. I made that comment over a month ago and I no longer remember what I was talking about. In general, 99.9% of code does not need reflection. If you're not sure if you need it, you probably don't.

If you have a specific question about reflection, I'll try to answer it.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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