1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import java.security.*;
  29 import java.lang.reflect.*;
  30 import java.lang.invoke.MethodHandles.Lookup;
  31 
  32 /*
  33  * Auxiliary to MethodHandleInfo, wants to nest in MethodHandleInfo but must be non-public.
  34  */
  35 /*non-public*/
  36 final
  37 class InfoFromMemberName implements MethodHandleInfo {
  38     private final MemberName member;
  39     private final int referenceKind;
  40 
  41     InfoFromMemberName(Lookup lookup, MemberName member, byte referenceKind) {
  42         assert(member.isResolved() || member.isMethodHandleInvoke() || member.isVarHandleMethodInvoke());
  43         assert(member.referenceKindIsConsistentWith(referenceKind));
  44         this.member = member;
  45         this.referenceKind = referenceKind;
  46     }
  47 
  48     @Override
  49     public Class<?> getDeclaringClass() {
  50         return member.getDeclaringClass();
  51     }
  52 
  53     @Override
  54     public String getName() {
  55         return member.getName();
  56     }
  57 
  58     @Override
  59     public MethodType getMethodType() {
  60         return member.getMethodOrFieldType();
  61     }
  62 
  63     @Override
  64     public int getModifiers() {
  65         return member.getModifiers();
  66     }
  67 
  68     @Override
  69     public int getReferenceKind() {
  70         return referenceKind;
  71     }
  72 
  73     @Override
  74     public String toString() {
  75         return MethodHandleInfo.toString(getReferenceKind(), getDeclaringClass(), getName(), getMethodType());
  76     }
  77 
  78     @Override
  79     public <T extends Member> T reflectAs(Class<T> expected, Lookup lookup) {
  80         if ((member.isMethodHandleInvoke() || member.isVarHandleMethodInvoke())
  81             && !member.isVarargs()) {
  82             // This member is an instance of a signature-polymorphic method, which cannot be reflected
  83             // A method handle invoker can come in either of two forms:
  84             // A generic placeholder (present in the source code, and varargs)
  85             // and a signature-polymorphic instance (synthetic and not varargs).
  86             // For more information see comments on {@link MethodHandleNatives#linkMethod}.
  87             throw new IllegalArgumentException("cannot reflect signature polymorphic method");
  88         }
  89         Member mem = AccessController.doPrivileged(new PrivilegedAction<>() {
  90                 public Member run() {
  91                     try {
  92                         return reflectUnchecked();
  93                     } catch (ReflectiveOperationException ex) {
  94                         throw new IllegalArgumentException(ex);
  95                     }
  96                 }
  97             });
  98         try {
  99             Class<?> defc = getDeclaringClass();
 100             byte refKind = (byte) getReferenceKind();
 101             lookup.checkAccess(refKind, defc, convertToMemberName(refKind, mem));
 102         } catch (IllegalAccessException ex) {
 103             throw new IllegalArgumentException(ex);
 104         }
 105         return expected.cast(mem);
 106     }
 107 
 108     private Member reflectUnchecked() throws ReflectiveOperationException {
 109         byte refKind = (byte) getReferenceKind();
 110         Class<?> defc = getDeclaringClass();
 111         boolean isPublic = Modifier.isPublic(getModifiers());
 112         if (member.isObjectConstructorOrStaticInitMethod()) {
 113             MethodType methodType = getMethodType();
 114             if (MethodHandleNatives.refKindIsObjectConstructor(refKind) &&
 115                 methodType.returnType() != void.class) {
 116                 // object constructor
 117                 throw new IllegalArgumentException("object constructor must be of void return type");
 118             } else if (MethodHandleNatives.refKindIsMethod(refKind) &&
 119                        methodType.returnType() != defc.asPrimaryType()) {
 120                 // static init factory
 121                 throw new IllegalArgumentException("static constructor must be of " + getDeclaringClass().getName());
 122             }
 123 
 124             return isPublic ? defc.getConstructor(methodType.parameterArray())
 125                             : defc.getDeclaredConstructor(methodType.parameterArray());
 126         } else if (MethodHandleNatives.refKindIsMethod(refKind)) {
 127             if (isPublic)
 128                 return defc.getMethod(getName(), getMethodType().parameterArray());
 129             else
 130                 return defc.getDeclaredMethod(getName(), getMethodType().parameterArray());
 131         } else if (MethodHandleNatives.refKindIsField(refKind)) {
 132             if (isPublic)
 133                 return defc.getField(getName());
 134             else
 135                 return defc.getDeclaredField(getName());
 136         } else {
 137             throw new IllegalArgumentException("referenceKind="+refKind);
 138         }
 139     }
 140 
 141     private static MemberName convertToMemberName(byte refKind, Member mem) throws IllegalAccessException {
 142         if (mem instanceof Method) {
 143             boolean wantSpecial = (refKind == REF_invokeSpecial);
 144             return new MemberName((Method) mem, wantSpecial);
 145         } else if (mem instanceof Constructor) {
 146             return new MemberName((Constructor) mem);
 147         } else if (mem instanceof Field) {
 148             boolean isSetter = (refKind == REF_putField || refKind == REF_putStatic);
 149             return new MemberName((Field) mem, isSetter);
 150         }
 151         throw new InternalError(mem.getClass().getName());
 152     }
 153 }