jdk/src/java.desktop/share/classes/java/beans/MethodRef.java
author serb
Fri, 12 Feb 2016 16:09:39 +0300
changeset 36451 0f5d1613d67d
parent 25859 3317bb8137f4
child 40441 648e8ee9cf8c
permissions -rw-r--r--
8136382: SimpleBeanInfo.loadImage succeeds when running with a security manager Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
     1
/*
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
     4
 *
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    10
 *
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    15
 * accompanied this code).
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    16
 *
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    20
 *
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    23
 * questions.
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    24
 */
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    25
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    26
package java.beans;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    27
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    28
import java.lang.ref.SoftReference;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    29
import java.lang.ref.WeakReference;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    30
import java.lang.reflect.Method;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    31
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    32
import static sun.reflect.misc.ReflectUtil.isPackageAccessible;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    33
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    34
final class MethodRef {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    35
    private String signature;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    36
    private SoftReference<Method> methodRef;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    37
    private WeakReference<Class<?>> typeRef;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    38
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    39
    void set(Method method) {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    40
        if (method == null) {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    41
            this.signature = null;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    42
            this.methodRef = null;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    43
            this.typeRef = null;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    44
        }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    45
        else {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    46
            this.signature = method.toGenericString();
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    47
            this.methodRef = new SoftReference<>(method);
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    48
            this.typeRef = new WeakReference<Class<?>>(method.getDeclaringClass());
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    49
        }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    50
    }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    51
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    52
    boolean isSet() {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    53
        return this.methodRef != null;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    54
    }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    55
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    56
    Method get() {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    57
        if (this.methodRef == null) {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    58
            return null;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    59
        }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    60
        Method method = this.methodRef.get();
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    61
        if (method == null) {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    62
            method = find(this.typeRef.get(), this.signature);
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    63
            if (method == null) {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    64
                this.signature = null;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    65
                this.methodRef = null;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    66
                this.typeRef = null;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    67
            }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    68
            else {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    69
                this.methodRef = new SoftReference<>(method);
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    70
            }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    71
        }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    72
        return isPackageAccessible(method.getDeclaringClass()) ? method : null;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    73
    }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    74
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    75
    private static Method find(Class<?> type, String signature) {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    76
        if (type != null) {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    77
            for (Method method : type.getMethods()) {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    78
                if (type.equals(method.getDeclaringClass())) {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    79
                    if (method.toGenericString().equals(signature)) {
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    80
                        return method;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    81
                    }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    82
                }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    83
            }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    84
        }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    85
        return null;
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    86
    }
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents:
diff changeset
    87
}