jaroslav@1646: /* jaroslav@1646: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. jaroslav@1646: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1646: * jaroslav@1646: * This code is free software; you can redistribute it and/or modify it jaroslav@1646: * under the terms of the GNU General Public License version 2 only, as jaroslav@1646: * published by the Free Software Foundation. Oracle designates this jaroslav@1646: * particular file as subject to the "Classpath" exception as provided jaroslav@1646: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1646: * jaroslav@1646: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1646: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1646: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1646: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1646: * accompanied this code). jaroslav@1646: * jaroslav@1646: * You should have received a copy of the GNU General Public License version jaroslav@1646: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1646: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1646: * jaroslav@1646: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1646: * or visit www.oracle.com if you need additional information or have any jaroslav@1646: * questions. jaroslav@1646: */ jaroslav@1646: jaroslav@1646: package java.lang.invoke; jaroslav@1646: jaroslav@1646: import java.security.*; jaroslav@1646: import java.lang.reflect.*; jaroslav@1646: import java.lang.invoke.MethodHandleNatives.Constants; jaroslav@1646: import java.lang.invoke.MethodHandles.Lookup; jaroslav@1646: import static java.lang.invoke.MethodHandleStatics.*; jaroslav@1646: jaroslav@1646: /* jaroslav@1646: * Auxiliary to MethodHandleInfo, wants to nest in MethodHandleInfo but must be non-public. jaroslav@1646: */ jaroslav@1646: /*non-public*/ jaroslav@1646: final jaroslav@1646: class InfoFromMemberName implements MethodHandleInfo { jaroslav@1646: private final MemberName member; jaroslav@1646: private final int referenceKind; jaroslav@1646: jaroslav@1646: InfoFromMemberName(Lookup lookup, MemberName member, byte referenceKind) { jaroslav@1646: assert(member.isResolved() || member.isMethodHandleInvoke()); jaroslav@1646: assert(member.referenceKindIsConsistentWith(referenceKind)); jaroslav@1646: this.member = member; jaroslav@1646: this.referenceKind = referenceKind; jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: public Class getDeclaringClass() { jaroslav@1646: return member.getDeclaringClass(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: public String getName() { jaroslav@1646: return member.getName(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: public MethodType getMethodType() { jaroslav@1646: return member.getMethodOrFieldType(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: public int getModifiers() { jaroslav@1646: return member.getModifiers(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: public int getReferenceKind() { jaroslav@1646: return referenceKind; jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: public String toString() { jaroslav@1646: return MethodHandleInfo.toString(getReferenceKind(), getDeclaringClass(), getName(), getMethodType()); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: public T reflectAs(Class expected, Lookup lookup) { jaroslav@1646: if (member.isMethodHandleInvoke() && !member.isVarargs()) { jaroslav@1646: // This member is an instance of a signature-polymorphic method, which cannot be reflected jaroslav@1646: // A method handle invoker can come in either of two forms: jaroslav@1646: // A generic placeholder (present in the source code, and varargs) jaroslav@1646: // and a signature-polymorphic instance (synthetic and not varargs). jaroslav@1646: // For more information see comments on {@link MethodHandleNatives#linkMethod}. jaroslav@1646: throw new IllegalArgumentException("cannot reflect signature polymorphic method"); jaroslav@1646: } jaroslav@1646: Member mem = AccessController.doPrivileged(new PrivilegedAction() { jaroslav@1646: public Member run() { jaroslav@1646: try { jaroslav@1646: return reflectUnchecked(); jaroslav@1646: } catch (ReflectiveOperationException ex) { jaroslav@1646: throw new IllegalArgumentException(ex); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: }); jaroslav@1646: try { jaroslav@1646: Class defc = getDeclaringClass(); jaroslav@1646: byte refKind = (byte) getReferenceKind(); jaroslav@1646: lookup.checkAccess(refKind, defc, convertToMemberName(refKind, mem)); jaroslav@1646: } catch (IllegalAccessException ex) { jaroslav@1646: throw new IllegalArgumentException(ex); jaroslav@1646: } jaroslav@1646: return expected.cast(mem); jaroslav@1646: } jaroslav@1646: jaroslav@1646: private Member reflectUnchecked() throws ReflectiveOperationException { jaroslav@1646: byte refKind = (byte) getReferenceKind(); jaroslav@1646: Class defc = getDeclaringClass(); jaroslav@1646: boolean isPublic = Modifier.isPublic(getModifiers()); jaroslav@1646: if (MethodHandleNatives.refKindIsMethod(refKind)) { jaroslav@1646: if (isPublic) jaroslav@1646: return defc.getMethod(getName(), getMethodType().parameterArray()); jaroslav@1646: else jaroslav@1646: return defc.getDeclaredMethod(getName(), getMethodType().parameterArray()); jaroslav@1646: } else if (MethodHandleNatives.refKindIsConstructor(refKind)) { jaroslav@1646: if (isPublic) jaroslav@1646: return defc.getConstructor(getMethodType().parameterArray()); jaroslav@1646: else jaroslav@1646: return defc.getDeclaredConstructor(getMethodType().parameterArray()); jaroslav@1646: } else if (MethodHandleNatives.refKindIsField(refKind)) { jaroslav@1646: if (isPublic) jaroslav@1646: return defc.getField(getName()); jaroslav@1646: else jaroslav@1646: return defc.getDeclaredField(getName()); jaroslav@1646: } else { jaroslav@1646: throw new IllegalArgumentException("referenceKind="+refKind); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static MemberName convertToMemberName(byte refKind, Member mem) throws IllegalAccessException { jaroslav@1646: if (mem instanceof Method) { jaroslav@1646: boolean wantSpecial = (refKind == REF_invokeSpecial); jaroslav@1646: return new MemberName((Method) mem, wantSpecial); jaroslav@1646: } else if (mem instanceof Constructor) { jaroslav@1646: return new MemberName((Constructor) mem); jaroslav@1646: } else if (mem instanceof Field) { jaroslav@1646: boolean isSetter = (refKind == REF_putField || refKind == REF_putStatic); jaroslav@1646: return new MemberName((Field) mem, isSetter); jaroslav@1646: } jaroslav@1646: throw new InternalError(mem.getClass().getName()); jaroslav@1646: } jaroslav@1646: }