merge the formfield patch from ooo-build
[ooovba.git] / javaunohelper / com / sun / star / lib / uno / helper / WeakBase.java
blob51254288c6052ca332ada5e2c87e00e56e5e4d11
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WeakBase.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.lib.uno.helper;
32 import com.sun.star.uno.XWeak;
33 import com.sun.star.uno.XAdapter;
34 import com.sun.star.lang.XTypeProvider;
35 import com.sun.star.uno.Type;
36 import java.util.Vector;
37 import java.util.Map;
38 import java.util.Hashtable;
41 /** This class can be used as the base class for UNO components. It implements the capability
42 * to be kept weak (com.sun.star.uno.XWeak) and it implements com.sun.star.lang.XTypeProvider
43 * which is necessary for using the component with StarBasic.
45 public class WeakBase implements XWeak, XTypeProvider
47 private final boolean DEBUG= false;
49 // Contains all WeakAdapter which have been created in this class
50 // They have to be notified when this object dies
51 private WeakAdapter m_adapter;
53 protected static Map _mapImplementationIds= new Hashtable();
54 protected static Map _mapTypes= new Hashtable();
56 /** Method of XWeak. The returned XAdapter implementation can be used to keap
57 * a weak reference to this object.
58 * @return a com.sun.star.uno.XAdapter implementation.
60 synchronized public XAdapter queryAdapter()
62 if (m_adapter == null)
63 m_adapter= new WeakAdapter(this);
64 return m_adapter;
67 /** Override of Object.finalize. When there are no references to this object anymore
68 * then the garbage collector calls this method. Thereby causing the adapter object
69 * to be notified. The adapter, in turn, notifies all listeners (com.sun.star.uno.XReference)
71 protected void finalize() throws java.lang.Throwable
73 if (m_adapter != null)
74 m_adapter.referentDying();
75 super.finalize();
78 /** Method of XTypeProvider. It returns an array of Type objects which represent
79 * all implemented UNO interfaces of this object.
80 * @return Type objects of all implemented interfaces.
82 public Type[] getTypes()
84 Type[] arTypes= (Type[]) _mapTypes.get( getClass());
85 if (arTypes == null)
87 Vector vec= new Vector();
88 Class currentClass= getClass();
91 Class interfaces[]= currentClass.getInterfaces();
92 for(int i = 0; i < interfaces.length; ++ i)
94 // Test if it is a UNO interface
95 if (com.sun.star.uno.XInterface.class.isAssignableFrom((interfaces[i])))
96 vec.add(new Type(interfaces[i]));
98 // get the superclass the currentClass inherits from
99 currentClass= currentClass.getSuperclass();
100 } while (currentClass != null);
102 Type types[]= new Type[vec.size()];
103 for( int i= 0; i < types.length; i++)
104 types[i]= (Type) vec.elementAt(i);
105 _mapTypes.put(getClass(), types);
106 arTypes= types;
108 return arTypes;
111 /** Method of XTypeProvider. It provides an identifier that represents the set of UNO
112 * interfaces implemented by this class. All instances of this class
113 * which run in the same Java Virtual Machine return the same array. (This only works as long
114 * the ClassLoader preserves the class even if no instance exist.)
115 *@return identifier as array of bytes
117 public byte[] getImplementationId()
119 byte[] id= null;
120 synchronized (_mapImplementationIds)
122 id= (byte[]) _mapImplementationIds.get(getClass());
124 if (id == null)
126 int hash = hashCode();
127 String sName= getClass().getName();
128 byte[] arName= sName.getBytes();
129 int nNameLength= arName.length;
131 id= new byte[ 4 + nNameLength];
132 id[0]= (byte)(hash & 0xff);
133 id[1]= (byte)((hash >>> 8) & 0xff);
134 id[2]= (byte)((hash >>> 16) & 0xff);
135 id[3]= (byte)((hash >>>24) & 0xff);
137 for (int i= 0; i < nNameLength; i++)
139 id[4 + i]= arName[i];
141 _mapImplementationIds.put(getClass(), id);
144 return id;