1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 //  #define TEST_LIST_CLASSES
24 //  #define TRACE(x) OSL_TRACE(x)
25 #define TRACE(x)
26 
27 #include <osl/diagnose.h>
28 #include <osl/mutex.hxx>
29 #include <uno/mapping.hxx>
30 #include <uno/dispatcher.h>
31 #include <cppuhelper/weak.hxx>
32 #include <cppuhelper/factory.hxx>
33 #include <cppuhelper/component.hxx>
34 #include <cppuhelper/typeprovider.hxx>
35 
36 #include "lrucache.hxx"
37 
38 #ifdef TEST_LIST_CLASSES
39 #include <list>
40 #include <algorithm>
41 #endif
42 #include <hash_map>
43 
44 #include <com/sun/star/uno/XComponentContext.hpp>
45 #include <com/sun/star/lang/XServiceInfo.hpp>
46 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
47 
48 #include <com/sun/star/reflection/XIdlClass.hpp>
49 #include <com/sun/star/reflection/XIdlReflection.hpp>
50 #include <com/sun/star/reflection/XIdlField.hpp>
51 #include <com/sun/star/reflection/XIdlField2.hpp>
52 #include <com/sun/star/reflection/XIdlMethod.hpp>
53 
54 using namespace std;
55 using namespace osl;
56 using namespace rtl;
57 using namespace cppu;
58 using namespace com::sun::star::uno;
59 using namespace com::sun::star::lang;
60 using namespace com::sun::star::reflection;
61 using namespace com::sun::star::container;
62 
63 
64 namespace stoc_corefl
65 {
66 
67 #ifdef TEST_LIST_CLASSES
68 typedef list< OUString > ClassNameList;
69 extern ClassNameList g_aClassNames;
70 #endif
71 
72 //--------------------------------------------------------------------------------------------------
73 Mutex & getMutexAccess();
74 
75 //--------------------------------------------------------------------------------------------------
td_equals(typelib_TypeDescription * pTD,typelib_TypeDescriptionReference * pType)76 inline bool td_equals( typelib_TypeDescription * pTD, typelib_TypeDescriptionReference * pType )
77 {
78 	return (pTD->pWeakRef == pType ||
79 			(pTD->pTypeName->length == pType->pTypeName->length &&
80 			 rtl_ustr_compare( pTD->pTypeName->buffer, pType->pTypeName->buffer ) == 0));
81 }
82 //--------------------------------------------------------------------------------------------------
getTypeByName(const OUString & rName)83 inline typelib_TypeDescription * getTypeByName( const OUString & rName )
84 {
85 	typelib_TypeDescription * pTypeDescr = 0;
86 	typelib_typedescription_getByName( &pTypeDescr, rName.pData );
87 	if (! pTypeDescr->bComplete)
88 		typelib_typedescription_complete( &pTypeDescr );
89 	return pTypeDescr;
90 }
91 
92 typedef std::hash_map< OUString, WeakReference< XIdlField >,
93 	FctHashOUString, equal_to< OUString > > OUString2Field;
94 typedef std::hash_map< OUString, WeakReference< XIdlMethod >,
95 	FctHashOUString, equal_to< OUString > > OUString2Method;
96 
97 //==================================================================================================
98 class IdlReflectionServiceImpl
99 	: public OComponentHelper
100 	, public XIdlReflection
101 	, public XHierarchicalNameAccess
102 	, public XServiceInfo
103 {
104 	Mutex									_aComponentMutex;
105 	Reference< XMultiServiceFactory >		_xMgr;
106 	Reference< XHierarchicalNameAccess >	_xTDMgr;
107 
108 	// caching
109 	LRU_CacheAnyByOUString					_aElements;
110 
111     Mapping						_aCpp2Uno;
112 	Mapping						_aUno2Cpp;
113 
114 	inline Reference< XIdlClass > constructClass( typelib_TypeDescription * pTypeDescr );
115 public:
getTDMgr() const116 	Reference< XHierarchicalNameAccess > getTDMgr() const
117 		{ return _xTDMgr; }
getSMgr() const118 	Reference< XMultiServiceFactory > getSMgr() const
119 		{ return _xMgr; }
120 
121     const Mapping & getCpp2Uno() throw(::com::sun::star::uno::RuntimeException);
122 	const Mapping & getUno2Cpp() throw(::com::sun::star::uno::RuntimeException);
123 	uno_Interface * mapToUno( const Any & rObj, typelib_InterfaceTypeDescription * pTo ) throw(::com::sun::star::uno::RuntimeException);
124 
125 	// ctor/ dtor
126 	IdlReflectionServiceImpl( const Reference< XComponentContext > & xContext );
127 	virtual ~IdlReflectionServiceImpl();
128 
129 	// XInterface
130 	virtual Any SAL_CALL queryInterface( const Type & rType ) throw(::com::sun::star::uno::RuntimeException);
131 	virtual void SAL_CALL acquire() throw();
132 	virtual void SAL_CALL release() throw();
133 
134 	// some XComponent part from OComponentHelper
135 	virtual void SAL_CALL dispose() throw(::com::sun::star::uno::RuntimeException);
136 
137 	// XServiceInfo
138 	virtual OUString SAL_CALL getImplementationName() throw(::com::sun::star::uno::RuntimeException);
139 	virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) throw(::com::sun::star::uno::RuntimeException);
140 	virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException);
141 
142 	// XTypeProvider
143 	virtual Sequence< Type > SAL_CALL getTypes() throw (::com::sun::star::uno::RuntimeException);
144 	virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() throw (::com::sun::star::uno::RuntimeException);
145 
146 	// XIdlReflection
147 	virtual Reference< XIdlClass > SAL_CALL forName( const OUString & rTypeName ) throw(::com::sun::star::uno::RuntimeException);
148 	virtual Reference< XIdlClass > SAL_CALL getType( const Any & rObj ) throw(::com::sun::star::uno::RuntimeException);
149 
150 	// XHierarchicalNameAccess
151 	virtual Any SAL_CALL getByHierarchicalName( const OUString & rName ) throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException);
152 	virtual sal_Bool SAL_CALL hasByHierarchicalName( const OUString & rName ) throw(::com::sun::star::uno::RuntimeException);
153 
154 	Reference< XIdlClass > forType( typelib_TypeDescription * pTypeDescr ) throw(::com::sun::star::uno::RuntimeException);
155 	Reference< XIdlClass > forType( typelib_TypeDescriptionReference * pRef ) throw(::com::sun::star::uno::RuntimeException);
156 };
157 
158 //==================================================================================================
159 class IdlClassImpl
160 	: public WeakImplHelper1< XIdlClass >
161 {
162 	IdlReflectionServiceImpl *	_pReflection;
163 
164 	OUString					_aName;
165 	TypeClass					_eTypeClass;
166 
167 	typelib_TypeDescription *	_pTypeDescr;
168 
169 public:
getTypeDescr() const170 	typelib_TypeDescription *	getTypeDescr() const
171 		{ return _pTypeDescr; }
getReflection() const172 	IdlReflectionServiceImpl *	getReflection() const
173 		{ return _pReflection; }
getSMgr() const174 	Reference< XMultiServiceFactory > getSMgr() const
175 		{ return _pReflection->getSMgr(); }
getTDMgr() const176 	Reference< XHierarchicalNameAccess > getTDMgr() const
177 		{ return getReflection()->getTDMgr(); }
178 
179 	// Ctor
180 	IdlClassImpl( IdlReflectionServiceImpl * pReflection,
181 				  const OUString & rName, typelib_TypeClass eTypeClass,
182 				  typelib_TypeDescription * pTypeDescr );
183 	virtual ~IdlClassImpl();
184 
185 	// XIdlClassImpl default implementation
186     virtual TypeClass SAL_CALL getTypeClass() throw(::com::sun::star::uno::RuntimeException);
187     virtual OUString SAL_CALL getName() throw(::com::sun::star::uno::RuntimeException);
188     virtual sal_Bool SAL_CALL equals( const Reference< XIdlClass >& xType ) throw(::com::sun::star::uno::RuntimeException);
189 
190     virtual sal_Bool SAL_CALL isAssignableFrom( const Reference< XIdlClass > & xType ) throw(::com::sun::star::uno::RuntimeException);
191     virtual void SAL_CALL createObject( Any & rObj ) throw(::com::sun::star::uno::RuntimeException);
192 
193 	// def impl ????
194     virtual Sequence< Reference< XIdlClass > > SAL_CALL getClasses() throw(::com::sun::star::uno::RuntimeException);
195     virtual Reference< XIdlClass > SAL_CALL getClass( const OUString & rName ) throw(::com::sun::star::uno::RuntimeException);
196     virtual Sequence< Reference< XIdlClass > > SAL_CALL getInterfaces() throw(::com::sun::star::uno::RuntimeException);
197 
198 	// structs, interfaces
199     virtual Sequence< Reference< XIdlClass > > SAL_CALL getSuperclasses() throw(::com::sun::star::uno::RuntimeException);
200 	// structs
201     virtual Reference< XIdlField > SAL_CALL getField( const OUString & rName ) throw(::com::sun::star::uno::RuntimeException);
202     virtual Sequence< Reference< XIdlField > > SAL_CALL getFields() throw(::com::sun::star::uno::RuntimeException);
203 	// interfaces
204     virtual Uik SAL_CALL getUik() throw(::com::sun::star::uno::RuntimeException);
205     virtual Reference< XIdlMethod > SAL_CALL getMethod( const OUString & rName ) throw(::com::sun::star::uno::RuntimeException);
206     virtual Sequence< Reference< XIdlMethod > > SAL_CALL getMethods() throw(::com::sun::star::uno::RuntimeException);
207 	// array
208     virtual Reference< XIdlClass > SAL_CALL getComponentType() throw(::com::sun::star::uno::RuntimeException);
209     virtual Reference< XIdlArray > SAL_CALL getArray() throw(::com::sun::star::uno::RuntimeException);
210 };
211 
212 //==================================================================================================
213 class InterfaceIdlClassImpl
214 	: public IdlClassImpl
215 {
216 	typedef pair< OUString, typelib_TypeDescription * > MemberInit;
217 
218 	Sequence< Reference< XIdlClass > >		_xSuperClasses;
219 
220 	MemberInit *							_pSortedMemberInit; // first methods, then attributes
221 	OUString2Field							_aName2Field;
222 	OUString2Method							_aName2Method;
223 	sal_Int32								_nMethods;
224 	sal_Int32								_nAttributes;
225 
226 	void initMembers();
227 
228 public:
getTypeDescr() const229 	typelib_InterfaceTypeDescription * getTypeDescr() const
230 		{ return (typelib_InterfaceTypeDescription *)IdlClassImpl::getTypeDescr(); }
231 
232 	// ctor/ dtor
InterfaceIdlClassImpl(IdlReflectionServiceImpl * pReflection,const OUString & rName,typelib_TypeClass eTypeClass,typelib_TypeDescription * pTypeDescr)233 	InterfaceIdlClassImpl( IdlReflectionServiceImpl * pReflection,
234 						   const OUString & rName, typelib_TypeClass eTypeClass,
235 						   typelib_TypeDescription * pTypeDescr )
236 		: IdlClassImpl( pReflection, rName, eTypeClass, pTypeDescr )
237 		, _pSortedMemberInit( 0 )
238 		, _nMethods( 0 )
239 		, _nAttributes( 0 )
240 		{}
241 	virtual ~InterfaceIdlClassImpl();
242 
243 	// IdlClassImpl modifications
244     virtual sal_Bool SAL_CALL isAssignableFrom( const Reference< XIdlClass > & xType ) throw(::com::sun::star::uno::RuntimeException);
245     virtual Sequence< Reference< XIdlClass > > SAL_CALL getSuperclasses() throw(::com::sun::star::uno::RuntimeException);
246     virtual Uik SAL_CALL getUik() throw(::com::sun::star::uno::RuntimeException);
247     virtual Reference< XIdlMethod > SAL_CALL getMethod( const OUString & rName ) throw(::com::sun::star::uno::RuntimeException);
248     virtual Sequence< Reference< XIdlMethod > > SAL_CALL getMethods() throw(::com::sun::star::uno::RuntimeException);
249     virtual Reference< XIdlField > SAL_CALL getField( const OUString & rName ) throw(::com::sun::star::uno::RuntimeException);
250     virtual Sequence< Reference< XIdlField > > SAL_CALL getFields() throw(::com::sun::star::uno::RuntimeException);
251     virtual void SAL_CALL createObject( Any & rObj ) throw(::com::sun::star::uno::RuntimeException);
252 };
253 
254 //==================================================================================================
255 class CompoundIdlClassImpl
256 	: public IdlClassImpl
257 {
258 	Reference< XIdlClass >					_xSuperClass;
259 
260 	Sequence< Reference< XIdlField > > *	_pFields;
261 	OUString2Field							_aName2Field;
262 
263 public:
getTypeDescr() const264 	typelib_CompoundTypeDescription * getTypeDescr() const
265 		{ return (typelib_CompoundTypeDescription *)IdlClassImpl::getTypeDescr(); }
266 
267 	// ctor/ dtor
CompoundIdlClassImpl(IdlReflectionServiceImpl * pReflection,const OUString & rName,typelib_TypeClass eTypeClass,typelib_TypeDescription * pTypeDescr)268 	CompoundIdlClassImpl( IdlReflectionServiceImpl * pReflection,
269 						  const OUString & rName, typelib_TypeClass eTypeClass,
270 						  typelib_TypeDescription * pTypeDescr )
271 		: IdlClassImpl( pReflection, rName, eTypeClass, pTypeDescr )
272 		, _pFields( 0 )
273 		{}
274 	virtual ~CompoundIdlClassImpl();
275 
276 	// IdlClassImpl modifications
277     virtual sal_Bool SAL_CALL isAssignableFrom( const Reference< XIdlClass > & xType ) throw(::com::sun::star::uno::RuntimeException);
278     virtual Sequence< Reference< XIdlClass > > SAL_CALL getSuperclasses() throw(::com::sun::star::uno::RuntimeException);
279     virtual Reference< XIdlField > SAL_CALL getField( const OUString & rName ) throw(::com::sun::star::uno::RuntimeException);
280     virtual Sequence< Reference< XIdlField > > SAL_CALL getFields() throw(::com::sun::star::uno::RuntimeException);
281 };
282 
283 //==================================================================================================
284 class ArrayIdlClassImpl
285 	: public IdlClassImpl
286 	, public XIdlArray
287 {
288 public:
getTypeDescr() const289 	typelib_IndirectTypeDescription * getTypeDescr() const
290 		{ return (typelib_IndirectTypeDescription *)IdlClassImpl::getTypeDescr(); }
291 
292 	// ctor
ArrayIdlClassImpl(IdlReflectionServiceImpl * pReflection,const OUString & rName,typelib_TypeClass eTypeClass,typelib_TypeDescription * pTypeDescr)293 	ArrayIdlClassImpl( IdlReflectionServiceImpl * pReflection,
294 					   const OUString & rName, typelib_TypeClass eTypeClass,
295 					   typelib_TypeDescription * pTypeDescr )
296 		: IdlClassImpl( pReflection, rName, eTypeClass, pTypeDescr )
297 		{}
298 
299 	virtual Any SAL_CALL queryInterface( const Type & rType ) throw(::com::sun::star::uno::RuntimeException);
300 	virtual void SAL_CALL acquire() throw();
301 	virtual void SAL_CALL release() throw();
302 
303 	// XTypeProvider
304 	virtual Sequence< Type > SAL_CALL getTypes() throw (::com::sun::star::uno::RuntimeException);
305 	virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() throw (::com::sun::star::uno::RuntimeException);
306 
307 	// IdlClassImpl modifications
308     virtual sal_Bool SAL_CALL isAssignableFrom( const Reference< XIdlClass > & xType ) throw(::com::sun::star::uno::RuntimeException);
309     virtual Reference< XIdlClass > SAL_CALL getComponentType() throw(::com::sun::star::uno::RuntimeException);
310     virtual Reference< XIdlArray > SAL_CALL getArray() throw(::com::sun::star::uno::RuntimeException);
311 
312 	// XIdlArray
313     virtual void SAL_CALL realloc( Any & rArray, sal_Int32 nLen ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
314     virtual sal_Int32 SAL_CALL getLen( const Any & rArray ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
315     virtual Any SAL_CALL get( const Any & rArray, sal_Int32 nIndex ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::ArrayIndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
316     virtual void SAL_CALL set( Any & rArray, sal_Int32 nIndex, const Any & rNewValue ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::ArrayIndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
317 };
318 
319 //==================================================================================================
320 class EnumIdlClassImpl
321 	: public IdlClassImpl
322 {
323 	Sequence< Reference< XIdlField > > * _pFields;
324 	OUString2Field						 _aName2Field;
325 
326 public:
getTypeDescr() const327 	typelib_EnumTypeDescription * getTypeDescr() const
328 		{ return (typelib_EnumTypeDescription *)IdlClassImpl::getTypeDescr(); }
329 
330 	// ctor/ dtor
EnumIdlClassImpl(IdlReflectionServiceImpl * pReflection,const OUString & rName,typelib_TypeClass eTypeClass,typelib_TypeDescription * pTypeDescr)331 	EnumIdlClassImpl( IdlReflectionServiceImpl * pReflection,
332 					  const OUString & rName, typelib_TypeClass eTypeClass,
333 					  typelib_TypeDescription * pTypeDescr )
334 		: IdlClassImpl( pReflection, rName, eTypeClass, pTypeDescr )
335 		, _pFields( 0 )
336 		{}
337 	virtual ~EnumIdlClassImpl();
338 
339 	// IdlClassImpl modifications
340     virtual Reference< XIdlField > SAL_CALL getField( const OUString & rName ) throw(::com::sun::star::uno::RuntimeException);
341     virtual Sequence< Reference< XIdlField > > SAL_CALL getFields() throw(::com::sun::star::uno::RuntimeException);
342     virtual void SAL_CALL createObject( Any & rObj ) throw(::com::sun::star::uno::RuntimeException);
343 };
344 
345 //==================================================================================================
346 class IdlMemberImpl
347 	: public WeakImplHelper1< XIdlMember >
348 {
349 	IdlReflectionServiceImpl *	_pReflection;
350 	OUString					_aName;
351 
352 	typelib_TypeDescription *	_pTypeDescr;
353 	typelib_TypeDescription *	_pDeclTypeDescr;
354 
355 protected:
356 	Reference< XIdlClass >		_xDeclClass;
357 
358 public:
getReflection() const359 	IdlReflectionServiceImpl *	getReflection() const
360 		{ return _pReflection; }
getSMgr() const361 	Reference< XMultiServiceFactory > getSMgr() const
362 		{ return _pReflection->getSMgr(); }
getTypeDescr() const363 	typelib_TypeDescription *	getTypeDescr() const
364 		{ return _pTypeDescr; }
getDeclTypeDescr() const365 	typelib_TypeDescription *	getDeclTypeDescr() const
366 		{ return _pDeclTypeDescr; }
367 
368 	// ctor/ dtor
369 	IdlMemberImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
370 				   typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr );
371 	virtual ~IdlMemberImpl();
372 
373 	// XIdlMember
374     virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() throw(::com::sun::star::uno::RuntimeException);
375     virtual OUString SAL_CALL getName() throw(::com::sun::star::uno::RuntimeException);
376 };
377 
378 //--------------------------------------------------------------------------------------------------
379 // coerces to type descr pTo else queries for it: the interface pointer is returned via rDest
380 // ## type to XidlClass coercion possible
extract(const Any & rObj,typelib_InterfaceTypeDescription * pTo,Reference<XInterface> & rDest,IdlReflectionServiceImpl * pRefl)381 inline sal_Bool extract(
382 	const Any & rObj, typelib_InterfaceTypeDescription * pTo,
383 	Reference< XInterface > & rDest,
384 	IdlReflectionServiceImpl * pRefl )
385 {
386 	rDest.clear();
387 	if (0 != pTo)
388 	{
389         if (! rObj.hasValue())
390             return sal_True;
391 		if (rObj.getValueTypeClass() == TypeClass_INTERFACE)
392 		{
393 			return ::uno_type_assignData(
394 				&rDest, ((typelib_TypeDescription *)pTo)->pWeakRef,
395 				const_cast< void * >( rObj.getValue() ), rObj.getValueTypeRef(),
396 				reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
397                 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
398                 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
399 		}
400 		else if (rObj.getValueTypeClass() == TypeClass_TYPE)
401 		{
402 			rDest = pRefl->forType( reinterpret_cast< const Type * >( rObj.getValue() )->getTypeLibType() );
403 			return rDest.is();
404 		}
405 	}
406 	return sal_False;
407 }
408 //--------------------------------------------------------------------------------------------------
coerce_assign(void * pDest,typelib_TypeDescription * pTD,const Any & rSource,IdlReflectionServiceImpl * pRefl)409 inline sal_Bool coerce_assign(
410 	void * pDest, typelib_TypeDescription * pTD, const Any & rSource,
411 	IdlReflectionServiceImpl * pRefl )
412 {
413     if (pTD->eTypeClass == typelib_TypeClass_INTERFACE)
414     {
415         Reference< XInterface > xVal;
416         if (extract( rSource, (typelib_InterfaceTypeDescription *)pTD, xVal, pRefl ))
417         {
418             if (*(XInterface **)pDest)
419                 (*(XInterface **)pDest)->release();
420             *(XInterface **)pDest = xVal.get();
421             if (*(XInterface **)pDest)
422                 (*(XInterface **)pDest)->acquire();
423             return sal_True;
424         }
425         return sal_False;
426     }
427     else if (pTD->eTypeClass == typelib_TypeClass_ANY)
428     {
429         return uno_assignData(
430             pDest, pTD,
431             (void *)&rSource, pTD,
432             reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
433             reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
434             reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
435     }
436     else
437     {
438         return uno_type_assignData(
439             pDest, pTD->pWeakRef,
440             (void *)rSource.getValue(), rSource.getValueTypeRef(),
441             reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
442             reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
443             reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
444     }
445 }
446 
447 }
448 
449 
450