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

foundation.cmo.api.mls.graphql.security.services.CopyFieldsUtils Maven / Gradle / Ivy

There is a newer version: 1.0.21
Show newest version
package foundation.cmo.api.mls.graphql.security.services;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class CopyFieldsUtils {

	public static void copyAtoB(Object a, Object b) {

		if (a == null || b == null) {
			return;
		}

		Collection fieldsA = getAllFields(a.getClass());
		Collection fieldsB = getAllFields(b.getClass());

		fieldsA.forEach((fieldA) -> {
			fieldsB.stream().filter((fieldB) -> (fieldA.getName().equals(fieldB.getName())))
					.forEachOrdered((fieldB) -> {
						try {
							fieldA.setAccessible(true);
							fieldB.setAccessible(true);

							Object objA = fieldA.get(a);
							Object objB = fieldB.get(b);

							if (objB == null) {
								fieldB.set(b, objA);
							}
						} catch (IllegalAccessException | IllegalArgumentException | SecurityException e) {
							e.printStackTrace(System.err);
						}
					});
		});
	}

	private static Collection getAllFields(Class type) {

		Collection ret = new ArrayList<>();
		while (type != Object.class) {
			ret.addAll(Arrays.asList(type.getDeclaredFields()));
			type = type.getSuperclass();
		}
		return ret;
	}
	
	public static Method getMethod(Class type, String name) {
		List methods = new ArrayList<>();
		methods.addAll(Arrays.asList(type.getDeclaredMethods()));
		methods.addAll(Arrays.asList(type.getMethods()));
		for (Method method : methods) {
			if (method.getName().equals(name)) {
				return method;
			}
		}
		throw new UnsupportedOperationException("Method not found");
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy