4

I am trying a certain program where I have to use Java Reflection API. Specifically, I need to know the actual data type of the fields in a given class. The way I found for doing this is to use the java.lang.reflect.Field class. This class exposes methods like getGenericType().getTypeName() and getType().getTypeName(). These return java.lang.String representation for the fully qualified names of the referenced data types in the class. For instance if my class is as follows:

class MyClass{Integer age;}

For the above scenario I would get that the data type of "age" is "java.lang.Integer type in String representation. However, the String representation would not be of much use in my case since I need the actual Integer data type not just an textual/String representation of its name. Is there any way to do so.

7
  • 6
    The getType() method of java.lang.reflect.Field already gives you the type, in the form of an instance of Class. What else exactly do you think you need? Commented Jul 20, 2015 at 7:38
  • 1
    Yeah, couldn't you just get rid of .getTypeName() and be done with it? Commented Jul 20, 2015 at 7:39
  • I solved a similar proble using Class clazz = field.getDeclaringClass(); Commented Jul 20, 2015 at 7:40
  • @Skizzo That gets you the class that contains the field, not the class of the field itself. The OP seems to need to get the type of the field itself. Commented Jul 20, 2015 at 7:42
  • 1
    Thanks every one for your inputs. @Jesper, you are correct that the Field.getType() would give me the Class representation of actual Data Type. But how to extract that actual data type from the Class representation; I do not think that there is any method in Class API which gives access to the data type which it represents. Although, there are methods like getSimpleName() and getTypeName(), but again they return String representation of the actual Data Type. It would immensely help me if there be some method on whose return type I may utilize instanceof operator, for instance. Commented Jul 20, 2015 at 14:28

1 Answer 1

2

You can use the class returned by Field getType method. See the example code bellow:

import java.lang.reflect.Field;

    public class Main {

      public static void main(String[] args) {
        Field[] declaredFields = MyClass.class.getDeclaredFields();
        for (Field declaredField : declaredFields) {
          Class<?> fieldType = declaredField.getType();
          String result=fieldType.getName();
          System.out.println(result);
        }
      }

    }

But be careful with primitive data types (example: boolean). In this case the fieldType.getName() function returns a uninstantiated string (boolean). You can handle it differently. The list of internal data types is here.

Sign up to request clarification or add additional context in comments.

1 Comment

Primitive type Class objects return uninstantiated Strings? To handle primitive types, you can make use of Class#isPrimitive; the link you provided is missing void.class/Void.TYPE.

Your Answer

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.