package com.framsticks.core; import org.apache.log4j.Logger; import com.framsticks.params.AccessInterface; import com.framsticks.params.CompositeParam; import com.framsticks.params.Param; import com.framsticks.params.annotations.AutoAppendAnnotation; import com.framsticks.params.annotations.FramsClassAnnotation; import com.framsticks.params.types.ProcedureParam; import com.framsticks.util.FramsticksException; import com.framsticks.util.StateFunctor; @FramsClassAnnotation public class ObjectInstance extends Instance { private static final Logger log = Logger.getLogger(ObjectInstance.class); @AutoAppendAnnotation public void setRootObject(Object object) { registry.registerAndBuild(object.getClass()); AccessInterface access = registry.createAccess(object.getClass()); setRoot(Param.build().forAccess(access).id(getName()).finish(CompositeParam.class), object); } public Object getRootObject() { return getRoot().getObject(); } public T getRootObject(Class type) { Object result = getRootObject(); if (result == null) { throw new FramsticksException().msg("object instance is empty").arg("instance", this); } if (!type.isInstance(result)) { throw new FramsticksException().msg("object instance holds object of different kind").arg("object", result).arg("requested", type).arg("instance", this); } return type.cast(result); } @Override public void fetchValues(final Path path, final StateFunctor stateFunctor) { assert isActive(); log.debug("requesting: " + path); fireFetch(path); stateFunctor.call(null); } @Override public void fetchValue(Path path, Param param, StateFunctor stateFunctor) { assert isActive(); fireFetch(path); stateFunctor.call(null); } @Override public void call(Path path, ProcedureParam param, Object[] arguments, StateFunctor stateFunctor) { assert isActive(); try { bindAccess(path).call(param, arguments); stateFunctor.call(null); } catch (FramsticksException e) { stateFunctor.call(e); } } }