blob: 6aabcf66e2a28c2ce1fb0165f0e907a97bf24e71 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
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
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
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 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package sun.swing;
26
27import java.lang.reflect.Constructor;
28import java.lang.reflect.Method;
29import javax.swing.UIDefaults;
30
31/**
32 * SwingLazyValue is a copy of ProxyLazyValue that does not snapshot the
33 * AccessControlContext or use a doPrivileged to resolve the class name.
34 * It's intented for use in places in Swing where we need ProxyLazyValue, this
35 * should never be used in a place where the developer could supply the
36 * arguments.
37 *
38 */
39public class SwingLazyValue implements UIDefaults.LazyValue {
40 private String className;
41 private String methodName;
42 private Object[] args;
43
44 public SwingLazyValue(String c) {
45 this(c, (String)null);
46 }
47 public SwingLazyValue(String c, String m) {
48 this(c, m, null);
49 }
50 public SwingLazyValue(String c, Object[] o) {
51 this(c, null, o);
52 }
53 public SwingLazyValue(String c, String m, Object[] o) {
54 className = c;
55 methodName = m;
56 if (o != null) {
57 args = (Object[])o.clone();
58 }
59 }
60
61 public Object createValue(final UIDefaults table) {
62 try {
63 Class c;
64 Object cl;
65 c = Class.forName(className, true, null);
66 if (methodName != null) {
67 Class[] types = getClassArray(args);
68 Method m = c.getMethod(methodName, types);
69 return m.invoke(c, args);
70 } else {
71 Class[] types = getClassArray(args);
72 Constructor constructor = c.getConstructor(types);
73 return constructor.newInstance(args);
74 }
75 } catch(Exception e) {
76 // Ideally we would throw an exception, unfortunately
77 // often times there are errors as an initial look and
78 // feel is loaded before one can be switched. Perhaps a
79 // flag should be added for debugging, so that if true
80 // the exception would be thrown.
81 }
82 return null;
83 }
84
85 private Class[] getClassArray(Object[] args) {
86 Class[] types = null;
87 if (args!=null) {
88 types = new Class[args.length];
89 for (int i = 0; i< args.length; i++) {
90 /* PENDING(ges): At present only the primitive types
91 used are handled correctly; this should eventually
92 handle all primitive types */
93 if (args[i] instanceof java.lang.Integer) {
94 types[i]=Integer.TYPE;
95 } else if (args[i] instanceof java.lang.Boolean) {
96 types[i]=Boolean.TYPE;
97 } else if (args[i] instanceof javax.swing.plaf.ColorUIResource) {
98 /* PENDING(ges) Currently the Reflection APIs do not
99 search superclasses of parameters supplied for
100 constructor/method lookup. Since we only have
101 one case where this is needed, we substitute
102 directly instead of adding a massive amount
103 of mechanism for this. Eventually this will
104 probably need to handle the general case as well.
105 */
106 types[i]=java.awt.Color.class;
107 } else {
108 types[i]=args[i].getClass();
109 }
110 }
111 }
112 return types;
113 }
114}