1 /* 2 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 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. 24 */ 25 package sun.swing; 26 27 import java.lang.reflect.Constructor; 28 import java.lang.reflect.Method; 29 import java.lang.reflect.AccessibleObject; 30 import java.security.AccessController; 31 import java.security.PrivilegedAction; 32 import javax.swing.UIDefaults; 33 import sun.reflect.misc.ReflectUtil; 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) { 61 args = o.clone(); 62 } 63 } 64 65 public Object createValue(final UIDefaults table) { 66 try { 67 ReflectUtil.checkPackageAccess(className); 68 Class<?> c = Class.forName(className, true, null); 69 if (methodName != null) { 70 Class<?>[] types = getClassArray(args); 71 Method m = c.getMethod(methodName, types); 72 makeAccessible(m); 73 return m.invoke(c, args); 74 } else { 75 Class<?>[] types = getClassArray(args); 76 Constructor<?> constructor = c.getConstructor(types); 77 makeAccessible(constructor); 78 return constructor.newInstance(args); 79 } 80 } catch (Exception e) { 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 90 private void makeAccessible(final AccessibleObject object) { 91 AccessController.doPrivileged(new PrivilegedAction<Void>() { 92 public Void run() { 93 object.setAccessible(true); 94 return null; 95 } 96 }); 97 } 98 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 }