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

liquibase.serializer.ReflectionSerializer Maven / Gradle / Ivy

package liquibase.serializer;

import liquibase.change.DatabaseChangeProperty;
import liquibase.exception.UnexpectedLiquibaseException;

import java.lang.reflect.Field;
import java.util.*;

public class ReflectionSerializer {

    private static ReflectionSerializer instance = new ReflectionSerializer();

    public static ReflectionSerializer getInstance() {
        return instance;
    }

    private ReflectionSerializer() {

    }

    public Set getFields(Object object) {
        Set returnSet = new HashSet();
        Set allFields = new HashSet();

        Class classToExtractFieldsFrom = object.getClass();
        while (!classToExtractFieldsFrom.equals(Object.class)) {
            allFields.addAll(Arrays.asList(classToExtractFieldsFrom.getDeclaredFields()));
            classToExtractFieldsFrom = classToExtractFieldsFrom.getSuperclass();
        }

        for (Field field : allFields) {
            if (field.getName().equals("serialVersionUID")) {
                continue;
            }
            if (field.isSynthetic() || field.getName().equals("$VRc")) { //from emma
                continue;
            }

            returnSet.add(field.getName());
        }

        return returnSet;
    }

    public Object getValue(Object object, String field) {
        try {
            Field foundField = null;
            Class classToCheck = object.getClass();
            while (foundField == null && !classToCheck.equals(Object.class)) {
                try {
                    foundField = classToCheck.getDeclaredField(field);
                } catch (NoSuchFieldException e) {
                    classToCheck = classToCheck.getSuperclass();
                }
            }
            if (foundField == null) {
                throw new UnexpectedLiquibaseException("No field "+field+" on "+object.getClass());
            }
            foundField.setAccessible(true);

            return foundField.get(object);
        } catch (Exception e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy