Package cs1302.tracer.trace
Class AstTypeResolver
java.lang.Object
cs1302.tracer.trace.AstTypeResolver
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).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordMetadata for a class's generic type parameters, fields, and constructors.static final recordMetadata for a variable or parameter within a method or constructor. -
Constructor Summary
ConstructorsConstructorDescriptionAstTypeResolver(com.github.javaparser.ast.CompilationUnit compilationUnit) Create anAstTypeResolverfrom a single compilation unit.AstTypeResolver(List<com.github.javaparser.ast.CompilationUnit> compilationUnits) Create anAstTypeResolverfrom a list of compilation units. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringextractRawTypeName(String typeString) Extracts the raw type name by stripping any generic type arguments.extractTypeArguments(String typeString) Extract top-level type arguments from a parameterized type string.getAllocationType(String classFqn, int lineNumber) Look up an object allocation type at a specific class and line number.getClassGenericInfo(String classFqn) Get class generic information for a given class FQN.getTypeBindings(String classFqn, String reifiedType) Extract type argument bindings by matching a class declaration's type parameters.static StringnormalizeWildcard(String arg) Normalizes wildcard type arguments to their concrete upper bound or Object.static booleanrawTypeMatches(String raw1, String raw2) Compare two raw type names, allowing for suffix matching when one is simple.reconcileRuntimeType(String runtimeClassFqn, String declaredType) Reconciles a concrete runtime class with a declared reference type to produce a fully parameterized concrete type name.static StringresolveAstType(com.github.javaparser.ast.type.Type type) Safely resolve an ASTTypeinto a descriptive type string.static StringresolveAstTypeWithParams(com.github.javaparser.ast.type.Type type, List<String> typeParams) Resolve an ASTType, preserving type parameter names rather than erasing to bounds.resolveObjectCreationType(com.github.javaparser.ast.expr.ObjectCreationExpr oce) Safely resolve anObjectCreationExprtarget type.resolveSubclassType(String concreteClassFqn, String interfaceOrSuperTypeString) Resolves the concrete subclass's reified type given an interface or superclass type 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.static StringsubstituteType(String typeWithParams, Map<String, String> bindings) Substitute type parameters in a declared type string with concrete type arguments.
-
Constructor Details
-
AstTypeResolver
Create anAstTypeResolverfrom a list of compilation units.- Parameters:
compilationUnits- The compilation units to analyze.
-
AstTypeResolver
public AstTypeResolver(com.github.javaparser.ast.CompilationUnit compilationUnit) Create anAstTypeResolverfrom 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 ASTType, 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
Safely resolve an ASTTypeinto a descriptive type string.- Parameters:
type- The AST type.- Returns:
- The resolved type name or raw AST type string.
-
extractRawTypeName
Extracts the raw type name by stripping any generic type arguments.- Parameters:
typeString- The type string.- Returns:
- Raw type name without angle brackets.
-
normalizeWildcard
Normalizes wildcard type arguments to their concrete upper bound or Object.- Parameters:
arg- The type argument.- Returns:
- Normalized type string.
-
rawTypeMatches
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
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
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
Safely resolve anObjectCreationExprtarget type.- Parameters:
oce- The object creation expression.- Returns:
- The resolved type string or raw type string.
-
getClassGenericInfo
Get class generic information for a given class FQN.- Parameters:
classFqn- Fully qualified class name.- Returns:
- Optional containing
AstTypeResolver.ClassGenericInfoif 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
Look up an object allocation type at a specific class and line number.- Parameters:
classFqn- Declaring class FQN.lineNumber- Line number wherenewoccurred.- Returns:
- Optional containing the resolved target type.
-
getTypeBindings
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
Extract top-level type arguments from a parameterized type string.- Parameters:
typeString- The type string.- Returns:
- A list of type argument strings.
-
substituteType
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.
-