< prev index next >

src/java.base/share/classes/java/lang/invoke/InfoFromMemberName.java

Print this page

  1 /*
  2  * Copyright (c) 2012, 2021, 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.MethodHandleNatives.Constants;
 31 import java.lang.invoke.MethodHandles.Lookup;
 32 import static java.lang.invoke.MethodHandleStatics.*;
 33 
 34 /*
 35  * Auxiliary to MethodHandleInfo, wants to nest in MethodHandleInfo but must be non-public.
 36  */
 37 /*non-public*/
 38 final class InfoFromMemberName implements MethodHandleInfo {
 39     private final MemberName member;
 40     private final int referenceKind;
 41 
 42     InfoFromMemberName(Lookup lookup, MemberName member, byte referenceKind) {
 43         assert(member.isResolved() || member.isMethodHandleInvoke() || member.isVarHandleMethodInvoke());
 44         assert(member.referenceKindIsConsistentWith(referenceKind));
 45         this.member = member;
 46         this.referenceKind = referenceKind;
 47     }
 48 
 49     @Override
 50     public Class<?> getDeclaringClass() {
 51         return member.getDeclaringClass();
 52     }

 94                         return reflectUnchecked();
 95                     } catch (ReflectiveOperationException ex) {
 96                         throw new IllegalArgumentException(ex);
 97                     }
 98                 }
 99             });
100         try {
101             Class<?> defc = getDeclaringClass();
102             byte refKind = (byte) getReferenceKind();
103             lookup.checkAccess(refKind, defc, convertToMemberName(refKind, mem));
104         } catch (IllegalAccessException ex) {
105             throw new IllegalArgumentException(ex);
106         }
107         return expected.cast(mem);
108     }
109 
110     private Member reflectUnchecked() throws ReflectiveOperationException {
111         byte refKind = (byte) getReferenceKind();
112         Class<?> defc = getDeclaringClass();
113         boolean isPublic = Modifier.isPublic(getModifiers());
114         if (MethodHandleNatives.refKindIsMethod(refKind)) {




















115             if (isPublic)
116                 return defc.getMethod(getName(), getMethodType().parameterArray());
117             else
118                 return defc.getDeclaredMethod(getName(), getMethodType().parameterArray());
119         } else if (MethodHandleNatives.refKindIsConstructor(refKind)) {
120             if (isPublic)
121                 return defc.getConstructor(getMethodType().parameterArray());
122             else
123                 return defc.getDeclaredConstructor(getMethodType().parameterArray());
124         } else if (MethodHandleNatives.refKindIsField(refKind)) {
125             if (isPublic)
126                 return defc.getField(getName());
127             else
128                 return defc.getDeclaredField(getName());
129         } else {
130             throw new IllegalArgumentException("referenceKind="+refKind);
131         }
132     }
133 
134     private static MemberName convertToMemberName(byte refKind, Member mem) throws IllegalAccessException {
135         if (mem instanceof Method) {
136             boolean wantSpecial = (refKind == REF_invokeSpecial);
137             return new MemberName((Method) mem, wantSpecial);
138         } else if (mem instanceof Constructor) {
139             return new MemberName((Constructor) mem);
140         } else if (mem instanceof Field) {
141             boolean isSetter = (refKind == REF_putField || refKind == REF_putStatic);
142             return new MemberName((Field) mem, isSetter);
143         }

  1 /*
  2  * Copyright (c) 2012, 2022, 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 jdk.internal.value.PrimitiveClass;
 29 
 30 import java.security.*;
 31 import java.lang.reflect.*;

 32 import java.lang.invoke.MethodHandles.Lookup;

 33 
 34 /*
 35  * Auxiliary to MethodHandleInfo, wants to nest in MethodHandleInfo but must be non-public.
 36  */
 37 /*non-public*/
 38 final class InfoFromMemberName implements MethodHandleInfo {
 39     private final MemberName member;
 40     private final int referenceKind;
 41 
 42     InfoFromMemberName(Lookup lookup, MemberName member, byte referenceKind) {
 43         assert(member.isResolved() || member.isMethodHandleInvoke() || member.isVarHandleMethodInvoke());
 44         assert(member.referenceKindIsConsistentWith(referenceKind));
 45         this.member = member;
 46         this.referenceKind = referenceKind;
 47     }
 48 
 49     @Override
 50     public Class<?> getDeclaringClass() {
 51         return member.getDeclaringClass();
 52     }

 94                         return reflectUnchecked();
 95                     } catch (ReflectiveOperationException ex) {
 96                         throw new IllegalArgumentException(ex);
 97                     }
 98                 }
 99             });
100         try {
101             Class<?> defc = getDeclaringClass();
102             byte refKind = (byte) getReferenceKind();
103             lookup.checkAccess(refKind, defc, convertToMemberName(refKind, mem));
104         } catch (IllegalAccessException ex) {
105             throw new IllegalArgumentException(ex);
106         }
107         return expected.cast(mem);
108     }
109 
110     private Member reflectUnchecked() throws ReflectiveOperationException {
111         byte refKind = (byte) getReferenceKind();
112         Class<?> defc = getDeclaringClass();
113         boolean isPublic = Modifier.isPublic(getModifiers());
114         if (member.isObjectConstructor()) {
115             MethodType methodType = getMethodType();
116             if (MethodHandleNatives.refKindIsObjectConstructor(refKind) &&
117                     methodType.returnType() != void.class) {
118                 // object constructor
119                 throw new IllegalArgumentException("object constructor must be of void return type");
120             }
121             return isPublic ? defc.getConstructor(methodType.parameterArray())
122                             : defc.getDeclaredConstructor(methodType.parameterArray());
123         } else if (member.isStaticValueFactoryMethod()) {
124             assert defc.isValue();
125             MethodType methodType = getMethodType();
126             Class<?> rtype = PrimitiveClass.isPrimitiveClass(defc) ? PrimitiveClass.asValueType(defc) : defc;
127             if (MethodHandleNatives.refKindIsMethod(refKind) &&
128                     methodType.returnType() != rtype) {
129                 // static vnew factory
130                 throw new IllegalArgumentException("static factory must be of " + defc.getName());
131             }
132             return isPublic ? defc.getConstructor(methodType.parameterArray())
133                             : defc.getDeclaredConstructor(methodType.parameterArray());
134         } else if (MethodHandleNatives.refKindIsMethod(refKind)) {
135             if (isPublic)
136                 return defc.getMethod(getName(), getMethodType().parameterArray());
137             else
138                 return defc.getDeclaredMethod(getName(), getMethodType().parameterArray());





139         } else if (MethodHandleNatives.refKindIsField(refKind)) {
140             if (isPublic)
141                 return defc.getField(getName());
142             else
143                 return defc.getDeclaredField(getName());
144         } else {
145             throw new IllegalArgumentException("referenceKind="+refKind);
146         }
147     }
148 
149     private static MemberName convertToMemberName(byte refKind, Member mem) throws IllegalAccessException {
150         if (mem instanceof Method) {
151             boolean wantSpecial = (refKind == REF_invokeSpecial);
152             return new MemberName((Method) mem, wantSpecial);
153         } else if (mem instanceof Constructor) {
154             return new MemberName((Constructor) mem);
155         } else if (mem instanceof Field) {
156             boolean isSetter = (refKind == REF_putField || refKind == REF_putStatic);
157             return new MemberName((Field) mem, isSetter);
158         }
< prev index next >