4

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.

asked Sep 7, 2012 at 12:29
4
  • 1
    Do you need to get the runtime value of a field? Commented Sep 7, 2012 at 12:34
  • 1
    Did you use PropertyUtils? Commented 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. Commented 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. Commented Sep 7, 2012 at 13:14

3 Answers 3

4

You can use Abstract Syntax Tree for this purpose. For Eclipse you can find details here.

You can also use following

-ANTLR

-javaparser

answered Sep 7, 2012 at 12:36
1
  • 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. +1 Commented Sep 7, 2012 at 12:48
1

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.

answered Sep 7, 2012 at 12:42
0

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);
 }
} 
answered Sep 7, 2012 at 12:44
1
  • 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. Commented Sep 7, 2012 at 12:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.