Class TreeUtils

java.lang.Object
org.sonar.python.tree.TreeUtils

public class TreeUtils extends Object
  • Method Details

    • firstAncestor

      @CheckForNull public static Tree firstAncestor(Tree tree, Predicate<Tree> predicate)
    • firstAncestorOfKind

      @CheckForNull public static Tree firstAncestorOfKind(Tree tree, Tree.Kind... kinds)
    • groupAssignmentByParentStatementList

      public static Collector<Tree,?,Map<Tree,Tree>> groupAssignmentByParentStatementList()
    • getTreeByPositionComparator

      public static Comparator<Tree> getTreeByPositionComparator()
    • tokens

      public static List<Token> tokens(Tree tree)
    • nonWhitespaceTokens

      public static List<Token> nonWhitespaceTokens(Tree tree)
    • hasDescendant

      public static boolean hasDescendant(Tree tree, Predicate<Tree> predicate)
    • flattenTuples

      public static Stream<Expression> flattenTuples(Expression expression)
    • getSymbolFromTree

      public static Optional<Symbol> getSymbolFromTree(@Nullable Tree tree)
    • getClassSymbolFromDef

      @CheckForNull public static ClassSymbol getClassSymbolFromDef(@Nullable ClassDef classDef)
    • getParentClassesFQN

      public static List<String> getParentClassesFQN(ClassDef classDef)
    • getFunctionSymbolFromDef

      @CheckForNull public static FunctionSymbol getFunctionSymbolFromDef(@Nullable FunctionDef functionDef)
    • nonTupleParameters

      public static List<Parameter> nonTupleParameters(FunctionDef functionDef)
    • positionalParameters

      public static List<Parameter> positionalParameters(FunctionDef functionDef)
    • topLevelFunctionDefs

      public static List<FunctionDef> topLevelFunctionDefs(ClassDef classDef)
      Collects all top-level function definitions within a class def. It is used to discover methods defined within "strange" constructs, such as class A: if p: def f(self): ...
    • findIndentationSize

      public static int findIndentationSize(Tree tree)
    • argumentByKeyword

      @CheckForNull public static RegularArgument argumentByKeyword(String keyword, List<Argument> arguments)
    • nthArgumentOrKeyword

      @CheckForNull public static RegularArgument nthArgumentOrKeyword(int argPosition, String keyword, List<Argument> arguments)
    • nthArgumentOrKeywordOptional

      public static Optional<RegularArgument> nthArgumentOrKeywordOptional(int argPosition, String keyword, List<Argument> arguments)
    • isBooleanLiteral

      public static boolean isBooleanLiteral(Tree tree)
    • nameFromExpression

      public static String nameFromExpression(Expression expression)
    • nameFromQualifiedOrCallExpression

      public static Optional<String> nameFromQualifiedOrCallExpression(Expression expression)
    • nameFromExpressionOrQualifiedExpression

      public static Optional<String> nameFromExpressionOrQualifiedExpression(Expression expression)
    • nameFromQualifiedExpression

      public static String nameFromQualifiedExpression(QualifiedExpression qualifiedExpression)
    • decoratorNameFromExpression

      @CheckForNull public static String decoratorNameFromExpression(Expression expression)
    • asyncTokenOfEnclosingFunction

      public static Optional<Token> asyncTokenOfEnclosingFunction(Tree tree)
    • isFunctionWithGivenDecoratorFQN

      public static boolean isFunctionWithGivenDecoratorFQN(Tree tree, String decoratorFQN)
    • isDecoratorWithFQN

      public static boolean isDecoratorWithFQN(Decorator decorator, String fullyQualifiedName)
    • fullyQualifiedNameFromQualifiedExpression

      public static Optional<String> fullyQualifiedNameFromQualifiedExpression(QualifiedExpression qualifiedExpression)
    • fullyQualifiedNameFromExpression

      public static Optional<String> fullyQualifiedNameFromExpression(Expression expression)
    • locationInFile

      @CheckForNull public static LocationInFile locationInFile(Tree tree, @Nullable String fileId)
    • getTreeSeparatorOrLastToken

      public static Token getTreeSeparatorOrLastToken(Tree tree)
      Statements can have a separator like semicolon. When handling ranges we want to take them into account.
    • toInstanceOfMapper

      public static <T extends Tree> Function<Tree,T> toInstanceOfMapper(Class<T> castToClass)
    • toOptionalInstanceOfMapper

      public static <T extends Tree> Function<Tree,Optional<T>> toOptionalInstanceOfMapper(Class<T> castToClass)
    • toOptionalInstanceOf

      public static <T extends Tree> Optional<T> toOptionalInstanceOf(Class<T> castToClass, @Nullable Tree tree)
    • toStreamInstanceOfMapper

      public static <T extends Tree> Function<Tree,Stream<T>> toStreamInstanceOfMapper(Class<T> castToClass)
    • firstChild

      public static Optional<Tree> firstChild(Tree tree, Predicate<Tree> filter)
    • treeToString

      public static String treeToString(Tree tree, boolean renderMultiline)
    • dottedNameToPartFqn

      public static List<String> dottedNameToPartFqn(DottedName dottedName)
    • stringValueFromNameOrQualifiedExpression

      public static Optional<String> stringValueFromNameOrQualifiedExpression(Expression expression)
      Extracts the string value from a name or qualified expression. Returns empty Optional for expressions that are not simple names or chains of qualified expressions.