{
notBlank(serviceId, "serviceId");
notNull(serviceInterface, "serviceInterface");
// module may be null.
ServiceDef def = _moduleDef.getServiceDef(serviceId);
if (def == null)
throw new IllegalArgumentException(IOCMessages.missingService(serviceId));
if (notVisible(def, module))
throw new RuntimeException(IOCMessages.serviceIsPrivate(serviceId));
Object service = findOrCreate(def);
try
{
return serviceInterface.cast(service);
}
catch (ClassCastException ex)
{
// This may be overkill: I don't know how this could happen
// given that the return type of the method determines
// the service interface.
throw new RuntimeException(IOCMessages.serviceWrongInterface(serviceId, def
.getServiceInterface(), serviceInterface));
}
}