All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nomin.util.ExplodingIntrospector.groovy Maven / Gradle / Ivy

package org.nomin.util

import java.lang.reflect.Field

/**
 * Uses fields to change state of an instance. It's useful when the class of the instance isn't satisfied to the JavaBeans
 * convention.
 * @author Dmitry Dobrynin
 * Created 16.04.2010 11:03:42
 */
class ExplodingIntrospector extends BaseReflectionIntrospector {
  PropertyAccessor createAccessor(String name, Class targetClass) {
    def field = searchField(targetClass, name)
    if (field) new FieldPropertyAccessor(name, TypeInfoFactory.typeInfo(field.genericType), field)
  }

  Set properties(Class targetClass) {
    Set properties = new HashSet()
    collectFields(targetClass).each { properties.add(it.name) }
    return properties;
  }

  protected Field searchField(Class clazz, String fieldName) {
    def field = clazz.declaredFields.find { it.name == fieldName }
    field ?: clazz.superclass ? searchField(clazz.superclass, fieldName) : null
  }

  protected List collectFields(Class targetClass) {
    def fields = targetClass != Object ? targetClass.declaredFields.collect { it } : []
    if (targetClass.superclass) fields + collectFields(targetClass.superclass)
    fields
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy