How to get fields in an object through reflection?

I have an object (mostly VO) in Java, and I don't know its type.
I need to get values ​​that are not null in this object.

How can I do that?

+48
java reflection
Jun 07 '10 at 12:45
source share
3 answers

You can use Class#getDeclaredFields() to get all declared fields of this class. You can use Field#get() to get the value.

In short:

 Object someObject = getItSomehow(); for (Field field : someObject.getClass().getDeclaredFields()) { field.setAccessible(true); // You might want to set modifier to public first. Object value = field.get(someObject); if (value != null) { System.out.println(field.getName() + "=" + value); } } 

To learn more about reflection, check out Sun's tutorial on this .




However, fields do not necessarily all represent VO properties. You prefer to define public methods, starting with get or is , and then calling it to capture the values ​​of real properties.

 for (Method method : someObject.getClass().getDeclaredMethods()) { if (Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0 && method.getReturnType() != void.class && (method.getName().startsWith("get") || method.getName().startsWith("is")) ) { Object value = method.invoke(someObject); if (value != null) { System.out.println(method.getName() + "=" + value); } } } 



This, in turn, says that there may be more elegant ways to solve your real problem. If you talk a little more about the functional requirement, for which, in your opinion, this is the right solution, we can offer the right solution. Many tools are available to massage the Javabei.

+89
Jun 07 2018-10-12T00:
source share

Here is a quick and dirty method that does what you want in general. You will need to add exception handling, and you probably want to cache BeanInfo types in weakhashmap.

 public Map<String, Object> getNonNullProperties(final Object thingy) { final Map<String, Object> nonNullProperties = new TreeMap<String, Object>(); try { final BeanInfo beanInfo = Introspector.getBeanInfo(thingy .getClass()); for (final PropertyDescriptor descriptor : beanInfo .getPropertyDescriptors()) { try { final Object propertyValue = descriptor.getReadMethod() .invoke(thingy); if (propertyValue != null) { nonNullProperties.put(descriptor.getName(), propertyValue); } } catch (final IllegalArgumentException e) { // handle this please } catch (final IllegalAccessException e) { // and this also } catch (final InvocationTargetException e) { // and this, too } } } catch (final IntrospectionException e) { // do something sensible here } return nonNullProperties; } 

See the following links:

+10
Jun 07 '10 at 13:50
source share

I have an object (mostly VO) in Java and I don't know its type. I need to get values ​​that are not null in this object.

Perhaps you don't need to speculate for this - here is a simple OO design that can solve your problem:

  • Add a Validation interface that issues a validate method that validates the fields and returns whatever is appropriate.
  • Implement an interface and method for all VOs.
  • When you get a VO, even if that particular type is unknown, you can cast it to Validation and easily check.

I assume that you need a field that is null to display the error message in a general way, so that should be enough. Let me know if for some reason this does not work for you.

+1
Jun 07 '10 at 13:16
source share



All Articles