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

com.envimate.mapmate.serialization.CircularReferenceDetector Maven / Gradle / Ivy

Go to download

MapMate is a modern mapping framework in the scope of mapping data in Json, XML, or YAML format into DTOs composed and vice versa.

There is a newer version: 1.6.8
Show newest version
/*
 * Copyright (C) 2017 [Richard Hauswald, Nune Isabekyan] (envimate GmbH - https://envimate.com/)
 */

package com.envimate.mapmate.serialization;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * CircularReferenceDetector provides ways to scan and detect circular references in a given object.
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public final class CircularReferenceDetector {

    private static final int WRAPPER_COUNT = 10;
    private static final Set> WRAPPER_TYPES = getWrapperTypes();

    /**
     * Detect scans a given object for circular references in its publicly accessible fields recursively.
     *
     * @param subject to be scanned
     * @throws CircularReferenceException if a circular reference is found.
     */
    void detect(final Object subject) {
        if (Objects.isNull(subject)) {
            return;
        }
        detect(subject, new ArrayList<>());
    }

    private void detect(final Object subject, final ArrayList references) {
        final Class type = subject.getClass();
        final Field[] fields = type.getFields();
        for (final Field field : fields) {
            final Object value = readFieldValue(subject, field);
            if (value != null) {
                if (!isWrapperType(value.getClass())) {
                    if (references.contains(value)) {
                        final String message = String.format("a circular reference has been detected for objects of type %s",
                                value.getClass().getName());
                        throw new CircularReferenceException(message);
                    } else {
                        final ArrayList forkedReferences = (ArrayList) references.clone();
                        forkedReferences.add(value);
                        detect(value, forkedReferences);
                    }
                }
            }
        }
    }

    private Object readFieldValue(final Object subject, final Field field) {
        try {
            final Object value = field.get(subject);
            return value;
        } catch (final IllegalAccessException e) {
            throw new UnsupportedOperationException("could not read field value", e);
        }
    }

    private static boolean isWrapperType(final Class clazz) {
        return WRAPPER_TYPES.contains(clazz);
    }

    private static Set> getWrapperTypes() {
        final Set> ret = new HashSet<>(WRAPPER_COUNT);
        ret.add(Boolean.class);
        ret.add(Character.class);
        ret.add(Byte.class);
        ret.add(Short.class);
        ret.add(Integer.class);
        ret.add(Long.class);
        ret.add(Float.class);
        ret.add(Double.class);
        ret.add(Void.class);
        ret.add(String.class);
        return ret;
    }
}