< prev index next >

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

Print this page
rev 49420 : 8200238: Reduce number of exceptions created when calling MemberName$Factory::resolveOrNull
Reviewed-by: lfoltan, acorn


1030                 result.add(buf[i]);
1031             }
1032             // Signature matching is not the same as type matching, since
1033             // one signature might correspond to several types.
1034             // So if matchType is a Class or MethodType, refilter the results.
1035             if (matchType != null && matchType != matchSig) {
1036                 for (Iterator<MemberName> it = result.iterator(); it.hasNext();) {
1037                     MemberName m = it.next();
1038                     if (!matchType.equals(m.getType()))
1039                         it.remove();
1040                 }
1041             }
1042             return result;
1043         }
1044         /** Produce a resolved version of the given member.
1045          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1046          *  Access checking is performed on behalf of the given {@code lookupClass}.
1047          *  If lookup fails or access is not permitted, null is returned.
1048          *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
1049          */
1050         private MemberName resolve(byte refKind, MemberName ref, Class<?> lookupClass) {

1051             MemberName m = ref.clone();  // JVM will side-effect the ref
1052             assert(refKind == m.getReferenceKind());
1053             try {
1054                 // There are 4 entities in play here:
1055                 //   * LC: lookupClass
1056                 //   * REFC: symbolic reference class (MN.clazz before resolution);
1057                 //   * DEFC: resolved method holder (MN.clazz after resolution);
1058                 //   * PTYPES: parameter types (MN.type)
1059                 //
1060                 // What we care about when resolving a MemberName is consistency between DEFC and PTYPES.
1061                 // We do type alias (TA) checks on DEFC to ensure that. DEFC is not known until the JVM
1062                 // finishes the resolution, so do TA checks right after MHN.resolve() is over.
1063                 //
1064                 // All parameters passed by a caller are checked against MH type (PTYPES) on every invocation,
1065                 // so it is safe to call a MH from any context.
1066                 //
1067                 // REFC view on PTYPES doesn't matter, since it is used only as a starting point for resolution and doesn't
1068                 // participate in method selection.
1069                 m = MethodHandleNatives.resolve(m, lookupClass);



1070                 m.checkForTypeAlias(m.getDeclaringClass());
1071                 m.resolution = null;
1072             } catch (ClassNotFoundException | LinkageError ex) {
1073                 // JVM reports that the "bytecode behavior" would get an error
1074                 assert(!m.isResolved());
1075                 m.resolution = ex;
1076                 return m;
1077             }
1078             assert(m.referenceKindIsConsistent());
1079             m.initResolved(true);
1080             assert(m.vminfoIsConsistent());
1081             return m;
1082         }
1083         /** Produce a resolved version of the given member.
1084          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1085          *  Access checking is performed on behalf of the given {@code lookupClass}.
1086          *  If lookup fails or access is not permitted, a {@linkplain ReflectiveOperationException} is thrown.
1087          *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
1088          */
1089         public
1090         <NoSuchMemberException extends ReflectiveOperationException>
1091         MemberName resolveOrFail(byte refKind, MemberName m, Class<?> lookupClass,
1092                                  Class<NoSuchMemberException> nsmClass)
1093                 throws IllegalAccessException, NoSuchMemberException {
1094             MemberName result = resolve(refKind, m, lookupClass);
1095             if (result.isResolved())
1096                 return result;
1097             ReflectiveOperationException ex = result.makeAccessException();
1098             if (ex instanceof IllegalAccessException)  throw (IllegalAccessException) ex;
1099             throw nsmClass.cast(ex);
1100         }
1101         /** Produce a resolved version of the given member.
1102          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1103          *  Access checking is performed on behalf of the given {@code lookupClass}.
1104          *  If lookup fails or access is not permitted, return null.
1105          *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
1106          */
1107         public
1108         MemberName resolveOrNull(byte refKind, MemberName m, Class<?> lookupClass) {
1109             MemberName result = resolve(refKind, m, lookupClass);
1110             if (result.isResolved())
1111                 return result;
1112             return null;
1113         }
1114         /** Return a list of all methods defined by the given class.
1115          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1116          *  Access checking is performed on behalf of the given {@code lookupClass}.
1117          *  Inaccessible members are not added to the last.
1118          */
1119         public List<MemberName> getMethods(Class<?> defc, boolean searchSupers,
1120                 Class<?> lookupClass) {
1121             return getMethods(defc, searchSupers, null, null, lookupClass);
1122         }
1123         /** Return a list of matching methods defined by the given class.
1124          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1125          *  Returned methods will match the name (if not null) and the type (if not null).
1126          *  Access checking is performed on behalf of the given {@code lookupClass}.
1127          *  Inaccessible members are not added to the last.
1128          */
1129         public List<MemberName> getMethods(Class<?> defc, boolean searchSupers,
1130                 String name, MethodType type, Class<?> lookupClass) {




1030                 result.add(buf[i]);
1031             }
1032             // Signature matching is not the same as type matching, since
1033             // one signature might correspond to several types.
1034             // So if matchType is a Class or MethodType, refilter the results.
1035             if (matchType != null && matchType != matchSig) {
1036                 for (Iterator<MemberName> it = result.iterator(); it.hasNext();) {
1037                     MemberName m = it.next();
1038                     if (!matchType.equals(m.getType()))
1039                         it.remove();
1040                 }
1041             }
1042             return result;
1043         }
1044         /** Produce a resolved version of the given member.
1045          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1046          *  Access checking is performed on behalf of the given {@code lookupClass}.
1047          *  If lookup fails or access is not permitted, null is returned.
1048          *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
1049          */
1050         private MemberName resolve(byte refKind, MemberName ref, Class<?> lookupClass,
1051                                    boolean speculativeResolve) {
1052             MemberName m = ref.clone();  // JVM will side-effect the ref
1053             assert(refKind == m.getReferenceKind());
1054             try {
1055                 // There are 4 entities in play here:
1056                 //   * LC: lookupClass
1057                 //   * REFC: symbolic reference class (MN.clazz before resolution);
1058                 //   * DEFC: resolved method holder (MN.clazz after resolution);
1059                 //   * PTYPES: parameter types (MN.type)
1060                 //
1061                 // What we care about when resolving a MemberName is consistency between DEFC and PTYPES.
1062                 // We do type alias (TA) checks on DEFC to ensure that. DEFC is not known until the JVM
1063                 // finishes the resolution, so do TA checks right after MHN.resolve() is over.
1064                 //
1065                 // All parameters passed by a caller are checked against MH type (PTYPES) on every invocation,
1066                 // so it is safe to call a MH from any context.
1067                 //
1068                 // REFC view on PTYPES doesn't matter, since it is used only as a starting point for resolution and doesn't
1069                 // participate in method selection.
1070                 m = MethodHandleNatives.resolve(m, lookupClass, speculativeResolve);
1071                 if (m == null && speculativeResolve) {
1072                     return null;
1073                 }
1074                 m.checkForTypeAlias(m.getDeclaringClass());
1075                 m.resolution = null;
1076             } catch (ClassNotFoundException | LinkageError ex) {
1077                 // JVM reports that the "bytecode behavior" would get an error
1078                 assert(!m.isResolved());
1079                 m.resolution = ex;
1080                 return m;
1081             }
1082             assert(m.referenceKindIsConsistent());
1083             m.initResolved(true);
1084             assert(m.vminfoIsConsistent());
1085             return m;
1086         }
1087         /** Produce a resolved version of the given member.
1088          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1089          *  Access checking is performed on behalf of the given {@code lookupClass}.
1090          *  If lookup fails or access is not permitted, a {@linkplain ReflectiveOperationException} is thrown.
1091          *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
1092          */
1093         public
1094         <NoSuchMemberException extends ReflectiveOperationException>
1095         MemberName resolveOrFail(byte refKind, MemberName m, Class<?> lookupClass,
1096                                  Class<NoSuchMemberException> nsmClass)
1097                 throws IllegalAccessException, NoSuchMemberException {
1098             MemberName result = resolve(refKind, m, lookupClass, false);
1099             if (result.isResolved())
1100                 return result;
1101             ReflectiveOperationException ex = result.makeAccessException();
1102             if (ex instanceof IllegalAccessException)  throw (IllegalAccessException) ex;
1103             throw nsmClass.cast(ex);
1104         }
1105         /** Produce a resolved version of the given member.
1106          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1107          *  Access checking is performed on behalf of the given {@code lookupClass}.
1108          *  If lookup fails or access is not permitted, return null.
1109          *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
1110          */
1111         public
1112         MemberName resolveOrNull(byte refKind, MemberName m, Class<?> lookupClass) {
1113             MemberName result = resolve(refKind, m, lookupClass, true);
1114             if (result != null && result.isResolved())
1115                 return result;
1116             return null;
1117         }
1118         /** Return a list of all methods defined by the given class.
1119          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1120          *  Access checking is performed on behalf of the given {@code lookupClass}.
1121          *  Inaccessible members are not added to the last.
1122          */
1123         public List<MemberName> getMethods(Class<?> defc, boolean searchSupers,
1124                 Class<?> lookupClass) {
1125             return getMethods(defc, searchSupers, null, null, lookupClass);
1126         }
1127         /** Return a list of matching methods defined by the given class.
1128          *  Super types are searched (for inherited members) if {@code searchSupers} is true.
1129          *  Returned methods will match the name (if not null) and the type (if not null).
1130          *  Access checking is performed on behalf of the given {@code lookupClass}.
1131          *  Inaccessible members are not added to the last.
1132          */
1133         public List<MemberName> getMethods(Class<?> defc, boolean searchSupers,
1134                 String name, MethodType type, Class<?> lookupClass) {


< prev index next >