Search in sources :

Example 1 with BaseResources

use of org.ovirt.engine.api.model.BaseResources in project ovirt-engine by oVirt.

the class LinkHelper method getInlineResources.

/**
 * Obtain a set of inline BaseResource objects from @obj
 *
 * i.e. return the value of any properties on @obj which are a
 * sub-type of BaseResource
 *
 * @param obj the object to check
 * @return    a list of any inline BaseResource objects
 */
@SuppressWarnings("unchecked")
private static List<BaseResource> getInlineResources(Object obj) {
    ArrayList<BaseResource> ret = new ArrayList<>();
    for (Method method : getRelevantMethods(obj.getClass())) {
        // We need to recursively scan everything that is in the model package, as there may be references
        // to resources deeply nested:
        Object inline = null;
        try {
            inline = method.invoke(obj);
        } catch (Exception e) {
        // invocation target exception should not occur on simple getter
        }
        if (inline != null) {
            if (inline instanceof BaseResource) {
                ret.add((BaseResource) inline);
            } else if (inline instanceof BaseResources) {
                BaseResources entities = (BaseResources) inline;
                Method getter = EntityHelper.getCollectionGetter(entities);
                try {
                    List<BaseResource> entitiesList = (List<BaseResource>) getter.invoke(entities);
                    for (BaseResource entity : entitiesList) {
                        ret.add(entity);
                    }
                } catch (Exception e) {
                    log.error("Error invoking method '{}' on class '{}'.", method.getName(), entities.getClass().getSimpleName());
                    log.error("Exception", e);
                }
            } else {
                ret.addAll(getInlineResources(inline));
            }
        }
    }
    return ret;
}
Also used : BaseResource(org.ovirt.engine.api.model.BaseResource) ArrayList(java.util.ArrayList) BaseResources(org.ovirt.engine.api.model.BaseResources) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 BaseResource (org.ovirt.engine.api.model.BaseResource)1 BaseResources (org.ovirt.engine.api.model.BaseResources)1