001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.swing;
026:
027: import java.lang.reflect.Constructor;
028: import java.lang.reflect.Method;
029: import javax.swing.UIDefaults;
030:
031: /**
032: * SwingLazyValue is a copy of ProxyLazyValue that does not snapshot the
033: * AccessControlContext or use a doPrivileged to resolve the class name.
034: * It's intented for use in places in Swing where we need ProxyLazyValue, this
035: * should never be used in a place where the developer could supply the
036: * arguments.
037: *
038: * @version 1.11 05/05/07
039: */
040: public class SwingLazyValue implements UIDefaults.LazyValue {
041: private String className;
042: private String methodName;
043: private Object[] args;
044:
045: public SwingLazyValue(String c) {
046: this (c, (String) null);
047: }
048:
049: public SwingLazyValue(String c, String m) {
050: this (c, m, null);
051: }
052:
053: public SwingLazyValue(String c, Object[] o) {
054: this (c, null, o);
055: }
056:
057: public SwingLazyValue(String c, String m, Object[] o) {
058: className = c;
059: methodName = m;
060: if (o != null) {
061: args = (Object[]) o.clone();
062: }
063: }
064:
065: public Object createValue(final UIDefaults table) {
066: try {
067: Class c;
068: Object cl;
069: c = Class.forName(className, true, null);
070: if (methodName != null) {
071: Class[] types = getClassArray(args);
072: Method m = c.getMethod(methodName, types);
073: return m.invoke(c, args);
074: } else {
075: Class[] types = getClassArray(args);
076: Constructor constructor = c.getConstructor(types);
077: return constructor.newInstance(args);
078: }
079: } catch (Exception e) {
080: // Ideally we would throw an exception, unfortunately
081: // often times there are errors as an initial look and
082: // feel is loaded before one can be switched. Perhaps a
083: // flag should be added for debugging, so that if true
084: // the exception would be thrown.
085: }
086: return null;
087: }
088:
089: private Class[] getClassArray(Object[] args) {
090: Class[] types = null;
091: if (args != null) {
092: types = new Class[args.length];
093: for (int i = 0; i < args.length; i++) {
094: /* PENDING(ges): At present only the primitive types
095: used are handled correctly; this should eventually
096: handle all primitive types */
097: if (args[i] instanceof java.lang.Integer) {
098: types[i] = Integer.TYPE;
099: } else if (args[i] instanceof java.lang.Boolean) {
100: types[i] = Boolean.TYPE;
101: } else if (args[i] instanceof javax.swing.plaf.ColorUIResource) {
102: /* PENDING(ges) Currently the Reflection APIs do not
103: search superclasses of parameters supplied for
104: constructor/method lookup. Since we only have
105: one case where this is needed, we substitute
106: directly instead of adding a massive amount
107: of mechanism for this. Eventually this will
108: probably need to handle the general case as well.
109: */
110: types[i] = java.awt.Color.class;
111: } else {
112: types[i] = args[i].getClass();
113: }
114: }
115: }
116: return types;
117: }
118: }
|