1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <sal/log.hxx>
24 #include <osl/diagnose.h>
26 #include <cppuhelper/servicefactory.hxx>
27 #include <cppuhelper/weak.hxx>
28 #include <cppuhelper/implbase1.hxx>
30 #include <ModuleA/XInterface1.hpp>
31 #include <ModuleC/XInterfaceA.hpp>
32 #include <ModuleC/XInterfaceB.hpp>
33 #include <ModuleC/ModuleC.hpp>
35 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
36 #include <com/sun/star/lang/XComponent.hpp>
37 #include <com/sun/star/reflection/XIdlReflection.hpp>
38 #include <com/sun/star/reflection/XIdlField2.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/registry/XSimpleRegistry.hpp>
41 #include <com/sun/star/registry/XImplementationRegistration.hpp>
43 #include <rtl/ustrbuf.hxx>
49 using namespace ModuleA
;
50 using namespace ModuleB
;
51 using namespace ModuleC
;
52 using namespace ModuleA::ModuleB
;
53 using namespace com::sun::star
;
54 using namespace css::uno
;
55 using namespace css::lang
;
56 using namespace css::beans
;
57 using namespace css::registry
;
58 using namespace css::reflection
;
59 using namespace css::container
;
63 class OInterfaceA
: public WeakImplHelper1
< XInterfaceA
>
67 virtual void SAL_CALL
methodA() throw (RuntimeException
)
70 virtual void SAL_CALL
methodB(sal_Int16
/*aShort*/) throw (RuntimeException
)
72 virtual Sequence
< StructB
> SAL_CALL
methodC(const StructC
& /*aStructC*/, StructA
& /*aStructA*/) throw (RuntimeException
)
73 { return Sequence
< StructB
>(); }
77 static inline bool uik_equals( const Uik
& rUik1
, const Uik
& rUik2
)
79 return (rUik1
.m_Data1
== rUik2
.m_Data1
&&
80 rUik1
.m_Data2
== rUik2
.m_Data2
&&
81 rUik1
.m_Data3
== rUik2
.m_Data3
&&
82 rUik1
.m_Data4
== rUik2
.m_Data4
&&
83 rUik1
.m_Data5
== rUik2
.m_Data5
);
86 static sal_Bool
test_corefl( const Reference
< XIdlReflection
> & xRefl
)
88 Reference
< XIdlClass
> xClass
;
89 Reference
< XHierarchicalNameAccess
> xHNameAccess( xRefl
, UNO_QUERY
);
90 OSL_ENSURE(xHNameAccess
.is(), "### cannot get XHierarchicalNameAccess!" );
92 OSL_ENSURE(xRefl
->forName("ModuleA.StructA")->getName() == "ModuleA.StructA", "test_RegCoreReflection(): error 2b");
93 OSL_ENSURE(xRefl
->forName("ModuleA.ExceptionB")->getTypeClass() == TypeClass_EXCEPTION
, "test_RegCoreReflection(): error 2c");
94 OSL_ENSURE(xRefl
->forName("ModuleA.ModuleB.EnumA").is(), "test_RegCoreReflection(): error 2e");
97 OSL_ENSURE(*(const sal_Bool
*)xHNameAccess
->getByHierarchicalName(OUString("ModuleC.aConstBoolean")).getValue() == aConstBoolean
, "test_RegCoreReflection(): error 4c");
98 OSL_ENSURE(*(const sal_Int8
*)xHNameAccess
->getByHierarchicalName(OUString("ModuleC.aConstByte")).getValue() == aConstByte
, "test_RegCoreReflection(): error 4e");
99 OSL_ENSURE(xHNameAccess
->getByHierarchicalName(OUString("ModuleC.aConstShort")) == aConstShort
, "test_RegCoreReflection(): error 4g");
100 OSL_ENSURE(xHNameAccess
->getByHierarchicalName(OUString("ModuleC.aConstUShort")) == aConstUShort
, "test_RegCoreReflection(): error 4i");
101 OSL_ENSURE(xHNameAccess
->getByHierarchicalName(OUString("ModuleC.aConstLong")) == aConstLong
, "test_RegCoreReflection(): error 4k");
102 OSL_ENSURE(xHNameAccess
->getByHierarchicalName(OUString("ModuleC.aConstULong")) == aConstULong
, "test_RegCoreReflection(): error 4m");
106 xClass
= xRefl
->forName("ModuleA.ModuleB.EnumA");
108 OSL_ENSURE(xClass
.is(), "test_RegCoreReflection(): error 5");
110 Sequence
<Reference
< XIdlField
> > fields
= xClass
->getFields();
113 (fields
.getLength() == 3) &&
114 (fields
.getArray()[0]->getName() == "VAL_1" ) &&
115 (*(EnumA
*)fields
.getArray()[0]->get(Any()).getValue() == EnumA_VAL_1
) &&
116 (fields
.getArray()[1]->getName() == "VAL_2" ) &&
117 (*(EnumA
*)fields
.getArray()[1]->get(Any()).getValue() == EnumA_VAL_2
) &&
118 (fields
.getArray()[2]->getName() == "VAL_3" ) &&
119 (*(EnumA
*)fields
.getArray()[2]->get(Any()).getValue() == EnumA_VAL_3
),
120 "test_RegCoreReflection(): error 6");
125 Reference
< XIdlClass
> xA
= xRefl
->forName("ModuleC.XInterfaceB");
127 xClass
= xRefl
->forName("ModuleC.XInterfaceB");
129 OSL_ENSURE(xClass
== xA
, "test_RegCoreReflection(): error 7");
130 OSL_ENSURE(xClass
.is(), "test_RegCoreReflection(): error 7a");
132 typelib_TypeDescription
* pTD
= 0;
133 OUString
aModuleName( "ModuleC.XInterfaceB" );
134 typelib_typedescription_getByName( &pTD
, aModuleName
.pData
);
135 OSL_ENSURE( pTD
, "### cannot get typedescription for ModuleC.XInterfaceB!" );
137 OSL_ENSURE( uik_equals( *(Uik
*)&((typelib_InterfaceTypeDescription
*)pTD
)->aUik
,
139 "test_RegCoreReflection(): error 8" );
140 typelib_typedescription_release( pTD
);
142 OSL_ENSURE(xClass
->getSuperclasses().getLength() == 1, "test_RegCoreReflection(): error 9");
143 OSL_ENSURE(xClass
->getSuperclasses().getArray()[0]->getName() == "ModuleC.XInterfaceA"), "test_RegCoreReflection(): error 10";
144 OSL_ENSURE(xClass
->getMethods().getLength() == 7, "test_RegCoreReflection(): error 11");
145 OSL_ENSURE(xA
->getMethods().getLength() == 7, "test_RegCoreReflection(): error 11a");
146 OSL_ENSURE(xClass
->getMethods().getArray()[3]->getName() == "methodA"), "test_RegCoreReflection(): 12";
147 OSL_ENSURE(xClass
->getMethods().getArray()[3]->getReturnType()->getTypeClass() == TypeClass_VOID
, "test_RegCoreReflection(): error 13");
148 OSL_ENSURE(xClass
->getMethods().getArray()[3]->getParameterTypes().getLength() == 0, "test_RegCoreReflection(): error 14");
149 OSL_ENSURE(xClass
->getMethods().getArray()[3]->getExceptionTypes().getLength() == 0, "test_RegCoreReflection(): error 15");
150 OSL_ENSURE(xClass
->getMethods().getArray()[4]->getName() == "methodB", "test_RegCoreReflection(): error 16");
151 OSL_ENSURE(xClass
->getMethods().getArray()[4]->getMode() == MethodMode_ONEWAY
, "test_RegCoreReflection(): error 16a");
152 OSL_ENSURE(xClass
->getMethods().getArray()[4]->getReturnType()->getTypeClass() == TypeClass_VOID
, "test_RegCoreReflection(): error 16");
153 OSL_ENSURE(xClass
->getMethods().getArray()[4]->getParameterTypes().getLength() == 1, "test_RegCoreReflection(): error 17");
154 OSL_ENSURE(xClass
->getMethods().getArray()[4]->getParameterTypes().getArray()[0]->getTypeClass() == TypeClass_SHORT
, "test_RegCoreReflection(): error 18");
155 OSL_ENSURE(xClass
->getMethods().getArray()[4]->getParameterInfos().getArray()[0].aName
== "aShort", "test_RegCoreReflection(): error 18a");
156 OSL_ENSURE(xClass
->getMethods().getArray()[4]->getParameterInfos().getArray()[0].aType
== xRefl
->forName("short"), "test_RegCoreReflection(): error 18b");
157 OSL_ENSURE(xClass
->getMethods().getArray()[4]->getParameterInfos().getArray()[0].aMode
== ParamMode_IN
, "test_RegCoreReflection(): error 18c");
158 OSL_ENSURE(xClass
->getMethods().getArray()[4]->getExceptionTypes().getLength() == 0, "test_RegCoreReflection(): error 19");
159 OSL_ENSURE(xClass
->getMethods().getArray()[5]->getName() == "methodC", "test_RegCoreReflection(): error 20");
160 OSL_ENSURE(xClass
->getMethods().getArray()[5]->getMode() == MethodMode_TWOWAY
, "test_RegCoreReflection(): error 20a");
161 OSL_ENSURE(xClass
->getMethods().getArray()[5]->getReturnType()->getTypeClass() == TypeClass_SEQUENCE
, "test_RegCoreReflection(): error 21");
162 OSL_ENSURE(xClass
->getMethods().getArray()[5]->getReturnType()->getComponentType()->getTypeClass() == TypeClass_STRUCT
, "test_RegCoreReflection(): error 22");
163 OSL_ENSURE(xClass
->getMethods().getArray()[5]->getReturnType()->getComponentType()->getName() == "ModuleA.StructB", "test_RegCoreReflection(): error 23");
164 OSL_ENSURE(xClass
->getMethods().getArray()[5]->getParameterTypes().getLength() == 2, "test_RegCoreReflection(): error 24");
165 OSL_ENSURE(xClass
->getMethods().getArray()[5]->getParameterTypes().getArray()[0]->getTypeClass() == TypeClass_STRUCT
, "test_RegCoreReflection(): error 25");
166 OSL_ENSURE(xClass
->getMethods().getArray()[5]->getParameterTypes().getArray()[0]->getName() == "ModuleA.StructC", "test_RegCoreReflection(): error 26");
167 OSL_ENSURE(xClass
->getMethods().getArray()[5]->getParameterTypes().getArray()[1]->getTypeClass() == TypeClass_STRUCT
, "test_RegCoreReflection(): error 27");
168 OSL_ENSURE(xClass
->getMethods().getArray()[5]->getParameterTypes().getArray()[1]->getName() == "ModuleA.StructA", "test_RegCoreReflection(): error 28");
169 OSL_ENSURE(xClass
->getMethods().getArray()[5]->getExceptionTypes().getLength() == 0, "test_RegCoreReflection(): error 29");
170 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getName() == "methodD", "test_RegCoreReflection(): error 30");
171 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getReturnType()->getTypeClass() == TypeClass_INTERFACE
, "test_RegCoreReflection(): error 31");
172 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getReturnType()->getName() == "ModuleC.XInterfaceA", "test_RegCoreReflection(): error 32");
173 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getParameterTypes().getLength() == 1, "test_RegCoreReflection(): error 33");
174 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getParameterTypes().getArray()[0]->getTypeClass() == TypeClass_ENUM
, "test_RegCoreReflection(): error 34");
175 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getParameterTypes().getArray()[0]->getName() == "ModuleA.ModuleB.EnumA", "test_RegCoreReflection(): error 35");
176 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getLength() == 3, "test_RegCoreReflection(): error 36");
177 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[0]->getTypeClass() == TypeClass_EXCEPTION
, "test_RegCoreReflection(): error 37");
178 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[0]->getName() == "ModuleA.ExceptionA", "test_RegCoreReflection(): error 38");
179 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[1]->getTypeClass() == TypeClass_EXCEPTION
, "test_RegCoreReflection(): error 38");
180 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[1]->getName() == "ModuleA.ExceptionB", "test_RegCoreReflection(): error 39");
181 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[2]->getTypeClass() == TypeClass_EXCEPTION
, "test_RegCoreReflection(): error 40");
182 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[2]->getName() == "ModuleA.ExceptionC", "test_RegCoreReflection(): error 41");
183 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[2]->getFields().getLength() == 3, "test_RegCoreReflection(): error 42");
184 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[2]->getFields().getArray()[0]->getType()->getTypeClass() == TypeClass_BOOLEAN
, "test_RegCoreReflection(): error 43");
185 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[2]->getFields().getArray()[0]->getType()->getName() == "boolean", "test_RegCoreReflection(): error 43a");
186 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[2]->getFields().getArray()[1]->getType()->getTypeClass() == TypeClass_STRUCT
, "test_RegCoreReflection(): error 44");
187 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[2]->getFields().getArray()[1]->getType()->getName() == "ModuleA.StructC", "test_RegCoreReflection(): error 45");
188 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[2]->getFields().getArray()[2]->getType()->getTypeClass() == TypeClass_INTERFACE
, "test_RegCoreReflection(): error 46");
189 OSL_ENSURE(xClass
->getMethods().getArray()[6]->getExceptionTypes().getArray()[2]->getFields().getArray()[2]->getType()->getName() == "ModuleA.XInterface1", "test_RegCoreReflection(): error 47");
191 // SequenceReflections
193 OSL_ENSURE(xRefl
->forName("[]ModuleA.StructA")->getTypeClass() == TypeClass_SEQUENCE
, "test_RegCoreReflection(): error 48");
194 OSL_ENSURE(xRefl
->forName("[]ModuleA.StructA")->getComponentType().is(), "test_RegCoreReflection(): error 49");
195 OSL_ENSURE(xRefl
->forName("[][]ModuleA.StructA")->getComponentType()->getComponentType()->getName() == "ModuleA.StructA", "test_RegCoreReflection(): error 50");
196 OSL_ENSURE(xRefl
->forName("[]com.sun.star.uno.XInterface") == xRefl
->forName("ModuleA.StructC")->getField(OUString("aInterfaceSeq"))->getType(), "test_RegCoreReflection(): error 51");
199 aStructC
.aLong
= aConstLong
;
200 aStructC
.aShort
= aConstShort
;
201 aStructC
.aFloat
= aConstFloat
;
202 aStructC
.aDouble
= aConstDouble
;
203 aStructC
.aInterfaceSeq
= Sequence
<Reference
<XInterface
> >();
207 xRefl
->forName("ModuleA.StructC")->getField(OUString("aInterfaceSeq"))->getType()->createObject(aAny
);
209 OSL_ENSURE(aAny
.getValueType() == cppu::UnoType
<Sequence
<Reference
< XInterface
> >>::get(), "test_RegCoreReflection(): error 51a");
211 Any
aStructAny(&aStructC
, cppu::UnoType
<StructC
>::get());
213 sal_Int32 nLong
= aConstLong
* 2;
214 aAny
.setValue( &nLong
, cppu::UnoType
<sal_Int32
>::get());
216 OSL_ENSURE(*(sal_Int32
*)xRefl
->forName("ModuleA.StructA")->getField(OUString( "aLong" ))->get(
217 Any(&aStructC
, cppu::UnoType
<StructC
>::get())).getValue() == aConstLong
, "test_RegCoreReflection(): error 52");
218 OSL_ENSURE(xRefl
->forName("ModuleA.StructA")->getField(OUString( "aLong" ))->getAccessMode() == FieldAccessMode_READWRITE
, "test_RegCoreReflection(): error 52a");
219 Reference
< XIdlField2
> rField ( xRefl
->forName("ModuleA.StructC")->getField(OUString( "aLong" )) , UNO_QUERY
);
220 rField
->set(aStructAny
, aAny
);
221 OSL_ENSURE(*(sal_Int32
*)xRefl
->forName("ModuleA.StructB")->getField(OUString( "aLong" ))->get(aStructAny
).getValue() == *(sal_Int32
*)aAny
.getValue(), "test_RegCoreReflection(): error 53");
223 xRefl
->forName("[]ModuleA.StructA")->createObject(aAny
);
225 OSL_ENSURE( aAny
.getValueTypeName() == "[]ModuleA.StructA", "test_RegCoreReflection(): error 54" );
226 xRefl
->forName("[][]ModuleA.StructA")->createObject(aAny
);
228 OSL_ENSURE( aAny
.getValueTypeName() == "[][]ModuleA.StructA", "test_RegCoreReflection(): error 56" );
230 OSL_ENSURE(xRefl
->forName("[][][]unsigned long")->getComponentType()->getComponentType()->getComponentType()->getTypeClass() == TypeClass_UNSIGNED_LONG
, "test_RegCoreReflection(): error 62");
234 SAL_WARN( "stoc", "case 1" );
235 Any bla
= xRefl
->forName("ModuleA.StructC")->getField(OUString("aString"))->get(Any());
236 OSL_ENSURE(sal_False
, "test_RegCoreReflection(): error 63");
239 catch (IllegalArgumentException
&)
245 SAL_WARN( "stoc", "case 2" );
249 rField
= Reference
< XIdlField2
> ( xRefl
->forName("ModuleA.StructC")->getField(OUString("aString")) , UNO_QUERY
);
250 rField
->set( blup
, gulp
);
251 OSL_ENSURE(sal_False
, "test_RegCoreReflection(): error 64");
254 catch (IllegalArgumentException
&)
260 SAL_WARN( "stoc", "case 3" );
265 rField
= Reference
< XIdlField2
> (
266 xRefl
->forName("ModuleA.StructC")->getField(OUString("aString")) , UNO_QUERY
);
267 xRefl
->forName("ModuleA.StructC")->getField(OUString("aString"))->set(blup
, gulp
);
268 OSL_ENSURE(sal_False
, "test_RegCoreReflection(): error 65");
271 catch (IllegalArgumentException
&)
276 gulp
<<= OUString(OUString("Test"));
279 xRefl
->forName("ModuleA.StructC")->getField(OUString("aString"))->set(blup
, gulp
);
281 Reference
< XInterfaceA
> xAI
= new OInterfaceA();
285 Sequence
< Any
> params
;
286 SAL_WARN( "stoc", "case 4" );
290 Any bla
= xRefl
->forName("ModuleC.XInterfaceA")->getMethod(OUString("methodC"))->invoke(a
, params
);
291 OSL_ENSURE(sal_False
, "test_RegCoreReflection(): error 66");
294 catch (IllegalArgumentException
&)
301 Sequence
< Any
> params(2);
303 params
.getArray()[0].setValue(&aStructC
, cppu::UnoType
<StructC
>::get());
304 params
.getArray()[1].setValue(&aStructC
, cppu::UnoType
<StructC
>::get());
308 Any bla
= xRefl
->forName("ModuleC.XInterfaceA")->getMethod(OUString("methodC"))->invoke(a
, params
);
312 Sequence
< Any
> params(2);
314 params
.getArray()[0].setValue(&aStructA
, cppu::UnoType
<StructA
>::get());
315 params
.getArray()[1].setValue(&aStructA
, cppu::UnoType
<StructA
>::get());
319 Any bla
= xRefl
->forName("ModuleC.XInterfaceA")->getMethod(OUString("methodC"))->invoke(a
, params
);
320 OSL_ENSURE(sal_False
, "test_RegCoreReflection(): error 67");
323 catch (IllegalArgumentException
&)
327 Sequence
< Any
> params(2);
329 params
.getArray()[0].setValue(&aStructC
, cppu::UnoType
<StructC
>::get());
330 params
.getArray()[1].setValue(&aStructA
, cppu::UnoType
<StructA
>::get());
334 bool result
= (xRefl
->forName("ModuleC.XInterfaceA")->getMethod(OUString("methodC"))->invoke(a
, params
).getValueType()
335 == cppu::UnoType
<Sequence
<StructB
>>::get()); (void)result
;
336 OSL_ENSURE(result
, "test_RegCoreReflection(): error 68");
343 sal_Bool bSucc
= sal_False
;
346 OUString
aLibName( "reflection.uno" SAL_DLLEXTENSION
);
348 Reference
< XMultiServiceFactory
> xMgr(
349 createRegistryServiceFactory(
350 OUString( "stoctest.rdb" ) ) );
351 Reference
< XComponentContext
> xContext
;
352 Reference
< beans::XPropertySet
> xProps( xMgr
, UNO_QUERY
);
353 OSL_ASSERT( xProps
.is() );
354 xProps
->getPropertyValue(
355 OUString( "DefaultContext" ) ) >>=
357 OSL_ASSERT( xContext
.is() );
359 Reference
< XIdlReflection
> xRefl
;
360 xContext
->getValueByName(
361 OUString( "/singletons/com.sun.star.reflection.theCoreReflection") )
364 xRefl
.is(), "### CoreReflection singleton not accessible!?" );
366 bSucc
= test_corefl( xRefl
);
368 Reference
< XComponent
>( xContext
, UNO_QUERY
)->dispose();
370 catch (const Exception
& rExc
)
372 OSL_FAIL( "### exception occurred!" );
374 OUStringToOString( rExc
.Message
, RTL_TEXTENCODING_ASCII_US
) );
375 OSL_TRACE( "### exception occurred: " );
376 OSL_TRACE( "%s", aMsg
.getStr() );
380 printf( "testcorefl %s !\n", (bSucc
? "succeeded" : "failed") );
381 return (bSucc
? 0 : -1);
384 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */