From 543158edba6678d3d76eee983a9d4edd2a422fee Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Mon, 17 Sep 2012 16:56:19 +0200 Subject: Require XComponentContext.getServiceManager to throw instead of returning null This is such a fatal error that there is probably no point in trying to handle it, so allow to simplify client code by removing the requirement to check for a null return value. Simplified some client code accordingly (modules configmgr and ure, and the code generated by cppumaker and javamaker). Change-Id: I51c0b270ec73409374f7439a47ee061407a46e31 --- codemaker/source/cppumaker/cpputype.cxx | 51 +++++----------- codemaker/source/cppumaker/includes.cxx | 6 +- codemaker/source/cppumaker/includes.hxx | 2 + codemaker/source/javamaker/javatype.cxx | 71 ++-------------------- codemaker/test/javamaker/java15/Test.java | 7 +-- configmgr/source/components.cxx | 6 +- configmgr/source/configurationregistry.cxx | 13 ++-- cppuhelper/source/component_context.cxx | 6 ++ .../com/sun/star/comp/helper/ComponentContext.java | 5 ++ udkapi/com/sun/star/uno/XComponentContext.idl | 5 +- ure/source/uretest/JavaClient.java | 7 +-- ure/source/uretest/cppmain.cc | 11 +--- 12 files changed, 56 insertions(+), 134 deletions(-) diff --git a/codemaker/source/cppumaker/cpputype.cxx b/codemaker/source/cppumaker/cpputype.cxx index b4637a9a30f6..23b0bc608596 100644 --- a/codemaker/source/cppumaker/cpputype.cxx +++ b/codemaker/source/cppumaker/cpputype.cxx @@ -3872,10 +3872,10 @@ sal_Bool ServiceType::dumpHxxFile( //TODO: Decide whether the types added to includes should rather be // added to m_dependencies (and thus be generated during // dumpDependedTypes): + includes.addCassert(); includes.addReference(); includes.addRtlUstringH(); includes.addRtlUstringHxx(); - includes.add("com/sun/star/lang/XMultiComponentFactory"); includes.add("com/sun/star/uno/DeploymentException"); includes.add("com/sun/star/uno/XComponentContext"); for (sal_uInt16 i = 0; i < ctors; ++i) { @@ -3949,27 +3949,16 @@ sal_Bool ServiceType::dumpHxxFile( " ::com::sun::star::uno::XComponentContext > const &" " the_context) {\n"); inc(); - o << indent() - << ("::com::sun::star::uno::Reference<" - " ::com::sun::star::lang::XMultiComponentFactory >" - " the_factory(the_context->getServiceManager());\n") - << indent() << "if (!the_factory.is()) {\n"; - inc(); - o << indent() - << ("throw ::com::sun::star::uno::DeploymentException(" - "::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(\"component" - " context fails to supply service manager\"))," - " the_context);\n"); - dec(); - o << indent() << "}\n" << indent() + o << indent() << "assert(the_context.is());\n" << indent() << "::com::sun::star::uno::Reference< " << scopedBaseName << " > the_instance;\n" << indent() << "try {\n"; inc(); o << indent() << "the_instance = ::com::sun::star::uno::Reference< " << scopedBaseName - << (" >(the_factory->createInstanceWithContext(" - "::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(\"") + << (" >(the_context->getServiceManager()->" + "createInstanceWithContext(::rtl::OUString(" + "RTL_CONSTASCII_USTRINGPARAM(\"") << fullName << "\")), the_context), ::com::sun::star::uno::UNO_QUERY);\n"; dec(); @@ -4038,19 +4027,7 @@ sal_Bool ServiceType::dumpHxxFile( } o << ") {\n"; inc(); - o << indent() - << ("::com::sun::star::uno::Reference<" - " ::com::sun::star::lang::XMultiComponentFactory >" - " the_factory(the_context->getServiceManager());\n") - << indent() << "if (!the_factory.is()) {\n"; - inc(); - o << indent() - << ("throw com::sun::star::uno::DeploymentException(" - "::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - "\"component context fails to supply service manager\"))," - " the_context);\n"); - dec(); - o << indent() << "}\n"; + o << indent() << "assert(the_context.is());\n"; if (!rest && params > 0) { o << indent() << ("::com::sun::star::uno::Sequence<" @@ -4108,8 +4085,9 @@ sal_Bool ServiceType::dumpHxxFile( o << indent() << "the_instance = ::com::sun::star::uno::Reference< " << scopedBaseName - << (" >(the_factory->createInstanceWithArgumentsAndContext(" - "::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(\"") + << (" >(the_context->getServiceManager()->" + "createInstanceWithArgumentsAndContext(::rtl::OUString(" + "RTL_CONSTASCII_USTRINGPARAM(\"") << fullName << "\")), "; if (rest) { o << translateUnoToCppIdentifier( @@ -4243,6 +4221,7 @@ sal_Bool SingletonType::dumpHxxFile( // m_dependencies (and thus be generated during dumpDependedTypes): includes.add("com/sun/star/uno/DeploymentException"); includes.add("com/sun/star/uno/XComponentContext"); + includes.addCassert(); includes.addAny(); includes.addReference(); includes.addRtlUstringH(); @@ -4258,11 +4237,13 @@ sal_Bool SingletonType::dumpHxxFile( << scopedBaseName << " > " << translateUnoToCppIdentifier("get", "method", ITM_NONGLOBAL, &cppName) << ("(::com::sun::star::uno::Reference<" - " ::com::sun::star::uno::XComponentContext > const & context) {\n"); + " ::com::sun::star::uno::XComponentContext > const & the_context)" + " {\n"); inc(); - o << indent() << "::com::sun::star::uno::Reference< " << scopedBaseName + o << indent() << "assert(the_context.is());\n" << indent() + << "::com::sun::star::uno::Reference< " << scopedBaseName << " > instance;\n" << indent() - << ("if (!(context->getValueByName(" + << ("if (!(the_context->getValueByName(" "::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(\"/singletons/") << fullName << "\"))) >>= instance) || !instance.is()) {\n"; inc(); @@ -4270,7 +4251,7 @@ sal_Bool SingletonType::dumpHxxFile( << ("throw ::com::sun::star::uno::DeploymentException(" "::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(\"component context" " fails to supply singleton ") - << fullName << " of type " << fullBaseName << "\")), context);\n"; + << fullName << " of type " << fullBaseName << "\")), the_context);\n"; dec(); o << indent() << "}\n" << indent() << "return instance;\n"; dec(); diff --git a/codemaker/source/cppumaker/includes.cxx b/codemaker/source/cppumaker/includes.cxx index b6c9a63f4734..9411312eca93 100644 --- a/codemaker/source/cppumaker/includes.cxx +++ b/codemaker/source/cppumaker/includes.cxx @@ -40,7 +40,8 @@ Includes::Includes( TypeManager const & manager, codemaker::Dependencies const & dependencies, bool hpp): m_manager(manager), m_map(dependencies.getMap()), m_hpp(hpp), - m_includeAny(dependencies.hasAnyDependency()), m_includeReference(false), + m_includeCassert(false), m_includeAny(dependencies.hasAnyDependency()), + m_includeReference(false), m_includeSequence(dependencies.hasSequenceDependency()), m_includeType(dependencies.hasTypeDependency()), m_includeCppuMacrosHxx(false), m_includeCppuUnotypeHxx(false), @@ -155,6 +156,9 @@ void Includes::dump(FileStream & out, rtl::OString const * companionHdl) { } } out << "#include \"sal/config.h\"\n"; + if (m_includeCassert) { + out << "\n#include \n"; + } if (companionHdl) { out << "\n"; dumpInclude(out, *companionHdl, false); diff --git a/codemaker/source/cppumaker/includes.hxx b/codemaker/source/cppumaker/includes.hxx index 7913d78e5ff0..dafc659211a4 100644 --- a/codemaker/source/cppumaker/includes.hxx +++ b/codemaker/source/cppumaker/includes.hxx @@ -37,6 +37,7 @@ public: ~Includes(); void add(rtl::OString const & registryType); + void addCassert() { m_includeCassert = true; } void addAny() { m_includeAny = true; } void addReference() { m_includeReference = true; } void addSequence() { m_includeSequence = true; } @@ -72,6 +73,7 @@ private: TypeManager const & m_manager; codemaker::Dependencies::Map m_map; bool m_hpp; + bool m_includeCassert; bool m_includeAny; bool m_includeReference; bool m_includeSequence; diff --git a/codemaker/source/javamaker/javatype.cxx b/codemaker/source/javamaker/javatype.cxx index 113354f22fa4..b762823ed311 100644 --- a/codemaker/source/javamaker/javatype.cxx +++ b/codemaker/source/javamaker/javatype.cxx @@ -2634,12 +2634,14 @@ void addConstructor( SAL_WNODEPRECATED_DECLARATIONS_POP code->loadLocalReference(0); // stack: context - code->instrInvokestatic( - className, rtl::OString(RTL_CONSTASCII_STRINGPARAM("$getFactory")), + code->instrInvokeinterface( + rtl::OString( + RTL_CONSTASCII_STRINGPARAM("com/sun/star/uno/XComponentContext")), + rtl::OString(RTL_CONSTASCII_STRINGPARAM("getServiceManager")), rtl::OString( RTL_CONSTASCII_STRINGPARAM( - "(Lcom/sun/star/uno/XComponentContext;)" - "Lcom/sun/star/lang/XMultiComponentFactory;"))); + "()Lcom/sun/star/lang/XMultiComponentFactory;")), + 1); // stack: factory code->loadStringConstant(unoName); // stack: factory serviceName @@ -2905,67 +2907,6 @@ void handleService( manager, realJavaBaseName, unoName, className, reader, i, name, base, defaultCtor, dependencies, cf.get()); } - // Synthetic getFactory method: - { - SAL_WNODEPRECATED_DECLARATIONS_PUSH - std::auto_ptr< ClassFile::Code > code(cf->newCode()); - SAL_WNODEPRECATED_DECLARATIONS_POP - code->loadLocalReference(0); - // stack: context - code->instrInvokeinterface( - rtl::OString( - RTL_CONSTASCII_STRINGPARAM( - "com/sun/star/uno/XComponentContext")), - rtl::OString(RTL_CONSTASCII_STRINGPARAM("getServiceManager")), - rtl::OString( - RTL_CONSTASCII_STRINGPARAM( - "()Lcom/sun/star/lang/XMultiComponentFactory;")), - 1); - // stack: factory - code->instrDup(); - // stack: factory factory - ClassFile::Code::Branch branch = code->instrIfnull(); - // stack: factory - code->instrAreturn(); - code->branchHere(branch); - code->instrPop(); - // stack: - - code->instrNew( - rtl::OString( - RTL_CONSTASCII_STRINGPARAM( - "com/sun/star/uno/DeploymentException"))); - // stack: ex - code->instrDup(); - // stack: ex ex - code->loadStringConstant( - rtl::OString( - RTL_CONSTASCII_STRINGPARAM( - "component context fails to supply service manager"))); - // stack: ex ex "..." - code->loadLocalReference(0); - // stack: ex ex "..." context - code->instrInvokespecial( - rtl::OString( - RTL_CONSTASCII_STRINGPARAM( - "com/sun/star/uno/DeploymentException")), - rtl::OString(RTL_CONSTASCII_STRINGPARAM("")), - rtl::OString( - RTL_CONSTASCII_STRINGPARAM( - "(Ljava/lang/String;Ljava/lang/Object;)V"))); - // stack: ex - code->instrAthrow(); - code->setMaxStackAndLocals(4, 1); - cf->addMethod( - static_cast< ClassFile::AccessFlags >( - ClassFile::ACC_PRIVATE | ClassFile::ACC_STATIC - | ClassFile::ACC_SYNTHETIC), - rtl::OString(RTL_CONSTASCII_STRINGPARAM("$getFactory")), - rtl::OString( - RTL_CONSTASCII_STRINGPARAM( - "(Lcom/sun/star/uno/XComponentContext;)" - "Lcom/sun/star/lang/XMultiComponentFactory;")), - code.get(), std::vector< rtl::OString >(), rtl::OString()); - } // Synthetic castInstance method: { SAL_WNODEPRECATED_DECLARATIONS_PUSH diff --git a/codemaker/test/javamaker/java15/Test.java b/codemaker/test/javamaker/java15/Test.java index 30b9e08d68ab..40401cf010be 100644 --- a/codemaker/test/javamaker/java15/Test.java +++ b/codemaker/test/javamaker/java15/Test.java @@ -19,7 +19,6 @@ package test.codemaker.javamaker.java15; import com.sun.star.lang.XMultiComponentFactory; -import com.sun.star.uno.DeploymentException; import com.sun.star.uno.XComponentContext; import complexlib.ComplexTestCase; @@ -69,18 +68,18 @@ public final class Test extends ComplexTestCase { } public XMultiComponentFactory getServiceManager() { - return null; + throw new com.sun.star.uno.RuntimeException(); } }; try { Service.create(context); failed(); - } catch (DeploymentException e) {} + } catch (com.sun.star.uno.RuntimeException e) {} try { Service.create( context, false, (byte) 1, (short) 2, Integer.valueOf(4)); failed(); - } catch (DeploymentException e) {} + } catch (com.sun.star.uno.RuntimeException e) {} } private static final class Ifc implements XIfc { diff --git a/configmgr/source/components.cxx b/configmgr/source/components.cxx index e42fb48352e2..c08744e99a8e 100644 --- a/configmgr/source/components.cxx +++ b/configmgr/source/components.cxx @@ -29,7 +29,6 @@ #include "com/sun/star/beans/XPropertySet.hpp" #include "com/sun/star/container/NoSuchElementException.hpp" #include "com/sun/star/lang/WrappedTargetException.hpp" -#include "com/sun/star/lang/XMultiComponentFactory.hpp" #include "com/sun/star/uno/Any.hxx" #include "com/sun/star/uno/Exception.hpp" #include "com/sun/star/uno/Reference.hxx" @@ -431,9 +430,8 @@ css::beans::Optional< css::uno::Any > Components::getExternalValue( if (j == externalServices_.end()) { css::uno::Reference< css::uno::XInterface > service; try { - service = css::uno::Reference< css::lang::XMultiComponentFactory >( - context_->getServiceManager(), css::uno::UNO_SET_THROW)-> - createInstanceWithContext(name, context_); + service = context_->getServiceManager()->createInstanceWithContext( + name, context_); } catch (css::uno::RuntimeException &) { // Assuming these exceptions are real errors: throw; diff --git a/configmgr/source/configurationregistry.cxx b/configmgr/source/configurationregistry.cxx index b70d5d3701e8..40b694109da3 100644 --- a/configmgr/source/configurationregistry.cxx +++ b/configmgr/source/configurationregistry.cxx @@ -28,7 +28,6 @@ #include "com/sun/star/container/NoSuchElementException.hpp" #include "com/sun/star/container/XHierarchicalNameAccess.hpp" #include "com/sun/star/container/XNamed.hpp" -#include "com/sun/star/lang/XMultiComponentFactory.hpp" #include "com/sun/star/lang/XMultiServiceFactory.hpp" #include "com/sun/star/lang/XServiceInfo.hpp" #include "com/sun/star/registry/InvalidRegistryException.hpp" @@ -323,13 +322,11 @@ Service::Service( assert(context.is()); try { provider_ = css::uno::Reference< css::lang::XMultiServiceFactory >( - (css::uno::Reference< css::lang::XMultiComponentFactory >( - context->getServiceManager(), css::uno::UNO_SET_THROW)-> - createInstanceWithContext( - rtl::OUString( - RTL_CONSTASCII_USTRINGPARAM( - "com.sun.star.configuration.DefaultProvider")), - context)), + context->getServiceManager()->createInstanceWithContext( + rtl::OUString( + RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.configuration.DefaultProvider")), + context), css::uno::UNO_QUERY_THROW); } catch (css::uno::RuntimeException &) { throw; diff --git a/cppuhelper/source/component_context.cxx b/cppuhelper/source/component_context.cxx index a07cd0e8f302..ac416f1745f9 100644 --- a/cppuhelper/source/component_context.cxx +++ b/cppuhelper/source/component_context.cxx @@ -672,6 +672,12 @@ Any ComponentContext::getValueByName( OUString const & rName ) Reference< lang::XMultiComponentFactory > ComponentContext::getServiceManager() throw (RuntimeException) { + if ( !m_xSMgr.is() ) + { + throw RuntimeException( + "null component context service manager", + static_cast(this) ); + } return m_xSMgr; } //__________________________________________________________________________________________________ diff --git a/javaunohelper/com/sun/star/comp/helper/ComponentContext.java b/javaunohelper/com/sun/star/comp/helper/ComponentContext.java index 5075eb8d224d..2294af8a12bd 100644 --- a/javaunohelper/com/sun/star/comp/helper/ComponentContext.java +++ b/javaunohelper/com/sun/star/comp/helper/ComponentContext.java @@ -207,6 +207,11 @@ public class ComponentContext implements XComponentContext, XComponent //______________________________________________________________________________________________ public XMultiComponentFactory getServiceManager() { + if (m_xSMgr == null) + { + throw new com.sun.star.uno.RuntimeException( + "null component context service manager" ); + } return m_xSMgr; } diff --git a/udkapi/com/sun/star/uno/XComponentContext.idl b/udkapi/com/sun/star/uno/XComponentContext.idl index cde3e2a9b6a2..c45a1c1d1728 100644 --- a/udkapi/com/sun/star/uno/XComponentContext.idl +++ b/udkapi/com/sun/star/uno/XComponentContext.idl @@ -44,12 +44,13 @@ published interface XComponentContext : XInterface any getValueByName( [in] string Name ); /** Gets the service manager instance to be used from key - /singletons/com.sun.star.lang.theServiceManager (maybe null ref). + /singletons/com.sun.star.lang.theServiceManager. This method has been added for convenience, because the service manager is used very often. @return - service manager, if any + service manager; throws RuntimeException in case service manager + is null */ com::sun::star::lang::XMultiComponentFactory getServiceManager(); }; diff --git a/ure/source/uretest/JavaClient.java b/ure/source/uretest/JavaClient.java index d37e6f8f5f74..f86d61af66cf 100644 --- a/ure/source/uretest/JavaClient.java +++ b/ure/source/uretest/JavaClient.java @@ -23,7 +23,6 @@ import com.sun.star.bridge.XBridgeFactory; import com.sun.star.comp.helper.Bootstrap; import com.sun.star.connection.Connector; import com.sun.star.lang.XComponent; -import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.lib.uno.helper.UnoUrl; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; @@ -34,13 +33,9 @@ public final class JavaClient { public static void main(String[] arguments) throws Exception { XComponentContext context = Bootstrap.createInitialComponentContext( null); - XMultiComponentFactory manager = context.getServiceManager(); - if (manager == null) { - throw new NullPointerException("no service manager"); - } XBridgeFactory factory = UnoRuntime.queryInterface( XBridgeFactory.class, - manager.createInstanceWithContext( + context.getServiceManager().createInstanceWithContext( "com.sun.star.bridge.BridgeFactory", context)); if (factory == null) { throw new NullPointerException("no bridge factory"); diff --git a/ure/source/uretest/cppmain.cc b/ure/source/uretest/cppmain.cc index 3cc496c14496..54e6c6890018 100644 --- a/ure/source/uretest/cppmain.cc +++ b/ure/source/uretest/cppmain.cc @@ -26,7 +26,6 @@ #include #include "com/sun/star/lang/XMain.hpp" -#include "com/sun/star/lang/XMultiComponentFactory.hpp" #include "com/sun/star/uno/Exception.hpp" #include "com/sun/star/uno/Reference.hxx" #include "com/sun/star/uno/RuntimeException.hpp" @@ -141,18 +140,12 @@ private: "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTscript", "com.sun.star.uri.VndSunStarPkgUrlReferenceFactory" }; - ::css::uno::Reference< ::css::lang::XMultiComponentFactory > manager( - context_->getServiceManager()); - if (!manager.is()) { - throw ::css::uno::RuntimeException( - ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("no service manager")), - static_cast< ::cppu::OWeakObject * >(this)); - } for (::std::size_t i = 0; i < SAL_N_ELEMENTS(services); ++i) { ::rtl::OUString name(::rtl::OUString::createFromAscii(services[i])); ::css::uno::Reference< ::css::uno::XInterface > instance; try { - instance = manager->createInstanceWithContext(name, context_); + instance = context_->getServiceManager()->createInstanceWithContext( + name, context_); } catch (::css::uno::RuntimeException &) { throw; } catch (::css::uno::Exception &) { -- cgit