Home » openjdk-7 » sun » swing » [javadoc | source]

    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   
   34   /**
   35    * SwingLazyValue is a copy of ProxyLazyValue that does not snapshot the
   36    * AccessControlContext or use a doPrivileged to resolve the class name.
   37    * It's intented for use in places in Swing where we need ProxyLazyValue, this
   38    * should never be used in a place where the developer could supply the
   39    * arguments.
   40    *
   41    */
   42   public class SwingLazyValue implements UIDefaults.LazyValue {
   43       private String className;
   44       private String methodName;
   45       private Object[] args;
   46   
   47       public SwingLazyValue(String c) {
   48           this(c, (String)null);
   49       }
   50       public SwingLazyValue(String c, String m) {
   51           this(c, m, null);
   52       }
   53       public SwingLazyValue(String c, Object[] o) {
   54           this(c, null, o);
   55       }
   56       public SwingLazyValue(String c, String m, Object[] o) {
   57           className = c;
   58           methodName = m;
   59           if (o != null) {
   60               args = o.clone();
   61           }
   62       }
   63   
   64       public Object createValue(final UIDefaults table) {
   65           try {
   66               Object cl;
   67               Class<?> c = Class.forName(className, true, null);
   68               if (methodName != null) {
   69                   Class[] types = getClassArray(args);
   70                   Method m = c.getMethod(methodName, types);
   71                   makeAccessible(m);
   72                   return m.invoke(c, args);
   73               } else {
   74                   Class[] types = getClassArray(args);
   75                   Constructor constructor = c.getConstructor(types);
   76                   makeAccessible(constructor);
   77                   return constructor.newInstance(args);
   78               }
   79           } catch (Exception e) {
   80               // Ideally we would throw an exception, unfortunately
   81               // often times there are errors as an initial look and
   82               // feel is loaded before one can be switched. Perhaps a
   83               // flag should be added for debugging, so that if true
   84               // the exception would be thrown.
   85           }
   86           return null;
   87       }
   88   
   89       private void makeAccessible(final AccessibleObject object) {
   90           AccessController.doPrivileged(new PrivilegedAction<Void>() {
   91               public Void run() {
   92                   object.setAccessible(true);
   93                   return null;
   94               }
   95           });
   96       }
   97   
   98       private Class[] getClassArray(Object[] args) {
   99           Class[] types = null;
  100           if (args!=null) {
  101               types = new Class[args.length];
  102               for (int i = 0; i< args.length; i++) {
  103                   /* PENDING(ges): At present only the primitive types
  104                      used are handled correctly; this should eventually
  105                      handle all primitive types */
  106                   if (args[i] instanceof java.lang.Integer) {
  107                       types[i]=Integer.TYPE;
  108                   } else if (args[i] instanceof java.lang.Boolean) {
  109                       types[i]=Boolean.TYPE;
  110                   } else if (args[i] instanceof javax.swing.plaf.ColorUIResource) {
  111                       /* PENDING(ges) Currently the Reflection APIs do not
  112                          search superclasses of parameters supplied for
  113                          constructor/method lookup.  Since we only have
  114                          one case where this is needed, we substitute
  115                          directly instead of adding a massive amount
  116                          of mechanism for this.  Eventually this will
  117                          probably need to handle the general case as well.
  118                       */
  119                       types[i]=java.awt.Color.class;
  120                   } else {
  121                       types[i]=args[i].getClass();
  122                   }
  123               }
  124           }
  125           return types;
  126       }
  127   }

Home » openjdk-7 » sun » swing » [javadoc | source]