Package com.tonic.analysis.source.lower
Class TypeResolver
- java.lang.Object
-
- com.tonic.analysis.source.lower.TypeResolver
-
public class TypeResolver extends Object
Resolves source-level type, field and method references to JVM names and descriptors, consulting the class being lowered, the class pool and the running JVM in that order.
-
-
Constructor Summary
Constructors Constructor Description TypeResolver(ClassPool classPool, String currentClass)Creates a resolver over a class pool and installs its superclass lookup as the frame-generation merge resolver.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanclassExists(String internalName)Tests whether a class is resolvable via the pool - already loaded, or loadable from the system class path.StringdescriptorOf(ParameterDecl param)Builds the descriptor of a declared parameter.StringdescriptorOf(SourceType type)Builds the JVM descriptor for a source type, erasing generics and resolving reference names through the imports and the pool.StringdescriptorOfMethod(String ownerClass, String methodName)Looks up the descriptor of a directly declared method, used to recover the specific parameter types that a lambda's untypedx ->source form drops.List<String>findAllMethodDescriptors(String ownerClass, String methodName)Collects every overload of a method name, falling back to the superclass and interfaces when the class itself declares none.SourceTypefindFieldType(String ownerClass, String fieldName)Resolves a field's declared type, searching the current class declaration first, then the field tables of the owner class and its superclasses via the ClassPool.String[]findLambdaMethod(String ownerClass, String enclosingMethod, int index)Finds an already-declared synthetic lambda method by position.SourceTypefunctionalConstructorParamType(String ownerClass, int arity, int index)Resolves the declared type of a constructor parameter that expects a functional argument.ClassPoolgetClassPool()StringgetCurrentClass()StringgetSuperclassName(String internalName)Resolves the direct superclass, taking user classes from the pool and falling back to loading JDK and system classes.booleanisInterface(String internalName)Tests whether a class is an interface, consulting the ClassPool and then reflection.booleanisStaticField(String ownerClass, String fieldName)Tests whether a field is declared static, searching the current class declaration first, then the owner class and its superclasses.booleanisStaticMethod(String ownerClass, String methodName, String descriptor)Checks the ACC_STATIC flag of a method declared on a pooled class.booleanisStaticMethodInCurrentClass(String methodName)Tests whether a method of the class being lowered is static, consulting the parsed class declaration (which may hold methods not yet present on the ClassFile) so that unqualified self-calls can be resolved as static or virtual.booleanisVarargsMethod(String ownerClass, String methodName, String descriptor)Tests whether the method with this exact descriptor on the owner (or a supertype) is declaredACC_VARARGS, falling back to reflection for classes absent from the pool.List<SourceType>paramTypesFromDescriptor(String methodDescriptor)Parses the parameter types of a method descriptor.SourceTyperesolveArrayElementType(SourceType arrayType)Strips one level off an array type.StringresolveClassName(String simpleName)Qualifies a type name to its internal form, trying type-variable erasure, explicit and wildcard imports, a java.lang shortlist, the current class, and finally the loaded classes.StringresolveConstructorDescriptor(String ownerClass, int expectedParamCount)Picks a constructor descriptor by arity, preferring the no-arg form when the count does not match.StringresolveConstructorDescriptor(String ownerClass, List<IRType> argTypes)Picks the constructor whose parameters best match the given argument IR types (exact descriptor preferred, then same primitive/reference kind), disambiguating same-arity overloads such asArrayList(int)vsArrayList(Collection).SourceTyperesolveFieldType(String ownerClass, String fieldName)Resolves a field's declared type, searching the source declaration, the pool class and its supertypes and interfaces, then reflection and the current class's original constant pool.StringresolveInternalName(String rawName)Resolves a parsed type name to its fully-qualified internal name.StringresolveMethodDescriptor(String ownerClass, String methodName, int expectedParamCount)Picks a method descriptor by arity, searching the superclass chain and interfaces when the class declares no overload of that name.StringresolveMethodDescriptor(String ownerClass, String methodName, List<IRType> argTypes)Resolves the declared descriptor of the best-matching overload, choosing among same-arity candidates by argument-type compatibility (exact descriptor, then primitive/reference kind) and searching the superclass and interfaces.StringresolveMethodDescriptorViaReflection(String ownerClass, String methodName, List<IRType> argTypes)Resolves a descriptor against the running JVM, picking the most specific overload the arguments are assignable to.SourceTyperesolveMethodReturnType(String ownerClass, String methodName, List<SourceType> argTypes)Resolves a call's return type, searching the source declaration, the pool class and its supertypes, then reflection when the exact parameter descriptor does not match.String[]resolveSamMethod(String interfaceName)Resolves the single abstract method of a functional interface, looking the interface up in the ClassPool first, then falling back to a table of common JDK functional interfaces.SourceTypereturnTypeFromDescriptor(String methodDescriptor)Parses the return type of a method descriptor.voidsetCurrentClassDecl(TypeDecl currentClassDecl)Sets the source declaration consulted before the class pool.voidsetImports(List<ImportDecl> imports)Sets the imports used to qualify simple type names.StringsignatureOf(SourceType type)Builds the JVM generic signature of a declared type.
-
-
-
Constructor Detail
-
TypeResolver
public TypeResolver(ClassPool classPool, String currentClass)
Creates a resolver over a class pool and installs its superclass lookup as the frame-generation merge resolver.- Parameters:
classPool- the pool holding the classes being loweredcurrentClass- the internal name of the class being lowered
-
-
Method Detail
-
getClassPool
public ClassPool getClassPool()
- Returns:
- the class pool
-
getCurrentClass
public String getCurrentClass()
- Returns:
- the current class
-
setCurrentClassDecl
public void setCurrentClassDecl(TypeDecl currentClassDecl)
Sets the source declaration consulted before the class pool.- Parameters:
currentClassDecl- the declaration being lowered
-
setImports
public void setImports(List<ImportDecl> imports)
Sets the imports used to qualify simple type names.- Parameters:
imports- the import declarations in source order
-
getSuperclassName
public String getSuperclassName(String internalName)
Resolves the direct superclass, taking user classes from the pool and falling back to loading JDK and system classes.- Parameters:
internalName- the class to look up- Returns:
- the superclass internal name, or null for
java/lang/Object, an unresolvable class, or any lookup failure
-
resolveFieldType
public SourceType resolveFieldType(String ownerClass, String fieldName)
Resolves a field's declared type, searching the source declaration, the pool class and its supertypes and interfaces, then reflection and the current class's original constant pool.- Parameters:
ownerClass- the owner's class name, simple or internalfieldName- the field name- Returns:
- the field type, or null when nothing declares the field
-
findFieldType
public SourceType findFieldType(String ownerClass, String fieldName)
Resolves a field's declared type, searching the current class declaration first, then the field tables of the owner class and its superclasses via the ClassPool.- Parameters:
ownerClass- internal name of the declaring classfieldName- the field to look up- Returns:
- the declared type, or null if the field cannot be found
-
isStaticField
public boolean isStaticField(String ownerClass, String fieldName)
Tests whether a field is declared static, searching the current class declaration first, then the owner class and its superclasses.- Parameters:
ownerClass- internal name of the declaring classfieldName- the field to look up- Returns:
- true if the field is static; false if it is not, or cannot be located
-
resolveSamMethod
public String[] resolveSamMethod(String interfaceName)
Resolves the single abstract method of a functional interface, looking the interface up in the ClassPool first, then falling back to a table of common JDK functional interfaces.- Parameters:
interfaceName- internal name of the functional interface- Returns:
[name, descriptor]of the abstract method, or null if it cannot be determined
-
returnTypeFromDescriptor
public SourceType returnTypeFromDescriptor(String methodDescriptor)
Parses the return type of a method descriptor.- Parameters:
methodDescriptor- the method descriptor to parse- Returns:
- the return type
-
descriptorOfMethod
public String descriptorOfMethod(String ownerClass, String methodName)
Looks up the descriptor of a directly declared method, used to recover the specific parameter types that a lambda's untypedx ->source form drops.- Parameters:
ownerClass- internal name of the declaring classmethodName- the method name, assumed unique on the class (e.g. a syntheticlambda$...)- Returns:
- the method descriptor, or null if the class or method is not found
-
findLambdaMethod
public String[] findLambdaMethod(String ownerClass, String enclosingMethod, int index)
Finds an already-declared synthetic lambda method by position.- Parameters:
ownerClass- internal name of the class declaring the lambdaenclosingMethod- the method the lambda appears inindex- position among that method's lambdas, ordered by the trailing counter- Returns:
[name, descriptor]of the lambda method, or null if the class or index has no match
-
paramTypesFromDescriptor
public List<SourceType> paramTypesFromDescriptor(String methodDescriptor)
Parses the parameter types of a method descriptor.- Parameters:
methodDescriptor- the method descriptor to parse- Returns:
- the parameter types in declaration order
-
isStaticMethodInCurrentClass
public boolean isStaticMethodInCurrentClass(String methodName)
Tests whether a method of the class being lowered is static, consulting the parsed class declaration (which may hold methods not yet present on the ClassFile) so that unqualified self-calls can be resolved as static or virtual.- Parameters:
methodName- the method to look up- Returns:
- true if the method is static; false if it is not, or is not declared
-
resolveMethodReturnType
public SourceType resolveMethodReturnType(String ownerClass, String methodName, List<SourceType> argTypes)
Resolves a call's return type, searching the source declaration, the pool class and its supertypes, then reflection when the exact parameter descriptor does not match.- Parameters:
ownerClass- the receiver's class name, simple or internalmethodName- the method nameargTypes- the argument types at the call site- Returns:
- the return type, or null when nothing resolves the call
-
resolveArrayElementType
public SourceType resolveArrayElementType(SourceType arrayType)
Strips one level off an array type.- Parameters:
arrayType- the array type- Returns:
- the element type
- Throws:
LoweringException- if the type is not an array
-
resolveMethodDescriptor
public String resolveMethodDescriptor(String ownerClass, String methodName, List<IRType> argTypes)
Resolves the declared descriptor of the best-matching overload, choosing among same-arity candidates by argument-type compatibility (exact descriptor, then primitive/reference kind) and searching the superclass and interfaces.- Parameters:
ownerClass- internal name of the receiver classmethodName- the method name to matchargTypes- the IR types of the call arguments, in order- Returns:
- the declared descriptor, or null when the class or a compatible method is not in the pool, leaving the caller to fall back to the argument types
-
functionalConstructorParamType
public SourceType functionalConstructorParamType(String ownerClass, int arity, int index)
Resolves the declared type of a constructor parameter that expects a functional argument.- Parameters:
ownerClass- internal name of the class being constructedarity- the number of constructor argumentsindex- position of the parameter to type- Returns:
- the interface type at that position, or null when no candidate constructor has one or more than one candidate does
-
resolveMethodDescriptorViaReflection
public String resolveMethodDescriptorViaReflection(String ownerClass, String methodName, List<IRType> argTypes)
Resolves a descriptor against the running JVM, picking the most specific overload the arguments are assignable to.- Parameters:
ownerClass- the internal name of the declaring classmethodName- the method name, or the JVM constructor name to match constructors insteadargTypes- the argument types at the call site- Returns:
- the descriptor, or null when the class is not loadable or no overload accepts the arguments
-
isVarargsMethod
public boolean isVarargsMethod(String ownerClass, String methodName, String descriptor)
Tests whether the method with this exact descriptor on the owner (or a supertype) is declaredACC_VARARGS, falling back to reflection for classes absent from the pool.- Parameters:
ownerClass- internal name of the declaring classmethodName- the method namedescriptor- the exact descriptor to match- Returns:
- true if that method is varargs
-
resolveMethodDescriptor
public String resolveMethodDescriptor(String ownerClass, String methodName, int expectedParamCount)
Picks a method descriptor by arity, searching the superclass chain and interfaces when the class declares no overload of that name.- Parameters:
ownerClass- the internal name of the class to searchmethodName- the method nameexpectedParamCount- the wanted parameter count, negative to skip arity matching- Returns:
- the descriptor, or null when nothing declares the method
-
resolveConstructorDescriptor
public String resolveConstructorDescriptor(String ownerClass, List<IRType> argTypes)
Picks the constructor whose parameters best match the given argument IR types (exact descriptor preferred, then same primitive/reference kind), disambiguating same-arity overloads such asArrayList(int)vsArrayList(Collection).- Parameters:
ownerClass- internal name of the class being constructedargTypes- the IR types of the call arguments, in order- Returns:
- the declared constructor descriptor, or null when the class is absent from the pool or no kind-compatible constructor exists, leaving the caller to build one from the argument types
-
resolveConstructorDescriptor
public String resolveConstructorDescriptor(String ownerClass, int expectedParamCount)
Picks a constructor descriptor by arity, preferring the no-arg form when the count does not match.- Parameters:
ownerClass- the internal name of the class being constructedexpectedParamCount- the wanted parameter count, negative to skip arity matching- Returns:
- the descriptor, or "()V" when the class is absent from the pool or declares no constructor
-
findAllMethodDescriptors
public List<String> findAllMethodDescriptors(String ownerClass, String methodName)
Collects every overload of a method name, falling back to the superclass and interfaces when the class itself declares none.- Parameters:
ownerClass- the internal name of the class to searchmethodName- the method name- Returns:
- the matching descriptors, empty if the class is absent from the pool or declares no match
-
isStaticMethod
public boolean isStaticMethod(String ownerClass, String methodName, String descriptor)
Checks the ACC_STATIC flag of a method declared on a pooled class.- Parameters:
ownerClass- the internal name of the declaring classmethodName- the method namedescriptor- the method descriptor- Returns:
- true if the method is declared static, false if it is not or the class is absent from the pool
-
resolveClassName
public String resolveClassName(String simpleName)
Qualifies a type name to its internal form, trying type-variable erasure, explicit and wildcard imports, a java.lang shortlist, the current class, and finally the loaded classes.- Parameters:
simpleName- the name as written in source, simple, dotted or already internal- Returns:
- the internal name, or the input unchanged when nothing resolves it
-
resolveInternalName
public String resolveInternalName(String rawName)
Resolves a parsed type name to its fully-qualified internal name.- Parameters:
rawName- the type name as written in source- Returns:
- the internal name
-
signatureOf
public String signatureOf(SourceType type)
Builds the JVM generic signature of a declared type.- Parameters:
type- the declared type- Returns:
- the signature, or null when the type carries no generic information.
-
descriptorOf
public String descriptorOf(ParameterDecl param)
Builds the descriptor of a declared parameter.- Parameters:
param- the declared parameter- Returns:
- the field descriptor of the parameter's type
-
descriptorOf
public String descriptorOf(SourceType type)
Builds the JVM descriptor for a source type, erasing generics and resolving reference names through the imports and the pool.- Parameters:
type- the source type- Returns:
- the descriptor
-
isInterface
public boolean isInterface(String internalName)
Tests whether a class is an interface, consulting the ClassPool and then reflection.- Parameters:
internalName- the class to look up- Returns:
- true if the class is an interface; false if it is not, or cannot be resolved
-
classExists
public boolean classExists(String internalName)
Tests whether a class is resolvable via the pool - already loaded, or loadable from the system class path.- Parameters:
internalName- the class to look up- Returns:
- true if the class resolves
-
-