I have a question regarding java reflection.
I need some kind of way to get the name of a specific field in a java class. Something that will work like this:
class X{
int x;
String getNameOfProperty(Object o){
....
}
}
Then, when calling
System.out.println(getNameOfProperty(x));
I would like to get "x";
I have managed to get the fields of a class using
X.class.getDeclaredFields();
but then i don't have any link between the property and the Field object associated with it.
-
1Do you need to get the runtime value of a field?Redandwhite– Redandwhite09/07/2012 12:34:53Commented Sep 7, 2012 at 12:34
-
1Did you use PropertyUtils?Andrzej Jozwik– Andrzej Jozwik09/07/2012 12:42:47Commented Sep 7, 2012 at 12:42
-
What is the user feature that the app. needs to implement? Explain it to me as if I were a potential end user with no tech. knowledge. Doing so will probably lead to better advice.Andrew Thompson– Andrew Thompson09/07/2012 13:08:42Commented Sep 7, 2012 at 13:08
-
I have a context class that has a considerable number of properties. The user populates some of them using a builder in order to customize the behavior of a feature. Some of these properties MUST be populated. I want to check them for null and throw an exception if some of them are so. I would like the exception message to contain the name of the property so that the user can check her code and see which property she omitted to assign. I don't want to hard code the name in the message.sebi– sebi09/07/2012 13:14:53Commented Sep 7, 2012 at 13:14
3 Answers 3
You can use Abstract Syntax Tree for this purpose. For Eclipse you can find details here.
You can also use following
-
Yeah, this is definitely a way to do, but i'm a novice in AST and they seem too complicated for what i need. I mean, i don't have a lot of time to invest in this... It's a very good answer though. +1sebi– sebi09/07/2012 12:48:19Commented Sep 7, 2012 at 12:48
It is really unclear what you want to ask here.
If I have understood your question correctly Field
class in java allows you to get value of object for that field at runtime. getDeclaredFields
return you Field[]
.
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/Field.html
Check get(Object o)
method
Returns the value of the field represented by this Field, on the specified object. The value is automatically wrapped in an object if it has a primitive type.
I think you can use getName() in the java.lang.reflect.Field class. You have got a lot of the fields alread in your code in your question
Consider this code in core.Test1 class. THe call to fieldName will return 'i' and 'd' and 42 and 6. (not that I used getDouble here and int fits into Dboule so there is no problem. You may have to check the type of the field you are tring to get a value for first.)
Is that what you are after?
private int i = 42;
private double d = 6.0d;
@Test
public void test() {
try {
Class<?> clazz = Class.forName("core.Test1");
Field[] fieldlist = clazz.getDeclaredFields();
for (int i = 0; i < fieldlist.length; i++) {
System.out.println("field name = " + fieldlist[i].getName());
System.out.println("value = " + fieldlist[i].getDouble(this));
System.out.println("field type = " + fieldlist[i].getType());
System.out.println("modifiers = " + Modifier.toString(fieldlist[i].getModifiers()));
}
}
catch (Throwable e) {
System.err.println(e);
}
}
-
Agree, but how do i know at runtime that fieldlist[i] is an abstraction of my property x? It could be an abstraction of some other property i might have, like y. I need this link.sebi– sebi09/07/2012 12:52:03Commented Sep 7, 2012 at 12:52