< prev index next >

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

Print this page

608                             element.caseLabel().getClass());
609                 }
610                 cb.constantInstruction(idx);
611                 cb.ireturn();
612             }
613             cb.labelBinding(dflt);
614             cb.constantInstruction(cases.size());
615             cb.ireturn();
616         };
617     }
618 
619     /*
620      * Construct the method handle that represents the method int typeSwitch(Object, int, BiPredicate, List)
621      */
622     private static MethodHandle generateTypeSwitch(MethodHandles.Lookup caller, Class<?> selectorType, Object[] labelConstants) {
623         List<EnumDesc<?>> enumDescs = new ArrayList<>();
624         List<Class<?>> extraClassLabels = new ArrayList<>();
625 
626         byte[] classBytes = ClassFile.of().build(ClassDesc.of(typeSwitchClassName(caller.lookupClass())),
627                 clb -> {
628                     clb.withFlags(AccessFlag.FINAL, AccessFlag.SUPER, AccessFlag.SYNTHETIC)
629                        .withMethodBody("typeSwitch",
630                                        TYPES_SWITCH_DESCRIPTOR,
631                                        ClassFile.ACC_FINAL | ClassFile.ACC_PUBLIC | ClassFile.ACC_STATIC,
632                                        generateTypeSwitchSkeleton(selectorType, labelConstants, enumDescs, extraClassLabels));
633         });
634 
635         try {
636             // this class is linked at the indy callsite; so define a hidden nestmate
637             MethodHandles.Lookup lookup;
638             lookup = caller.defineHiddenClass(classBytes, true, NESTMATE, STRONG);
639             MethodHandle typeSwitch = lookup.findStatic(lookup.lookupClass(),
640                                                         "typeSwitch",
641                                                         MethodType.methodType(int.class,
642                                                                               Object.class,
643                                                                               int.class,
644                                                                               BiPredicate.class,
645                                                                               List.class));
646             typeSwitch = MethodHandles.insertArguments(typeSwitch, 2, new ResolvedEnumLabels(caller, enumDescs.toArray(EnumDesc[]::new)),
647                                                        List.copyOf(extraClassLabels));
648             typeSwitch = MethodHandles.explicitCastArguments(typeSwitch,

608                             element.caseLabel().getClass());
609                 }
610                 cb.constantInstruction(idx);
611                 cb.ireturn();
612             }
613             cb.labelBinding(dflt);
614             cb.constantInstruction(cases.size());
615             cb.ireturn();
616         };
617     }
618 
619     /*
620      * Construct the method handle that represents the method int typeSwitch(Object, int, BiPredicate, List)
621      */
622     private static MethodHandle generateTypeSwitch(MethodHandles.Lookup caller, Class<?> selectorType, Object[] labelConstants) {
623         List<EnumDesc<?>> enumDescs = new ArrayList<>();
624         List<Class<?>> extraClassLabels = new ArrayList<>();
625 
626         byte[] classBytes = ClassFile.of().build(ClassDesc.of(typeSwitchClassName(caller.lookupClass())),
627                 clb -> {
628                     clb.withFlags(AccessFlag.FINAL, (PreviewFeatures.isEnabled())  ? AccessFlag.IDENTITY : AccessFlag.SUPER, AccessFlag.SYNTHETIC)
629                        .withMethodBody("typeSwitch",
630                                        TYPES_SWITCH_DESCRIPTOR,
631                                        ClassFile.ACC_FINAL | ClassFile.ACC_PUBLIC | ClassFile.ACC_STATIC,
632                                        generateTypeSwitchSkeleton(selectorType, labelConstants, enumDescs, extraClassLabels));
633         });
634 
635         try {
636             // this class is linked at the indy callsite; so define a hidden nestmate
637             MethodHandles.Lookup lookup;
638             lookup = caller.defineHiddenClass(classBytes, true, NESTMATE, STRONG);
639             MethodHandle typeSwitch = lookup.findStatic(lookup.lookupClass(),
640                                                         "typeSwitch",
641                                                         MethodType.methodType(int.class,
642                                                                               Object.class,
643                                                                               int.class,
644                                                                               BiPredicate.class,
645                                                                               List.class));
646             typeSwitch = MethodHandles.insertArguments(typeSwitch, 2, new ResolvedEnumLabels(caller, enumDescs.toArray(EnumDesc[]::new)),
647                                                        List.copyOf(extraClassLabels));
648             typeSwitch = MethodHandles.explicitCastArguments(typeSwitch,
< prev index next >