package net.ranides.assira.reflection.impl;

import net.ranides.assira.reflection.AdapterFactory;

import java.io.Serializable;

/**
 * Serialized form of generated adapter class.
 *
 * It stores name of AdapterFactory, type of adapter and reference to wrapped object.
 *
 * Please note, that wrapped object must be Serializable.
 */
public class GCAdapterSForm implements Serializable {

    private static final long serialVersionUID = 2L;

    private final String adapter;

    private final Class<?> target;

    private final Object that;

    /**
     * Constructor used by adapter.writeReplace
     *
     * @param adapter adapter name
     * @param target adapter type
     * @param that wrapped object
     */
    public GCAdapterSForm(String adapter, Class<?> target, Object that) {
        this.adapter = adapter;
        this.target = target;
        this.that = that;
    }

    Object readResolve() {
        return AdapterFactory.getInstance(adapter).cast(target, that);
    }
}
