Class AstTypeResolver

java.lang.Object
cs1302.tracer.trace.AstTypeResolver

public class AstTypeResolver extends Object
Extracts and indexes static type information from JavaParser ASTs to support generic type reification during runtime tracing.

Normative References:

  • The Java Language Specification (Java SE 21 Edition), §4.5 (Parameterized Types).
  • The Java Language Specification (Java SE 21 Edition), §4.6 (Type Erasure).
  • The Java Language Specification (Java SE 21 Edition), §8.1.2 (Generic Classes and Type Parameters).
  • The Java Language Specification (Java SE 21 Edition), §15.9 (Class Instance Creation Expressions).
  • The Java Virtual Machine Specification (Java SE 21 Edition), §4.7.9 (The Signature Attribute).
  • Field Details

    • TYPE_SOLVER

      public static final com.github.javaparser.ast.DataKey<com.github.javaparser.resolution.TypeSolver> TYPE_SOLVER
      Source solver retained for invocation-level inference.
  • Constructor Details

    • AstTypeResolver

      public AstTypeResolver(List<com.github.javaparser.ast.CompilationUnit> compilationUnits)
      Create an AstTypeResolver from a list of compilation units.
      Parameters:
      compilationUnits - The compilation units to analyze.
    • AstTypeResolver

      public AstTypeResolver(com.github.javaparser.ast.CompilationUnit compilationUnit)
      Create an AstTypeResolver from a single compilation unit.
      Parameters:
      compilationUnit - The compilation unit to analyze.
  • Method Details

    • resolveAstTypeWithParams

      public static String resolveAstTypeWithParams(com.github.javaparser.ast.type.Type type, List<String> typeParams)
      Resolve an AST Type, preserving type parameter names rather than erasing to bounds.
      Parameters:
      type - The AST type.
      typeParams - Known type parameters in the current scope.
      Returns:
      The descriptive type string.
    • resolveAstType

      public static String resolveAstType(com.github.javaparser.ast.type.Type type)
      Safely resolve an AST Type into a descriptive type string.
      Parameters:
      type - The AST type.
      Returns:
      The resolved type name or raw AST type string.
    • extractRawTypeName

      public static String extractRawTypeName(String typeString)
      Extracts the raw type name by stripping any generic type arguments.
      Parameters:
      typeString - The type string.
      Returns:
      Raw type name without angle brackets.
    • normalizeWildcard

      public static String normalizeWildcard(String arg)
      Normalizes wildcard type arguments to their concrete upper bound or Object.
      Parameters:
      arg - The type argument.
      Returns:
      Normalized type string.
    • rawTypeMatches

      public static boolean rawTypeMatches(String raw1, String raw2)
      Compare two raw type names, allowing for suffix matching when one is simple.
      Parameters:
      raw1 - First raw type name.
      raw2 - Second raw type name.
      Returns:
      True if they match exactly or as suffixes.
    • resolveSubclassType

      public String resolveSubclassType(String concreteClassFqn, String interfaceOrSuperTypeString)
      Resolves the concrete subclass's reified type given an interface or superclass type string.
      Parameters:
      concreteClassFqn - The concrete class FQN.
      interfaceOrSuperTypeString - The parameterized interface or superclass type.
      Returns:
      The parameterized concrete type string, or raw class name.
    • reconcileRuntimeType

      public String reconcileRuntimeType(String runtimeClassFqn, String declaredType)
      Reconciles a concrete runtime class with a declared reference type to produce a fully parameterized concrete type name.
      Parameters:
      runtimeClassFqn - The concrete runtime class FQN.
      declaredType - The declared variable type.
      Returns:
      Reconciled concrete type name.
    • resolveObjectCreationType

      public String resolveObjectCreationType(com.github.javaparser.ast.expr.ObjectCreationExpr oce)
      Safely resolve an ObjectCreationExpr target type.
      Parameters:
      oce - The object creation expression.
      Returns:
      The resolved type string or raw type string.
    • getClassGenericInfo

      public Optional<AstTypeResolver.ClassGenericInfo> getClassGenericInfo(String classFqn)
      Get class generic information for a given class FQN.
      Parameters:
      classFqn - Fully qualified class name.
      Returns:
      Optional containing AstTypeResolver.ClassGenericInfo if found.
    • resolveVariableType

      public Optional<String> resolveVariableType(String classFqn, String methodName, String variableName, int lineNumber)
      Look up the AST declared type for a variable or parameter in a method at a given line.
      Parameters:
      classFqn - Declaring class FQN.
      methodName - Method name or <init>.
      variableName - Variable name.
      lineNumber - Current line number.
      Returns:
      Optional containing the declared AST type string.
    • getAllocationType

      public Optional<String> getAllocationType(String classFqn, int lineNumber)
      Look up an object allocation type at a specific class and line number.
      Parameters:
      classFqn - Declaring class FQN.
      lineNumber - Line number where new occurred.
      Returns:
      Optional containing the resolved target type.
    • getTypeBindings

      public Map<String,String> getTypeBindings(String classFqn, String reifiedType)
      Extract type argument bindings by matching a class declaration's type parameters.
      Parameters:
      classFqn - The fully qualified class name.
      reifiedType - The reified generic type string.
      Returns:
      A map of type parameter names to concrete type argument strings.
    • extractTypeArguments

      public static List<String> extractTypeArguments(String typeString)
      Extract top-level type arguments from a parameterized type string.
      Parameters:
      typeString - The type string.
      Returns:
      A list of type argument strings.
    • substituteType

      public static String substituteType(String typeWithParams, Map<String,String> bindings)
      Substitute type parameters in a declared type string with concrete type arguments.
      Parameters:
      typeWithParams - The declared type string (e.g. "K" or "Pair<K, V>").
      bindings - The map of type parameter names to concrete type arguments.
      Returns:
      The substituted type string.