< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2008, 2017, 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


 504     public boolean isField() {
 505         return testAllFlags(IS_FIELD);
 506     }
 507     /** Query whether this member is a type. */
 508     public boolean isType() {
 509         return testAllFlags(IS_TYPE);
 510     }
 511     /** Utility method to query whether this member is neither public, private, nor protected. */
 512     public boolean isPackage() {
 513         return !testAnyFlags(ALL_ACCESS);
 514     }
 515     /** Query whether this member has a CallerSensitive annotation. */
 516     public boolean isCallerSensitive() {
 517         return testAllFlags(CALLER_SENSITIVE);
 518     }
 519 
 520     /** Utility method to query whether this member is accessible from a given lookup class. */
 521     public boolean isAccessibleFrom(Class<?> lookupClass) {
 522         int mode = (ALL_ACCESS|MethodHandles.Lookup.PACKAGE|MethodHandles.Lookup.MODULE);
 523         return VerifyAccess.isMemberAccessible(this.getDeclaringClass(), this.getDeclaringClass(), flags,
 524                                                lookupClass, mode);
 525     }
 526 
 527     /**
 528      * Check if MemberName is a call to a method named {@code name} in class {@code declaredClass}.
 529      */
 530     public boolean refersTo(Class<?> declc, String n) {
 531         return clazz == declc && getName().equals(n);
 532     }
 533 
 534     /** Initialize a query.   It is not resolved. */
 535     private void init(Class<?> defClass, String name, Object type, int flags) {
 536         // defining class is allowed to be null (for a naked name/type pair)
 537         //name.toString();  // null check
 538         //type.equals(type);  // null check
 539         // fill in fields:
 540         this.clazz = defClass;
 541         this.name = name;
 542         this.type = type;
 543         this.flags = flags;
 544         assert(testAnyFlags(ALL_KINDS));


 913         if (refKind != REF_NONE) {
 914             buf.append('/');
 915             buf.append(MethodHandleNatives.refKindName(refKind));
 916         }
 917         //buf.append("#").append(System.identityHashCode(this));
 918         return buf.toString();
 919     }
 920     private static String getName(Object obj) {
 921         if (obj instanceof Class<?>)
 922             return ((Class<?>)obj).getName();
 923         return String.valueOf(obj);
 924     }
 925 
 926     public IllegalAccessException makeAccessException(String message, Object from) {
 927         message = message + ": "+ toString();
 928         if (from != null)  {
 929             if (from == MethodHandles.publicLookup()) {
 930                 message += ", from public Lookup";
 931             } else {
 932                 Module m;

 933                 if (from instanceof MethodHandles.Lookup) {
 934                     MethodHandles.Lookup lookup = (MethodHandles.Lookup)from;

 935                     m = lookup.lookupClass().getModule();

 936                 } else {
 937                     m = from.getClass().getModule();

 938                 }
 939                 message += ", from " + from + " (" + m + ")";




 940             }
 941         }
 942         return new IllegalAccessException(message);
 943     }
 944     private String message() {
 945         if (isResolved())
 946             return "no access";
 947         else if (isConstructor())
 948             return "no such constructor";
 949         else if (isMethod())
 950             return "no such method";
 951         else
 952             return "no such field";
 953     }
 954     public ReflectiveOperationException makeAccessException() {
 955         String message = message() + ": "+ toString();
 956         ReflectiveOperationException ex;
 957         if (isResolved() || !(resolution instanceof NoSuchMethodError ||
 958                               resolution instanceof NoSuchFieldError))
 959             ex = new IllegalAccessException(message);


   1 /*
   2  * Copyright (c) 2008, 2019, 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


 504     public boolean isField() {
 505         return testAllFlags(IS_FIELD);
 506     }
 507     /** Query whether this member is a type. */
 508     public boolean isType() {
 509         return testAllFlags(IS_TYPE);
 510     }
 511     /** Utility method to query whether this member is neither public, private, nor protected. */
 512     public boolean isPackage() {
 513         return !testAnyFlags(ALL_ACCESS);
 514     }
 515     /** Query whether this member has a CallerSensitive annotation. */
 516     public boolean isCallerSensitive() {
 517         return testAllFlags(CALLER_SENSITIVE);
 518     }
 519 
 520     /** Utility method to query whether this member is accessible from a given lookup class. */
 521     public boolean isAccessibleFrom(Class<?> lookupClass) {
 522         int mode = (ALL_ACCESS|MethodHandles.Lookup.PACKAGE|MethodHandles.Lookup.MODULE);
 523         return VerifyAccess.isMemberAccessible(this.getDeclaringClass(), this.getDeclaringClass(), flags,
 524                                                lookupClass, null, mode);
 525     }
 526 
 527     /**
 528      * Check if MemberName is a call to a method named {@code name} in class {@code declaredClass}.
 529      */
 530     public boolean refersTo(Class<?> declc, String n) {
 531         return clazz == declc && getName().equals(n);
 532     }
 533 
 534     /** Initialize a query.   It is not resolved. */
 535     private void init(Class<?> defClass, String name, Object type, int flags) {
 536         // defining class is allowed to be null (for a naked name/type pair)
 537         //name.toString();  // null check
 538         //type.equals(type);  // null check
 539         // fill in fields:
 540         this.clazz = defClass;
 541         this.name = name;
 542         this.type = type;
 543         this.flags = flags;
 544         assert(testAnyFlags(ALL_KINDS));


 913         if (refKind != REF_NONE) {
 914             buf.append('/');
 915             buf.append(MethodHandleNatives.refKindName(refKind));
 916         }
 917         //buf.append("#").append(System.identityHashCode(this));
 918         return buf.toString();
 919     }
 920     private static String getName(Object obj) {
 921         if (obj instanceof Class<?>)
 922             return ((Class<?>)obj).getName();
 923         return String.valueOf(obj);
 924     }
 925 
 926     public IllegalAccessException makeAccessException(String message, Object from) {
 927         message = message + ": "+ toString();
 928         if (from != null)  {
 929             if (from == MethodHandles.publicLookup()) {
 930                 message += ", from public Lookup";
 931             } else {
 932                 Module m;
 933                 Class<?> plc;
 934                 if (from instanceof MethodHandles.Lookup) {
 935                     MethodHandles.Lookup lookup = (MethodHandles.Lookup)from;
 936                     from = lookup.lookupClass();
 937                     m = lookup.lookupClass().getModule();
 938                     plc = lookup.previousLookupClass();
 939                 } else {
 940                     m = ((Class<?>)from).getModule();
 941                     plc = null;
 942                 }
 943                 message += ", from " + from + " (" + m + ")";
 944                 if (plc != null) {
 945                     message += ", previous lookup " +
 946                         plc.getName() + " (" + plc.getModule() + ")";
 947                 }
 948             }
 949         }
 950         return new IllegalAccessException(message);
 951     }
 952     private String message() {
 953         if (isResolved())
 954             return "no access";
 955         else if (isConstructor())
 956             return "no such constructor";
 957         else if (isMethod())
 958             return "no such method";
 959         else
 960             return "no such field";
 961     }
 962     public ReflectiveOperationException makeAccessException() {
 963         String message = message() + ": "+ toString();
 964         ReflectiveOperationException ex;
 965         if (isResolved() || !(resolution instanceof NoSuchMethodError ||
 966                               resolution instanceof NoSuchFieldError))
 967             ex = new IllegalAccessException(message);


< prev index next >