• Post Reply Bookmark Topic Watch Topic
  • New Topic

get Property-Field-Name via Reflection  RSS feed

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

how can I get the name of the field via Reflection?

for example:

The field in my object calls "prop1" (and has getter/setters):

now I want the NAME (not the value) of prop1: (the name is obviously 'prop1')



but this does not work.

Any Ideas?
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getDeclaredFields[]
does not help me,
cause I only want to have the name of a specific field (and not all).
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterate over the return of getDeclaredFields() to find the one you want.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when iterating to getDeclaredFields, I have to know the field name what I am searching for to be able to extract it (Or?)

for example:



if(fields[i].getName()=="...") involves that I know the field name before getting it, but I want to get the field name via the property.

Something like:



does not exist.

I only want the Meta-Information (the field-name) of a specified field.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try checking for type and value:
(assuming prop1 is not null)
However, this a) does not handle primitives well (the isInstance part), and b) there could be multiple fields that match.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your question is "If my class has methods named "getX()" and "setX()", I think there's probably a field named something like "X". How can I find out exactly what it's called?"

The answer is that there doesn't have to be any such field, by any name, so reflection cannot help you. For example, getX() might look like



Even if the method returns a variable, it might be named "variableX" or "valueOfX" or anything; or it might be an element of an array, or a value in a HashMap, or a variety of other things.

If you need to find this out about one particular class, you can disassemble it with the "javap" tool and look at the bytecode to see how the method is implemented. But you're not going to be able to reliably figure this out from code -- at least not without allowing for a lot of alternative possibilities, including the possibility that there's no variable at all. You'd do this using a bytecode library like BCEL.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your question is "If my class has methods named "getX()" and "setX()", I think there's probably a field named something like "X". How can I find out exactly what it's called?"


I have solved it via Enum's:



So I can get the Name of the property as a String-value. However, with Reflection-Api I cannot say:




 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But to make things complex. Reflection-Api allows us to write flexible Code. So look at that:



So you see, I have two for-Loops with the same logic-procedure but different Objects and Properties. I want to use the Reflection-Api to improve doAction()-Method:





How should be the getObjectByIds()-Method? Is that possible via Reflection-Api.
Do I have to use instanceof-Operator for each Class?

What is the best solution for such scenarios?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nimo frey wrote:
What is the best solution for such scenarios?


Java is a (somewhat) strongly-typed language, and so the best Java solution is your original one with the two different methods. Using complicated reflection code to avoid a few overloaded methods is considered bad style. If you want to write the other kind of code, maybe you should consider Groovy, a dynamic language for the JVM in which you can easily do so.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this:
You simply pass the collection to add to to this method as well. The method type T makes sure that the collection can store objects of the given class.

Now I added a cast to T that will cause a compiler warning. I think this cast is unnecessary because entityManager.find already return a T, but if not you could use clazz.cast(...) instead of the (T) part.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello rob,

yes that works great! thank you !

the only thing I do not understand is, why to have to write this:



instead of this:



I understand, that this is a generic method and <T> the generic collection, but for what "<T> void"?
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use a generic method with a Map:



So I guess, I have to cast "c" somehow to a HashMap, but how?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, c already is a Map, so you shouldn't need to cast it. You need to look which methods there are in the Map interface: is there a removeAll method?
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, now it works!

Generics are really cool.

Can I use anonymous generic inner-methods within a method in java?




 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found out that Closures or lamba-expressions (methods within methods) cannot be expresses in Java.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nimo frey wrote:hello rob,

yes that works great! thank you !

the only thing I do not understand is, why to have to write this:



instead of this:



I understand, that this is a generic method and <T> the generic collection, but for what "<T> void"?

<T> is the method-specific generic type; just like you can have a class generic type like List<E>, so can a method. You have to declare that type before you can use it; what is T in the second line?

The void is just the method return type.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<T> is the method-specific generic type


Okay now it s clear (<T>void instead of void <T> or List<T> as method-declaration is a little inhomogene

But something is wrong with this method-declaration:



 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map<String,Collection<X>> is not the same as Map<String,List<X>>.

Perhaps you can make it even more generic:

This way, your telling the compiler the map's values must be Collection, or anything that extends (e.g. List) or implements Collection - as long as their generic types are ? super T.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh now it s clear, thank you!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!