Interface Switch<T>

Type Parameters:
T - Constant type
All Known Subinterfaces:
Switch.EnumSwitch<E>, Switch.StringSwitch
All Known Implementing Classes:
AbstractSwitch, ClassTransfromerMethodCreatorImpl.ClassTransformerEnumSwitchImpl, EnumSwitchImpl, StringSwitchImpl

public interface Switch<T>
A switch statement.

This construct is not thread-safe and should not be re-used.

  • Method Details

    • fallThrough

      void fallThrough()
      Enables fall through.

      By default, the fall through is disabled. A case block is treated as a switch rule block; i.e. it's not necessary to add the break statement to prevent the fall through. However, if fall through is enabled then a case block is treated as a labeled statement group; i.e. it's necessary to add the break statement to prevent the fall through.

      For example, if fall through is disabled then:

       
       StringSwitch s = method.stringSwitch(val);
             s.caseOf(List.of("boom", "foo"), bc -> {...});
       
       
      is an equivalent of:
       switch (val) {
           case "boom", "foo" -> // statements provided by the consumer
       }
       
      However, if fall though is enabled then:
       
       StringSwitch s = method.stringSwitch(val);
       s.fallThrough();
       s.caseOf(List.of("boom", "foo"), bc -> {...});
       
       
      is an equivalent of:
       switch (val) {
           case "val1":
           case "val2":
               // statements provided by the consumer
       }
       
    • caseOf

      void caseOf(T value, Consumer<BytecodeCreator> caseBlockConsumer)
      Adds a case block.
      Parameters:
      value - The value for the case label
      caseBlockConsumer - The consumer used to define the case block
      Throws:
      IllegalArgumentException - If a case block for the specified value was already added
    • caseOf

      void caseOf(List<T> values, Consumer<BytecodeCreator> caseBlockConsumer)
      Adds multiple case labels for a single block.
      Parameters:
      values -
      caseBlockConsumer -
      Throws:
      IllegalArgumentException - If a case block for the specified value was already added
    • defaultCase

      void defaultCase(Consumer<BytecodeCreator> defatultBlockConsumer)
      Adds the default block.
      Parameters:
      defatultBlockConsumer -
    • doBreak

      void doBreak(BytecodeCreator creator)
      Writes bytecode into the provided BytecodeCreator to make it exit the switch, effectively issuing a Java 'break' statement.
      Parameters:
      creator -
      See Also: