2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com
.sun
.star
.lib
.uno
.helper
;
20 import com
.sun
.star
.uno
.XWeak
;
21 import com
.sun
.star
.uno
.XAdapter
;
22 import com
.sun
.star
.lang
.XTypeProvider
;
23 import com
.sun
.star
.uno
.Type
;
24 import java
.util
.ArrayList
;
26 import java
.util
.HashMap
;
29 /** This class can be used as the base class for UNO components. It implements the capability
30 * to be kept weak (com.sun.star.uno.XWeak) and it implements com.sun.star.lang.XTypeProvider
31 * which is necessary for using the component with StarBasic.
33 public class WeakBase
implements XWeak
, XTypeProvider
35 // Contains all WeakAdapter which have been created in this class
36 // They have to be notified when this object dies
37 private WeakAdapter m_adapter
;
39 protected static Map
<Class
<?
>,Type
[]> _mapTypes
= new HashMap
<Class
<?
>,Type
[]>();
41 /** Method of XWeak. The returned XAdapter implementation can be used to keep
42 * a weak reference to this object.
43 * @return a com.sun.star.uno.XAdapter implementation.
45 synchronized public XAdapter
queryAdapter()
47 if (m_adapter
== null)
48 m_adapter
= new WeakAdapter(this);
52 /** Override of Object.finalize. When there are no references to this object anymore
53 * then the garbage collector calls this method. Thereby causing the adapter object
54 * to be notified. The adapter, in turn, notifies all listeners (com.sun.star.uno.XReference)
57 protected void finalize() throws java
.lang
.Throwable
59 if (m_adapter
!= null)
60 m_adapter
.referentDying();
64 /** Method of XTypeProvider. It returns an array of Type objects which represent
65 * all implemented UNO interfaces of this object.
66 * @return Type objects of all implemented interfaces.
68 public Type
[] getTypes()
70 Type
[] arTypes
= _mapTypes
.get( getClass());
73 ArrayList
<Type
> vec
= new ArrayList
<Type
>();
74 Class currentClass
= getClass();
77 Class interfaces
[]= currentClass
.getInterfaces();
78 for(int i
= 0; i
< interfaces
.length
; ++ i
)
80 // Test if it is a UNO interface
81 if (com
.sun
.star
.uno
.XInterface
.class.isAssignableFrom((interfaces
[i
])))
82 vec
.add(new Type(interfaces
[i
]));
84 // get the superclass the currentClass inherits from
85 currentClass
= currentClass
.getSuperclass();
86 } while (currentClass
!= null);
88 Type types
[]= vec
.toArray(new Type
[vec
.size()]);
89 _mapTypes
.put(getClass(), types
);
95 /** Obsolete method of XTypeProvider.
97 public byte[] getImplementationId()