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

info.unterrainer.commons.jreutils.FieldParser Maven / Gradle / Ivy

There is a newer version: 0.3.15
Show newest version
package info.unterrainer.commons.jreutils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
class FieldParser {

	private Object instanceToSearchIn;
	private Class annotation;

	private String[] pathArray;
	private int pos = -1;
	private String current;
	private boolean isLast;

	public FieldParser(final String path, final Object instanceToSearchIn,
			final Class annotation) {
		this.instanceToSearchIn = instanceToSearchIn;
		this.annotation = annotation;
		this.pathArray = path.split("\\.");
		advance();
	}

	private void advance() {
		pos++;
		if (pos > pathArray.length - 2)
			isLast = true;
		if (pos > pathArray.length - 1)
			current = null;
		current = pathArray[pos];
	}

	public  T parse() throws IllegalArgumentException, IllegalAccessException {
		return parse(instanceToSearchIn);
	}

	@SuppressWarnings("unchecked")
	private  T parse(final Object instance) throws IllegalArgumentException, IllegalAccessException {
		if (pathArray.length == 0)
			return null;

		for (Field field : instance.getClass().getDeclaredFields())
			if (field.isAnnotationPresent(annotation) && field.getName().equals(current)) {
				field.setAccessible(true);
				Object fieldInstance = field.get(instance);
				if (isLast)
					return (T) fieldInstance;
				advance();
				return parse(fieldInstance);
			}
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy