< prev index next >

src/java.base/share/classes/java/lang/runtime/SwitchBootstraps.java

Print this page

687                 }
688                 cb.loadConstant(idx)
689                   .ireturn();
690             }
691             cb.labelBinding(dflt)
692               .loadConstant(labelConstants.length)
693               .ireturn();
694         };
695     }
696 
697     /*
698      * Construct the method handle that represents the method int typeSwitch(Object, int, BiPredicate, List)
699      */
700     private static MethodHandle generateTypeSwitch(MethodHandles.Lookup caller, Class<?> selectorType, Object[] labelConstants) {
701         boolean addExtraInfo = needsExtraInfo(selectorType, labelConstants);
702         List<EnumDesc<?>> enumDescs = addExtraInfo ? new ArrayList<>() : null;
703         List<Class<?>> extraClassLabels = addExtraInfo ? new ArrayList<>() : null;
704 
705         byte[] classBytes = ClassFile.of().build(ConstantUtils.binaryNameToDesc(typeSwitchClassName(caller.lookupClass())),
706                 clb -> {
707                     clb.withFlags(AccessFlag.FINAL, AccessFlag.SUPER, AccessFlag.SYNTHETIC)
708                        .withMethodBody("typeSwitch",
709                                        addExtraInfo ? MTD_TYPE_SWITCH_EXTRA : MTD_TYPE_SWITCH,
710                                        ClassFile.ACC_FINAL | ClassFile.ACC_PUBLIC | ClassFile.ACC_STATIC,
711                                        generateTypeSwitchSkeleton(selectorType, labelConstants, enumDescs, extraClassLabels));
712         });
713 
714         try {
715             // this class is linked at the indy callsite; so define a hidden nestmate
716             MethodHandles.Lookup lookup;
717             lookup = caller.defineHiddenClass(classBytes, true, NESTMATE, STRONG);
718             MethodHandle typeSwitch = lookup.findStatic(lookup.lookupClass(),
719                                                         "typeSwitch",
720                                                         addExtraInfo ? MT_TYPE_SWITCH_EXTRA : MT_TYPE_SWITCH);
721             if (addExtraInfo) {
722                 typeSwitch = MethodHandles.insertArguments(typeSwitch, 2, new ResolvedEnumLabels(caller, enumDescs.toArray(new EnumDesc<?>[0])),
723                         List.copyOf(extraClassLabels));
724             }
725             return typeSwitch;
726         } catch (Throwable t) {
727             throw new IllegalArgumentException(t);

687                 }
688                 cb.loadConstant(idx)
689                   .ireturn();
690             }
691             cb.labelBinding(dflt)
692               .loadConstant(labelConstants.length)
693               .ireturn();
694         };
695     }
696 
697     /*
698      * Construct the method handle that represents the method int typeSwitch(Object, int, BiPredicate, List)
699      */
700     private static MethodHandle generateTypeSwitch(MethodHandles.Lookup caller, Class<?> selectorType, Object[] labelConstants) {
701         boolean addExtraInfo = needsExtraInfo(selectorType, labelConstants);
702         List<EnumDesc<?>> enumDescs = addExtraInfo ? new ArrayList<>() : null;
703         List<Class<?>> extraClassLabels = addExtraInfo ? new ArrayList<>() : null;
704 
705         byte[] classBytes = ClassFile.of().build(ConstantUtils.binaryNameToDesc(typeSwitchClassName(caller.lookupClass())),
706                 clb -> {
707                     clb.withFlags(AccessFlag.FINAL, (PreviewFeatures.isEnabled())  ? AccessFlag.IDENTITY : AccessFlag.SUPER, AccessFlag.SYNTHETIC)
708                        .withMethodBody("typeSwitch",
709                                        addExtraInfo ? MTD_TYPE_SWITCH_EXTRA : MTD_TYPE_SWITCH,
710                                        ClassFile.ACC_FINAL | ClassFile.ACC_PUBLIC | ClassFile.ACC_STATIC,
711                                        generateTypeSwitchSkeleton(selectorType, labelConstants, enumDescs, extraClassLabels));
712         });
713 
714         try {
715             // this class is linked at the indy callsite; so define a hidden nestmate
716             MethodHandles.Lookup lookup;
717             lookup = caller.defineHiddenClass(classBytes, true, NESTMATE, STRONG);
718             MethodHandle typeSwitch = lookup.findStatic(lookup.lookupClass(),
719                                                         "typeSwitch",
720                                                         addExtraInfo ? MT_TYPE_SWITCH_EXTRA : MT_TYPE_SWITCH);
721             if (addExtraInfo) {
722                 typeSwitch = MethodHandles.insertArguments(typeSwitch, 2, new ResolvedEnumLabels(caller, enumDescs.toArray(new EnumDesc<?>[0])),
723                         List.copyOf(extraClassLabels));
724             }
725             return typeSwitch;
726         } catch (Throwable t) {
727             throw new IllegalArgumentException(t);
< prev index next >