Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
|
42 |
package org.netbeans.modules.applemenu; |
43 |
|
44 |
import java.awt.Font; |
45 |
import java.lang.ref.Reference; |
46 |
import java.lang.ref.ReferenceQueue; |
47 |
import java.lang.reflect.Field; |
48 |
|
49 |
// #57664: Random crash on Mac OS X. Apple JDK apparently has a bug in |
50 |
// the native part of the font handling code. Font instances seems to share |
51 |
// native data structures which are released when the instances are finalized. |
52 |
// Because the native data are shared, their premature release causes random |
53 |
// JVM crashes. |
54 |
// |
55 |
// This evil hack forces Font.finalize() not to run. The native font data |
56 |
// are leaked but it's still better than total crash |
57 |
|
58 |
class FontReferenceQueue extends ReferenceQueue { |
59 |
/** |
60 |
* Polls this queue to see if a reference object is available. If one is |
61 |
* available without further delay then it is removed from the queue and |
62 |
* returned. Otherwise this method immediately returns <tt>null</tt>. |
63 |
* |
64 |
* @return A reference object, if one was immediately available, |
65 |
* otherwise <code>null</code> |
66 |
*/ |
67 |
public Reference poll() { |
68 |
Reference ref; |
69 |
|
70 |
for (;;) { |
71 |
ref = super.poll(); |
72 |
if (ref == null) |
73 |
break; |
74 |
|
75 |
Object obj = ref.get(); |
76 |
if (! (obj instanceof Font)) |
77 |
break; |
78 |
} |
79 |
return ref; |
80 |
} |
81 |
|
82 |
/** |
83 |
* Removes the next reference object in this queue, blocking until either |
84 |
* one becomes available or the given timeout period expires. |
85 |
* |
86 |
* <p> This method does not offer real-time guarantees: It schedules the |
87 |
* timeout as if by invoking the {@link Object#wait(long)} method. |
88 |
* |
89 |
* @param timeout If positive, block for up <code>timeout</code> |
90 |
* milliseconds while waiting for a reference to be |
91 |
* added to this queue. If zero, block indefinitely. |
92 |
* |
93 |
* @return A reference object, if one was available within the specified |
94 |
* timeout period, otherwise <code>null</code> |
95 |
* |
96 |
* @throws IllegalArgumentException |
97 |
* If the value of the timeout argument is negative |
98 |
* |
99 |
* @throws InterruptedException |
100 |
* If the timeout wait is interrupted |
101 |
*/ |
102 |
public Reference remove(long timeout) throws IllegalArgumentException, InterruptedException { |
103 |
// timeout is not handled correctly, but good enough for our purposes |
104 |
|
105 |
Reference ref; |
106 |
|
107 |
for (;;) { |
108 |
ref = super.remove(timeout); |
109 |
if (ref == null) |
110 |
break; |
111 |
|
112 |
Object obj = ref.get(); |
113 |
if (! (obj instanceof Font)) |
114 |
break; |
115 |
} |
116 |
return ref; |
117 |
} |
118 |
|
119 |
static void install() { |
120 |
try { |
121 |
Class clzz = Class.forName("java.lang.ref.Finalizer"); // NOI18N |
122 |
Field fld = clzz.getDeclaredField("queue"); // NOI18N |
123 |
fld.setAccessible(true); |
124 |
fld.set(null, new FontReferenceQueue()); |
125 |
} catch (NoClassDefFoundError ex) { |
126 |
// ex.printStackTrace(); |
127 |
} catch (ClassNotFoundException ex) { |
128 |
// ex.printStackTrace(); |
129 |
} catch (Exception ex) { |
130 |
// ex.printStackTrace(); |
131 |
} |
132 |
|
133 |
} |
134 |
} |