Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/swing/SwingLazyValue.java
38829 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.swing;2526import java.lang.reflect.Constructor;27import java.lang.reflect.Method;28import java.lang.reflect.AccessibleObject;29import java.security.AccessController;30import java.security.PrivilegedAction;31import javax.swing.UIDefaults;32import sun.reflect.misc.ReflectUtil;3334/**35* SwingLazyValue is a copy of ProxyLazyValue that does not snapshot the36* AccessControlContext or use a doPrivileged to resolve the class name.37* It's intented for use in places in Swing where we need ProxyLazyValue, this38* should never be used in a place where the developer could supply the39* arguments.40*41*/42public class SwingLazyValue implements UIDefaults.LazyValue {43private String className;44private String methodName;45private Object[] args;4647public SwingLazyValue(String c) {48this(c, (String)null);49}50public SwingLazyValue(String c, String m) {51this(c, m, null);52}53public SwingLazyValue(String c, Object[] o) {54this(c, null, o);55}56public SwingLazyValue(String c, String m, Object[] o) {57className = c;58methodName = m;59if (o != null) {60args = o.clone();61}62}6364public Object createValue(final UIDefaults table) {65try {66ReflectUtil.checkPackageAccess(className);67Class<?> c = Class.forName(className, true, null);68if (methodName != null) {69Class[] types = getClassArray(args);70Method m = c.getMethod(methodName, types);71makeAccessible(m);72return m.invoke(c, args);73} else {74Class[] types = getClassArray(args);75Constructor constructor = c.getConstructor(types);76makeAccessible(constructor);77return constructor.newInstance(args);78}79} catch (Exception e) {80// Ideally we would throw an exception, unfortunately81// often times there are errors as an initial look and82// feel is loaded before one can be switched. Perhaps a83// flag should be added for debugging, so that if true84// the exception would be thrown.85}86return null;87}8889private void makeAccessible(final AccessibleObject object) {90AccessController.doPrivileged(new PrivilegedAction<Void>() {91public Void run() {92object.setAccessible(true);93return null;94}95});96}9798private Class[] getClassArray(Object[] args) {99Class[] types = null;100if (args!=null) {101types = new Class[args.length];102for (int i = 0; i< args.length; i++) {103/* PENDING(ges): At present only the primitive types104used are handled correctly; this should eventually105handle all primitive types */106if (args[i] instanceof java.lang.Integer) {107types[i]=Integer.TYPE;108} else if (args[i] instanceof java.lang.Boolean) {109types[i]=Boolean.TYPE;110} else if (args[i] instanceof javax.swing.plaf.ColorUIResource) {111/* PENDING(ges) Currently the Reflection APIs do not112search superclasses of parameters supplied for113constructor/method lookup. Since we only have114one case where this is needed, we substitute115directly instead of adding a massive amount116of mechanism for this. Eventually this will117probably need to handle the general case as well.118*/119types[i]=java.awt.Color.class;120} else {121types[i]=args[i].getClass();122}123}124}125return types;126}127}128129130