author | darcy |
Thu, 30 Jan 2014 09:15:13 -0800 | |
changeset 22618 | 927e4fe6cb24 |
parent 20800 | 1f6b7d81410d |
child 22644 | 965bba13a5f0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package sun.swing; |
|
26 |
||
27 |
import java.lang.reflect.Constructor; |
|
28 |
import java.lang.reflect.Method; |
|
4272
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
29 |
import java.lang.reflect.AccessibleObject; |
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
30 |
import java.security.AccessController; |
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
31 |
import java.security.PrivilegedAction; |
2 | 32 |
import javax.swing.UIDefaults; |
20800 | 33 |
import sun.reflect.misc.ReflectUtil; |
2 | 34 |
|
35 |
/** |
|
36 |
* SwingLazyValue is a copy of ProxyLazyValue that does not snapshot the |
|
37 |
* AccessControlContext or use a doPrivileged to resolve the class name. |
|
38 |
* It's intented for use in places in Swing where we need ProxyLazyValue, this |
|
39 |
* should never be used in a place where the developer could supply the |
|
40 |
* arguments. |
|
41 |
* |
|
42 |
*/ |
|
43 |
public class SwingLazyValue implements UIDefaults.LazyValue { |
|
44 |
private String className; |
|
45 |
private String methodName; |
|
46 |
private Object[] args; |
|
47 |
||
48 |
public SwingLazyValue(String c) { |
|
49 |
this(c, (String)null); |
|
50 |
} |
|
51 |
public SwingLazyValue(String c, String m) { |
|
52 |
this(c, m, null); |
|
53 |
} |
|
54 |
public SwingLazyValue(String c, Object[] o) { |
|
55 |
this(c, null, o); |
|
56 |
} |
|
57 |
public SwingLazyValue(String c, String m, Object[] o) { |
|
58 |
className = c; |
|
59 |
methodName = m; |
|
60 |
if (o != null) { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
61 |
args = o.clone(); |
2 | 62 |
} |
63 |
} |
|
64 |
||
65 |
public Object createValue(final UIDefaults table) { |
|
66 |
try { |
|
20800 | 67 |
ReflectUtil.checkPackageAccess(className); |
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
68 |
Class<?> c = Class.forName(className, true, null); |
2 | 69 |
if (methodName != null) { |
70 |
Class[] types = getClassArray(args); |
|
71 |
Method m = c.getMethod(methodName, types); |
|
4272
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
72 |
makeAccessible(m); |
2 | 73 |
return m.invoke(c, args); |
74 |
} else { |
|
75 |
Class[] types = getClassArray(args); |
|
76 |
Constructor constructor = c.getConstructor(types); |
|
4272
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
77 |
makeAccessible(constructor); |
2 | 78 |
return constructor.newInstance(args); |
79 |
} |
|
4272
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
80 |
} catch (Exception e) { |
2 | 81 |
// Ideally we would throw an exception, unfortunately |
82 |
// often times there are errors as an initial look and |
|
83 |
// feel is loaded before one can be switched. Perhaps a |
|
84 |
// flag should be added for debugging, so that if true |
|
85 |
// the exception would be thrown. |
|
86 |
} |
|
87 |
return null; |
|
88 |
} |
|
89 |
||
4272
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
90 |
private void makeAccessible(final AccessibleObject object) { |
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
91 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
92 |
public Void run() { |
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
93 |
object.setAccessible(true); |
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
94 |
return null; |
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
95 |
} |
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
96 |
}); |
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
97 |
} |
f4742294ef29
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents:
1639
diff
changeset
|
98 |
|
2 | 99 |
private Class[] getClassArray(Object[] args) { |
100 |
Class[] types = null; |
|
101 |
if (args!=null) { |
|
102 |
types = new Class[args.length]; |
|
103 |
for (int i = 0; i< args.length; i++) { |
|
104 |
/* PENDING(ges): At present only the primitive types |
|
105 |
used are handled correctly; this should eventually |
|
106 |
handle all primitive types */ |
|
107 |
if (args[i] instanceof java.lang.Integer) { |
|
108 |
types[i]=Integer.TYPE; |
|
109 |
} else if (args[i] instanceof java.lang.Boolean) { |
|
110 |
types[i]=Boolean.TYPE; |
|
111 |
} else if (args[i] instanceof javax.swing.plaf.ColorUIResource) { |
|
112 |
/* PENDING(ges) Currently the Reflection APIs do not |
|
113 |
search superclasses of parameters supplied for |
|
114 |
constructor/method lookup. Since we only have |
|
115 |
one case where this is needed, we substitute |
|
116 |
directly instead of adding a massive amount |
|
117 |
of mechanism for this. Eventually this will |
|
118 |
probably need to handle the general case as well. |
|
119 |
*/ |
|
120 |
types[i]=java.awt.Color.class; |
|
121 |
} else { |
|
122 |
types[i]=args[i].getClass(); |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
return types; |
|
127 |
} |
|
128 |
} |