src/jdk/internal/dynalink/beans/AccessibleMembersLookup.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 2605
afbc625eaca7
parent 1205
4112748288bb
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset 7c756d901f9a

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 /*
aoqi@0 27 * This file is available under and governed by the GNU General Public
aoqi@0 28 * License version 2 only, as published by the Free Software Foundation.
aoqi@0 29 * However, the following notice accompanied the original version of this
aoqi@0 30 * file, and Oracle licenses the original version of this file under the BSD
aoqi@0 31 * license:
aoqi@0 32 */
aoqi@0 33 /*
aoqi@0 34 Copyright 2009-2013 Attila Szegedi
aoqi@0 35
aoqi@0 36 Licensed under both the Apache License, Version 2.0 (the "Apache License")
aoqi@0 37 and the BSD License (the "BSD License"), with licensee being free to
aoqi@0 38 choose either of the two at their discretion.
aoqi@0 39
aoqi@0 40 You may not use this file except in compliance with either the Apache
aoqi@0 41 License or the BSD License.
aoqi@0 42
aoqi@0 43 If you choose to use this file in compliance with the Apache License, the
aoqi@0 44 following notice applies to you:
aoqi@0 45
aoqi@0 46 You may obtain a copy of the Apache License at
aoqi@0 47
aoqi@0 48 http://www.apache.org/licenses/LICENSE-2.0
aoqi@0 49
aoqi@0 50 Unless required by applicable law or agreed to in writing, software
aoqi@0 51 distributed under the License is distributed on an "AS IS" BASIS,
aoqi@0 52 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
aoqi@0 53 implied. See the License for the specific language governing
aoqi@0 54 permissions and limitations under the License.
aoqi@0 55
aoqi@0 56 If you choose to use this file in compliance with the BSD License, the
aoqi@0 57 following notice applies to you:
aoqi@0 58
aoqi@0 59 Redistribution and use in source and binary forms, with or without
aoqi@0 60 modification, are permitted provided that the following conditions are
aoqi@0 61 met:
aoqi@0 62 * Redistributions of source code must retain the above copyright
aoqi@0 63 notice, this list of conditions and the following disclaimer.
aoqi@0 64 * Redistributions in binary form must reproduce the above copyright
aoqi@0 65 notice, this list of conditions and the following disclaimer in the
aoqi@0 66 documentation and/or other materials provided with the distribution.
aoqi@0 67 * Neither the name of the copyright holder nor the names of
aoqi@0 68 contributors may be used to endorse or promote products derived from
aoqi@0 69 this software without specific prior written permission.
aoqi@0 70
aoqi@0 71 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
aoqi@0 72 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
aoqi@0 73 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
aoqi@0 74 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
aoqi@0 75 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
aoqi@0 76 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
aoqi@0 77 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
aoqi@0 78 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
aoqi@0 79 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
aoqi@0 80 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
aoqi@0 81 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
aoqi@0 82 */
aoqi@0 83
aoqi@0 84 package jdk.internal.dynalink.beans;
aoqi@0 85
aoqi@0 86 import java.lang.reflect.Method;
aoqi@0 87 import java.lang.reflect.Modifier;
aoqi@0 88 import java.util.Arrays;
aoqi@0 89 import java.util.Collection;
aoqi@0 90 import java.util.HashMap;
aoqi@0 91 import java.util.LinkedHashSet;
aoqi@0 92 import java.util.Map;
aoqi@0 93 import java.util.Set;
aoqi@0 94
aoqi@0 95 /**
aoqi@0 96 * Utility class for discovering accessible methods and inner classes. Normally, a public member declared on a class is
aoqi@0 97 * accessible (that is, it can be invoked from anywhere). However, this is not the case if the class itself is not
aoqi@0 98 * public, or belongs to a restricted-access package. In that case, it is required to lookup a member in a publicly
aoqi@0 99 * accessible superclass or implemented interface of the class, and use it instead of the member discovered on the
aoqi@0 100 * class.
aoqi@0 101 *
aoqi@0 102 * @author Attila Szegedi
aoqi@0 103 */
aoqi@0 104 class AccessibleMembersLookup {
aoqi@0 105 private final Map<MethodSignature, Method> methods;
aoqi@0 106 private final Set<Class<?>> innerClasses;
attila@962 107 private final boolean instance;
aoqi@0 108
aoqi@0 109 /**
aoqi@0 110 * Creates a mapping for all accessible methods and inner classes on a class.
aoqi@0 111 *
aoqi@0 112 * @param clazz the inspected class
aoqi@0 113 * @param instance true to inspect instance methods, false to inspect static methods.
aoqi@0 114 */
attila@962 115 AccessibleMembersLookup(final Class<?> clazz, final boolean instance) {
aoqi@0 116 this.methods = new HashMap<>();
aoqi@0 117 this.innerClasses = new LinkedHashSet<>();
aoqi@0 118 this.instance = instance;
aoqi@0 119 lookupAccessibleMembers(clazz);
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 /**
aoqi@0 123 * Returns an accessible method equivalent of a method.
aoqi@0 124 *
aoqi@0 125 * @param m the method whose accessible equivalent is requested.
aoqi@0 126 * @return the accessible equivalent for the method (can be the same as the passed in method), or null if there is
aoqi@0 127 * no accessible method equivalent.
aoqi@0 128 */
aoqi@0 129 Method getAccessibleMethod(final Method m) {
aoqi@0 130 return m == null ? null : methods.get(new MethodSignature(m));
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 Collection<Method> getMethods() {
aoqi@0 134 return methods.values();
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 Class<?>[] getInnerClasses() {
aoqi@0 138 return innerClasses.toArray(new Class<?>[innerClasses.size()]);
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 /**
aoqi@0 142 * A helper class that represents a method signature - name and argument types.
aoqi@0 143 *
aoqi@0 144 * @author Attila Szegedi
aoqi@0 145 */
aoqi@0 146 static final class MethodSignature {
aoqi@0 147 private final String name;
aoqi@0 148 private final Class<?>[] args;
aoqi@0 149
aoqi@0 150 /**
aoqi@0 151 * Creates a new method signature from arbitrary data.
aoqi@0 152 *
aoqi@0 153 * @param name the name of the method this signature represents.
aoqi@0 154 * @param args the argument types of the method.
aoqi@0 155 */
attila@962 156 MethodSignature(final String name, final Class<?>[] args) {
aoqi@0 157 this.name = name;
aoqi@0 158 this.args = args;
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 /**
aoqi@0 162 * Creates a signature for the given method.
aoqi@0 163 *
aoqi@0 164 * @param method the method for which a signature is created.
aoqi@0 165 */
aoqi@0 166 MethodSignature(final Method method) {
aoqi@0 167 this(method.getName(), method.getParameterTypes());
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 /**
aoqi@0 171 * Compares this object to another object
aoqi@0 172 *
aoqi@0 173 * @param o the other object
aoqi@0 174 * @return true if the other object is also a method signature with the same name, same number of arguments, and
aoqi@0 175 * same types of arguments.
aoqi@0 176 */
aoqi@0 177 @Override
aoqi@0 178 public boolean equals(final Object o) {
aoqi@0 179 if(o instanceof MethodSignature) {
aoqi@0 180 final MethodSignature ms = (MethodSignature)o;
aoqi@0 181 return ms.name.equals(name) && Arrays.equals(args, ms.args);
aoqi@0 182 }
aoqi@0 183 return false;
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 /**
aoqi@0 187 * Returns a hash code, consistent with the overridden {@link #equals(Object)}.
aoqi@0 188 */
aoqi@0 189 @Override
aoqi@0 190 public int hashCode() {
aoqi@0 191 return name.hashCode() ^ Arrays.hashCode(args);
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 @Override
aoqi@0 195 public String toString() {
aoqi@0 196 final StringBuilder b = new StringBuilder();
aoqi@0 197 b.append("[MethodSignature ").append(name).append('(');
aoqi@0 198 if(args.length > 0) {
aoqi@0 199 b.append(args[0].getCanonicalName());
aoqi@0 200 for(int i = 1; i < args.length; ++i) {
aoqi@0 201 b.append(", ").append(args[i].getCanonicalName());
aoqi@0 202 }
aoqi@0 203 }
aoqi@0 204 return b.append(")]").toString();
aoqi@0 205 }
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 private void lookupAccessibleMembers(final Class<?> clazz) {
aoqi@0 209 boolean searchSuperTypes;
aoqi@0 210
aoqi@0 211 if(!CheckRestrictedPackage.isRestrictedClass(clazz)) {
aoqi@0 212 searchSuperTypes = false;
attila@962 213 for(final Method method: clazz.getMethods()) {
aoqi@0 214 final boolean isStatic = Modifier.isStatic(method.getModifiers());
aoqi@0 215 if(instance != isStatic) {
aoqi@0 216 final MethodSignature sig = new MethodSignature(method);
aoqi@0 217 if(!methods.containsKey(sig)) {
aoqi@0 218 final Class<?> declaringClass = method.getDeclaringClass();
aoqi@0 219 if(declaringClass != clazz && CheckRestrictedPackage.isRestrictedClass(declaringClass)) {
aoqi@0 220 //Sometimes, the declaring class of a method (Method.getDeclaringClass())
aoqi@0 221 //retrieved through Class.getMethods() for a public class will be a
aoqi@0 222 //non-public superclass. For such a method, we need to find a method with
aoqi@0 223 //the same name and signature in a public superclass or implemented
aoqi@0 224 //interface.
aoqi@0 225 //This typically doesn't happen with classes emitted by a reasonably modern
aoqi@0 226 //javac, as it'll create synthetic delegator methods in all public
aoqi@0 227 //immediate subclasses of the non-public class. We have, however, observed
aoqi@0 228 //this in the wild with class files compiled with older javac that doesn't
aoqi@0 229 //generate the said synthetic delegators.
aoqi@0 230 searchSuperTypes = true;
aoqi@0 231 } else {
aoqi@0 232 // don't allow inherited static
aoqi@0 233 if (!isStatic || clazz == declaringClass) {
aoqi@0 234 methods.put(sig, method);
aoqi@0 235 }
aoqi@0 236 }
aoqi@0 237 }
aoqi@0 238 }
aoqi@0 239 }
attila@962 240 for(final Class<?> innerClass: clazz.getClasses()) {
aoqi@0 241 // Add both static and non-static classes, regardless of instance flag. StaticClassLinker will just
aoqi@0 242 // expose non-static classes with explicit constructor outer class argument.
aoqi@0 243 // NOTE: getting inner class objects through getClasses() does not resolve them, so if those classes
aoqi@0 244 // were not yet loaded, they'll only get loaded in a non-resolved state; no static initializers for
aoqi@0 245 // them will trigger just by doing this.
aoqi@0 246 innerClasses.add(innerClass);
aoqi@0 247 }
aoqi@0 248 } else {
aoqi@0 249 searchSuperTypes = true;
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 // don't need to search super types for static methods
aoqi@0 253 if(instance && searchSuperTypes) {
aoqi@0 254 // If we reach here, the class is either not public, or it is in a restricted package. Alternatively, it is
aoqi@0 255 // public, but some of its methods claim that their declaring class is non-public. We'll try superclasses
aoqi@0 256 // and implemented interfaces then looking for public ones.
aoqi@0 257 final Class<?>[] interfaces = clazz.getInterfaces();
aoqi@0 258 for(int i = 0; i < interfaces.length; i++) {
aoqi@0 259 lookupAccessibleMembers(interfaces[i]);
aoqi@0 260 }
aoqi@0 261 final Class<?> superclass = clazz.getSuperclass();
aoqi@0 262 if(superclass != null) {
aoqi@0 263 lookupAccessibleMembers(superclass);
aoqi@0 264 }
aoqi@0 265 }
aoqi@0 266 }
aoqi@0 267 }

mercurial