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

net.sf.esfinge.querybuilder.utils.reflection.ReflectionOperations Maven / Gradle / Ivy

Go to download

The Esfinge QueryBuilder JDBC is the persistence framework for Esfinge QueryBuilder using JDBC.

The newest version!
package net.sf.esfinge.querybuilder.utils.reflection;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import net.sf.esfinge.querybuilder.annotations.JoinColumn;

public class ReflectionOperations {
	public static boolean SetFieldValue(Field field, Object objetoToSetValue,
			Object value) throws Exception {
		boolean result = true;
		try {
			boolean fieldActualStatus = field.isAccessible();
			field.setAccessible((!fieldActualStatus ? true : false));
			field.set(objetoToSetValue, value);
			field.setAccessible(fieldActualStatus);
		} catch (Exception err) {
			result = false;
		}
		return result;
	}

	public Object findAttributeInClass(Object obj, String fieldName) {

		Class cls = obj.getClass();
		Object objectResult = null;
		String newField = null;
		Object joinTable = null;

		Field fieldlist[] = cls.getDeclaredFields();

		for (Field fld : fieldlist) {

			if (fld.getName().equalsIgnoreCase(fieldName)) {

				String method = "get"
						+ fld.getName().substring(0, 1).toUpperCase()
						+ fld.getName().substring(1);
				try {
					Method meth = cls.getMethod(method, null);
					joinTable = meth.invoke(obj, null);
				} catch (Exception e1) {
					objectResult = null;
				}

				if (fld.isAnnotationPresent(JoinColumn.class)) {

					JoinColumn jnc = fld.getAnnotation(JoinColumn.class);
					newField = jnc.referencedColumnName();

				}

				Class newCls = fld.getType();

				Field newFieldList[] = newCls.getDeclaredFields();

				for (Field newFld : newFieldList) {

					if (newFld.getName().equalsIgnoreCase(newField)) {

						newFld.setAccessible(true);

						try {
							objectResult = newFld.get(joinTable);
						} catch (Exception e) {
							objectResult = null;
						}

						break;

					}

				}

				break;
			}

		}

		return objectResult;

	}

	public static Object findAttributeInClass(Object enclosingObject,
			Class attributeToFind) throws Exception {
		Object ObjectResult = null;
		boolean actualStatus = false;
		try {

			for (Field field : enclosingObject.getClass().getDeclaredFields()) {
				if (field.getType().equals(attributeToFind)) {
					String fieldClass = field.getType().toString().split(" ")[1]
							.toString();
					actualStatus = field.isAccessible();
					field.setAccessible(!actualStatus ? true : actualStatus);
					ObjectResult = field.get(enclosingObject);

					if (ObjectResult == null) {
						ObjectResult = Class.forName(fieldClass).newInstance();
					}

					field.set(enclosingObject, ObjectResult);
					field.setAccessible(actualStatus);
					break;
				}
			}
		} catch (Exception err) {
			ObjectResult = null;
		}

		return ObjectResult;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy