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

org.beanone.flattener.DefaultFlattener Maven / Gradle / Ivy

Go to download

A utility library that allows one to turn a JavaBean into a flat Map of attributes

There is a newer version: 1.4
Show newest version
package org.beanone.flattener;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.beanone.flattener.api.FlattenerRegistry;
import org.beanone.flattener.exception.FlattenerException;

/**
 * Flattener for any java bean that does not have a registered Flattener.
 *
 * @author Hongyan Li
 *
 */
class DefaultFlattener extends AbstractFlattener {
	protected DefaultFlattener(FlattenerRegistry flattenerRegistry) {
		super(flattenerRegistry);
	}

	private List getAllFields(Class clazz) {
		final List returns = new ArrayList<>();
		if (clazz != null) {
			returns.addAll(Arrays.asList(clazz.getDeclaredFields()));
			returns.addAll(getAllFields(clazz.getSuperclass()));
		}

		return returns;
	}

	private boolean hasModifier(int modifiers, int modifier) {
		return (modifiers & modifier) == modifier;
	}

	private boolean isStaticFinal(Field f) {
		return hasModifier(f.getModifiers(), Modifier.FINAL)
		        && hasModifier(f.getModifiers(), Modifier.STATIC);
	}

	@Override
	protected void doFlat(Object object, KeyValueHandler handler) {
		try {
			final List fields = getAllFields(object.getClass());
			for (final Field f : fields) {
				if (!f.isSynthetic() && !isStaticFinal(f)) {
					f.setAccessible(true);
					handler.handle(f.getName(), f.get(object), true);
				}
			}
		} catch (final IllegalAccessException e) {
			throw new FlattenerException(e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy