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

info.unterrainer.commons.jreutils.Reflecting 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 java.util.ArrayList;
import java.util.List;

import lombok.experimental.UtilityClass;

@UtilityClass
public class Reflecting {

	/**
	 * Scans object tree connected by annotated fields for a given class using
	 * reflection and returns found paths.
	 *
	 * @param classToScan         the class to start scanning in
	 * @param fieldTypeToFind     the type to search for
	 * @param annotationToScanFor the annotation the fields we should scan are
	 *                            annotated with
	 * @return a list of paths where the given fields have been found
	 */
	public List getPathsOf(final Class classToScan, final Class fieldTypeToFind,
			final Class annotationToScanFor) {
		return getPathsOf(classToScan, fieldTypeToFind, annotationToScanFor, "");
	}

	private List getPathsOf(final Class classToScan, final Class typeToFind,
			final Class annotation, final String currentPath) {
		List paths = new ArrayList<>();

		for (Field field : classToScan.getDeclaredFields()) {

			Class currentType = field.getType();
			if (field.isAnnotationPresent(annotation)) {
				if (typeToFind.isAssignableFrom(currentType)) {
					paths.add(currentPath + field.getName());
					continue;
				}
				paths.addAll(getPathsOf(currentType, typeToFind, annotation, currentPath + field.getName() + "."));
			}
		}
		return paths;
	}

	/**
	 * Gets the instance of a specific field given by path
	 * 
	 * @param 
	 * @param path
	 * @param instanceToSearchIn
	 * @param annotationToScanFor
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public  T getFieldByPath(final String path, final Object instanceToSearchIn,
			final Class annotationToScanFor)
			throws IllegalArgumentException, IllegalAccessException {
		FieldParser p = new FieldParser(path, instanceToSearchIn, annotationToScanFor);
		return p.parse();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy