Class TreeUtilsAfterJava11.CaseUtils

  • Enclosing class:
    TreeUtilsAfterJava11

    public static class TreeUtilsAfterJava11.CaseUtils
    extends java.lang.Object
    Utility methods for accessing CaseTree methods.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static @Nullable com.sun.source.tree.Tree getBody​(com.sun.source.tree.CaseTree caseTree)
      Returns the body of the case statement if it is of the form case <expression> -> <expression>.
      static java.util.List<? extends com.sun.source.tree.ExpressionTree> getExpressions​(com.sun.source.tree.CaseTree caseTree)
      Get the list of expressions from a case expression.
      static @Nullable com.sun.source.tree.ExpressionTree getGuard​(com.sun.source.tree.CaseTree caseTree)
      Returns the guard, the expression after when, of caseTree.
      static java.util.List<? extends com.sun.source.tree.Tree> getLabels​(com.sun.source.tree.CaseTree caseTree)
      Get the list of labels from a case expression.
      static boolean isCaseRule​(com.sun.source.tree.CaseTree caseTree)
      Returns true if this is a case rule (as opposed to a case statement).
      static boolean isDefaultCaseLabelTree​(com.sun.source.tree.Tree tree)
      Returns true if tree is a DefaultCaseLabelTree.
      static boolean isDefaultCaseTree​(com.sun.source.tree.CaseTree caseTree)
      Returns true if this is the default case for a switch statement or expression.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isCaseRule

        public static boolean isCaseRule​(com.sun.source.tree.CaseTree caseTree)
        Returns true if this is a case rule (as opposed to a case statement).
        Parameters:
        caseTree - a case tree
        Returns:
        true if caseTree is a case rule
      • getBody

        public static @Nullable com.sun.source.tree.Tree getBody​(com.sun.source.tree.CaseTree caseTree)
        Returns the body of the case statement if it is of the form case <expression> -> <expression>. This method should only be called if CaseTree.getStatements() returns null.
        Parameters:
        caseTree - the case expression to get the body from
        Returns:
        the body of the case tree
      • isDefaultCaseTree

        public static boolean isDefaultCaseTree​(com.sun.source.tree.CaseTree caseTree)
        Returns true if this is the default case for a switch statement or expression. (Also, returns true if caseTree is case null, default:.)
        Parameters:
        caseTree - a case tree
        Returns:
        true if caseTree is the default case for a switch statement or expression
      • isDefaultCaseLabelTree

        public static boolean isDefaultCaseLabelTree​(com.sun.source.tree.Tree tree)
        Returns true if tree is a DefaultCaseLabelTree.
        Parameters:
        tree - a tree to check
        Returns:
        true if tree is a DefaultCaseLabelTree
      • getLabels

        public static java.util.List<? extends com.sun.source.tree.Tree> getLabels​(com.sun.source.tree.CaseTree caseTree)
        Get the list of labels from a case expression. For default, this is empty. For case null, default, the list contains null. Otherwise, in JDK 11 and earlier, this is a list of a single expression tree. In JDK 12+, the list may have multiple expression trees. In JDK 21+, the list might contain a single pattern tree.
        Parameters:
        caseTree - the case expression to get the labels from
        Returns:
        the list of case labels in the case
      • getExpressions

        public static java.util.List<? extends com.sun.source.tree.ExpressionTree> getExpressions​(com.sun.source.tree.CaseTree caseTree)
        Get the list of expressions from a case expression. For the default case, this is empty. Otherwise, in JDK 11 and earlier, this is a singleton list. In JDK 12 onwards, there can be multiple expressions per case.
        Parameters:
        caseTree - the case expression to get the expressions from
        Returns:
        the list of expressions in the case
      • getGuard

        public static @Nullable com.sun.source.tree.ExpressionTree getGuard​(com.sun.source.tree.CaseTree caseTree)
        Returns the guard, the expression after when, of caseTree. Wrapper around CaseTree#getGuard that can be called on any version of Java.
        Parameters:
        caseTree - the case tree
        Returns:
        the guard on the case tree or null if one does not exist