jdk/src/share/classes/java/beans/Statement.java
author prappo
Tue, 01 Jul 2014 14:44:37 +0100
changeset 25187 08aff438def8
parent 24969 afa6934dd8e8
permissions -rw-r--r--
8048874: Replace uses of 'new Byte', 'new Short' and 'new Character' with appropriate alternative across core classes Reviewed-by: chegar, psandoz, prappo Contributed-by: Otavio Santana <otaviojava@java.net>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14887
226dd1cda199 8005065: [findbugs] reference to mutable array in JavaBeans
malenkov
parents: 11120
diff changeset
     2
 * Copyright (c) 2000, 2012, 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: 5203
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: 5203
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: 5203
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5203
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5203
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.reflect.AccessibleObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Method;
5185
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
    32
import java.security.AccessControlContext;
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
    33
import java.security.AccessController;
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
    34
import java.security.PrivilegedActionException;
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
    35
import java.security.PrivilegedExceptionAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.beans.finder.ClassFinder;
1851
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
    38
import com.sun.beans.finder.ConstructorFinder;
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
    39
import com.sun.beans.finder.MethodFinder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.reflect.misc.MethodUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A <code>Statement</code> object represents a primitive statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * in which a single method is applied to a target and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * a set of arguments - as in <code>"a.setFoo(b)"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Note that where this example uses names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * to denote the target and its argument, a statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * object does not require a name space and is constructed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the values themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The statement object associates the named method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * with its environment as a simple set of values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * the target and an array of argument values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class Statement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static Object[] emptyArray = new Object[]{};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static ExceptionListener defaultExceptionListener = new ExceptionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public void exceptionThrown(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            System.err.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            // e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            System.err.println("Continuing ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
5185
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
    70
    private final AccessControlContext acc = AccessController.getContext();
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
    71
    private final Object target;
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
    72
    private final String methodName;
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
    73
    private final Object[] arguments;
3240
2f79c1748c93 6329581: RFE: LTP: java.beans.XMLEncoder does not manage ClassLoader.
malenkov
parents: 1851
diff changeset
    74
    ClassLoader loader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    77
     * Creates a new {@link Statement} object
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    78
     * for the specified target object to invoke the method
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    79
     * specified by the name and by the array of arguments.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    80
     * <p>
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    81
     * The {@code target} and the {@code methodName} values should not be {@code null}.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    82
     * Otherwise an attempt to execute this {@code Expression}
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    83
     * will result in a {@code NullPointerException}.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    84
     * If the {@code arguments} value is {@code null},
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    85
     * an empty array is used as the value of the {@code arguments} property.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    87
     * @param target  the target object of this statement
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    88
     * @param methodName  the name of the method to invoke on the specified target
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    89
     * @param arguments  the array of arguments to invoke the specified method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    @ConstructorProperties({"target", "methodName", "arguments"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public Statement(Object target, String methodName, Object[] arguments) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.methodName = methodName;
14887
226dd1cda199 8005065: [findbugs] reference to mutable array in JavaBeans
malenkov
parents: 11120
diff changeset
    95
        this.arguments = (arguments == null) ? emptyArray : arguments.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
    99
     * Returns the target object of this statement.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   100
     * If this method returns {@code null},
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   101
     * the {@link #execute} method
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   102
     * throws a {@code NullPointerException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   104
     * @return the target object of this statement
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public Object getTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   111
     * Returns the name of the method to invoke.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   112
     * If this method returns {@code null},
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   113
     * the {@link #execute} method
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   114
     * throws a {@code NullPointerException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   116
     * @return the name of the method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public String getMethodName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return methodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   123
     * Returns the arguments for the method to invoke.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   124
     * The number of arguments and their types
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   125
     * must match the method being  called.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   126
     * {@code null} can be used as a synonym of an empty array.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   128
     * @return the array of arguments
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public Object[] getArguments() {
14887
226dd1cda199 8005065: [findbugs] reference to mutable array in JavaBeans
malenkov
parents: 11120
diff changeset
   131
        return this.arguments.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
5131
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   135
     * The {@code execute} method finds a method whose name is the same
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   136
     * as the {@code methodName} property, and invokes the method on
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * When the target's class defines many methods with the given name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * the implementation should choose the most specific method using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * the algorithm specified in the Java Language Specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * (15.11). The dynamic class of the target and arguments are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * in place of the compile-time type information and, like the
5131
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   144
     * {@link java.lang.reflect.Method} class itself, conversion between
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * primitive values and their associated wrapper classes is handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * internally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The following method types are handled as special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Static methods may be called by using a class object as the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * The reserved method name "new" may be used to call a class's constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * as if all classes defined static "new" methods. Constructor invocations
5131
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   155
     * are typically considered {@code Expression}s rather than {@code Statement}s
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * as they return a value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <li>
5131
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   158
     * The method names "get" and "set" defined in the {@link java.util.List}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * interface may also be applied to array instances, mapping to
5131
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   160
     * the static methods of the same name in the {@code Array} class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * </ul>
5131
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   162
     *
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   163
     * @throws NullPointerException if the value of the {@code target} or
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   164
     *                              {@code methodName} property is {@code null}
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   165
     * @throws NoSuchMethodException if a matching method is not found
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   166
     * @throws SecurityException if a security manager exists and
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   167
     *                           it denies the method invocation
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   168
     * @throws Exception that is thrown by the invoked method
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   169
     *
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   170
     * @see java.lang.reflect.Method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public void execute() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        invoke();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    Object invoke() throws Exception {
5185
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   177
        AccessControlContext acc = this.acc;
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   178
        if ((acc == null) && (System.getSecurityManager() != null)) {
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   179
            throw new SecurityException("AccessControlContext is not set");
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   180
        }
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   181
        try {
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   182
            return AccessController.doPrivileged(
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   183
                    new PrivilegedExceptionAction<Object>() {
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   184
                        public Object run() throws Exception {
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   185
                            return invokeInternal();
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   186
                        }
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   187
                    },
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   188
                    acc
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   189
            );
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   190
        }
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   191
        catch (PrivilegedActionException exception) {
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   192
            throw exception.getException();
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   193
        }
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   194
    }
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   195
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 3240
diff changeset
   196
    private Object invokeInternal() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        Object target = getTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        String methodName = getMethodName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (target == null || methodName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new NullPointerException((target == null ? "target" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                            "methodName") + " should not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        Object[] arguments = getArguments();
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   206
        if (arguments == null) {
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   207
            arguments = emptyArray;
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   208
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // Class.forName() won't load classes outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // of core from a class inside core. Special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // case this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (target == Class.class && methodName.equals("forName")) {
3240
2f79c1748c93 6329581: RFE: LTP: java.beans.XMLEncoder does not manage ClassLoader.
malenkov
parents: 1851
diff changeset
   213
            return ClassFinder.resolveClass((String)arguments[0], this.loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   215
        Class<?>[] argClasses = new Class<?>[arguments.length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        for(int i = 0; i < arguments.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            argClasses[i] = (arguments[i] == null) ? null : arguments[i].getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        AccessibleObject m = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (target instanceof Class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            For class methods, simluate the effect of a meta class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            by taking the union of the static methods of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            actual class, with the instance methods of "Class.class"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            and the overloaded "newInstance" methods defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            This way "System.class", for example, will perform both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            the static method getProperties() and the instance method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            getSuperclass() defined in "Class.class".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (methodName.equals("new")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                methodName = "newInstance";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            // Provide a short form for array instantiation by faking an nary-constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (methodName.equals("newInstance") && ((Class)target).isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                Object result = Array.newInstance(((Class)target).getComponentType(), arguments.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                for(int i = 0; i < arguments.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    Array.set(result, i, arguments[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (methodName.equals("newInstance") && arguments.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                // The Character class, as of 1.4, does not have a constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                // which takes a String. All of the other "wrapper" classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                // for Java's primitive types have a String constructor so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                // fake such a constructor here so that this special case can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                // ignored elsewhere.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                if (target == Character.class && arguments.length == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    argClasses[0] == String.class) {
25187
08aff438def8 8048874: Replace uses of 'new Byte', 'new Short' and 'new Character' with appropriate alternative across core classes
prappo
parents: 24969
diff changeset
   251
                    return ((String)arguments[0]).charAt(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
1851
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   253
                try {
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   254
                    m = ConstructorFinder.findConstructor((Class)target, argClasses);
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   255
                }
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   256
                catch (NoSuchMethodException exception) {
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   257
                    m = null;
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   258
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (m == null && target != Class.class) {
1851
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   261
                m = getMethod((Class)target, methodName, argClasses);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (m == null) {
1851
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   264
                m = getMethod(Class.class, methodName, argClasses);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            This special casing of arrays is not necessary, but makes files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            involving arrays much shorter and simplifies the archiving infrastrcure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            The Array.set() method introduces an unusual idea - that of a static method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            changing the state of an instance. Normally statements with side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            effects on objects are instance methods of the objects themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            and we reinstate this rule (perhaps temporarily) by special-casing arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (target.getClass().isArray() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                (methodName.equals("set") || methodName.equals("get"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                int index = ((Integer)arguments[0]).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                if (methodName.equals("get")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    return Array.get(target, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    Array.set(target, index, arguments[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
1851
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   287
            m = getMethod(target.getClass(), methodName, argClasses);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (m != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (m instanceof Method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    return MethodUtil.invoke((Method)m, target, arguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    return ((Constructor)m).newInstance(arguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                throw new Exception("Statement cannot invoke: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                    methodName + " on " + target.getClass(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                    iae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            catch (InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                Throwable te = ite.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                if (te instanceof Exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    throw (Exception)te;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    throw ite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        throw new NoSuchMethodException(toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    String instanceName(Object instance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (instance == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } else if (instance.getClass() == String.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return "\""+(String)instance + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // Note: there is a minor problem with using the non-caching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            // NameGenerator method. The return value will not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            // specific information about the inner class name. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            // In 1.4.2 an inner class would be represented as JList$1 now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            // would be named Class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return NameGenerator.unqualifiedClassName(instance.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Prints the value of this statement using a Java-style syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        // Respect a subclass's implementation here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        Object target = getTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        String methodName = getMethodName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        Object[] arguments = getArguments();
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   340
        if (arguments == null) {
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   341
            arguments = emptyArray;
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 3240
diff changeset
   342
        }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14887
diff changeset
   343
        StringBuilder result = new StringBuilder(instanceName(target) + "." + methodName + "(");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        int n = arguments.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        for(int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            result.append(instanceName(arguments[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (i != n -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                result.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        result.append(");");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
1851
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   354
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   355
    static Method getMethod(Class<?> type, String name, Class<?>... args) {
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   356
        try {
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   357
            return MethodFinder.findMethod(type, name, args);
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   358
        }
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   359
        catch (NoSuchMethodException exception) {
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   360
            return null;
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   361
        }
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 2
diff changeset
   362
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}