Interface Context


  • public interface Context
    Context is very similar to scope. In the context we look for solving symbols.
    Author:
    Federico Tomassetti
    • Method Detail

      • getWrappedNode

        <N extends Node> N getWrappedNode()
        Returns the node wrapped in the context
      • getParent

        Optional<Context> getParent()
        Returns:
        The parent context, if there is one. For example, a method exists within a compilation unit.
      • solveGenericType

        default Optional<ResolvedType> solveGenericType​(String name)
        Default to no generics available in this context, delegating solving to the parent context. Contexts which have generics available to it will override this method. For example class and method declarations, and method calls.
        Parameters:
        name - For example, solving T within class Foo<T> {} or
        Returns:
        The resolved generic type, if found.
      • solveType

        @Deprecated
        default SymbolReference<ResolvedTypeDeclaration> solveType​(String name)
        Deprecated.
        Consider using method solveType(String, List) that also consider the type arguments. If you want to keep to use the new function, but keep the same behavior consider passing type arguments as null.
        Default to being unable to solve any reference in this context, delegating solving to the parent context. Contexts which exist as the "parent" of a resolvable type will override this method. For example, a compilation unit can contain classes. A class declaration can also contain types (e.g. a subclass).
        Parameters:
        name - For example, solving List or java.util.List.
        Returns:
        The declaration associated with the given type name.
      • solveType

        default SymbolReference<ResolvedTypeDeclaration> solveType​(String name,
                                                                   @Nullable
                                                                   List<ResolvedType> typeArguments)
        Method used to solve a name with an expected list of type arguments.
        This method differs from solveType(String) by taking the type arguments in consideration. For example, lets imagine that we have a project containing the following classes:
        • com/example/Alpha.java
        • com/example/Beta.java
        Where Alpha creates a inner interface called CustomInterface and Beta implements Alpha.CustomInterface and also declares a inner interface called CustomInterface with type arguments. Using this method we can specify which type arguments we are expecting and will be resolved with the type matching that declaration.
        Parameters:
        name - The name to be solved.
        typeArguments - The list of expected type arguments.
        Returns:
        The declaration associated with the given type name.
      • solveTypeInParentContext

        @Deprecated
        default SymbolReference<ResolvedTypeDeclaration> solveTypeInParentContext​(String name)
        Deprecated.
        Consider using method solveTypeInParentContext(String, List) that also consider the type arguments. If you want to keep to use the new function, but keep the same behavior consider passing type arguments as null.
        Solve a name in the parent context.
        Parameters:
        name - The name to be solved.
        Returns:
        The declaration associated with the given type name.
      • solveTypeInParentContext

        default SymbolReference<ResolvedTypeDeclaration> solveTypeInParentContext​(String name,
                                                                                  @Nullable
                                                                                  List<ResolvedType> typeArguments)
        Solve a name with type arguments in the parent context.
        Parameters:
        name - The name to be solved.
        typeArguments - The list of expected type arguments.
        Returns:
        The declaration associated with the given type name.
      • solveSymbol

        default SymbolReference<? extends ResolvedValueDeclaration> solveSymbol​(String name)
        Used where a symbol is being used (e.g. solving x when used as an argument doubleThis(x), or calculation return x * 2;).
        Parameters:
        name - the variable / reference / identifier used.
        Returns:
        // FIXME: Better documentation on how this is different to solveSymbolAsValue()
      • solveSymbolAsValue

        default Optional<Value> solveSymbolAsValue​(String name)
        Used where a symbol is being used (e.g. solving x when used as an argument doubleThis(x), or calculation return x * 2;).
        Parameters:
        name - the variable / reference / identifier used.
        Returns:
        // FIXME: Better documentation on how this is different to solveSymbol()
      • solveSymbolAsValueInParentContext

        default Optional<Value> solveSymbolAsValueInParentContext​(String name)
      • fieldsExposedToChild

        default List<ResolvedFieldDeclaration> fieldsExposedToChild​(Node child)
        The fields that are declared and in this immediate context made visible to a given child. This list could include values which are shadowed.
      • localVariablesExposedToChild

        default List<VariableDeclarator> localVariablesExposedToChild​(Node child)
        The local variables that are declared in this immediate context and made visible to a given child. This list could include values which are shadowed.
      • parametersExposedToChild

        default List<Parameter> parametersExposedToChild​(Node child)
        The parameters that are declared in this immediate context and made visible to a given child. This list could include values which are shadowed.
      • typePatternExprsExposedToChild

        default List<TypePatternExpr> typePatternExprsExposedToChild​(Node child)
        The pattern expressions that are declared in this immediate context and made visible to a given child. This list could include values which are shadowed.
      • typePatternExprsExposedFromChildren

        default List<TypePatternExpr> typePatternExprsExposedFromChildren()
      • negatedTypePatternExprsExposedFromChildren

        default List<TypePatternExpr> negatedTypePatternExprsExposedFromChildren()
      • localVariableDeclarationInScope

        default Optional<VariableDeclarator> localVariableDeclarationInScope​(String name)
        Aim to resolve the given name by looking for a variable matching it.

        To do it consider local variables that are visible in a certain scope as defined in JLS 6.3. Scope of a Declaration.

        1. The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement.

        2. The scope of a local variable declared in the ForInit part of a basic for statement (§14.14.1) includes all of the following: 2.1 Its own initializer 2.2 Any further declarators to the right in the ForInit part of the for statement 2.3 The Expression and ForUpdate parts of the for statement 2.4 The contained Statement

        3. The scope of a local variable declared in the FormalParameter part of an enhanced for statement (§14.14.2) is the contained Statement. 4. The scope of a parameter of an exception handler that is declared in a catch clause of a try statement (§14.20) is the entire block associated with the catch.

        5. The scope of a variable declared in the ResourceSpecification of a try-with-resources statement (§14.20.3) is from the declaration rightward over the remainder of the ResourceSpecification and the entire try block associated with the try-with-resources statement.

      • typePatternExprInScope

        default Optional<TypePatternExpr> typePatternExprInScope​(String name)
        With respect to solving, the AST "parent" of a block statement is not necessarily the same as the scope parent.
        Example:
        
          public String x() {
              if(x) {
                  // Parent node: the block attached to the method declaration
                  // Scope-parent: the block attached to the method declaration
              } else if {
                  // Parent node: the if
                  // Scope-parent: the block attached to the method declaration
              } else {
                  // Parent node: the elseif
                  // Scope-parent: the block attached to the method declaration
              }
          }
         
      • solveMethodAsUsage

        Optional<MethodUsage> solveMethodAsUsage​(String name,
                                                 List<ResolvedType> argumentsTypes)
        Similar to solveMethod but we return a MethodUsage. A MethodUsage corresponds to a MethodDeclaration plus the resolved type variables.