jdk/src/share/classes/sun/swing/SwingLazyValue.java
author darcy
Thu, 30 Jan 2014 09:15:13 -0800
changeset 22618 927e4fe6cb24
parent 20800 1f6b7d81410d
child 22644 965bba13a5f0
permissions -rw-r--r--
8033221: Fix serial lint warnings in sun.swing.* Reviewed-by: alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4272
diff changeset
     2
 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4272
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4272
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4272
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4272
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4272
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Method;
4272
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    29
import java.lang.reflect.AccessibleObject;
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    30
import java.security.AccessController;
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    31
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.UIDefaults;
20800
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 5506
diff changeset
    33
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * SwingLazyValue is a copy of ProxyLazyValue that does not snapshot the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * AccessControlContext or use a doPrivileged to resolve the class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * It's intented for use in places in Swing where we need ProxyLazyValue, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * should never be used in a place where the developer could supply the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class SwingLazyValue implements UIDefaults.LazyValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private String methodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Object[] args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public SwingLazyValue(String c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        this(c, (String)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public SwingLazyValue(String c, String m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this(c, m, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public SwingLazyValue(String c, Object[] o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this(c, null, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public SwingLazyValue(String c, String m, Object[] o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        className = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        methodName = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (o != null) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    61
            args = o.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public Object createValue(final UIDefaults table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
20800
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 5506
diff changeset
    67
            ReflectUtil.checkPackageAccess(className);
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    68
            Class<?> c = Class.forName(className, true, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (methodName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                Class[] types = getClassArray(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                Method m = c.getMethod(methodName, types);
4272
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    72
                makeAccessible(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                return m.invoke(c, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                Class[] types = getClassArray(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                Constructor constructor = c.getConstructor(types);
4272
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    77
                makeAccessible(constructor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                return constructor.newInstance(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
4272
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    80
        } catch (Exception e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            // Ideally we would throw an exception, unfortunately
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            // often times there are errors as an initial look and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // feel is loaded before one can be switched. Perhaps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            // flag should be added for debugging, so that if true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // the exception would be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
4272
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    90
    private void makeAccessible(final AccessibleObject object) {
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    91
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    92
            public Void run() {
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    93
                object.setAccessible(true);
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    94
                return null;
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    95
            }
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    96
        });
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    97
    }
f4742294ef29 6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
alexp
parents: 1639
diff changeset
    98
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private Class[] getClassArray(Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Class[] types = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (args!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            types = new Class[args.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            for (int i = 0; i< args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                /* PENDING(ges): At present only the primitive types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                   used are handled correctly; this should eventually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                   handle all primitive types */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                if (args[i] instanceof java.lang.Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    types[i]=Integer.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                } else if (args[i] instanceof java.lang.Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    types[i]=Boolean.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                } else if (args[i] instanceof javax.swing.plaf.ColorUIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    /* PENDING(ges) Currently the Reflection APIs do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                       search superclasses of parameters supplied for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                       constructor/method lookup.  Since we only have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                       one case where this is needed, we substitute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                       directly instead of adding a massive amount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                       of mechanism for this.  Eventually this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                       probably need to handle the general case as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    types[i]=java.awt.Color.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    types[i]=args[i].getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return types;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}