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

org.faktorips.runtime.DefaultUnresolvedReference Maven / Gradle / Ivy

Go to download

Runtime library for Faktor-IPS. When using the JAXB support use either faktorips-runtime-jakarta-xml or faktorips-runtime-javax-xml as dependency. When using CSV to read tables, add the optional dependencies to opencsv, commons-lang3 and commons-text. If you want to run Faktor-IPS tests as JUnit tests, you need to provide either junit (JUnit 4) or junit-jupiter-api (JUnit 5).

There is a newer version: 25.1.0.a20241030-01
Show newest version
/*******************************************************************************
 * Copyright (c) Faktor Zehn GmbH - faktorzehn.org
 * 
 * This source code is available under the terms of the AGPL Affero General Public License version
 * 3.
 * 
 * Please see LICENSE.txt for full license terms, including the additional permissions and
 * restrictions as well as the possibility of alternative license terms.
 *******************************************************************************/

package org.faktorips.runtime;

import java.lang.reflect.Method;

/**
 * 
 * @author Jan Ortmann
 */
public class DefaultUnresolvedReference implements IUnresolvedReference {

    private Object sourceObj;
    private Object sourceObjId;
    private Method establishMethod;
    private Object targetId;
    private Class targetClass;

    public DefaultUnresolvedReference(Object sourceObj, Object sourceObjId, Method establishMethod,
            Class targetClass, Object targetId) {

        super();
        this.sourceObj = sourceObj;
        this.sourceObjId = sourceObjId;
        this.establishMethod = establishMethod;
        this.targetClass = targetClass;
        this.targetId = targetId;
    }

    public DefaultUnresolvedReference(Object sourceObj, Object sourceObjId, String establishMethodName,
            Class targetClass, Object targetId) {

        super();
        this.sourceObj = sourceObj;
        this.sourceObjId = sourceObjId;
        this.targetClass = targetClass;
        this.targetId = targetId;
        try {
            establishMethod = sourceObj.getClass().getMethod(establishMethodName, targetClass);
        } catch (NoSuchMethodException | SecurityException e) {
            throwNewRuntimeException(sourceObj, establishMethodName, targetClass);
        }
    }

    private void throwNewRuntimeException(Object sourceObj, String establishMethodName, Class targetClass) {
        throw new RuntimeException("Can't get method to establish association, sourceClass="
                + sourceObj.getClass().getName() + "method=" + establishMethodName + ", targetClass="
                + targetClass.getName());
    }

    @Override
    public void resolve(IObjectReferenceStore store) throws Exception {
        Object target = store.getObject(targetClass, targetId);
        establishMethod.invoke(sourceObj, target);
    }

    public Method getEstablishMethod() {
        return establishMethod;
    }

    public Object getSourceObj() {
        return sourceObj;
    }

    public Object getSourceObjId() {
        return sourceObjId;
    }

    public Class getTargetClass() {
        return targetClass;
    }

    public Object getTargetId() {
        return targetId;
    }

    @Override
    public String toString() {
        return "Unresolved reference: " + "From " + sourceObj + "(" + sourceObjId + ")" + "To: " + targetClass + "("
                + targetId + ")" + "Method to estabalish: " + establishMethod;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy